|
| 1 | +const path = require('path') |
| 2 | + |
| 3 | +module.exports = function createBaseConfig ({ |
| 4 | + sourceDir, |
| 5 | + publicPath = '/', |
| 6 | + layoutPath = path.resolve(__dirname, '../default-layout/Layout.vue') |
| 7 | +}) { |
| 8 | + const Config = require('webpack-chain') |
| 9 | + const { VueLoaderPlugin } = require('vue-loader') |
| 10 | + const CSSExtractPlugin = require('mini-css-extract-plugin') |
| 11 | + |
| 12 | + const isProd = process.env.NODE_ENV === 'production' |
| 13 | + |
| 14 | + const config = new Config() |
| 15 | + |
| 16 | + config |
| 17 | + .set('mode', isProd ? 'production' : 'development') |
| 18 | + .output |
| 19 | + .path(path.resolve(sourceDir, 'dist')) |
| 20 | + .filename('[name].[chunkhash].js') |
| 21 | + .publicPath(publicPath) |
| 22 | + |
| 23 | + config.alias |
| 24 | + .set('~layout', layoutPath) |
| 25 | + |
| 26 | + config.resolve |
| 27 | + .set('symlinks', true) |
| 28 | + .extensions |
| 29 | + .merge(['.js', '.jsx', '.vue', '.json']) |
| 30 | + .end() |
| 31 | + .modules |
| 32 | + .add('node_modules') |
| 33 | + .add(path.resolve(sourceDir, '../node_modules')) |
| 34 | + .add(path.resolve(__dirname, '../../node_modules')) |
| 35 | + |
| 36 | + config.resolveLoader |
| 37 | + .set('symlinks', true) |
| 38 | + .modules |
| 39 | + .add('node_modules') |
| 40 | + .add(path.resolve(sourceDir, '../node_modules')) |
| 41 | + .add(path.resolve(__dirname, '../../node_modules')) |
| 42 | + |
| 43 | + config.module |
| 44 | + .noParse(/^(vue|vue-router|vuex|vuex-router-sync)$/) |
| 45 | + |
| 46 | + config.module |
| 47 | + .rule('vue') |
| 48 | + .test(/\.vue$/) |
| 49 | + .use('vue-loader') |
| 50 | + .loader('vue-loader') |
| 51 | + .options({ |
| 52 | + compilerOptions: { |
| 53 | + preserveWhitespace: false |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + config.module |
| 58 | + .rule('images') |
| 59 | + .test(/\.(png|jpe?g|gif)(\?.*)?$/) |
| 60 | + .use('url-loader') |
| 61 | + .loader('url-loader') |
| 62 | + .options({ |
| 63 | + limit: 10000, |
| 64 | + name: `img/[name].[hash:8].[ext]` |
| 65 | + }) |
| 66 | + |
| 67 | + // do not base64-inline SVGs. |
| 68 | + // https://github.com/facebookincubator/create-react-app/pull/1180 |
| 69 | + config.module |
| 70 | + .rule('svg') |
| 71 | + .test(/\.(svg)(\?.*)?$/) |
| 72 | + .use('file-loader') |
| 73 | + .loader('file-loader') |
| 74 | + .options({ |
| 75 | + name: `img/[name].[hash:8].[ext]` |
| 76 | + }) |
| 77 | + |
| 78 | + config.module |
| 79 | + .rule('media') |
| 80 | + .test(/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/) |
| 81 | + .use('url-loader') |
| 82 | + .loader('url-loader') |
| 83 | + .options({ |
| 84 | + limit: inlineLimit, |
| 85 | + name: `media/[name].[hash:8].[ext]` |
| 86 | + }) |
| 87 | + |
| 88 | + config.module |
| 89 | + .rule('fonts') |
| 90 | + .test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i) |
| 91 | + .use('url-loader') |
| 92 | + .loader('url-loader') |
| 93 | + .options({ |
| 94 | + limit: inlineLimit, |
| 95 | + name: `fonts/[name].[hash:8].[ext]` |
| 96 | + }) |
| 97 | + |
| 98 | + createCSSRule(config, 'css', /\.css$/) |
| 99 | + createCSSRule(config, 'scss', /\.scss$/, 'sass-loader') |
| 100 | + createCSSRule(config, 'sass', /\.sass$/, 'sass-loader', { indentedSyntax: true }) |
| 101 | + createCSSRule(config, 'less', /\.less$/, 'less-loader') |
| 102 | + createCSSRule(config, 'stylus', /\.styl(us)?$/, 'stylus-loader') |
| 103 | + |
| 104 | + config |
| 105 | + .plugin('vue-loader') |
| 106 | + .use(VueLoaderPlugin) |
| 107 | + |
| 108 | + if (isProd) { |
| 109 | + config |
| 110 | + .plugin('extract-css') |
| 111 | + .use(CSSExtractPlugin, [{ filename: 'styles.css' }]) |
| 112 | + } |
| 113 | + |
| 114 | + return config |
| 115 | +} |
0 commit comments