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

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

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

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
  getPartialIdentifier
fxy060608's avatar
init v3  
fxy060608 已提交
18 19
} = require('../util')

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

fxy060608's avatar
fxy060608 已提交
24
const runtimePath = '@dcloudio/uni-mp-weixin/dist/mp.js'
fxy060608's avatar
fxy060608 已提交
25
const wxsPath = '@dcloudio/uni-mp-weixin/dist/wxs.js'
fxy060608's avatar
fxy060608 已提交
26
const uniCloudPath = path.resolve(__dirname, '../../packages/uni-cloud/dist/index.js')
27
const cryptoPath = path.resolve(__dirname, '../crypto.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
      'getRegExp': [wxsPath, 'getRegExp'],
40 41 42 43
      'uniCloud': [uniCloudPath, 'default'],
      'crypto': [cryptoPath, 'default'],
      'window.crypto': [cryptoPath, 'default'],
      'global.crypto': [cryptoPath, 'default']
fxy060608's avatar
fxy060608 已提交
44 45
    }
  }
fxy060608's avatar
fxy060608 已提交
46
  return { // app-view
fxy060608's avatar
fxy060608 已提交
47
    '__f__': [path.resolve(__dirname, '../format-log.js'), 'default'],
fxy060608's avatar
fxy060608 已提交
48 49
    'getDate': [wxsPath, 'getDate'],
    'getRegExp': [wxsPath, 'getRegExp']
fxy060608's avatar
init v3  
fxy060608 已提交
50 51 52 53 54
  }
}

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

fxy060608's avatar
fxy060608 已提交
81 82
    const rules = []

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

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

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

fxy060608's avatar
fxy060608 已提交
192 193 194 195 196 197 198 199 200 201 202 203
    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 已提交
204
    const compilerOptions = {
fxy060608's avatar
init v3  
fxy060608 已提交
205 206 207
      preserveWhitespace: false,
      service: isAppService,
      view: isAppView
fxy060608's avatar
fxy060608 已提交
208
    }
fxy060608's avatar
init v3  
fxy060608 已提交
209 210 211 212

    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
213
      .test(vueLoader.test)
fxy060608's avatar
fxy060608 已提交
214
      .use('vue-loader') //  service 层移除 style 节点,view 层返回固定 script
215 216
      .loader(vueLoader.loader)
      .tap(options => Object.assign(options, vueLoader.options({
fxy060608's avatar
fxy060608 已提交
217
        isAppService,
218 219
        isAppView
      }, compilerOptions), cacheConfig))
fxy060608's avatar
fxy060608 已提交
220
      .end()
fxy060608's avatar
fxy060608 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

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

fxy060608's avatar
fxy060608 已提交
238 239 240 241 242
    if (isAppView) {
      if (process.env.NODE_ENV === 'production') {
        require('../h5/cssnano-options')(webpackConfig)
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
243

244 245 246 247 248 249
    if (isAppService) { // service 层移除 css 相关
      ['css', 'postcss', 'scss', 'sass', 'less', 'stylus'].forEach(cssLang => {
        webpackConfig.module.rules.delete(cssLang)
      })
    }

fxy060608's avatar
init v3  
fxy060608 已提交
250 251 252 253 254 255 256 257 258 259 260 261
    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')
}