index.js 5.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const fs = require('fs')
const path = require('path')
fxy060608's avatar
fxy060608 已提交
3
const webpack = require('webpack')
fxy060608's avatar
fxy060608 已提交
4 5 6

const {
  getMainEntry,
fxy060608's avatar
fxy060608 已提交
7
  getH5Options
fxy060608's avatar
fxy060608 已提交
8 9
} = 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
const modifyVueLoader = require('../vue-loader')
fxy060608's avatar
fxy060608 已提交
15

fxy060608's avatar
fxy060608 已提交
16 17
const WebpackHtmlAppendPlugin = require('../../packages/webpack-html-append-plugin')

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

fxy060608's avatar
fxy060608 已提交
20
function resolve (dir) {
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29 30
  return path.resolve(__dirname, '../../', dir)
}

const {
  title,
  publicPath,
  template,
  devServer
} = getH5Options()

fxy060608's avatar
fxy060608 已提交
31
const runtimePath = '@dcloudio/uni-mp-weixin/dist/mp.js'
fxy060608's avatar
fxy060608 已提交
32
const wxsPath = '@dcloudio/uni-mp-weixin/dist/wxs.js'
fxy060608's avatar
fxy060608 已提交
33
const uniCloudPath = path.resolve(__dirname, '../../packages/uni-cloud/dist/index.js')
fxy060608's avatar
fxy060608 已提交
34

fxy060608's avatar
fxy060608 已提交
35
function getProvides () {
36
  return {
fxy060608's avatar
fxy060608 已提交
37
    'uniCloud': [uniCloudPath, 'default'],
fxy060608's avatar
fxy060608 已提交
38
    'wx.nextTick': [runtimePath, 'nextTick'],
fxy060608's avatar
fxy060608 已提交
39 40 41
    'Page': [runtimePath, 'Page'],
    'Component': [runtimePath, 'Component'],
    'Behavior': [runtimePath, 'Behavior'],
fxy060608's avatar
fxy060608 已提交
42 43
    'getDate': [wxsPath, 'getDate'],
    'getRegExp': [wxsPath, 'getRegExp']
fxy060608's avatar
fxy060608 已提交
44 45 46 47
  }
}

const plugins = [
fxy060608's avatar
fxy060608 已提交
48
  new WebpackUniAppPlugin(),
fxy060608's avatar
fxy060608 已提交
49 50
  new webpack.ProvidePlugin(getProvides())
]
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63

if (process.env.NODE_ENV !== 'production') {
  plugins.push(new WebpackHtmlAppendPlugin(
    `
        <script>
        ${fs.readFileSync(path.resolve(__dirname, './auto-reload.js'), 'utf8')}
        </script>
        `
  ))
}

const vueConfig = {
  parallel: false, // 因为传入了自定义 compiler,避免参数丢失,禁用parallel
64 65 66 67
  publicPath,
  transpileDependencies: [
    wxsPath,
    runtimePath
fxy060608's avatar
fxy060608 已提交
68
  ],
fxy060608's avatar
fxy060608 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  pages: {
    index: {
      // page 的入口
      entry: path.resolve(process.env.UNI_INPUT_DIR, getMainEntry()),
      // 模板来源
      template,
      // 在 dist/index.html 的输出
      filename: 'index.html',
      // 当使用 title 选项时,
      // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
      title,
      // 在这个页面中包含的块,默认情况下会包含
      // 提取出来的通用 chunk 和 vendor chunk。
      chunks: ['chunk-vendors', 'chunk-common', 'index'],
      baseUrl: publicPath
    }
  }
}

if (devServer && Object.keys(devServer).length) {
  vueConfig.devServer = devServer
}

