webpack.nvue.conf.js 12.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const fs = require('fs')
fxy060608's avatar
fxy060608 已提交
2 3
const path = require('path')
const webpack = require('webpack')
fxy060608's avatar
fxy060608 已提交
4
const VueLoaderPlugin = require('@dcloudio/vue-cli-plugin-uni/packages/vue-loader/lib/plugin')
fxy060608's avatar
fxy060608 已提交
5
const CopyWebpackPlugin = require('copy-webpack-plugin')
6
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
7
const TerserPlugin = require('terser-webpack-plugin')
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12

const {
  getNVueMainEntry,
  nvueJsPreprocessOptions,
  nvueHtmlPreprocessOptions,
Q
qiang 已提交
13
  getTemplatePath
fxy060608's avatar
fxy060608 已提交
14
} = require('@dcloudio/uni-cli-shared')
fxy060608's avatar
fxy060608 已提交
15
const fileLoader = require('@dcloudio/uni-cli-shared/lib/file-loader')
fxy060608's avatar
fxy060608 已提交
16 17 18 19
const WebpackAppPlusNVuePlugin = process.env.UNI_USING_V3
  ? require('../packages/webpack-app-plus-plugin')
  : require('../packages/webpack-app-plus-nvue-plugin')

fxy060608's avatar
fxy060608 已提交
20
const WebpackErrorsPlugin = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-errors-plugin')
fxy060608's avatar
fxy060608 已提交
21
const WebpackUniMPPlugin = require('@dcloudio/webpack-uni-mp-loader/lib/plugin/index-new')
fxy060608's avatar
fxy060608 已提交
22 23

const onErrors = require('@dcloudio/vue-cli-plugin-uni/util/on-errors')
24
const onWarnings = require('@dcloudio/vue-cli-plugin-uni/util/on-warnings')
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

const cssLoaders = require('./css-loader.conf')
const vueLoaderOptions = require('./vue-loader.conf')

const jsPreprocessorLoader = {
  loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader',
  options: nvueJsPreprocessOptions
}

const htmlPreprocessorLoader = {
  loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader',
  options: nvueHtmlPreprocessOptions
}

const uniPath = process.env.UNI_USING_V8
  ? '../packages/uni-app-plus-nvue-v8/dist/index.js'
  : '../packages/uni-app-plus-nvue/dist/index.js'

fxy060608's avatar
fxy060608 已提交
43 44 45
const uniCloudPath = require.resolve('@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js')

const provide = {
fxy060608's avatar
fxy060608 已提交
46
  uniCloud: [uniCloudPath, 'default']
fxy060608's avatar
fxy060608 已提交
47
}
fxy060608's avatar
fxy060608 已提交
48

fxy060608's avatar
fxy060608 已提交
49 50 51 52 53
if (
  process.env.UNI_USING_V3 ||
  process.env.UNI_USING_NATIVE ||
  process.env.UNI_USING_V3_NATIVE
) {
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59
  provide['uni.getCurrentSubNVue'] = [path.resolve(__dirname,
    '../packages/uni-app-plus-nvue/dist/get-current-sub-nvue.js'), 'default']
  provide['uni.requireNativePlugin'] = [path.resolve(__dirname,
    '../packages/uni-app-plus-nvue/dist/require-native-plugin.js'), 'default']
}

fxy060608's avatar
fxy060608 已提交
60
if (!process.env.UNI_USING_V3 && !process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
61
  if (!process.env.UNI_USING_NATIVE) {
fxy060608's avatar
fxy060608 已提交
62
    provide.uni = [path.resolve(__dirname, uniPath), 'default']
fxy060608's avatar
fxy060608 已提交
63 64 65
  }

  if (process.env.UNI_USING_V8) {
fxy060608's avatar
fxy060608 已提交
66
    provide.plus = [path.resolve(__dirname, uniPath), 'weexPlus']
fxy060608's avatar
fxy060608 已提交
67
  }
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73
}

