index.js 7.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 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
init v3  
fxy060608 已提交
14
const {
fxy060608's avatar
fxy060608 已提交
15 16
  isUnaryTag,
  getPartialIdentifier
fxy060608's avatar
init v3  
fxy060608 已提交
17 18
} = require('../util')

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

fxy060608's avatar
fxy060608 已提交
23
const runtimePath = '@dcloudio/uni-mp-weixin/dist/mp.js'
fxy060608's avatar
fxy060608 已提交
24
const wxsPath = '@dcloudio/uni-mp-weixin/dist/wxs.js'
fxy060608's avatar
fxy060608 已提交
25

fxy060608's avatar
fxy060608 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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'],
      'getRegExp': [wxsPath, 'getRegExp']
    }
  }
  return { // app-view
    'getDate': [wxsPath, 'getDate'],
    'getRegExp': [wxsPath, 'getRegExp']
fxy060608's avatar
init v3  
fxy060608 已提交
42 43 44 45 46
  }
}

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

    webpackConfig.optimization.splitChunks = false

fxy060608's avatar
fxy060608 已提交
77 78 79 80 81
    let devtool = false

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

fxy060608's avatar
fxy060608 已提交
83 84
    const rules = []

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

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

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

fxy060608's avatar
fxy060608 已提交
181 182 183 184 185 186 187 188 189 190 191
    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 已提交
192
    // 处理静态资源
fxy060608's avatar
fxy060608 已提交
193 194 195
    webpackConfig.module
      .rule('svg')
      .use('file-loader')
fxy060608's avatar
fxy060608 已提交
196 197
      .options(fileLoaderOptions)

fxy060608's avatar
fxy060608 已提交
198
    const staticTypes = ['images', 'media', 'fonts']
fxy060608's avatar
fxy060608 已提交
199 200 201 202 203 204 205 206 207
    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 已提交
208
            options: fileLoaderOptions
fxy060608's avatar
fxy060608 已提交
209
          }
fxy060608's avatar
fxy060608 已提交
210 211
        }))
    })
fxy060608's avatar
fxy060608 已提交
212

fxy060608's avatar
fxy060608 已提交
213 214 215 216 217 218 219 220 221 222 223 224
    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 已提交
225
    const compilerOptions = {
fxy060608's avatar
init v3  
fxy060608 已提交
226 227 228 229
      isUnaryTag,
      preserveWhitespace: false,
      service: isAppService,
      view: isAppView
fxy060608's avatar
fxy060608 已提交
230
    }
fxy060608's avatar
init v3  
fxy060608 已提交
231 232 233 234 235

    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
      .test([/\.vue$/, /\.nvue$/])
fxy060608's avatar
fxy060608 已提交
236
      .use('vue-loader') //  service 层移除 style 节点,view 层返回固定 script
fxy060608's avatar
init v3  
fxy060608 已提交
237 238
      .loader(path.resolve(__dirname, '../../packages/vue-loader/lib'))
      .tap(options => Object.assign(options, {
fxy060608's avatar
fxy060608 已提交
239
        isAppService,
fxy060608's avatar
init v3  
fxy060608 已提交
240 241
        isAppView,
        compiler: getPlatformCompiler(),
fxy060608's avatar
fxy060608 已提交
242
        compilerOptions
fxy060608's avatar
fxy060608 已提交
243
      }, cacheConfig))
fxy060608's avatar
fxy060608 已提交
244
      .end()
fxy060608's avatar
fxy060608 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      .use('uniapp-custom-block-loader')
      .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-custom-block-loader'))
      .options({
        compiler: getPlatformCompiler()
      })

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

fxy060608's avatar
fxy060608 已提交
267 268 269 270 271
    if (isAppView) {
      if (process.env.NODE_ENV === 'production') {
        require('../h5/cssnano-options')(webpackConfig)
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284

    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')
}