index.js 6.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10
const path = require('path')
const webpack = require('webpack')

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

fxy060608's avatar
fxy060608 已提交
11
const WebpackUniAppPlugin = require('../../packages/webpack-uni-app-loader/plugin/index')
fxy060608's avatar
fxy060608 已提交
12

fxy060608's avatar
fxy060608 已提交
13
const modifyVueLoader = require('../vue-loader')
fxy060608's avatar
fxy060608 已提交
14

fxy060608's avatar
fxy060608 已提交
15
const {
fxy060608's avatar
fxy060608 已提交
16
  createTemplateCacheLoader
fxy060608's avatar
fxy060608 已提交
17
} = require('../cache-loader')
fxy060608's avatar
fxy060608 已提交
18

fxy060608's avatar
fxy060608 已提交
19
function createUniMPPlugin () {
20
  const WebpackUniMPPlugin = require('@dcloudio/webpack-uni-mp-loader/lib/plugin/index-new')
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25
  return new WebpackUniMPPlugin()
}

function getProvides () {
  const uniPath = require.resolve('@dcloudio/uni-' + process.env.UNI_PLATFORM)
fxy060608's avatar
fxy060608 已提交
26
  const uniCloudPath = path.resolve(__dirname, '../../packages/uni-cloud/dist/index.js')
fxy060608's avatar
fxy060608 已提交
27
  const provides = {
fxy060608's avatar
fxy060608 已提交
28 29
    uni: [uniPath, 'default'],
    uniCloud: [uniCloudPath, 'default']
fxy060608's avatar
fxy060608 已提交
30 31 32
  }

  if (process.env.UNI_USING_COMPONENTS) {
fxy060608's avatar
fxy060608 已提交
33 34 35
    provides.createApp = [uniPath, 'createApp']
    provides.createPage = [uniPath, 'createPage']
    provides.createComponent = [uniPath, 'createComponent']
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41
  }

  if (
    process.env.UNI_PLATFORM === 'app-plus' &&
    process.env.UNI_USING_V8
  ) {
fxy060608's avatar
fxy060608 已提交
42
    provides.__f__ = [path.resolve(__dirname, '../format-log.js'), 'default']
43

fxy060608's avatar
fxy060608 已提交
44
    const cryptoProvide = [path.resolve(__dirname, '../crypto.js'), 'default']
fxy060608's avatar
fxy060608 已提交
45
    provides.crypto = cryptoProvide
46 47
    provides['window.crypto'] = cryptoProvide
    provides['global.crypto'] = cryptoProvide
fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55
  }

  // TODO 目前依赖库 megalo 通过判断 wx 对象是否存在来识别平台做不同处理
  if (
    process.env.UNI_PLATFORM !== 'mp-qq' &&
    process.env.UNI_PLATFORM !== 'mp-weixin' &&
    process.env.UNI_PLATFORM !== 'app-plus'
  ) { // 非微信小程序,自动注入 wx 对象
fxy060608's avatar
fxy060608 已提交
56
    provides.wx = provides.uni
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61 62 63 64
  }
  return provides
}