if (
  process.env.UNI_PLATFORM === 'app-plus' &&
  process.env.UNI_USING_V8
) {
fxy060608's avatar
fxy060608 已提交
74 75
  provide.__f__ = [require.resolve('@dcloudio/vue-cli-plugin-uni/lib/format-log.js'), 'default']
  provide.crypto = [require.resolve('@dcloudio/vue-cli-plugin-uni/lib/crypto.js'), 'default']
fxy060608's avatar
fxy060608 已提交
76
}
77

fxy060608's avatar
fxy060608 已提交
78 79 80 81
const plugins = [
  new VueLoaderPlugin(),
  new webpack.DefinePlugin({
    'process.env': {
fxy060608's avatar
fxy060608 已提交
82
      NODE_ENV: JSON.stringify(process.env.NODE_ENV),
83 84 85
      UNI_APP_ID: JSON.stringify(process.env.UNI_APP_ID),
      UNI_APP_NAME: JSON.stringify(process.env.UNI_APP_NAME),
      UNI_PLATFORM: JSON.stringify(process.env.UNI_PLATFORM),
fxy060608's avatar
fxy060608 已提交
86 87
      VUE_APP_PLATFORM: JSON.stringify(process.env.UNI_PLATFORM),
      UNI_CLOUD_PROVIDER: process.env.UNI_CLOUD_PROVIDER,
雪洛's avatar
雪洛 已提交
88 89
      UNICLOUD_DEBUG: process.env.UNICLOUD_DEBUG,
      RUN_BY_HBUILDERX: process.env.RUN_BY_HBUILDERX,
90
      UNI_AUTOMATOR_WS_ENDPOINT: JSON.stringify(process.env.UNI_AUTOMATOR_WS_ENDPOINT),
91
      UNI_STAT_UNI_CLOUD: process.env.UNI_STAT_UNI_CLOUD || '""',
M
mehaotian 已提交
92 93 94
      UNI_STATISTICS_CONFIG: process.env.UNI_STATISTICS_CONFIG || '""',
      UNI_STAT_DEBUG: process.env.UNI_STAT_DEBUG || '""',
      UNI_COMPILER_VERSION: JSON.stringify(process.env.UNI_COMPILER_VERSION)
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103
    }
  }),
  new webpack.BannerPlugin({
    banner: '"use weex:vue";',
    raw: true,
    exclude: 'Vue'
  }),
  new webpack.ProvidePlugin(provide),
  new WebpackErrorsPlugin({
104 105
    onErrors,
    onWarnings
fxy060608's avatar
fxy060608 已提交
106
  }),
107
  new WebpackAppPlusNVuePlugin()
fxy060608's avatar
fxy060608 已提交
108 109
]

fxy060608's avatar
fxy060608 已提交
110 111 112 113
if (process.env.NODE_ENV === 'development') {
  plugins.push(require('@dcloudio/uni-cli-shared/lib/source-map').createEvalSourceMapDevToolPlugin())
}

114
// const excludeModuleReg = /node_modules(?!(\/|\\).*(weex).*)/
fxy060608's avatar
fxy060608 已提交
115 116

