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

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

fxy060608's avatar
fxy060608 已提交
10 11
const modifyVueLoader = require('./vue-loader')

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

fxy060608's avatar
fxy060608 已提交
14
function resolve (dir) {
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  return path.resolve(__dirname, '../../', dir)
}

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

const plugins = []

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
  publicPath,
  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 已提交
65
  webpackConfig (webpackConfig) {
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    return {
      devtool: process.env.NODE_ENV === 'production' ? false : 'source-map',
      resolve: {
        extensions: ['.nvue'],
        alias: {
          'vue-router': resolve('packages/h5-vue-router'),
          'uni-h5': require.resolve('@dcloudio/uni-h5')
        }
      },
      module: {
        rules: [{
          test: /App\.vue$/,
          use: {
            loader: 'wrap-loader',
            options: {
              before: [`<template><App :keepAliveInclude="keepAliveInclude"/></template>`]
            }
          }
fxy060608's avatar
fxy060608 已提交
84 85 86 87 88
        }, {
          resourceQuery: /vue&type=template/,
          use: [{
            loader: resolve('packages/h5-vue-template-loader')
          }]
fxy060608's avatar
fxy060608 已提交
89
        }, {
fxy060608's avatar
fxy060608 已提交
90
          resourceQuery: [/lang=wxs/, /blockType=wxs/],
fxy060608's avatar
fxy060608 已提交
91 92 93
          use: [{
            loader: resolve('packages/webpack-uni-filter-loader')
          }]
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100 101 102 103
        }]
      },
      resolveLoader: {
        alias: {
          'vue-style-loader': resolve('packages/h5-vue-style-loader')
        }
      },
      plugins
    }
  },
fxy060608's avatar
fxy060608 已提交
104
  chainWebpack (webpackConfig, api) {
fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110 111 112
    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 已提交
113 114

    modifyVueLoader(webpackConfig, require('./compiler-options'), api)
fxy060608's avatar
fxy060608 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    if (process.env.NODE_ENV === 'production') {
      const module = webpackConfig.module
      // TODO 临时 hack calc:false 看看 vue cli 后续是否开放 cssnano 的配置
      const cssnanoOptions = {
        sourceMap: false,
        plugins: [require('cssnano')({
          preset: ['default', getPlatformCssnano()]
        })]
      }

      module.rule('css').oneOf('vue-modules').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('css').oneOf('vue').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('css').oneOf('normal-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('css').oneOf('normal').use('cssnano').loader('postcss-loader').options(cssnanoOptions)

      module.rule('postcss').oneOf('vue-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('postcss').oneOf('vue').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('postcss').oneOf('normal-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('postcss').oneOf('normal').use('cssnano').loader('postcss-loader').options(cssnanoOptions)

      module.rule('scss').oneOf('vue-modules').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('scss').oneOf('vue').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('scss').oneOf('normal-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('scss').oneOf('normal').use('cssnano').loader('postcss-loader').options(cssnanoOptions)

      module.rule('sass').oneOf('vue-modules').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('sass').oneOf('vue').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('sass').oneOf('normal-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('sass').oneOf('normal').use('cssnano').loader('postcss-loader').options(cssnanoOptions)

      module.rule('less').oneOf('vue-modules').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('less').oneOf('vue').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('less').oneOf('normal-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('less').oneOf('normal').use('cssnano').loader('postcss-loader').options(cssnanoOptions)

      module.rule('stylus').oneOf('vue-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('stylus').oneOf('vue').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
      module.rule('stylus').oneOf('normal-modules').use('cssnano').loader('postcss-loader').options(
        cssnanoOptions)
      module.rule('stylus').oneOf('normal').use('cssnano').loader('postcss-loader').options(cssnanoOptions)
    }
  }
}