index.js 8.7 KB
Newer Older
fxy060608's avatar
init v3  
fxy060608 已提交
1 2 3 4
const path = require('path')
const webpack = require('webpack')

const {
fxy060608's avatar
fxy060608 已提交
5 6
  getMainEntry,
  getPlatformStat,
雪洛's avatar
雪洛 已提交
7
  getPlatformPush,
fxy060608's avatar
fxy060608 已提交
8
  getPlatformUniCloud
fxy060608's avatar
init v3  
fxy060608 已提交
9 10
} = require('@dcloudio/uni-cli-shared')

11 12
const vueLoader = require('@dcloudio/uni-cli-shared/lib/vue-loader')

fxy060608's avatar
fxy060608 已提交
13 14 15 16
const {
  getGlobalUsingComponentsCode
} = require('@dcloudio/uni-cli-shared/lib/pages')

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

fxy060608's avatar
init v3  
fxy060608 已提交
19
const {
fxy060608's avatar
fxy060608 已提交
20
  getPartialIdentifier
fxy060608's avatar
init v3  
fxy060608 已提交
21 22
} = require('../util')

fxy060608's avatar
fxy060608 已提交
23 24 25 26
// const {
//   createTemplateCacheLoader
// } = require('../cache-loader')

fxy060608's avatar
fxy060608 已提交
27
const runtimePath = '@dcloudio/uni-mp-weixin/dist/mp.js'
fxy060608's avatar
fxy060608 已提交
28
const wxsPath = '@dcloudio/uni-mp-weixin/dist/wxs.js'
fxy060608's avatar
fxy060608 已提交
29
const uniCloudPath = path.resolve(__dirname, '../../packages/uni-cloud/dist/index.js')
30
const cryptoPath = path.resolve(__dirname, '../crypto.js')
fxy060608's avatar
fxy060608 已提交
31

fxy060608's avatar
fxy060608 已提交
32 33 34
function getProvides (isAppService) {
  if (isAppService) {
    return { // app-service
fxy060608's avatar
fxy060608 已提交
35 36
      __f__: [path.resolve(__dirname, '../format-log.js'), 'default'],
      wx: [runtimePath, 'default'],
fxy060608's avatar
fxy060608 已提交
37
      'wx.nextTick': [runtimePath, 'nextTick'],
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44
      Page: [runtimePath, 'Page'],
      Component: [runtimePath, 'Component'],
      Behavior: [runtimePath, 'Behavior'],
      getDate: [wxsPath, 'getDate'],
      getRegExp: [wxsPath, 'getRegExp'],
      uniCloud: [uniCloudPath, 'default'],
      crypto: [cryptoPath, 'default'],
45 46
      'window.crypto': [cryptoPath, 'default'],
      'global.crypto': [cryptoPath, 'default']
fxy060608's avatar
fxy060608 已提交
47 48
    }
  }
fxy060608's avatar
fxy060608 已提交
49
  return { // app-view
fxy060608's avatar
fxy060608 已提交
50 51 52
    __f__: [path.resolve(__dirname, '../format-log.js'), 'default'],
    getDate: [wxsPath, 'getDate'],
    getRegExp: [wxsPath, 'getRegExp']
fxy060608's avatar
init v3  
fxy060608 已提交
53 54 55 56 57
  }
}