const rules = [{
fxy060608's avatar
fxy060608 已提交
117
  test: /\.(png|jpg|gif|ttf|eot|woff|woff2)$/i,
fxy060608's avatar
fxy060608 已提交
118
  use: [fileLoader]
fxy060608's avatar
fxy060608 已提交
119
}, {
fxy060608's avatar
fxy060608 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  test: path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'),
  use: [{
    loader: '@dcloudio/webpack-uni-pages-loader'
  }],
  type: 'javascript/auto'
}, {
  test: path.resolve(process.env.UNI_INPUT_DIR, getNVueMainEntry()),
  use: [{
    loader: '@dcloudio/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main'
  }]
}, {
  test: /\.js$/,
  use: [{
    loader: 'babel-loader',
    options: {
      babelrc: false
    }
  },
138
    jsPreprocessorLoader
139 140 141 142
  ]
  // exclude (modulePath) { // nvue js均提供babel,否则还得提供transpileDependencies配置
  //   return excludeModuleReg.test(modulePath) && modulePath.indexOf('@dcloudio') === -1
  // }
fxy060608's avatar
fxy060608 已提交
143 144
},
{
fxy060608's avatar
fxy060608 已提交
145
  test: [/\.nvue(\?[^?]+)?$/, /\.vue(\?[^?]+)?$/],
fxy060608's avatar
fxy060608 已提交
146
  use: [{
fxy060608's avatar
fxy060608 已提交
147
    loader: require.resolve('@dcloudio/vue-cli-plugin-uni/packages/vue-loader'),
fxy060608's avatar
fxy060608 已提交
148
    options: vueLoaderOptions
fxy060608's avatar
fxy060608 已提交
149
  }]
fxy060608's avatar
fxy060608 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
},
{
  test: /\.pug$/,
  oneOf: [{
    resourceQuery: /vue/,
    use: [{
      loader: 'pug-plain-loader'
    }]
  },
  {
    use: [{
      loader: 'raw-loader'
    }, {
      loader: 'pug-plain-loader'
    }]
  }
  ]
},
{
  resourceQuery: /vue&type=template/,
  use: [htmlPreprocessorLoader]
}
].concat(cssLoaders)

fxy060608's avatar
fxy060608 已提交
174
if (process.env.UNI_USING_NVUE_COMPILER || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
175 176 177 178 179 180
  rules.unshift({
    resourceQuery: function (query) {
      return query.indexOf('vue&type=template') !== -1 && query.indexOf('mpType=page') !== -1
    },
    use: [{
      loader: '@dcloudio/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/template'
181 182
    }, {
      loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-app-loader/page-meta'
fxy060608's avatar
fxy060608 已提交
183 184 185
    }]
  })
}
fxy060608's avatar
fxy060608 已提交
186 187 188 189 190 191 192 193
rules.unshift({
  resourceQuery: function (query) {
    return query.indexOf('vue&type=template') !== -1 && query.indexOf('mpType=page') === -1
  },
  use: [{
    loader: '@dcloudio/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/template.recycle'
  }]
})
194 195 196 197

if (process.env.UNI_USING_V3_NATIVE) {
  try {
    const automatorJson = require.resolve('@dcloudio/uni-automator/dist/automator.json')
198
    const patterns = [{
199 200 201 202 203 204 205 206 207 208 209 210
      from: automatorJson,
      to: '../.automator/' + (process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM) +
        '/.automator.json',
      transform (content) {
        if (process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
          return JSON.stringify({
            version: require('@dcloudio/uni-automator/package.json').version,
            wsEndpoint: process.env.UNI_AUTOMATOR_WS_ENDPOINT
          })
        }
        return ''
      }
211 212
    }]
    plugins.push(new CopyWebpackPlugin(CopyWebpackPluginVersion > 5 ? { patterns } : patterns))
213
  } catch (e) { }
214
}
fxy060608's avatar
fxy060608 已提交
215

fxy060608's avatar
fxy060608 已提交
216
if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
217
  plugins.push(new WebpackUniMPPlugin())
218 219
  const assetsDir = 'static'
  const hybridDir = 'hybrid/html'
220
  const patterns = [{
221 222
    from: path.resolve(process.env.UNI_INPUT_DIR, assetsDir),
    to: assetsDir
fxy060608's avatar
fxy060608 已提交
223
  }]
fxy060608's avatar
fxy060608 已提交
224 225 226 227
  // 自动化测试时,不启用androidPrivacy.json
  if (!process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
    const androidPrivacyPath = path.resolve(process.env.UNI_INPUT_DIR, 'androidPrivacy.json')
    if (fs.existsSync(androidPrivacyPath)) {
228
      patterns.push({
fxy060608's avatar
fxy060608 已提交
229 230 231 232
        from: androidPrivacyPath,
        to: 'androidPrivacy.json'
      })
    }
fxy060608's avatar
fxy060608 已提交
233
  }
