mp.js 5.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
const path = require('path')
const webpack = require('webpack')

const moduleAlias = require('module-alias')
// TODO 重写 vue scoped(若升级 vue 编译器,需要确认该文件路径是否发生变化)
moduleAlias.addAlias('./stylePlugins/scoped', path.resolve(__dirname, './scoped.js'))

const {
  parseEntry,
  getMainEntry,
  getPlatformExts,
  getPlatformCompiler,
  getPlatformCssnano
} = require('@dcloudio/uni-cli-shared')

function createUniMPPlugin () {
  if (process.env.UNI_USING_COMPONENTS) {
    const WebpackUniMPPlugin = require('@dcloudio/webpack-uni-mp-loader/lib/plugin/index-new')
    return new WebpackUniMPPlugin()
  }
  const WebpackUniMPPlugin = require('@dcloudio/webpack-uni-mp-loader/lib/plugin')
  return new WebpackUniMPPlugin()
}

function getProvides () {
  const uniPath = require.resolve('@dcloudio/uni-' + process.env.UNI_PLATFORM)
  const provides = {
    'uni': [uniPath, 'default']
  }

  if (process.env.UNI_USING_COMPONENTS) {
    provides['createApp'] = [uniPath, 'createApp']
    provides['createPage'] = [uniPath, 'createPage']
    provides['createComponent'] = [uniPath, 'createComponent']
  }

  if (
    process.env.UNI_PLATFORM === 'app-plus' &&
    process.env.UNI_USING_V8
  ) {
    provides['__f__'] = [path.resolve(__dirname, 'format-log.js'), 'default']
  }

  // TODO 目前依赖库 megalo 通过判断 wx 对象是否存在来识别平台做不同处理
  if (
    process.env.UNI_PLATFORM !== 'mp-qq' &&
    process.env.UNI_PLATFORM !== 'mp-weixin' &&
    process.env.UNI_PLATFORM !== 'app-plus'
  ) { // 非微信小程序,自动注入 wx 对象
    provides['wx'] = provides['uni']
  }
  return provides
}

module.exports = {
  vueConfig: {
    parallel: false
  },
  webpackConfig (webpackConfig) {
    if (!webpackConfig.optimization) {
      webpackConfig.optimization = {}
    }
    // disable noEmitOnErrors
    webpackConfig.optimization.noEmitOnErrors = false

    webpackConfig.optimization.runtimeChunk = {
      name: 'common/runtime'
    }

    webpackConfig.optimization.splitChunks = require('./split-chunks')()

    parseEntry()

    let devtool = false
    if (process.env.NODE_ENV !== 'production') {
      if (process.env.UNI_PLATFORM === 'app-plus') {
        if (process.env.UNI_USING_V8) {
          devtool = 'eval-source-map'
        } else {
          devtool = 'eval'
        }
      } else {
        devtool = 'sourcemap'
      }
    }

    return {
      devtool,
      mode: process.env.NODE_ENV,
      entry () {
        return process.UNI_ENTRY
      },
      output: {
        filename: '[name].js',
        chunkFilename: '[id].js',
        globalObject: process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'global',
        sourceMapFilename: '../.sourcemap/' + process.env.UNI_PLATFORM + '/[name].js.map'
      },
      resolve: {
        extensions: ['.nvue'],
        alias: { // 仅 mp-weixin
          'mpvue-page-factory': require.resolve(
            '@dcloudio/vue-cli-plugin-uni/packages/mpvue-page-factory')
        }
      },
      module: {
        rules: [{
          test: path.resolve(process.env.UNI_INPUT_DIR, getMainEntry()),
          use: [{
            loader: '@dcloudio/webpack-uni-mp-loader/lib/main'
          }]
        }, {
          resourceQuery: /vue&type=script/,
          use: [{
            loader: '@dcloudio/webpack-uni-mp-loader/lib/script'
          }]
        }, {
          resourceQuery: /vue&type=template/,
          use: [{
            loader: '@dcloudio/webpack-uni-mp-loader/lib/template'
          }]
fxy060608's avatar
fxy060608 已提交
122 123 124 125 126 127
        }, {
          resourceQuery: [/blockType=wxs/, /blockType=filter/, /blockType=import-sjs/],
          use: [{
            loader: require.resolve(
              '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-filter-loader')
          }]
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132 133 134 135 136
        }]
      },
      plugins: [
        createUniMPPlugin(),
        new webpack.ProvidePlugin(getProvides())
      ]
    }
  },
  chainWebpack (webpackConfig) {
fxy060608's avatar
fxy060608 已提交
137 138 139 140 141 142 143
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      webpackConfig.module
        .rule('js')
        .exclude
        .add(/\.filter\.js$/)
    }

fxy060608's avatar
fxy060608 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157
    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
      .test([/\.vue$/, /\.nvue$/])
      .use('vue-loader')
      .tap(options => Object.assign(options, {
        compiler: getPlatformCompiler(),
        compilerOptions: process.env.UNI_USING_COMPONENTS ? {
          preserveWhitespace: false
        } : require('./mp-compiler-options'),
        cacheDirectory: false,
        cacheIdentifier: false
      }))
      .end()
158 159 160 161 162 163
      .use('uniapp-custom-block-loader')
      .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-custom-block-loader'))
      .options({
        compiler: getPlatformCompiler()
      })
      .end()
fxy060608's avatar
fxy060608 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      .use('uniapp-nvue-loader')
      .loader(require.resolve('@dcloudio/webpack-uni-mp-loader/lib/style.js'))
      .end()
      .uses
      .delete('cache-loader')

    webpackConfig.plugin('extract-css')
      .init((Plugin, args) => new Plugin({
        filename: '[name]' + getPlatformExts().style
      }))

    if (process.env.NODE_ENV === 'production') {
      webpackConfig.plugin('optimize-css')
        .init((Plugin, args) => new Plugin({
          sourceMap: false,
          cssnanoOptions: {
            preset: [
              'default',
              getPlatformCssnano()
            ]
          }

        }))
    }

    webpackConfig.plugins.delete('hmr')
    webpackConfig.plugins.delete('html')
    webpackConfig.plugins.delete('copy')
    webpackConfig.plugins.delete('preload')
    webpackConfig.plugins.delete('prefetch')
  }
}