module.exports = {
  vueConfig: {
    parallel: false
  },
fxy060608's avatar
fxy060608 已提交
65
  webpackConfig (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75
    if (!webpackConfig.optimization) {
      webpackConfig.optimization = {}
    }
    // disable noEmitOnErrors
    webpackConfig.optimization.noEmitOnErrors = false

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

fxy060608's avatar
fxy060608 已提交
76
    webpackConfig.optimization.splitChunks = require('../split-chunks')()
fxy060608's avatar
fxy060608 已提交
77 78 79

    parseEntry()

fxy060608's avatar
fxy060608 已提交
80
    const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : ''
fxy060608's avatar
fxy060608 已提交
81

fxy060608's avatar
fxy060608 已提交
82
    const beforeCode = 'import \'uni-pages\';'
fxy060608's avatar
fxy060608 已提交
83 84

    return {
fxy060608's avatar
fxy060608 已提交
85
      mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91 92 93
      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'
fxy060608's avatar
fxy060608 已提交
94 95 96
      },
      performance: {
        hints: false
fxy060608's avatar
fxy060608 已提交
97 98 99 100 101 102 103 104 105 106 107 108
      },
      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: [{
109
            loader: path.resolve(__dirname, '../../packages/wrap-loader'),
fxy060608's avatar
fxy060608 已提交
110 111
            options: {
              before: [
fxy060608's avatar
fxy060608 已提交
112
                beforeCode + require('../util').getAutomatorCode() + statCode
fxy060608's avatar
fxy060608 已提交
113 114 115
              ]
            }
          }, {
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121 122 123 124 125 126
            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'
127 128
          }, {
            loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-app-loader/page-meta'
fxy060608's avatar
fxy060608 已提交
129
          }]
fxy060608's avatar
fxy060608 已提交
130
        }, createTemplateCacheLoader(api), {
fxy060608's avatar
fxy060608 已提交
131 132 133
          resourceQuery: [
            /lang=wxs/,
            /lang=filter/,
fxy060608's avatar
fxy060608 已提交
134
            /lang=sjs/,
fxy060608's avatar
fxy060608 已提交
135 136
            /blockType=wxs/,
            /blockType=filter/,
fxy060608's avatar
fxy060608 已提交
137
            /blockType=sjs/
fxy060608's avatar
fxy060608 已提交
138
          ],
fxy060608's avatar
fxy060608 已提交
139 140 141 142
          use: [{
            loader: require.resolve(
              '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-filter-loader')
          }]
fxy060608's avatar
fxy060608 已提交
143 144 145
        }]
      },
      plugins: [
fxy060608's avatar
fxy060608 已提交
146
        new WebpackUniAppPlugin(),
fxy060608's avatar
fxy060608 已提交
147 148 149 150 151
        createUniMPPlugin(),
        new webpack.ProvidePlugin(getProvides())
      ]
    }
  },
fxy060608's avatar
fxy060608 已提交
152
  chainWebpack (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
153 154 155 156 157 158 159
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      webpackConfig.module
        .rule('js')
        .exclude
        .add(/\.filter\.js$/)
    }

fxy060608's avatar
fxy060608 已提交
160
    const compilerOptions = process.env.UNI_USING_COMPONENTS ? {} : require('../mp-compiler-options')
fxy060608's avatar
fxy060608 已提交
161

162
    modifyVueLoader(webpackConfig, {}, compilerOptions, api)
fxy060608's avatar
fxy060608 已提交
163

fxy060608's avatar
fxy060608 已提交
164 165
    const styleExt = getPlatformExts().style

fxy060608's avatar
fxy060608 已提交
166 167
    webpackConfig.plugin('extract-css')
      .init((Plugin, args) => new Plugin({
fxy060608's avatar
fxy060608 已提交
168
        filename: '[name]' + styleExt
fxy060608's avatar
fxy060608 已提交
169 170
      }))

fxy060608's avatar
fxy060608 已提交
171 172 173 174
    if (
      process.env.NODE_ENV === 'production' &&
      process.env.UNI_PLATFORM !== 'app-plus'
    ) {
fxy060608's avatar
fxy060608 已提交
175
      const OptimizeCssnanoPlugin = require('../../packages/@intervolga/optimize-cssnano-plugin/index.js')
fxy060608's avatar
fxy060608 已提交
176
      webpackConfig.plugin('optimize-css')
fxy060608's avatar
fxy060608 已提交
177
        .init((Plugin, args) => new OptimizeCssnanoPlugin({
fxy060608's avatar
fxy060608 已提交
178
          sourceMap: false,
fxy060608's avatar
fxy060608 已提交
179 180 181
          filter (assetName) {
            return path.extname(assetName) === styleExt
          },
fxy060608's avatar
fxy060608 已提交
182 183 184
          cssnanoOptions: {
            preset: [
              'default',
fxy060608's avatar
fxy060608 已提交
185 186 187
              Object.assign({}, getPlatformCssnano(), {
                discardComments: true
              })
fxy060608's avatar
fxy060608 已提交
188 189 190 191 192 193 194 195 196 197 198 199
            ]
          }

        }))
    }

    webpackConfig.plugins.delete('hmr')
    webpackConfig.plugins.delete('html')
    webpackConfig.plugins.delete('copy')
    webpackConfig.plugins.delete('preload')
    webpackConfig.plugins.delete('prefetch')
  }
fxy060608's avatar
fxy060608 已提交
200
}