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

module.exports = function createBaseConfig ({
  sourceDir,
E
Evan You 已提交
5 6
  publicPath,
  themePath
E
init  
Evan You 已提交
7 8 9 10 11 12
}) {
  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 已提交
13
  const inlineLimit = 10000
E
init  
Evan You 已提交
14 15 16 17 18 19

  const config = new Config()

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

  config.resolve
    .set('symlinks', true)
E
Evan You 已提交
26
    .alias
E
Evan You 已提交
27
      .set('~theme', themePath)
E
Evan You 已提交
28
      .end()
E
init  
Evan You 已提交
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
    .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 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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 已提交
72 73 74 75 76 77
  config.module
    .rule('images')
      .test(/\.(png|jpe?g|gif)(\?.*)?$/)
      .use('url-loader')
        .loader('url-loader')
        .options({
E
Evan You 已提交
78
          limit: inlineLimit,
E
wip  
Evan You 已提交
79
          name: `_assets/img/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
80 81 82 83 84 85 86 87 88 89
        })

  // 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 已提交
90
          name: `_assets/img/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
91 92 93 94 95 96 97 98 99
        })

  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 已提交
100
          name: `_assets/media/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
101 102 103 104 105 106 107 108 109
        })

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

E
Evan You 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  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 已提交
138 139 140 141 142 143 144 145

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

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

  return config
}