index.js 4.6 KB
Newer Older
fxy060608's avatar
init v3  
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12
const path = require('path')
const webpack = require('webpack')

const {
  getMainEntry,
  getPlatformCompiler
} = require('@dcloudio/uni-cli-shared')

const {
  isUnaryTag
} = require('../util')

13
function getProvides () {
fxy060608's avatar
init v3  
fxy060608 已提交
14
  return {
fxy060608's avatar
fxy060608 已提交
15 16 17
    '__f__': [path.resolve(__dirname, '../format-log.js'), 'default'],
    'Behavior': ['@dcloudio/uni-mp-weixin/dist/mp.js', 'Behavior'],
    'Component': ['@dcloudio/uni-mp-weixin/dist/mp.js', 'Component']
fxy060608's avatar
init v3  
fxy060608 已提交
18 19 20 21 22 23 24
  }
}

const v3 = {
  vueConfig: {
    parallel: false
  },
25
  webpackConfig (webpackConfig, vueOptions) {
fxy060608's avatar
init v3  
fxy060608 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38
    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 已提交
39
    if (isAppService) {
fxy060608's avatar
init v3  
fxy060608 已提交
40 41 42 43 44 45 46 47 48 49 50
      webpackConfig.optimization.runtimeChunk = {
        name: 'app-config'
      }
    } else if (isAppView) {
      webpackConfig.optimization.runtimeChunk = false
    }

    webpackConfig.optimization.splitChunks = false

    let devtool = false

fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60
    const rules = []

    if (isAppView) {
      rules.push({ // 解析组件,css 等
        resourceQuery: /vue&type=script/,
        use: [{
          loader: path.resolve(__dirname,
            '../../packages/webpack-uni-app-loader/view/script')
        }]
      })
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69
    }

    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 已提交
70 71
    return {
      devtool,
fxy060608's avatar
fxy060608 已提交
72
      mode: isAppView ? 'production' : process.env.NODE_ENV,
fxy060608's avatar
init v3  
fxy060608 已提交
73 74 75
      externals: {
        vue: 'Vue'
      },
fxy060608's avatar
fxy060608 已提交
76
      entry,
fxy060608's avatar
init v3  
fxy060608 已提交
77 78
      output: {
        filename: '[name].js',
fxy060608's avatar
fxy060608 已提交
79
        chunkFilename: '[id].js',
fxy060608's avatar
fxy060608 已提交
80
        globalObject: 'this'
fxy060608's avatar
init v3  
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      },
      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 已提交
98
              '../../packages/webpack-uni-app-loader/view/main.js'),
fxy060608's avatar
init v3  
fxy060608 已提交
99
            options: {
fxy060608's avatar
fxy060608 已提交
100
              compiler: getPlatformCompiler(),
fxy060608's avatar
init v3  
fxy060608 已提交
101 102 103 104 105
              before: [
                beforeCode + statCode
              ]
            }
          }]
fxy060608's avatar
fxy060608 已提交
106 107 108
        },
        ...rules
        ]
fxy060608's avatar
init v3  
fxy060608 已提交
109 110 111 112 113 114
      },
      plugins: [
        new webpack.ProvidePlugin(getProvides())
      ]
    }
  },
fxy060608's avatar
fxy060608 已提交
115 116
  chainWebpack (webpackConfig, vueOptions) {
    webpackConfig.entryPoints.delete('app')
fxy060608's avatar
fxy060608 已提交
117

fxy060608's avatar
init v3  
fxy060608 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
    const isAppService = !!vueOptions.pluginOptions['uni-app-plus']['service']
    const isAppView = !!vueOptions.pluginOptions['uni-app-plus']['view']

    const compilerOptions = {
      isUnaryTag,
      preserveWhitespace: false,
      service: isAppService,
      view: isAppView
    }

    // disable vue cache-loader
    webpackConfig.module
      .rule('vue')
      .test([/\.vue$/, /\.nvue$/])
fxy060608's avatar
fxy060608 已提交
132
      .use('vue-loader') //  service 层移除 style 节点,view 层返回固定 script
fxy060608's avatar
init v3  
fxy060608 已提交
133 134
      .loader(path.resolve(__dirname, '../../packages/vue-loader/lib'))
      .tap(options => Object.assign(options, {
fxy060608's avatar
fxy060608 已提交
135
        isAppService,
fxy060608's avatar
init v3  
fxy060608 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        isAppView,
        compiler: getPlatformCompiler(),
        compilerOptions,
        cacheDirectory: false,
        cacheIdentifier: false
      }))
      .end()
      .uses
      .delete('cache-loader')
    // .end()
    // .use('uniapp-custom-block-loader')
    // .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-custom-block-loader'))
    // .options({
    //   compiler: getPlatformCompiler()
    // })
fxy060608's avatar
fxy060608 已提交
151 152 153 154 155
    if (isAppView) {
      if (process.env.NODE_ENV === 'production') {
        require('../h5/cssnano-options')(webpackConfig)
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168

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