234
  const hybridHtmlPath = path.resolve(process.env.UNI_INPUT_DIR, hybridDir)
235
  if (fs.existsSync(hybridHtmlPath)) {
236
    patterns.push({
237
      from: hybridHtmlPath,
238
      to: hybridDir
239
    })
240
  }
fxy060608's avatar
fxy060608 已提交
241

242 243 244
  global.uniModules.forEach(module => {
    const modules = 'uni_modules/'
    const assets = modules + module + '/' + assetsDir
Q
qiang 已提交
245 246
    const assetsPath = path.resolve(process.env.UNI_INPUT_DIR, assets)
    if (fs.existsSync(assetsPath)) {
247
      patterns.push({
Q
qiang 已提交
248 249 250 251 252 253 254
        from: assetsPath,
        to: assets
      })
    }
    const hybridHtml = modules + module + '/' + hybridDir
    const hybridHtmlPath = path.resolve(process.env.UNI_INPUT_DIR, hybridHtml)
    if (fs.existsSync(hybridHtmlPath)) {
255
      patterns.push({
Q
qiang 已提交
256 257 258 259
        from: hybridHtmlPath,
        to: hybridHtml
      })
    }
260 261
  })

Q
qiang 已提交
262
  if (process.env.UNI_USING_NVUE_COMPILER) {
263
    patterns.push({
Q
qiang 已提交
264 265 266
      from: path.resolve(getTemplatePath(), 'common'),
      to: process.env.UNI_OUTPUT_DIR
    })
267
  } else if (process.env.UNI_USING_V3_NATIVE) {
268
    patterns.push({
269 270 271
      from: path.resolve(getTemplatePath(), 'weex'),
      to: process.env.UNI_OUTPUT_DIR
    })
Q
qiang 已提交
272 273 274 275 276 277 278 279
  } else {
    let nativeTemplatePath = path.resolve(process.env.UNI_HBUILDERX_PLUGINS, 'weapp-tools/template/v8-native')
    if (!fs.existsSync(nativeTemplatePath)) { // 兼容旧版本
      nativeTemplatePath = path.resolve(
        process.env.UNI_HBUILDERX_PLUGINS,
        'weapp-tools/template/v8'
      )
    }
280
    patterns.push({
Q
qiang 已提交
281 282 283 284 285 286 287 288
      from: nativeTemplatePath,
      to: process.env.UNI_OUTPUT_DIR
    }, {
      from: path.resolve(
        process.env.UNI_HBUILDERX_PLUGINS,
        'weapp-tools/template/common'
      ),
      to: process.env.UNI_OUTPUT_DIR,
289 290 291 292 293 294 295 296
      globOptions: {
        ignore: [
          '*.js',
          '*.json',
          '__uniapppicker.html',
          '__uniappview.html'
        ]
      }
Q
qiang 已提交
297 298
    })
  }
299
  plugins.push(new CopyWebpackPlugin(CopyWebpackPluginVersion > 5 ? { patterns } : patterns))
fxy060608's avatar
fxy060608 已提交
300 301
}

fxy060608's avatar
fxy060608 已提交
302 303 304 305 306 307 308 309 310 311
try {
  if (process.env.UNI_HBUILDERX_PLUGINS) {
    require(path.resolve(process.env.UNI_HBUILDERX_PLUGINS, 'uni_helpers/lib/bytenode'))
    const {
      W
    } = require(path.resolve(process.env.UNI_HBUILDERX_PLUGINS, 'uni_helpers'))
    plugins.push(new W({
      dir: process.env.UNI_INPUT_DIR
    }))
  }
312
} catch (e) { }
fxy060608's avatar
fxy060608 已提交
313

