configure-webpack.js 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
const path = require('path')

const webpack = require('webpack')
const CopyPlugin = require('copy-webpack-plugin')
5
const CopyPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
fxy060608's avatar
fxy060608 已提交
6 7

const HandlerPlugin = require('@hap-toolkit/packager/lib/plugin/handler-plugin')
fxy060608's avatar
fxy060608 已提交
8
const ZipPlugin = require('@hap-toolkit/packager/lib/plugin/zip-plugin')
fxy060608's avatar
fxy060608 已提交
9 10 11 12
const NotifyPlugin = require('@hap-toolkit/packager/lib/plugin/notify-plugin')

const Css2jsonPlugin = require('@hap-toolkit/dsl-vue/lib/plugin/css2json-plugin')

fxy060608's avatar
fxy060608 已提交
13
const InstVuePlugin = require('./plugin/instvue-plugin')
fxy060608's avatar
fxy060608 已提交
14

fxy060608's avatar
fxy060608 已提交
15
const parseManifest = require('./manifest/index')
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20
const validate = require('./validate')

parseManifest(process.UNI_PAGES, process.UNI_MANIFEST)

validate()
fxy060608's avatar
fxy060608 已提交
21

fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27 28
const env = {
  // 平台:native
  NODE_PLATFORM: 'native',
  // 阶段: dev|test|release
  NODE_PHASE: process.env.NODE_PHASE
}

fxy060608's avatar
fxy060608 已提交
29
const dslFilename = ('vue.' + (process.env.NODE_ENV === 'production' ? 'prod' : 'dev') + '.js')
fxy060608's avatar
fxy060608 已提交
30

fxy060608's avatar
fxy060608 已提交
31
const manifest = global.framework.manifest
fxy060608's avatar
fxy060608 已提交
32

fxy060608's avatar
fxy060608 已提交
33 34
function genPriorities (entryPagePath) {
  const o = [/^i18n\/.+\.json$/i, 'manifest.json', 'app.js', /^common\//i]
fxy060608's avatar
fxy060608 已提交
35 36
  if (entryPagePath) {
    o.splice(3, 0, new RegExp(`^${entryPagePath}/$`), new RegExp(`^${entryPagePath}/.+`))
fxy060608's avatar
fxy060608 已提交
37
  } else console.error('未配置入口页面')
fxy060608's avatar
fxy060608 已提交
38 39 40
  return o
}

fxy060608's avatar
fxy060608 已提交
41 42
const uniCloudPath = require.resolve('@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js')

43 44 45 46 47
const patterns = [{
  from: path.resolve(__dirname, '../dist/' + dslFilename),
  to: 'dsl.js'
}]

fxy060608's avatar
fxy060608 已提交
48 49
module.exports = {
  devtool: false,
fxy060608's avatar
fxy060608 已提交
50
  entry () {
fxy060608's avatar
fxy060608 已提交
51
    return process.UNI_ENTRY
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56 57 58 59
  },
  output: {
    filename: '[name].js',
    chunkFilename: '[name].js'
  },
  optimization: {
    splitChunks: false,
    runtimeChunk: false
fxy060608's avatar
fxy060608 已提交
60
  },
fxy060608's avatar
fxy060608 已提交
61 62 63
  externals: {
    vue: 'Vue'
  },
fxy060608's avatar
fxy060608 已提交
64 65
  module: {
    rules: [{
fxy060608's avatar
fxy060608 已提交
66
      // 暂不考虑ts
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72
      test: path.resolve(process.env.UNI_INPUT_DIR, 'main.js'),
      use: [{
        loader: path.resolve(__dirname, 'loader/main-loader')
      }]
    }]
  },
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78 79 80 81 82
  plugins: [
    new webpack.DefinePlugin({
      // 平台:na
      ENV_PLATFORM: JSON.stringify(env.NODE_PLATFORM),
      // 阶段: dev|test|release
      ENV_PHASE: JSON.stringify(env.NODE_PHASE),
      ENV_PHASE_DV: env.NODE_PHASE === 'dev',
      ENV_PHASE_QA: env.NODE_PHASE === 'test',
      ENV_PHASE_OL: env.NODE_PHASE === 'prod'
    }),
fxy060608's avatar
fxy060608 已提交
83 84 85
    new webpack.ProvidePlugin({
      uniCloud: [uniCloudPath, 'default']
    }),
86
    new CopyPlugin(CopyPluginVersion > 5 ? { patterns } : patterns),
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92
    new HandlerPlugin({}),
    new Css2jsonPlugin(),
    new InstVuePlugin(),
    new ZipPlugin({
      name: manifest.package,
      icon: manifest.icon,
fxy060608's avatar
fxy060608 已提交
93
      versionCode: manifest.versionCode,
fxy060608's avatar
fxy060608 已提交
94 95
      output: path.resolve(process.env.UNI_OUTPUT_DIR, '..'),
      pathBuild: process.env.UNI_OUTPUT_DIR,
fxy060608's avatar
fxy060608 已提交
96 97
      pathSignFolder: './sign',
      sign: process.env.NODE_ENV === 'production' ? 'release' : 'debug',
fxy060608's avatar
fxy060608 已提交
98
      priorities: genPriorities(manifest.router.entry),
fxy060608's avatar
fxy060608 已提交
99
      subpackages: undefined,
fxy060608's avatar
fxy060608 已提交
100
      comment: '',
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106
      cwd: process.env.UNI_INPUT_DIR,
      disableStreamPack: undefined,
      disableSubpackages: undefined
    }),
    new NotifyPlugin()
  ]
107
}