webpack.nvue.conf.js 9.1 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 6 7 8 9 10
const CopyWebpackPlugin = require('copy-webpack-plugin')

const {
  getNVueMainEntry,
  nvueJsPreprocessOptions,
  nvueHtmlPreprocessOptions,
Q
qiang 已提交
11 12
  devtoolModuleFilenameTemplate,
  getTemplatePath
fxy060608's avatar
fxy060608 已提交
13 14
} = require('@dcloudio/uni-cli-shared')

fxy060608's avatar
fxy060608 已提交
15 16 17 18
const WebpackAppPlusNVuePlugin = process.env.UNI_USING_V3
  ? require('../packages/webpack-app-plus-plugin')
  : require('../packages/webpack-app-plus-nvue-plugin')

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

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

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 已提交
42 43 44 45 46
const uniCloudPath = require.resolve('@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js')

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

fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
if (process.env.UNI_USING_V3 || process.env.UNI_USING_NATIVE) {
  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']
}

if (!process.env.UNI_USING_V3) {
  if (!process.env.UNI_USING_NATIVE) {
    provide['uni'] = [path.resolve(__dirname, uniPath), 'default']
  }

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

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

fxy060608's avatar
fxy060608 已提交
73 74 75 76
const plugins = [
  new VueLoaderPlugin(),
  new webpack.DefinePlugin({
    'process.env': {
77
      'NODE_ENV': JSON.stringify(process.env.NODE_ENV),
fxy060608's avatar
fxy060608 已提交
78 79
      'VUE_APP_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM),
      'UNI_CLOUD_PROVIDER': process.env.UNI_CLOUD_PROVIDER,
雪洛's avatar
雪洛 已提交
80
      'HBX_USER_TOKEN': JSON.stringify(process.env.HBX_USER_TOKEN || '')
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89
    }
  }),
  new webpack.BannerPlugin({
    banner: '"use weex:vue";',
    raw: true,
    exclude: 'Vue'
  }),
  new webpack.ProvidePlugin(provide),
  new WebpackErrorsPlugin({
90 91
    onErrors,
    onWarnings
fxy060608's avatar
fxy060608 已提交
92
  }),
93
  new WebpackAppPlusNVuePlugin()
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
]

const excludeModuleReg = /node_modules(?!(\/|\\).*(weex).*)/

const rules = [{
  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
    }
  },
117
  jsPreprocessorLoader
fxy060608's avatar
fxy060608 已提交
118 119 120
  ],
  exclude (modulePath) {
    return excludeModuleReg.test(modulePath) && modulePath.indexOf('@dcloudio') === -1
121
  }
fxy060608's avatar
fxy060608 已提交
122 123
},
{
fxy060608's avatar
fxy060608 已提交
124
  test: [/\.nvue(\?[^?]+)?$/, /\.vue(\?[^?]+)?$/],
fxy060608's avatar
fxy060608 已提交
125
  use: [{
fxy060608's avatar
fxy060608 已提交
126
    loader: require.resolve('@dcloudio/vue-cli-plugin-uni/packages/vue-loader'),
fxy060608's avatar
fxy060608 已提交
127
    options: vueLoaderOptions
fxy060608's avatar
fxy060608 已提交
128
  }]
fxy060608's avatar
fxy060608 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
},
{
  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)

if (process.env.UNI_USING_NVUE_COMPILER) {
  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'
160 161
    }, {
      loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-app-loader/page-meta'
fxy060608's avatar
fxy060608 已提交
162 163 164
    }]
  })
}
fxy060608's avatar
fxy060608 已提交
165 166 167 168 169 170 171 172 173
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'
  }]
})

fxy060608's avatar
fxy060608 已提交
174
if (process.env.UNI_USING_NATIVE) {
fxy060608's avatar
fxy060608 已提交
175
  plugins.push(new WebpackUniMPPlugin())
Q
qiang 已提交
176
  const array = [{
fxy060608's avatar
fxy060608 已提交
177 178
    from: path.resolve(process.env.UNI_INPUT_DIR, 'static'),
    to: 'static'
Q
qiang 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  }]
  if (process.env.UNI_USING_NVUE_COMPILER) {
    array.push({
      from: path.resolve(getTemplatePath(), 'common'),
      to: process.env.UNI_OUTPUT_DIR
    }, {
      from: path.resolve(
        process.env.UNI_HBUILDERX_PLUGINS,
        'weapp-tools/template/common'
      ),
      to: process.env.UNI_OUTPUT_DIR,
      ignore: [
        '*.js',
        '*.json',
        '__uniapppicker.html',
        '__uniappview.html',
        '__uniappmarker@3x.png',
        '__uniappopenlocation.html',
        '__uniapppicker.html'
      ]
    })
  } 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'
      )
    }
    array.push({
      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,
      ignore: [
        '*.js',
        '*.json',
        '__uniapppicker.html',
        '__uniappview.html'
      ]
    })
  }
  plugins.push(new CopyWebpackPlugin(array))
fxy060608's avatar
fxy060608 已提交
226 227
}

fxy060608's avatar
fxy060608 已提交
228
module.exports = function () {
fxy060608's avatar
fxy060608 已提交
229 230
  return {
    target: 'node', // 激活 vue-loader 的 isServer 逻辑
231
    mode: process.env.NODE_ENV,
fxy060608's avatar
fxy060608 已提交
232 233
    devtool: process.env.NODE_ENV === 'development' ? 'inline-source-map' : false,
    watch: process.env.NODE_ENV === 'development',
fxy060608's avatar
fxy060608 已提交
234 235 236
    entry () {
      return process.UNI_NVUE_ENTRY
    },
fxy060608's avatar
fxy060608 已提交
237 238
    externals: {
      'vue': 'Vue'
239 240 241
    },
    performance: {
      hints: false
fxy060608's avatar
fxy060608 已提交
242 243
    },
    optimization: {
244
      namedModules: false
fxy060608's avatar
fxy060608 已提交
245 246 247 248 249 250 251 252 253 254
    },
    output: {
      path: process.env.UNI_OUTPUT_DIR,
      filename: '[name].js',
      devtoolModuleFilenameTemplate
    },
    resolve: {
      extensions: ['.js', '.nvue', '.vue', '.json'],
      alias: {
        '@': process.env.UNI_INPUT_DIR,
255
        'uni-pages': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'),
fxy060608's avatar
fxy060608 已提交
256
        '@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat'),
fxy060608's avatar
fxy060608 已提交
257 258 259
        'uni-app-style': path.resolve(process.env.UNI_INPUT_DIR, getNVueMainEntry()) + '?' + JSON.stringify({
          type: 'appStyle'
        }),
fxy060608's avatar
fxy060608 已提交
260 261 262 263 264 265 266
        'uni-stat-config': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') +
          '?' +
          JSON.stringify({
            type: 'stat'
          })
      },
      modules: [
fxy060608's avatar
fxy060608 已提交
267
        'node_modules',
fxy060608's avatar
fxy060608 已提交
268
        path.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
fxy060608's avatar
fxy060608 已提交
269 270 271 272 273
        path.resolve(process.env.UNI_INPUT_DIR, 'node_modules')
      ]
    },
    resolveLoader: {
      alias: {
fxy060608's avatar
fxy060608 已提交
274 275 276
        'babel-loader': require.resolve('babel-loader', {
          paths: [require.resolve('@vue/cli-plugin-babel')]
        })
fxy060608's avatar
fxy060608 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
      }
    },
    module: {
      rules
    },
    plugins,
    stats: {
      reasons: true,
      errorDetails: true
    },
    node: {
      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
    }
  }
fxy060608's avatar
fxy060608 已提交
329
}