module.exports = {
  vueConfig,
fxy060608's avatar
fxy060608 已提交
94
  webpackConfig (webpackConfig) {
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103 104 105 106
    let useBuiltIns = 'usage'

    const statCode = process.env.UNI_USING_STAT ? `import '@dcloudio/uni-stat';` : ''

    try {
      const babelConfig = require(path.resolve(process.env.UNI_CLI_CONTEXT, 'babel.config.js'))
      useBuiltIns = babelConfig.presets[0][1].useBuiltIns
    } catch (e) {}

    const beforeCode = (useBuiltIns === 'entry' ? `import '@babel/polyfill';` : '') +
      `import 'uni-pages';import 'uni-${process.env.UNI_PLATFORM}';`

fxy060608's avatar
fxy060608 已提交
107
    const qihooCode = process.env.UNI_SUB_PLATFORM === 'mp-360'
fxy060608's avatar
fxy060608 已提交
108
      ? `
fxy060608's avatar
fxy060608 已提交
109 110 111 112 113 114
import 'uni-touch-emulator';
import qh from 'uni-qh';
global.qh = qh;
global.onAppShow = function(){};
` : ''

fxy060608's avatar
fxy060608 已提交
115
    return {
fxy060608's avatar
fxy060608 已提交
116
      devtool: process.env.NODE_ENV === 'production' ? false : 'cheap-module-eval-source-map',
fxy060608's avatar
fxy060608 已提交
117 118 119 120
      resolve: {
        extensions: ['.nvue'],
        alias: {
          'vue-router': resolve('packages/h5-vue-router'),
fxy060608's avatar
fxy060608 已提交
121
          'uni-h5': require.resolve('@dcloudio/uni-h5'),
fxy060608's avatar
fxy060608 已提交
122
          'uni-qh': path.resolve(__dirname, 'qh-api.js'),
fxy060608's avatar
fxy060608 已提交
123
          'uni-touch-emulator': path.resolve(__dirname, 'touch-emulator.js')
fxy060608's avatar
fxy060608 已提交
124 125 126 127
        }
      },
      module: {
        rules: [{
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132
          test: path.resolve(process.env.UNI_INPUT_DIR, getMainEntry()),
          use: [{
            loader: 'wrap-loader',
            options: {
              before: [
fxy060608's avatar
fxy060608 已提交
133
                qihooCode + beforeCode + statCode + getGlobalUsingComponentsCode()
fxy060608's avatar
fxy060608 已提交
134 135 136 137
              ]
            }
          }]
        }, {
fxy060608's avatar
fxy060608 已提交
138 139 140 141 142 143 144
          test: /App\.vue$/,
          use: {
            loader: 'wrap-loader',
            options: {
              before: [`<template><App :keepAliveInclude="keepAliveInclude"/></template>`]
            }
          }
fxy060608's avatar
fxy060608 已提交
145 146 147 148 149 150
        }, { // 解析组件,css 等
          resourceQuery: /vue&type=script/,
          use: [{
            loader: path.resolve(__dirname,
              '../../packages/webpack-uni-app-loader/using-components')
          }]
fxy060608's avatar
fxy060608 已提交
151 152 153
        }, {
          resourceQuery: /vue&type=template/,
          use: [{
fxy060608's avatar
fxy060608 已提交
154
            loader: resolve('packages/webpack-uni-app-loader/filter-modules-template.js')
155 156
          }, {
            loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-app-loader/page-meta'
fxy060608's avatar
fxy060608 已提交
157
          }]
fxy060608's avatar
fxy060608 已提交
158
        }, {
fxy060608's avatar
fxy060608 已提交
159
          resourceQuery: [/lang=wxs/, /blockType=wxs/],
fxy060608's avatar
fxy060608 已提交
160 161 162
          use: [{
            loader: resolve('packages/webpack-uni-filter-loader')
          }]
fxy060608's avatar
fxy060608 已提交
163
        }]
164 165 166 167 168
      },
      resolveLoader: {
        alias: {
          'vue-style-loader': resolve('packages/h5-vue-style-loader')
        }
fxy060608's avatar
fxy060608 已提交
169 170 171 172
      },
      plugins
    }
  },
fxy060608's avatar
fxy060608 已提交
173
  chainWebpack (webpackConfig, vueOptions, api) {
fxy060608's avatar
fxy060608 已提交
174 175 176 177 178 179 180 181
    webpackConfig.plugins.delete('copy')

    if (!process.env.UNI_OPT_PREFETCH) {
      webpackConfig.plugins.delete('prefetch-index')
    }
    if (!process.env.UNI_OPT_PRELOAD) {
      webpackConfig.plugins.delete('preload-index')
    }
fxy060608's avatar
fxy060608 已提交
182 183

    modifyVueLoader(webpackConfig, require('./compiler-options'), api)
fxy060608's avatar
fxy060608 已提交
184 185

    if (process.env.NODE_ENV === 'production') {
fxy060608's avatar
fxy060608 已提交
186
      require('./cssnano-options')(webpackConfig)
fxy060608's avatar
fxy060608 已提交
187 188 189
    }
  }
}