-
Notifications
You must be signed in to change notification settings - Fork 26
tec: Upgrade to Webpack 4 #360
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use strict' | ||
const path = require('path') | ||
const config = require('../config') | ||
const ExtractTextPlugin = require('extract-text-webpack-plugin') | ||
const MiniCssExtractPlugin = require('mini-css-extract-plugin') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'require' is not defined no-undef |
||
const packageConfig = require('../package.json') | ||
|
||
exports.assetsPath = function (_path) { | ||
|
@@ -25,13 +25,32 @@ exports.cssLoaders = function (options) { | |
const postcssLoader = { | ||
loader: 'postcss-loader', | ||
options: { | ||
sourceMap: options.sourceMap | ||
sourceMap: options.sourceMap, | ||
plugins: () => [ | ||
require('autoprefixer')({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'require' is not defined no-undef There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'require' is not defined no-undef |
||
browsers: ['last 2 versions'], | ||
}), | ||
], | ||
} | ||
} | ||
|
||
// generate loader string to be used with extract text plugin | ||
function generateLoaders (loader, loaderOptions) { | ||
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader] | ||
const loaders = [] | ||
|
||
// Extract CSS when that option is specified | ||
// (which is the case during production build) | ||
if (options.extract) { | ||
loaders.push(MiniCssExtractPlugin.loader) | ||
} else { | ||
loaders.push('vue-style-loader') | ||
} | ||
|
||
loaders.push(cssLoader) | ||
|
||
if (options.usePostCSS) { | ||
loaders.push(postcssLoader) | ||
} | ||
|
||
if (loader) { | ||
loaders.push({ | ||
|
@@ -42,29 +61,16 @@ exports.cssLoaders = function (options) { | |
}) | ||
} | ||
|
||
// Extract CSS when that option is specified | ||
// (which is the case during production build) | ||
if (options.extract) { | ||
return ExtractTextPlugin.extract({ | ||
use: loaders, | ||
fallback: 'vue-style-loader' | ||
}) | ||
} else { | ||
return ['vue-style-loader'].concat(loaders) | ||
} | ||
return loaders | ||
} | ||
|
||
// https://vue-loader.vuejs.org/en/configurations/extract-css.html | ||
return { | ||
css: generateLoaders(), | ||
postcss: generateLoaders(), | ||
less: generateLoaders('less'), | ||
sass: generateLoaders('sass', { indentedSyntax: true }), | ||
scss: generateLoaders('sass', { | ||
data: "@import 'src/styles/variables/index';", | ||
}), | ||
stylus: generateLoaders('stylus'), | ||
styl: generateLoaders('stylus') | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ const path = require('path') | |
const utils = require('./utils') | ||
const config = require('../config') | ||
const vueLoaderConfig = require('./vue-loader.conf') | ||
const { VueLoaderPlugin } = require('vue-loader') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'require' is not defined no-undef |
||
|
||
function resolve (dir) { | ||
return path.join(__dirname, '..', dir) | ||
|
@@ -31,6 +32,9 @@ module.exports = { | |
? config.build.assetsPublicPath | ||
: config.dev.assetsPublicPath | ||
}, | ||
plugins: [ | ||
new VueLoaderPlugin(), | ||
], | ||
resolve: { | ||
extensions: ['.js', '.vue', '.json'], | ||
alias: { | ||
|
@@ -49,7 +53,10 @@ module.exports = { | |
{ | ||
test: /\.js$/, | ||
loader: 'babel-loader', | ||
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')] | ||
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')], | ||
exclude: file => ( | ||
/node_modules/.test(file) && !/\.vue\.js/.test(file) | ||
), | ||
}, | ||
{ | ||
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ const merge = require('webpack-merge') | |
const baseWebpackConfig = require('./webpack.base.conf') | ||
const CopyWebpackPlugin = require('copy-webpack-plugin') | ||
const HtmlWebpackPlugin = require('html-webpack-plugin') | ||
const ExtractTextPlugin = require('extract-text-webpack-plugin') | ||
const MiniCssExtractPlugin = require('mini-css-extract-plugin') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'require' is not defined no-undef |
||
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') | ||
const UglifyJsPlugin = require('uglifyjs-webpack-plugin') | ||
|
||
|
@@ -16,6 +16,7 @@ const env = process.env.NODE_ENV === 'testing' | |
: require('../config/prod.env') | ||
|
||
const webpackConfig = merge(baseWebpackConfig, { | ||
mode: 'production', | ||
module: { | ||
rules: utils.styleLoaders({ | ||
sourceMap: config.build.productionSourceMap, | ||
|
@@ -27,30 +28,15 @@ const webpackConfig = merge(baseWebpackConfig, { | |
output: { | ||
path: config.build.assetsRoot, | ||
filename: utils.assetsPath('js/[name].[chunkhash].js'), | ||
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') | ||
}, | ||
plugins: [ | ||
// http://vuejs.github.io/vue-loader/en/workflow/production.html | ||
new webpack.DefinePlugin({ | ||
'process.env': env | ||
}), | ||
new UglifyJsPlugin({ | ||
uglifyOptions: { | ||
compress: { | ||
warnings: false | ||
} | ||
}, | ||
sourceMap: config.build.productionSourceMap, | ||
parallel: true | ||
}), | ||
// extract css into its own file | ||
new ExtractTextPlugin({ | ||
filename: utils.assetsPath('css/[name].[contenthash].css'), | ||
// Setting the following option to `false` will not extract CSS from codesplit chunks. | ||
// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack. | ||
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`, | ||
// increasing file size: https://github.com/vuejs-templates/webpack/issues/1110 | ||
allChunks: true, | ||
new MiniCssExtractPlugin({ | ||
filename: utils.assetsPath('css/[name].[chunkhash].css'), | ||
}), | ||
// Compress extracted CSS. We are using this plugin so that possible | ||
// duplicated CSS from different components can be deduped. | ||
|
@@ -75,42 +61,14 @@ const webpackConfig = merge(baseWebpackConfig, { | |
// more options: | ||
// https://github.com/kangax/html-minifier#options-quick-reference | ||
}, | ||
// necessary to consistently work with multiple chunks via CommonsChunkPlugin | ||
// necessary to consistently work with multiple chunks | ||
chunksSortMode: 'dependency' | ||
}), | ||
|
||
new webpack.NamedChunksPlugin(), | ||
|
||
// keep module.id stable when vendor modules does not change | ||
new webpack.HashedModuleIdsPlugin(), | ||
// enable scope hoisting | ||
new webpack.optimize.ModuleConcatenationPlugin(), | ||
// split vendor js into its own file | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'vendor', | ||
minChunks (module) { | ||
// any required modules inside node_modules are extracted to vendor | ||
return ( | ||
module.resource && | ||
/\.js$/.test(module.resource) && | ||
module.resource.indexOf( | ||
path.join(__dirname, '../node_modules') | ||
) === 0 | ||
) | ||
} | ||
}), | ||
// extract webpack runtime and module manifest to its own file in order to | ||
// prevent vendor hash from being updated whenever app bundle is updated | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'manifest', | ||
minChunks: Infinity | ||
}), | ||
// This instance extracts shared chunks from code splitted chunks and bundles them | ||
// in a separate chunk, similar to the vendor chunk | ||
// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'app', | ||
async: 'vendor-async', | ||
children: true, | ||
minChunks: 3 | ||
}), | ||
|
||
// copy custom static assets | ||
new CopyWebpackPlugin([ | ||
|
@@ -120,7 +78,31 @@ const webpackConfig = merge(baseWebpackConfig, { | |
ignore: ['.*'] | ||
} | ||
]) | ||
] | ||
], | ||
optimization: { | ||
splitChunks: { | ||
chunks: 'all', | ||
cacheGroups: { | ||
vendor: { | ||
name: 'vendor', | ||
test: /[\\/]node_modules[\\/]/, | ||
enforce: true, | ||
}, | ||
}, | ||
}, | ||
runtimeChunk: 'single', | ||
minimizer: [ | ||
new UglifyJsPlugin({ | ||
uglifyOptions: { | ||
compress: { | ||
warnings: false | ||
} | ||
}, | ||
sourceMap: config.build.productionSourceMap, | ||
parallel: true | ||
}), | ||
], | ||
}, | ||
}) | ||
|
||
if (config.build.productionGzip) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'require' is not defined no-undef