Skip to content

Commit 96d61e3

Browse files
committed
feat update webpack build config
- add optimize-css-assets-webpack-plugin webpack-bundle-analyzer - upgrade config env @see https://github.com/vuejs-templates/webpack/
1 parent 2f268d4 commit 96d61e3

12 files changed

+181
-96
lines changed

build/utils.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,48 @@ exports.assetsPath = function (_path) {
1111

1212
exports.cssLoaders = function (options) {
1313
options = options || {}
14+
15+
var cssLoader = {
16+
loader: 'css-loader',
17+
options: {
18+
minimize: process.env.NODE_ENV === 'production',
19+
sourceMap: options.sourceMap
20+
}
21+
}
22+
1423
// generate loader string to be used with extract text plugin
15-
function generateLoaders (loaders) {
16-
var sourceLoader = loaders.map(function (loader) {
17-
var extraParamChar
18-
if (/\?/.test(loader)) {
19-
loader = loader.replace(/\?/, '-loader?')
20-
extraParamChar = '&'
21-
} else {
22-
loader = loader + '-loader'
23-
extraParamChar = '?'
24-
}
25-
return loader + (options.sourceMap ? extraParamChar + 'sourceMap' : '')
26-
}).join('!')
24+
function generateLoaders (loader, loaderOptions) {
25+
var loaders = [cssLoader]
26+
if (loader) {
27+
loaders.push({
28+
loader: loader + '-loader',
29+
options: Object.assign({}, loaderOptions, {
30+
sourceMap: options.sourceMap
31+
})
32+
})
33+
}
2734

2835
// Extract CSS when that option is specified
2936
// (which is the case during production build)
3037
if (options.extract) {
31-
return ExtractTextPlugin.extract('vue-style-loader', sourceLoader)
38+
return ExtractTextPlugin.extract({
39+
use: loaders,
40+
fallback: 'vue-style-loader'
41+
})
3242
} else {
33-
return ['vue-style-loader', sourceLoader].join('!')
43+
return ['vue-style-loader'].concat(loaders)
3444
}
3545
}
3646

37-
// http://vuejs.github.io/vue-loader/en/configurations/extract-css.html
47+
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
3848
return {
39-
css: generateLoaders(['css']),
40-
postcss: generateLoaders(['css']),
41-
less: generateLoaders(['css', 'less']),
42-
sass: generateLoaders(['css', 'sass?indentedSyntax']),
43-
scss: generateLoaders(['css', 'sass']),
44-
stylus: generateLoaders(['css', 'stylus']),
45-
styl: generateLoaders(['css', 'stylus'])
49+
css: generateLoaders(),
50+
postcss: generateLoaders(),
51+
less: generateLoaders('less'),
52+
sass: generateLoaders('sass', { indentedSyntax: true }),
53+
scss: generateLoaders('sass'),
54+
stylus: generateLoaders('stylus'),
55+
styl: generateLoaders('stylus')
4656
}
4757
}
4858

@@ -54,7 +64,7 @@ exports.styleLoaders = function (options) {
5464
var loader = loaders[extension]
5565
output.push({
5666
test: new RegExp('\\.' + extension + '$'),
57-
loader: loader
67+
use: loader
5868
})
5969
}
6070
return output

build/vue-loader.conf.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var utils = require('./utils')
2+
var config = require('../config')
3+
var isProduction = process.env.NODE_ENV === 'production'
4+
5+
module.exports = {
6+
loaders: utils.cssLoaders({
7+
sourceMap: isProduction
8+
? config.build.productionSourceMap
9+
: config.dev.cssSourceMap,
10+
extract: isProduction
11+
}),
12+
postcss: [
13+
require('autoprefixer')({
14+
browsers: ['last 2 versions']
15+
})
16+
]
17+
}

build/webpack.base.conf.js

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ var path = require('path')
22
var config = require('../config')
33
var utils = require('./utils')
44
var projectRoot = path.resolve(__dirname, '../')
5-
6-
var env = process.env.NODE_ENV
7-
// check env & config/index.js to decide whether to enable CSS source maps for the
8-
// various preprocessor loaders added to vue-loader at the end of this file
9-
var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
10-
var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
11-
var useCssSourceMap = cssSourceMapDev || cssSourceMapProd
5+
var vueLoaderConfig = require('./vue-loader.conf')
126

137
module.exports = {
148
entry: {
@@ -20,81 +14,65 @@ module.exports = {
2014
filename: '[name].js'
2115
},
2216
resolve: {
23-
extensions: ['', '.js', '.vue', '.json'],
24-
fallback: [path.join(__dirname, '../node_modules')],
17+
extensions: ['.js', '.vue', '.json'],
2518
alias: {
2619
'vue$': 'vue/dist/vue.common.js',
2720
'src': path.resolve(__dirname, '../src'),
2821
'assets': path.resolve(__dirname, '../src/assets'),
2922
'components': path.resolve(__dirname, '../src/components')
30-
}
31-
},
32-
resolveLoader: {
33-
fallback: [path.join(__dirname, '../node_modules')]
23+
},
24+
modules: [
25+
path.join(__dirname, '../src'),
26+
'node_modules'
27+
],
3428
},
3529
module: {
36-
preLoaders: [
30+
rules: [
3731
{
38-
test: /\.vue$/,
39-
loader: 'eslint',
32+
test: /\.(vue|js)$/,
33+
loader: 'eslint-loader',
34+
enforce: "pre",
4035
include: [
4136
path.join(projectRoot, 'src')
4237
],
43-
exclude: /node_modules/
38+
exclude: /node_modules/,
39+
options: {
40+
formatter: require('eslint-friendly-formatter')
41+
}
4442
},
45-
{
46-
test: /\.js$/,
47-
loader: 'eslint',
48-
include: [
49-
path.join(projectRoot, 'src')
50-
],
51-
exclude: /node_modules/
52-
}
53-
],
54-
loaders: [
5543
{
5644
test: /\.vue$/,
57-
loader: 'vue'
45+
loader: 'vue-loader',
46+
options: vueLoaderConfig
5847
},
5948
{
6049
test: /\.js$/,
61-
loader: 'babel',
50+
loader: 'babel-loader',
6251
include: [
6352
path.join(projectRoot, 'src')
6453
],
6554
exclude: /node_modules/
6655
},
6756
{
6857
test: /\.json$/,
69-
loader: 'json'
58+
loader: 'json-loader'
7059
},
7160
{
7261
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
73-
loader: 'url',
74-
query: {
62+
loader: 'url-loader',
63+
options: {
7564
limit: 10000,
7665
name: utils.assetsPath('img/[name].[hash:7].[ext]')
7766
}
7867
},
7968
{
8069
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
81-
loader: 'url',
82-
query: {
70+
loader: 'url-loader',
71+
options: {
8372
limit: 10000,
8473
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
8574
}
8675
}
8776
]
88-
},
89-
eslint: {
90-
formatter: require('eslint-friendly-formatter')
91-
},
92-
vue: {
93-
loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),
94-
postcss: [
95-
require('autoprefixer')({
96-
browsers: ['last 2 versions']
97-
})
98-
]
9977
}
10078
}

build/webpack.dev.conf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Object.keys(baseWebpackConfig.entry).forEach(function (name) {
1313

1414
module.exports = merge(baseWebpackConfig, {
1515
module: {
16-
loaders: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
16+
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
1717
},
18-
// eval-source-map is faster for development
19-
devtool: '#eval-source-map',
18+
// cheap-module-eval-source-map is faster for development
19+
devtool: '#cheap-module-eval-source-map',
2020
plugins: [
2121
new webpack.DefinePlugin({
2222
'process.env': config.dev.env

build/webpack.prer.conf.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@ var merge = require('webpack-merge')
66
var baseWebpackConfig = require('./webpack.base.conf')
77
var ExtractTextPlugin = require('extract-text-webpack-plugin')
88
var HtmlWebpackPlugin = require('html-webpack-plugin')
9+
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
910
var env = config.prerender.env
1011

1112
var webpackConfig = merge(baseWebpackConfig, {
1213
module: {
13-
loaders: utils.styleLoaders({ sourceMap: config.prerender.productionSourceMap, extract: true })
14+
rules: utils.styleLoaders({ sourceMap: config.prerender.productionSourceMap, extract: true })
1415
},
1516
devtool: config.prerender.productionSourceMap ? '#source-map' : false,
1617
output: {
1718
path: config.prerender.assetsRoot,
1819
filename: utils.assetsPath('js/[name].[chunkhash].js'),
1920
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
2021
},
21-
vue: {
22-
loaders: utils.cssLoaders({
23-
sourceMap: config.prerender.productionSourceMap,
24-
extract: true
25-
})
26-
},
2722
plugins: [
2823
// http://vuejs.github.io/vue-loader/en/workflow/production.html
2924
new webpack.DefinePlugin({
@@ -32,11 +27,19 @@ var webpackConfig = merge(baseWebpackConfig, {
3227
new webpack.optimize.UglifyJsPlugin({
3328
compress: {
3429
warnings: false
35-
}
30+
},
31+
sourceMap: true
3632
}),
3733
new webpack.optimize.OccurrenceOrderPlugin(),
3834
// extract css into its own file
3935
new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),
36+
// Compress extracted CSS. We are using this plugin so that possible
37+
// duplicated CSS from different components can be deduped.
38+
new OptimizeCSSPlugin({
39+
cssProcessorOptions: {
40+
safe: true
41+
}
42+
}),
4043
// generate dist index.html with correct asset hash for caching.
4144
// you can customize output by editing /index.html
4245
// see https://github.com/ampedandwired/html-webpack-plugin
@@ -95,4 +98,9 @@ if (config.prerender.productionGzip) {
9598
)
9699
}
97100

101+
if (config.prerender.bundleAnalyzerReport) {
102+
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
103+
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
104+
}
105+
98106
module.exports = webpackConfig

build/webpack.prod.conf.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@ var merge = require('webpack-merge')
66
var baseWebpackConfig = require('./webpack.base.conf')
77
var ExtractTextPlugin = require('extract-text-webpack-plugin')
88
var HtmlWebpackPlugin = require('html-webpack-plugin')
9+
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
910
var env = config.build.env
1011

1112
var webpackConfig = merge(baseWebpackConfig, {
1213
module: {
13-
loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true })
14+
rules: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true })
1415
},
1516
devtool: config.build.productionSourceMap ? '#source-map' : false,
1617
output: {
1718
path: config.build.assetsRoot,
1819
filename: utils.assetsPath('js/[name].[chunkhash].js'),
1920
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
2021
},
21-
vue: {
22-
loaders: utils.cssLoaders({
23-
sourceMap: config.build.productionSourceMap,
24-
extract: true
25-
})
26-
},
2722
plugins: [
2823
// http://vuejs.github.io/vue-loader/en/workflow/production.html
2924
new webpack.DefinePlugin({
@@ -32,11 +27,19 @@ var webpackConfig = merge(baseWebpackConfig, {
3227
new webpack.optimize.UglifyJsPlugin({
3328
compress: {
3429
warnings: false
35-
}
30+
},
31+
sourceMap: true
3632
}),
3733
new webpack.optimize.OccurrenceOrderPlugin(),
3834
// extract css into its own file
3935
new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),
36+
// Compress extracted CSS. We are using this plugin so that possible
37+
// duplicated CSS from different components can be deduped.
38+
new OptimizeCSSPlugin({
39+
cssProcessorOptions: {
40+
safe: true
41+
}
42+
}),
4043
// generate dist index.html with correct asset hash for caching.
4144
// you can customize output by editing /index.html
4245
// see https://github.com/ampedandwired/html-webpack-plugin
@@ -95,4 +98,9 @@ if (config.build.productionGzip) {
9598
)
9699
}
97100

101+
if (config.build.bundleAnalyzerReport) {
102+
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
103+
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
104+
}
105+
98106
module.exports = webpackConfig

config/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ module.exports = {
1414
// Before setting to `true`, make sure to:
1515
// npm install --save-dev compression-webpack-plugin
1616
productionGzip: false,
17-
productionGzipExtensions: ['js', 'css']
17+
productionGzipExtensions: ['js', 'css'],
18+
// Run the build command with an extra argument to
19+
// View the bundle analyzer report after build finishes:
20+
// `npm run build --report`
21+
// Set to `true` or `false` to always turn it on or off
22+
bundleAnalyzerReport: process.env.npm_config_report
1823
},
1924
prerender: {
2025
env: require('./prer.env'),

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"http-proxy-middleware": "^0.17.2",
6161
"json-loader": "^0.5.4",
6262
"opn": "^4.0.2",
63+
"optimize-css-assets-webpack-plugin": "^1.3.1",
6364
"ora": "^0.3.0",
6465
"semver": "^5.3.0",
6566
"shelljs": "^0.7.4",
@@ -68,6 +69,7 @@
6869
"vue-style-loader": "^3.0.1",
6970
"vue-template-compiler": "^2.3.3",
7071
"webpack": "^2.5.1",
72+
"webpack-bundle-analyzer": "^2.8.1",
7173
"webpack-dev-middleware": "^1.10.2",
7274
"webpack-hot-middleware": "^2.18.0",
7375
"webpack-merge": "^4.1.0"

src/pages/about/join_us.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ layout
105105

106106
<script>
107107
import layout from './layout'
108-
import jobs from 'json!yaml!./assets/jobs.yml'
108+
import jobs from 'json-loader!yaml-loader!./assets/jobs.yml'
109109
import smoothscroll from 'smoothscroll'
110110
111111
export default {

src/pages/about/pricing.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ layout(:colors="colorSet", heroHeight="430")
2323
import layout from '../../components/layout'
2424
import subproduct from './pricing_subproduct'
2525
import smy from './pricing_summary'
26-
import products from 'json!yaml!./pricing_products.yml'
26+
import products from 'json-loader!yaml-loader!./pricing_products.yml'
2727
import colors from '../../services/colors'
2828
import icon from './pricing_icon'
2929

0 commit comments

Comments
 (0)