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

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

  const config = new Config()

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

  config.resolve
    .set('symlinks', true)
E
Evan You 已提交
29
    .alias
E
Evan You 已提交
30
      .set('~theme', themePath)
E
tweaks  
Evan You 已提交
31 32
      .set('~notFound', notFoundPath)
      .set('~source', sourceDir)
E
Evan You 已提交
33
      .end()
E
init  
Evan You 已提交
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
    .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 已提交
63 64 65 66 67 68 69 70 71 72 73
  config.module
    .rule('markdown')
      .test(/\.md$/)
      .use('vue-loader')
        .loader('vue-loader')
        .options({
          compilerOptions: {
            preserveWhitespace: false
          }
        })
        .end()
E
Evan You 已提交
74 75
      .use('markdown-loader')
        .loader(require.resolve('./markdownLoader'))
E
Evan You 已提交
76
        .options({ markdown })
E
Evan You 已提交
77

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

  // 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 已提交
96
          name: `_assets/img/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
97 98 99 100 101 102 103 104 105
        })

  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 已提交
106
          name: `_assets/media/[name].[hash:8].[ext]`
E
init  
Evan You 已提交
107 108 109 110 111 112 113 114 115
        })

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

E
Evan You 已提交
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
  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 已提交
144 145 146 147 148 149 150 151

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

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

  return config
}