const v3 = {
  vueConfig: {
58
    parallel: false
fxy060608's avatar
init v3  
fxy060608 已提交
59
  },
fxy060608's avatar
fxy060608 已提交
60
  webpackConfig (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
61 62
    const isAppService = !!vueOptions.pluginOptions['uni-app-plus'].service
    const isAppView = !!vueOptions.pluginOptions['uni-app-plus'].view
fxy060608's avatar
init v3  
fxy060608 已提交
63

fxy060608's avatar
fxy060608 已提交
64
    const statCode = getPlatformStat()
fxy060608's avatar
fxy060608 已提交
65
    const pushCode = getPlatformPush()
雪洛's avatar
雪洛 已提交
66
    const uniCloudCode = getPlatformUniCloud()
fxy060608's avatar
init v3  
fxy060608 已提交
67

fxy060608's avatar
fxy060608 已提交
68
    const beforeCode = 'import \'uni-pages\';'
fxy060608's avatar
init v3  
fxy060608 已提交
69 70 71 72
    if (!webpackConfig.optimization) {
      webpackConfig.optimization = {}
    }
    // disable noEmitOnErrors
73 74 75 76 77
    if (webpack.version[0] > 4) {
      webpackConfig.optimization.emitOnErrors = true
    } else {
      webpackConfig.optimization.noEmitOnErrors = false
    }
fxy060608's avatar
init v3  
fxy060608 已提交
78

fxy060608's avatar
fxy060608 已提交
79
    if (isAppService) {
fxy060608's avatar
init v3  
fxy060608 已提交
80 81 82
      webpackConfig.optimization.runtimeChunk = {
        name: 'app-config'
      }
fxy060608's avatar
fxy060608 已提交
83
      webpackConfig.optimization.splitChunks = require('../split-chunks')()
fxy060608's avatar
init v3  
fxy060608 已提交
84 85
    } else if (isAppView) {
      webpackConfig.optimization.runtimeChunk = false
fxy060608's avatar
fxy060608 已提交
86
      webpackConfig.optimization.splitChunks = false
fxy060608's avatar
init v3  
fxy060608 已提交
87 88
    }

fxy060608's avatar
fxy060608 已提交
89 90
    const rules = []

fxy060608's avatar
fxy060608 已提交
91
    const scriptLoaders = []
fxy060608's avatar
fxy060608 已提交
92
    if (isAppView) {
fxy060608's avatar
fxy060608 已提交
93 94 95
      scriptLoaders.push({
        loader: path.resolve(__dirname,
          '../../packages/webpack-uni-app-loader/view/script')
fxy060608's avatar
fxy060608 已提交
96
      })
fxy060608's avatar
fxy060608 已提交
97
    }
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105
    scriptLoaders.push({
      loader: path.resolve(__dirname,
        '../../packages/webpack-uni-app-loader/using-components')
    })
    rules.push({ // 解析组件,css 等
      resourceQuery: /vue&type=script/,
      use: scriptLoaders
    })
fxy060608's avatar
fxy060608 已提交
106 107 108 109 110 111
    // TODO 临时方案,将 wxs 也编译至 service
    rules.push({
      resourceQuery: [/lang=wxs/, /blockType=wxs/],
      use: [{
        loader: path.resolve(__dirname, '../../packages/webpack-uni-filter-loader')
      }]
fxy060608's avatar
fxy060608 已提交
112
    })
fxy060608's avatar
fxy060608 已提交
113

114 115 116 117 118 119 120 121 122
    if (isAppService) {
      rules.push({
        test: [/\.css$/, /\.p(ost)?css$/, /\.scss$/, /\.sass$/, /\.less$/, /\.styl(us)?$/],
        use: [{
          loader: path.resolve(__dirname, '../../packages/webpack-uni-app-loader/service/style.js')
        }]
      })
    }

fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129
    const entry = {}
    if (isAppService) {
      entry['app-service'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry())
    } else if (isAppView) {
      entry['app-view'] = path.resolve(process.env.UNI_INPUT_DIR, getMainEntry())
    }

fxy060608's avatar
init v3  
fxy060608 已提交
130
    return {
fxy060608's avatar
fxy060608 已提交
131
      mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
fxy060608's avatar
init v3  
fxy060608 已提交
132 133 134
      externals: {
        vue: 'Vue'
      },
fxy060608's avatar
fxy060608 已提交
135 136
      entry () {
        return entry
fxy060608's avatar
fxy060608 已提交
137
      },
fxy060608's avatar
init v3  
fxy060608 已提交
138 139
      output: {
        filename: '[name].js',
fxy060608's avatar
fxy060608 已提交
140
        chunkFilename: '[id].js',
141
        // webpack5 use strict
Q
qiang 已提交
142
        globalObject: webpack.version[0] > 4 ? '(new Function("return this")())' : 'this'
fxy060608's avatar
init v3  
fxy060608 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      },
      performance: {
        hints: false
      },
      resolve: {
        extensions: ['.nvue']
      },
      resolveLoader: {
        alias: {
          'vue-style-loader': path.resolve(__dirname, '../../packages/app-vue-style-loader')
        }
      },
      module: {
        rules: [{
          test: path.resolve(process.env.UNI_INPUT_DIR, getMainEntry()),
          use: [{
159 160 161
            loader: isAppService
              ? path.resolve(__dirname, '../../packages/wrap-loader') : path.resolve(__dirname,
                '../../packages/webpack-uni-app-loader/view/main.js'),
fxy060608's avatar
init v3  
fxy060608 已提交
162
            options: {
fxy060608's avatar
fxy060608 已提交
163
              compiler: vueLoader.compiler,
fxy060608's avatar
init v3  
fxy060608 已提交
164
              before: [
雪洛's avatar
雪洛 已提交
165
                beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + uniCloudCode +
fxy060608's avatar
fxy060608 已提交
166
                  getGlobalUsingComponentsCode()
fxy060608's avatar
init v3  
fxy060608 已提交
167 168 169
              ]
            }
          }]
fxy060608's avatar
fxy060608 已提交
170
        },
fxy060608's avatar
fxy060608 已提交
171 172 173 174 175
        {
          resourceQuery: /vue&type=template/,
          use: [{
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/filter-modules-template.js')
176 177 178
          }, {
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/page-meta')
fxy060608's avatar
fxy060608 已提交
179 180
          }]
        },
fxy060608's avatar
fxy060608 已提交
181 182 183 184 185 186 187
        {
          type: 'javascript/auto',
          resourceQuery: /uts-proxy/,
          use: [{
            loader: require.resolve('@dcloudio/uni-cli-shared/lib/uts/uts-loader.js')
          }]
        },
fxy060608's avatar
fxy060608 已提交
188
        ...rules
fxy060608's avatar
fxy060608 已提交
189 190 191 192 193 194
          // v3 暂不支持 cache
          // createTemplateCacheLoader(api,
          //   isAppService
          //     ? 'uni-template-compiler-service'
          //     : 'uni-template-compiler-view'
          // )
fxy060608's avatar
fxy060608 已提交
195
        ]
fxy060608's avatar
init v3  
fxy060608 已提交
196 197
      },
      plugins: [
fxy060608's avatar
fxy060608 已提交
198
        new WebpackUniAppPlugin(),
fxy060608's avatar
fxy060608 已提交
199
        new webpack.ProvidePlugin(getProvides(isAppService))
fxy060608's avatar
init v3  
fxy060608 已提交
200 201 202
      ]
    }
  },
fxy060608's avatar
fxy060608 已提交
203
  chainWebpack (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
204
    webpackConfig.entryPoints.delete('app')
fxy060608's avatar
fxy060608 已提交
205

fxy060608's avatar
fxy060608 已提交
206 207
    const isAppService = !!vueOptions.pluginOptions['uni-app-plus'].service
    const isAppView = !!vueOptions.pluginOptions['uni-app-plus'].view
fxy060608's avatar
init v3  
fxy060608 已提交
208

fxy060608's avatar
fxy060608 已提交
209 210 211 212 213 214 215 216 217 218 219 220
    const cacheConfig = {
      cacheDirectory: false,
      cacheIdentifier: false
    }

    if (process.env.UNI_USING_CACHE) {
      Object.assign(cacheConfig, api.genCacheConfig(
        'vue-template-compiler/' + process.env.UNI_PLATFORM,
        getPartialIdentifier()
      ))
    }

fxy060608's avatar
fxy060608 已提交
221
    const compilerOptions = {
fxy060608's avatar
init v3  
fxy060608 已提交
222 223 224
      preserveWhitespace: false,
      service: isAppService,
      view: isAppView
fxy060608's avatar
fxy060608 已提交
225
    }
fxy060608's avatar
init v3  
fxy060608 已提交
226 227 228 229

    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
230
      .test(vueLoader.test)
fxy060608's avatar
fxy060608 已提交
231
      .use('vue-loader') //  service 层移除 style 节点,view 层返回固定 script
232 233
      .loader(vueLoader.loader)
      .tap(options => Object.assign(options, vueLoader.options({
fxy060608's avatar
fxy060608 已提交
234
        isAppService,
235 236
        isAppView
      }, compilerOptions), cacheConfig))
fxy060608's avatar
fxy060608 已提交
237
      .end()
fxy060608's avatar
fxy060608 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    // 是否启用 cache
    if (process.env.UNI_USING_CACHE) {
      webpackConfig.module
        .rule('vue')
        .use('cache-loader')
        .tap(options => Object.assign(options, api.genCacheConfig(
          'vue-loader/' + process.env.UNI_PLATFORM,
          getPartialIdentifier()
        )))
    } else {
      webpackConfig.module
        .rule('vue')
        .uses
        .delete('cache-loader')
    }
fxy060608's avatar
fxy060608 已提交
254

fxy060608's avatar
fxy060608 已提交
255
    if (isAppView) {
256 257 258 259 260 261 262 263
      if (process.env.UNI_USING_V3_SCOPED) {
        webpackConfig.module
          .rule('vue')
          .use('uniapp-app-style-scoped')
          .loader(path.resolve(__dirname,
            '../../packages/webpack-uni-app-loader/view/style'))
      }

fxy060608's avatar
fxy060608 已提交
264 265 266 267
      if (process.env.NODE_ENV === 'production') {
        require('../h5/cssnano-options')(webpackConfig)
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
268

269 270 271 272 273 274
    if (isAppService) { // service 层移除 css 相关
      ['css', 'postcss', 'scss', 'sass', 'less', 'stylus'].forEach(cssLang => {
        webpackConfig.module.rules.delete(cssLang)
      })
    }

Q
qiang 已提交
275 276
    webpackConfig.plugin('vue-loader').use(require(vueLoader.loader).VueLoaderPlugin)

fxy060608's avatar
init v3  
fxy060608 已提交
277 278 279 280 281 282 283 284 285 286 287
    webpackConfig.plugins.delete('hmr')
    webpackConfig.plugins.delete('html')
    webpackConfig.plugins.delete('copy')
    webpackConfig.plugins.delete('preload')
    webpackConfig.plugins.delete('prefetch')
  }
}
if (process.env.UNI_USING_V3) {
  module.exports = v3
} else {
  module.exports = require('../mp')
fxy060608's avatar
fxy060608 已提交
288
}