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

const {
  getMainEntry,
fxy060608's avatar
fxy060608 已提交
6
  isInHBuilderX
fxy060608's avatar
init v3  
fxy060608 已提交
7 8
} = require('@dcloudio/uni-cli-shared')

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

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

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

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

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

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

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

const v3 = {
  vueConfig: {
fxy060608's avatar
fxy060608 已提交
56
    parallel: false,
fxy060608's avatar
fxy060608 已提交
57
    transpileDependencies: [
fxy060608's avatar
fxy060608 已提交
58
      wxsPath,
fxy060608's avatar
fxy060608 已提交
59 60
      runtimePath
    ]
fxy060608's avatar
init v3  
fxy060608 已提交
61
  },
fxy060608's avatar
fxy060608 已提交
62
  webpackConfig (webpackConfig, vueOptions, api) {
fxy060608's avatar
init v3  
fxy060608 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
    const isAppService = !!vueOptions.pluginOptions['uni-app-plus']['service']
    const isAppView = !!vueOptions.pluginOptions['uni-app-plus']['view']

    const statCode = process.env.UNI_USING_STAT ? `import '@dcloudio/uni-stat';` : ''

    const beforeCode = `import 'uni-pages';`

    if (!webpackConfig.optimization) {
      webpackConfig.optimization = {}
    }
    // disable noEmitOnErrors
    webpackConfig.optimization.noEmitOnErrors = false

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

86
    let devtool = false
fxy060608's avatar
fxy060608 已提交
87 88 89 90

    if (isAppService && process.env.NODE_ENV !== 'production') {
      devtool = 'eval-source-map'
    }
fxy060608's avatar
init v3  
fxy060608 已提交
91

fxy060608's avatar
fxy060608 已提交
92 93
    const rules = []

fxy060608's avatar
fxy060608 已提交
94
    const scriptLoaders = []
fxy060608's avatar
fxy060608 已提交
95
    if (isAppView) {
fxy060608's avatar
fxy060608 已提交
96 97 98
      scriptLoaders.push({
        loader: path.resolve(__dirname,
          '../../packages/webpack-uni-app-loader/view/script')
fxy060608's avatar
fxy060608 已提交
99
      })
fxy060608's avatar
fxy060608 已提交
100
    }
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108
    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 已提交
109 110 111 112 113 114
    // TODO 临时方案,将 wxs 也编译至 service
    rules.push({
      resourceQuery: [/lang=wxs/, /blockType=wxs/],
      use: [{
        loader: path.resolve(__dirname, '../../packages/webpack-uni-filter-loader')
      }]
fxy060608's avatar
fxy060608 已提交
115
    })
fxy060608's avatar
fxy060608 已提交
116

117 118 119 120 121 122 123 124 125
    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 已提交
126 127 128 129 130 131 132
    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 已提交
133 134
    return {
      devtool,
fxy060608's avatar
fxy060608 已提交
135
      mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
fxy060608's avatar
init v3  
fxy060608 已提交
136 137 138
      externals: {
        vue: 'Vue'
      },
fxy060608's avatar
fxy060608 已提交
139 140
      entry () {
        return entry
fxy060608's avatar
fxy060608 已提交
141
      },
fxy060608's avatar
init v3  
fxy060608 已提交
142 143
      output: {
        filename: '[name].js',
fxy060608's avatar
fxy060608 已提交
144
        chunkFilename: '[id].js',
fxy060608's avatar
fxy060608 已提交
145
        globalObject: 'this'
fxy060608's avatar
init v3  
fxy060608 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      },
      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: [{
            loader: isAppService ? 'wrap-loader' : path.resolve(__dirname,
fxy060608's avatar
fxy060608 已提交
163
              '../../packages/webpack-uni-app-loader/view/main.js'),
fxy060608's avatar
init v3  
fxy060608 已提交
164
            options: {
fxy060608's avatar
fxy060608 已提交
165
              compiler: vueLoader.compiler,
fxy060608's avatar
init v3  
fxy060608 已提交
166
              before: [
fxy060608's avatar
fxy060608 已提交
167
                beforeCode + statCode + getGlobalUsingComponentsCode()
fxy060608's avatar
init v3  
fxy060608 已提交
168 169 170
              ]
            }
          }]
fxy060608's avatar
fxy060608 已提交
171
        },
fxy060608's avatar
fxy060608 已提交
172 173 174 175 176
        {
          resourceQuery: /vue&type=template/,
          use: [{
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/filter-modules-template.js')
177 178 179
          }, {
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/page-meta')
fxy060608's avatar
fxy060608 已提交
180 181
          }]
        },
fxy060608's avatar
fxy060608 已提交
182
        ...rules
fxy060608's avatar
fxy060608 已提交
183 184 185 186 187 188
          // v3 暂不支持 cache
          // createTemplateCacheLoader(api,
          //   isAppService
          //     ? 'uni-template-compiler-service'
          //     : 'uni-template-compiler-view'
          // )
fxy060608's avatar
fxy060608 已提交
189
        ]
fxy060608's avatar
init v3  
fxy060608 已提交
190 191
      },
      plugins: [
fxy060608's avatar
fxy060608 已提交
192
        new WebpackUniAppPlugin(),
fxy060608's avatar
fxy060608 已提交
193
        new webpack.ProvidePlugin(getProvides(isAppService))
fxy060608's avatar
init v3  
fxy060608 已提交
194 195 196
      ]
    }
  },
