webpack.nvue.conf.js 7.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12
const path = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const {
  getNVueMainEntry,
  nvueJsPreprocessOptions,
  nvueHtmlPreprocessOptions,
  devtoolModuleFilenameTemplate
} = require('@dcloudio/uni-cli-shared')

13
const WebpackAppPlusNVuePlugin = require('../packages/webpack-app-plus-nvue-plugin')
fxy060608's avatar
fxy060608 已提交
14
const WebpackErrorsPlugin = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-errors-plugin')
fxy060608's avatar
fxy060608 已提交
15
const WebpackUniMPPlugin = require('@dcloudio/webpack-uni-mp-loader/lib/plugin/index-new')
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

const onErrors = require('@dcloudio/vue-cli-plugin-uni/util/on-errors')

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'

const provide = {}

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']
}

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']
}
52

fxy060608's avatar
fxy060608 已提交
53 54 55 56
const plugins = [
  new VueLoaderPlugin(),
  new webpack.DefinePlugin({
    'process.env': {
57
      'NODE_ENV': JSON.stringify(process.env.NODE_ENV),
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64 65 66 67 68 69
      'VUE_APP_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM)
    }
  }),
  new webpack.BannerPlugin({
    banner: '"use weex:vue";',
    raw: true,
    exclude: 'Vue'
  }),
  new webpack.ProvidePlugin(provide),
  new WebpackErrorsPlugin({
    onErrors
  }),
70
  new WebpackAppPlusNVuePlugin()
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
]

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
    }
  },
  jsPreprocessorLoader
fxy060608's avatar
fxy060608 已提交
95 96 97
  ],
  exclude (modulePath) {
    return excludeModuleReg.test(modulePath) && modulePath.indexOf('@dcloudio') === -1
98
  }
fxy060608's avatar
fxy060608 已提交
99 100 101 102
},
{
  test: /\.nvue(\?[^?]+)?$/,
  use: [{
fxy060608's avatar
fxy060608 已提交
103
    loader: path.resolve(__dirname, '../packages/vue-loader'),
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108 109 110
    options: vueLoaderOptions
  }],
  exclude: excludeModuleReg
},
{
  test: /\.vue(\?[^?]+)?$/,
  use: [{
fxy060608's avatar
fxy060608 已提交
111
    loader: path.resolve(__dirname, '../packages/vue-loader'),
fxy060608's avatar
fxy060608 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    options: vueLoaderOptions
  }],
  exclude: excludeModuleReg
},
{
  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'
    }]
  })
}
fxy060608's avatar
fxy060608 已提交
149 150 151 152 153 154 155 156 157
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 已提交
158
if (process.env.UNI_USING_NATIVE) {
fxy060608's avatar
fxy060608 已提交
159
  plugins.push(new WebpackUniMPPlugin())
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  plugins.push(new CopyWebpackPlugin([{
    from: path.resolve(process.env.UNI_INPUT_DIR, 'static'),
    to: 'static'
  }, {
    from: path.resolve(
      process.env.UNI_HBUILDERX_PLUGINS,
      'weapp-tools/template/v8'
    ),
    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'
    ]
  }]))
}

fxy060608's avatar
fxy060608 已提交
184
module.exports = function () {
fxy060608's avatar
fxy060608 已提交
185 186
  return {
    target: 'node', // 激活 vue-loader 的 isServer 逻辑
187
    mode: process.env.NODE_ENV,
fxy060608's avatar
fxy060608 已提交
188 189
    devtool: process.env.NODE_ENV === 'development' ? 'inline-source-map' : false,
    watch: process.env.NODE_ENV === 'development',
fxy060608's avatar
fxy060608 已提交
190 191 192
    entry () {
      return process.UNI_NVUE_ENTRY
    },
fxy060608's avatar
fxy060608 已提交
193 194
    externals: {
      'vue': 'Vue'
fxy060608's avatar
fxy060608 已提交
195 196 197
    },
    performance: {
      hints: false
fxy060608's avatar
fxy060608 已提交
198 199
    },
    optimization: {
fxy060608's avatar
fxy060608 已提交
200
      namedModules: false
fxy060608's avatar
fxy060608 已提交
201 202 203 204 205 206 207 208 209 210
    },
    output: {
      path: process.env.UNI_OUTPUT_DIR,
      filename: '[name].js',
      devtoolModuleFilenameTemplate
    },
    resolve: {
      extensions: ['.js', '.nvue', '.vue', '.json'],
      alias: {
        '@': process.env.UNI_INPUT_DIR,
211
        'uni-pages': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'),
fxy060608's avatar
fxy060608 已提交
212
        '@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat'),
fxy060608's avatar
fxy060608 已提交
213 214 215
        'uni-app-style': path.resolve(process.env.UNI_INPUT_DIR, getNVueMainEntry()) + '?' + JSON.stringify({
          type: 'appStyle'
        }),
fxy060608's avatar
fxy060608 已提交
216 217 218 219 220 221 222
        'uni-stat-config': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') +
          '?' +
          JSON.stringify({
            type: 'stat'
          })
      },
      modules: [
fxy060608's avatar
fxy060608 已提交
223
        'node_modules',
fxy060608's avatar
fxy060608 已提交
224
        path.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
fxy060608's avatar
fxy060608 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        path.resolve(process.env.UNI_INPUT_DIR, 'node_modules')
      ]
    },
    resolveLoader: {
      alias: {
        'babel-loader': require.resolve('babel-loader')
      }
    },
    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
    }
  }
}