fxy060608's avatar
fxy060608 已提交
314
module.exports = function () {
fxy060608's avatar
fxy060608 已提交
315 316
  return {
    target: 'node', // 激活 vue-loader 的 isServer 逻辑
317
    mode: process.env.NODE_ENV,
fxy060608's avatar
fxy060608 已提交
318
    devtool: false,
fxy060608's avatar
fxy060608 已提交
319
    watch: process.env.NODE_ENV === 'development',
fxy060608's avatar
fxy060608 已提交
320 321 322
    entry () {
      return process.UNI_NVUE_ENTRY
    },
fxy060608's avatar
fxy060608 已提交
323
    externals: {
fxy060608's avatar
fxy060608 已提交
324
      vue: 'Vue'
325 326 327
    },
    performance: {
      hints: false
fxy060608's avatar
fxy060608 已提交
328
    },
329
    optimization: Object.assign({
330 331 332 333 334 335 336 337 338
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            output: {
              ascii_only: true
            }
          }
        })
      ]
339
    }, webpack.version[0] > 4 ? {} : { namedModules: false }),
fxy060608's avatar
fxy060608 已提交
340 341
    output: {
      path: process.env.UNI_OUTPUT_DIR,
fxy060608's avatar
fxy060608 已提交
342
      filename: '[name].js'
fxy060608's avatar
fxy060608 已提交
343 344 345 346
    },
    resolve: {
      extensions: ['.js', '.nvue', '.vue', '.json'],
      alias: {
347 348 349
        '@/pages.json': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') + '?' + JSON.stringify({
          type: 'origin-pages-json'
        }),
fxy060608's avatar
fxy060608 已提交
350
        '@': process.env.UNI_INPUT_DIR,
Q
qiang 已提交
351
        'uni-polyfill': require.resolve('@dcloudio/uni-cli-shared/lib/uni-polyfill.js'),
352
        'uni-pages': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'),
fxy060608's avatar
fxy060608 已提交
353 354 355
        'uni-app-style': path.resolve(process.env.UNI_INPUT_DIR, getNVueMainEntry()) + '?' + JSON.stringify({
          type: 'appStyle'
        }),
fxy060608's avatar
fxy060608 已提交
356 357 358 359 360 361 362
        'uni-stat-config': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') +
          '?' +
          JSON.stringify({
            type: 'stat'
          })
      },
      modules: [
fxy060608's avatar
fxy060608 已提交
363
        'node_modules',
fxy060608's avatar
fxy060608 已提交
364
        path.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
fxy060608's avatar
fxy060608 已提交
365 366 367 368 369
        path.resolve(process.env.UNI_INPUT_DIR, 'node_modules')
      ]
    },
    resolveLoader: {
      alias: {
fxy060608's avatar
fxy060608 已提交
370 371 372
        'babel-loader': require.resolve('babel-loader', {
          paths: [require.resolve('@vue/cli-plugin-babel')]
        })
fxy060608's avatar
fxy060608 已提交
373 374 375 376 377 378 379 380 381 382
      }
    },
    module: {
      rules
    },
    plugins,
    stats: {
      reasons: true,
      errorDetails: true
    },
383
    node: webpack.version[0] > 4 ? false : {
fxy060608's avatar
fxy060608 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
      global: false,
      Buffer: false,
      __filename: false,
      __dirname: false,
      setImmediate: false,
      clearImmediate: false,
      assert: false,
      buffer: false,
      child_process: false,
      cluster: false,
      console: false,
      constants: false,
      crypto: false,
      dgram: false,
      dns: false,
      domain: false,
      events: false,
      fs: false,
      http: false,
      https: false,
      module: false,
      net: false,
      os: false,
      path: false,
      process: false,
      punycode: false,
      querystring: false,
      readline: false,
      repl: false,
      stream: false,
      string_decoder: false,
      sys: false,
      timers: false,
      tls: false,
      tty: false,
      url: false,
      util: false,
      vm: false,
      zlib: false
    }
  }
425
}