fxy060608's avatar
fxy060608 已提交
197
  chainWebpack (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
198
    webpackConfig.entryPoints.delete('app')
fxy060608's avatar
fxy060608 已提交
199

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

fxy060608's avatar
fxy060608 已提交
203 204 205 206 207 208 209 210 211 212 213
    const fileLoaderOptions = isInHBuilderX ? {
      emitFile: isAppView,
      name: '[path][name].[ext]',
      context: process.env.UNI_INPUT_DIR
    } : {
      emitFile: isAppView,
      outputPath (url, resourcePath, context) {
        return path.relative(process.env.UNI_INPUT_DIR, resourcePath)
      }
    }

fxy060608's avatar
fxy060608 已提交
214
    // 处理静态资源
fxy060608's avatar
fxy060608 已提交
215 216 217
    webpackConfig.module
      .rule('svg')
      .use('file-loader')
fxy060608's avatar
fxy060608 已提交
218 219
      .options(fileLoaderOptions)

fxy060608's avatar
fxy060608 已提交
220
    const staticTypes = ['images', 'media', 'fonts']
fxy060608's avatar
fxy060608 已提交
221 222 223 224 225 226 227 228 229
    staticTypes.forEach(staticType => {
      webpackConfig.module
        .rule(staticType)
        .use('url-loader')
        .loader('url-loader')
        .tap(options => Object.assign(options, {
          limit: 1,
          fallback: {
            loader: 'file-loader',
fxy060608's avatar
fxy060608 已提交
230
            options: fileLoaderOptions
fxy060608's avatar
fxy060608 已提交
231
          }
fxy060608's avatar
fxy060608 已提交
232 233
        }))
    })
fxy060608's avatar
fxy060608 已提交
234

fxy060608's avatar
fxy060608 已提交
235 236 237 238 239 240 241 242 243 244 245 246
    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 已提交
247
    const compilerOptions = {
fxy060608's avatar
init v3  
fxy060608 已提交
248 249 250
      preserveWhitespace: false,
      service: isAppService,
      view: isAppView
fxy060608's avatar
fxy060608 已提交
251
    }
fxy060608's avatar
init v3  
fxy060608 已提交
252 253 254 255

    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
256
      .test(vueLoader.test)
fxy060608's avatar
fxy060608 已提交
257
      .use('vue-loader') //  service 层移除 style 节点,view 层返回固定 script
258 259
      .loader(vueLoader.loader)
      .tap(options => Object.assign(options, vueLoader.options({
fxy060608's avatar
fxy060608 已提交
260
        isAppService,
261 262
        isAppView
      }, compilerOptions), cacheConfig))
fxy060608's avatar
fxy060608 已提交
263
      .end()
fxy060608's avatar
fxy060608 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    // 是否启用 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 已提交
280

fxy060608's avatar
fxy060608 已提交
281 282 283 284 285
    if (isAppView) {
      if (process.env.NODE_ENV === 'production') {
        require('../h5/cssnano-options')(webpackConfig)
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
286

287 288 289 290 291 292
    if (isAppService) { // service 层移除 css 相关
      ['css', 'postcss', 'scss', 'sass', 'less', 'stylus'].forEach(cssLang => {
        webpackConfig.module.rules.delete(cssLang)
      })
    }

fxy060608's avatar
init v3  
fxy060608 已提交
293 294 295 296 297 298 299 300 301 302 303 304
    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')
}