index.js 8.9 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 9
  getPlatformCompiler
} = require('@dcloudio/uni-cli-shared')

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

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

fxy060608's avatar
init v3  
fxy060608 已提交
16
const {
fxy060608's avatar
fxy060608 已提交
17 18
  isUnaryTag,
  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')
fxy060608's avatar
fxy060608 已提交
28

fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38
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 已提交
39 40
      'getRegExp': [wxsPath, 'getRegExp'],
      'uniCloud': [uniCloudPath, 'default']
fxy060608's avatar
fxy060608 已提交
41 42
    }
  }
fxy060608's avatar
fxy060608 已提交
43
  return { // app-view
fxy060608's avatar
fxy060608 已提交
44
    '__f__': [path.resolve(__dirname, '../format-log.js'), 'default'],
fxy060608's avatar
fxy060608 已提交
45 46
    'getDate': [wxsPath, 'getDate'],
    'getRegExp': [wxsPath, 'getRegExp']
fxy060608's avatar
init v3  
fxy060608 已提交
47 48 49 50 51
  }
}

const v3 = {
  vueConfig: {
fxy060608's avatar
fxy060608 已提交
52
    parallel: false,
fxy060608's avatar
fxy060608 已提交
53
    transpileDependencies: [
fxy060608's avatar
fxy060608 已提交
54
      wxsPath,
fxy060608's avatar
fxy060608 已提交
55 56
      runtimePath
    ]
fxy060608's avatar
init v3  
fxy060608 已提交
57
  },
fxy060608's avatar
fxy060608 已提交
58
  webpackConfig (webpackConfig, vueOptions, api) {
fxy060608's avatar
init v3  
fxy060608 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
    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 已提交
72
    if (isAppService) {
fxy060608's avatar
init v3  
fxy060608 已提交
73 74 75
      webpackConfig.optimization.runtimeChunk = {
        name: 'app-config'
      }
fxy060608's avatar
fxy060608 已提交
76
      webpackConfig.optimization.splitChunks = require('../split-chunks')()
fxy060608's avatar
init v3  
fxy060608 已提交
77 78
    } else if (isAppView) {
      webpackConfig.optimization.runtimeChunk = false
fxy060608's avatar
fxy060608 已提交
79
      webpackConfig.optimization.splitChunks = false
fxy060608's avatar
init v3  
fxy060608 已提交
80 81
    }

82
    let devtool = false
fxy060608's avatar
fxy060608 已提交
83 84 85 86

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

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

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

113 114 115 116 117 118 119 120 121
    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 已提交
122 123 124 125 126 127 128
    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 已提交
129 130
    return {
      devtool,
fxy060608's avatar
fxy060608 已提交
131
      mode: process.env.NODE_ENV,
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',
fxy060608's avatar
fxy060608 已提交
141
        globalObject: 'this'
fxy060608's avatar
init v3  
fxy060608 已提交
142 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: [{
            loader: isAppService ? 'wrap-loader' : path.resolve(__dirname,
fxy060608's avatar
fxy060608 已提交
159
              '../../packages/webpack-uni-app-loader/view/main.js'),
fxy060608's avatar
init v3  
fxy060608 已提交
160
            options: {
fxy060608's avatar
fxy060608 已提交
161
              compiler: getPlatformCompiler(),
fxy060608's avatar
init v3  
fxy060608 已提交
162
              before: [
fxy060608's avatar
fxy060608 已提交
163
                beforeCode + statCode + getGlobalUsingComponentsCode()
fxy060608's avatar
init v3  
fxy060608 已提交
164 165 166
              ]
            }
          }]
fxy060608's avatar
fxy060608 已提交
167
        },
fxy060608's avatar
fxy060608 已提交
168 169 170 171 172
        {
          resourceQuery: /vue&type=template/,
          use: [{
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/filter-modules-template.js')
173 174 175
          }, {
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/page-meta')
fxy060608's avatar
fxy060608 已提交
176 177
          }]
        },
fxy060608's avatar
fxy060608 已提交
178
        ...rules
fxy060608's avatar
fxy060608 已提交
179 180 181 182 183 184
          // v3 暂不支持 cache
          // createTemplateCacheLoader(api,
          //   isAppService
          //     ? 'uni-template-compiler-service'
          //     : 'uni-template-compiler-view'
          // )
fxy060608's avatar
fxy060608 已提交
185
        ]
fxy060608's avatar
init v3  
fxy060608 已提交
186 187
      },
      plugins: [
fxy060608's avatar
fxy060608 已提交
188
        new WebpackUniAppPlugin(),
fxy060608's avatar
fxy060608 已提交
189
        new webpack.ProvidePlugin(getProvides(isAppService))
fxy060608's avatar
init v3  
fxy060608 已提交
190 191 192
      ]
    }
  },
fxy060608's avatar
fxy060608 已提交
193
  chainWebpack (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
194
    webpackConfig.entryPoints.delete('app')
fxy060608's avatar
fxy060608 已提交
195

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

fxy060608's avatar
fxy060608 已提交
199 200 201 202 203 204 205 206 207 208 209
    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 已提交
210
    // 处理静态资源
fxy060608's avatar
fxy060608 已提交
211 212 213
    webpackConfig.module
      .rule('svg')
      .use('file-loader')
fxy060608's avatar
fxy060608 已提交
214 215
      .options(fileLoaderOptions)

fxy060608's avatar
fxy060608 已提交
216
    const staticTypes = ['images', 'media', 'fonts']
fxy060608's avatar
fxy060608 已提交
217 218 219 220 221 222 223 224 225
    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 已提交
226
            options: fileLoaderOptions
fxy060608's avatar
fxy060608 已提交
227
          }
fxy060608's avatar
fxy060608 已提交
228 229
        }))
    })
fxy060608's avatar
fxy060608 已提交
230

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

    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
      .test([/\.vue$/, /\.nvue$/])
fxy060608's avatar
fxy060608 已提交
254
      .use('vue-loader') //  service 层移除 style 节点,view 层返回固定 script
fxy060608's avatar
fxy060608 已提交
255
      .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/vue-loader'))
fxy060608's avatar
init v3  
fxy060608 已提交
256
      .tap(options => Object.assign(options, {
fxy060608's avatar
fxy060608 已提交
257
        isAppService,
fxy060608's avatar
init v3  
fxy060608 已提交
258 259
        isAppView,
        compiler: getPlatformCompiler(),
fxy060608's avatar
fxy060608 已提交
260
        compilerOptions
fxy060608's avatar
fxy060608 已提交
261
      }, cacheConfig))
fxy060608's avatar
fxy060608 已提交
262
      .end()
fxy060608's avatar
fxy060608 已提交
263 264 265 266 267 268 269
    // .use('uniapp-custom-block-loader')
    // .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-custom-block-loader'))
    // .options({
    //   isAppService,
    //   isAppView,
    //   compiler: getPlatformCompiler()
    // })
fxy060608's avatar
fxy060608 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

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

fxy060608's avatar
fxy060608 已提交
287 288 289 290 291
    if (isAppView) {
      if (process.env.NODE_ENV === 'production') {
        require('../h5/cssnano-options')(webpackConfig)
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
292

293 294 295 296 297 298
    if (isAppService) { // service 层移除 css 相关
      ['css', 'postcss', 'scss', 'sass', 'less', 'stylus'].forEach(cssLang => {
        webpackConfig.module.rules.delete(cssLang)
      })
    }

fxy060608's avatar
init v3  
fxy060608 已提交
299 300 301 302 303 304 305 306 307 308 309 310
    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')
}