Skip to content

Commit d167a48

Browse files
committed
Edit file to adapt to webpack4.x (not done)
Known BUG: 1. Can't load more css files when swtich theme. 2. Unknown causes system instability
1 parent 5ce28c5 commit d167a48

File tree

5 files changed

+108
-141
lines changed

5 files changed

+108
-141
lines changed

build/utils.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
const path = require('path')
33
const config = require('../config')
4-
const ExtractTextPlugin = require('extract-text-webpack-plugin')
4+
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
55

66
exports.assetsPath = function(_path) {
77
const assetsSubDirectory = process.env.NODE_ENV === 'production' ?
@@ -34,35 +34,27 @@ exports.cssLoaders = function(options) {
3434

3535
// generate loader string to be used with extract text plugin
3636
function generateLoaders(loader, loaderOptions) {
37-
let loaders = cssLoader
37+
let loaders = []
3838
if (loader) {
39-
loaders = {
39+
loaders = [{
4040
loader: loader + '-loader',
4141
options: Object.assign({}, loaderOptions, {
4242
sourceMap: options.sourceMap
4343
})
44-
}
45-
// loaders.push({
46-
// loader: loader + '-loader',
47-
// options: Object.assign({}, loaderOptions, {
48-
// sourceMap: options.sourceMap
49-
// })
50-
// })
51-
} else{
52-
return []
44+
}]
5345
}
5446

55-
// Extract CSS when that option is specified
56-
// (which is the case during production build)
5747
if (options.extract) {
58-
return ExtractTextPlugin.extract({
59-
use: [loaders, 'postcss-loader'],
60-
fallback: 'vue-style-loader',
61-
publicPath: '../../'
62-
})
48+
let extractLoader = {
49+
loader: MiniCssExtractPlugin.loader,
50+
options: {
51+
// fallback: 'vue-style-loader',
52+
publicPath: '../../'
53+
}
54+
}
55+
return [extractLoader, 'css-loader'].concat(['postcss-loader'], loaders)
6356
} else {
64-
// return ['vue-style-loader'].concat(loaders)
65-
return [loaders]
57+
return ['vue-style-loader', 'css-loader'].concat(['postcss-loader'], loaders)
6658
}
6759
}
6860

@@ -84,11 +76,9 @@ exports.styleLoaders = function(options) {
8476
const loaders = exports.cssLoaders(options)
8577
for (const extension in loaders) {
8678
const loader = loaders[extension]
87-
const extLoader = ['vue-style-loader', 'css-loader']
88-
8979
output.push({
9080
test: new RegExp('\\.' + extension + '$'),
91-
use: extLoader.concat(loader, ['postcss-loader'])
81+
use: loader
9282
})
9383
}
9484
return output

build/webpack.prod.conf.js

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@ const merge = require('webpack-merge')
77
const baseWebpackConfig = require('./webpack.base.conf')
88
const CopyWebpackPlugin = require('copy-webpack-plugin')
99
const HtmlWebpackPlugin = require('html-webpack-plugin')
10-
const ExtractTextPlugin = require('extract-text-webpack-plugin')
10+
const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
11+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
1112
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
1213

14+
function recursiveIssuer(m) {
15+
if (m.issuer) {
16+
return recursiveIssuer(m.issuer);
17+
} else if (m.name) {
18+
return m.name;
19+
} else {
20+
return false;
21+
}
22+
}
23+
1324
const webpackConfig = merge(baseWebpackConfig, {
1425
entry: {
1526
'theme-dark': './src/assets/css/theme-dark.scss',
1627
'theme-default': './src/assets/css/theme-default.scss'
1728
},
1829
module: {
1930
rules: utils.styleLoaders({
20-
sourceMap: config.build.productionSourceMap
31+
sourceMap: config.build.productionSourceMap,
32+
extract: true
2133
})
2234
},
2335
devtool: config.build.productionSourceMap ? '#source-map' : false,
@@ -31,13 +43,18 @@ const webpackConfig = merge(baseWebpackConfig, {
3143
// https://github.com/webpack/webpack/issues/6409
3244
// https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693
3345
optimization: {
34-
minimize: true,
35-
runtimeChunk: true,
46+
minimizer: [
47+
// js mini
48+
new UglifyJsPlugin({
49+
cache: true,
50+
parallel: true,
51+
sourceMap: config.build.productionSourceMap // set to true if you want JS source maps
52+
}),
53+
// css mini
54+
new OptimizeCSSPlugin({})
55+
],
3656
splitChunks: {
37-
// chunks: "async",
38-
// maxAsyncRequests: 5,
39-
// maxInitialRequests: 3,
40-
chunks: 'all',
57+
chunks: 'async',
4158
minSize: 0,
4259
maxAsyncRequests: Infinity,
4360
maxInitialRequests: Infinity,
@@ -50,21 +67,26 @@ const webpackConfig = merge(baseWebpackConfig, {
5067
priority: -20,
5168
reuseExistingChunk: true,
5269
},
53-
css: {
54-
name: function(el){
55-
console.log(el)
56-
return utils.assetsPath('css/'+ el.name +'.'+ el.hash +'.css')
57-
},
58-
test: /\.(css|sass|scss|less)(\?.*)?$/,
59-
chunks: 'all'
70+
themeDark: {
71+
name: 'theme-dark',
72+
test: (m,c,entry = 'theme-dark') => recursiveIssuer(m) === entry,
73+
chunks: 'all',
74+
enforce: true
75+
},
76+
themeDefault: {
77+
name: 'theme-default',
78+
test: (m,c,entry = 'theme-default') => recursiveIssuer(m) === entry,
79+
chunks: 'all',
80+
enforce: true
6081
},
6182
echarts: {
6283
name: 'echarts',
63-
enforce: true,
84+
chunks: 'all',
6485
test: function(module){
6586
var context = module.context;
6687
return context && (context.indexOf('echarts') >= 0 || context.indexOf('zrender') >= 0)
67-
}
88+
},
89+
enforce: true
6890
},
6991
vendors: {
7092
name: 'vendors',
@@ -80,26 +102,13 @@ const webpackConfig = merge(baseWebpackConfig, {
80102
},
81103
chunks: "all"
82104
}
83-
84-
// commons: {
85-
// test: /[\\/]node_modules[\\/]/,
86-
// name: "vendors",
87-
// chunks: "all"
88-
// }
89105
}
90106
}
91107
},
92108
plugins: [
93109
// extract css into its own file
94-
// new ExtractTextPlugin({
95-
// filename: utils.assetsPath('css/[name].[contenthash:7].css')
96-
// }),
97-
// Compress extracted CSS. We are using this plugin so that possible
98-
// duplicated CSS from different components can be deduped.
99-
new OptimizeCSSPlugin({
100-
cssProcessorOptions: {
101-
safe: true
102-
}
110+
new MiniCssExtractPlugin({
111+
filename: utils.assetsPath('css/[name].[contenthash:7].css')
103112
}),
104113
// generate dist index.html with correct asset hash for caching.
105114
// you can customize output by editing /index.html
@@ -108,48 +117,13 @@ const webpackConfig = merge(baseWebpackConfig, {
108117
filename: config.build.index,
109118
template: './src/index.html',
110119
inject: true,
111-
// minify: {
112-
// removeComments: true,
113-
// collapseWhitespace: true,
114-
// removeAttributeQuotes: true
115-
// // more options:
116-
// // https://github.com/kangax/html-minifier#options-quick-reference
117-
// },
118120
minify: false,
119121
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
120122
chunksSortMode: 'dependency',
121123
excludeChunks: ['theme-default', 'theme-dark']
122124
}),
123125
// keep module.id stable when vender modules does not change
124126
new webpack.HashedModuleIdsPlugin(),
125-
// split vendor js into its own file
126-
// new webpack.optimize.CommonsChunkPlugin({
127-
// name: 'vendor',
128-
// minChunks: function(module) {
129-
// // any required modules inside node_modules are extracted to vendor
130-
// return (
131-
// module.resource &&
132-
// /\.js$/.test(module.resource) &&
133-
// module.resource.indexOf(
134-
// path.join(__dirname, '../node_modules')
135-
// ) === 0
136-
// )
137-
// }
138-
// }),
139-
// split echarts into its own file
140-
// new webpack.optimize.CommonsChunkPlugin({
141-
// async: ['echarts'],
142-
// minChunks(module) {
143-
// var context = module.context;
144-
// return context && (context.indexOf('echarts') >= 0 || context.indexOf('zrender') >= 0);
145-
// }
146-
// }),
147-
// extract webpack runtime and module manifest to its own file in order to
148-
// prevent vendor hash from being updated whenever app bundle is updated
149-
// new webpack.optimize.CommonsChunkPlugin({
150-
// name: 'manifest',
151-
// chunks: ['vendor']
152-
// }),
153127
// copy custom static assets
154128
new CopyWebpackPlugin([
155129
{

package-lock.json

Lines changed: 51 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"semver": "^5.4.1",
6868
"shelljs": "^0.8.2",
6969
"style-loader": "^0.21.0",
70+
"uglifyjs-webpack-plugin": "^1.2.5",
7071
"url-loader": "^1.0.1",
7172
"vue-loader": "^15.0.11",
7273
"vue-style-loader": "^4.1.0",

src/router/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ NProgress.configure({ showSpinner: false });
4141
Vue.use(VueRouter)
4242

4343
const router = new VueRouter({
44-
mode: 'history',
44+
mode: 'hash',
4545
routes: staticRoute
4646
})
4747

0 commit comments

Comments
 (0)