baseConfig.js 3.9 KB
Newer Older
E
init  
Evan You 已提交
1 2 3 4
const path = require('path')

module.exports = function createBaseConfig ({
  sourceDir,
E
Evan You 已提交
5
  publicPath,
E
tweaks  
Evan You 已提交
6 7
  themePath,
  notFoundPath
E
init  
Evan You 已提交
8 9 10 11 12 13
}) {
  const Config = require('webpack-chain')
  const { VueLoaderPlugin } = require('vue-loader')
  const CSSExtractPlugin = require('mini-css-extract-plugin')

  const isProd = process.env.NODE_ENV === 'production'
E
Evan You 已提交
14
  const inlineLimit = 10000
E
init  
Evan You 已提交
15 16 17 18 19 20

  const config = new Config()

  config
    .set('mode', isProd ? 'production' : 'development')
    .output
E
Evan You 已提交
21
      .path(path.resolve(sourceDir, '_dist'))
E
wip  
Evan You 已提交
22
      .filename(isProd ? '_assets/js/[name].[chunkhash:8].js' : '_assets/js/[name].js')
E
init  
Evan You 已提交
23 24 25 26
      .publicPath(publicPath)

  config.resolve
    .set('symlinks', true)
E
Evan You 已提交
27
    .alias
E
Evan You 已提交
28
      .set('~theme', themePath)
E
tweaks  
Evan You 已提交
29 30
      .set('~notFound', notFoundPath)
      .set('~source', sourceDir)
E
Evan You 已提交
31
      .end()
E
init  
Evan You 已提交
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
    .extensions
      .merge(['.js', '.jsx', '.vue', '.json'])
      .end()
    .modules
      .add('node_modules')
      .add(path.resolve(sourceDir, '../node_modules'))
      .add(path.resolve(__dirname, '../../node_modules'))

  config.resolveLoader
    .set('symlinks', true)
    .modules
      .add('node_modules')
      .add(path.resolve(sourceDir, '../node_modules'))
      .add(path.resolve(__dirname, '../../node_modules'))

  config.module
    .noParse(/^(vue|vue-router|vuex|vuex-router-sync)$/)

  config.module
    .rule('vue')
      .test(/\.vue$/)
      .use('vue-loader')
        .loader('vue-loader')
        .options({
          compilerOptions: {
            preserveWhitespace: false
          }
        })

E
Evan You 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
  config.module
    .rule('markdown')
      .test(/\.md$/)
      .use('vue-loader')
        .loader('vue-loader')
        .options({
          compilerOptions: {
            preserveWhitespace: false
          }
        })
        .end()
      .use('markdown-to-vue-loader')
        .loader('markdown-to-vue-loader')

E
init  
Evan You 已提交
75 76 77 78 79 80
  config.module
    .rule('images')
      .test(/\.(png|jpe?g|gif)(\?.*)?$/)
      .use('url-loader')
        .loader('url-loader')
        .options({
E
Evan You 已提交
81
          limit: inlineLimit,
E
wip  
Evan You 已提交
82
          name: `_assets/img/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
83 84 85 86 87 88 89 90 91 92
        })

  // do not base64-inline SVGs.
  // https://github.com/facebookincubator/create-react-app/pull/1180
  config.module
    .rule('svg')
      .test(/\.(svg)(\?.*)?$/)
      .use('file-loader')
        .loader('file-loader')
        .options({
E
wip  
Evan You 已提交
93
          name: `_assets/img/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
94 95 96 97 98 99 100 101 102
        })

  config.module
    .rule('media')
      .test(/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/)
      .use('url-loader')
        .loader('url-loader')
        .options({
          limit: inlineLimit,
E
wip  
Evan You 已提交
103
          name: `_assets/media/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
104 105 106 107 108 109 110 111 112
        })

  config.module
    .rule('fonts')
      .test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
      .use('url-loader')
        .loader('url-loader')
        .options({
          limit: inlineLimit,
E
wip  
Evan You 已提交
113
          name: `_assets/fonts/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
114 115
        })

E
Evan You 已提交
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
  function createCSSRule (lang, test, loader, options) {
    const rule = config.module
      .rule(lang)
        .test(test)

    if (isProd) {
      rule
        .use('extract-css-loader').loader(CSSExtractPlugin.loader).end()
        .use('css-loader').loader('css-loader').options({ minimize: true })
    } else {
      rule
        .use('vue-style-loader').loader('vue-style-loader').end()
        .use('css-loader').loader('css-loader')
    }

    if (loader) {
      rule.use(loader).loader(loader).options(options)
    }
  }

  createCSSRule('css', /\.css$/)
  createCSSRule('scss', /\.scss$/, 'sass-loader')
  createCSSRule('sass', /\.sass$/, 'sass-loader', { indentedSyntax: true })
  createCSSRule('less', /\.less$/, 'less-loader')
  createCSSRule('stylus', /\.styl(us)?$/, 'stylus-loader')
E
init  
Evan You 已提交
141 142 143 144 145 146 147 148

  config
    .plugin('vue-loader')
    .use(VueLoaderPlugin)

  if (isProd) {
    config
      .plugin('extract-css')
E
wip  
Evan You 已提交
149
      .use(CSSExtractPlugin, [{ filename: '_assets/css/styles.[hash:8].css' }])
E
init  
Evan You 已提交
150 151 152 153
  }

  return config
}