Skip to content

Commit f093fcc

Browse files
author
白唯
committed
build(webpack): 修改 webpack配置做代码分离,减小vendor 大小,提高加载速度
1 parent c4413c0 commit f093fcc

File tree

10 files changed

+201
-112
lines changed

10 files changed

+201
-112
lines changed

Diff for: .eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
module.exports = {
23
root: true,
34
parserOptions: {

Diff for: package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"url": "http://me.ibwei.com"
99
},
1010
"scripts": {
11-
"serve": "vue-cli-service serve",
11+
"serve": "vue-cli-service serve --stats-json",
1212
"build": "vue-cli-service build",
1313
"lint": "vue-cli-service lint",
1414
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0 -s",
@@ -18,7 +18,8 @@
1818
"test:unit": "vue-cli-service test:unit",
1919
"test-dev:unit": "vue-cli-service test:unit --watch",
2020
"test:api": "vue-cli-service test:unit ./tests/api/*.spec.ts",
21-
"test-dev:api": "vue-cli-service test:unit ./tests/api/*.spec.ts --watch"
21+
"test-dev:api": "vue-cli-service test:unit ./tests/api/*.spec.ts --watch",
22+
"analysis": "vue-cli-service build --stats-json"
2223
},
2324
"main": "dist/index.js",
2425
"files": [
@@ -72,14 +73,15 @@
7273
"http-server": "^0.12.3",
7374
"less": "^2.7.0",
7475
"less-loader": "^5.0.0",
76+
"lint-staged": "^10.5.1",
7577
"prettier": "^1.19.1",
7678
"style-resources-loader": "^1.3.2",
7779
"ts-node": "^9.0.0",
7880
"typedoc": "^0.19.0",
7981
"typescript": "~3.9.3",
80-
"lint-staged": "^10.5.1",
8182
"vue-cli-plugin-style-resources-loader": "~0.1.4",
82-
"vue-property-decorator": "^9.0.0"
83+
"vue-property-decorator": "^9.0.0",
84+
"webpack-bundle-analyzer": "^4.3.0"
8385
},
8486
"gitHooks": {
8587
"pre-commit": "lint-staged"

Diff for: src/@types/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { AppStateType } from '@/store/modules/app/state'
22
import { ConsoleStateType } from '@/store/modules/console/state'
33
import { UserStateType } from '@/store/modules/user/state'
4+
import { TeamStateType } from '@/store/modules/user/modules/team/state'
45

56
// vuex state 的模块的类型
67
type ModuleType = {
78
app: AppStateType
89
console: ConsoleStateType
9-
user: UserStateType
10+
user: UserStateType & { team: TeamStateType }
1011
}
1112

1213
// 所有的StateType

Diff for: src/store/modules/user/modules/team/actions.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
//
3+
}

Diff for: src/store/modules/user/modules/team/getters.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
//
3+
}

Diff for: src/store/modules/user/modules/team/mutations.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
__set(state: any, msg: { key: string; val: any }) {
3+
state[msg.key] = msg.val
4+
}
5+
}

Diff for: src/store/modules/user/modules/team/state.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Module } from 'vuex'
2+
import { StateType } from '../../../../../@types/index'
3+
const state = {
4+
teamName: '雪狼团队'
5+
}
6+
type TeamStateType = typeof state
7+
8+
const ModuleTeam: Module<TeamStateType, StateType> = {
9+
namespaced: true,
10+
...state
11+
}
12+
13+
export { TeamStateType, state }
14+
export default ModuleTeam

Diff for: src/store/modules/user/state.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StateType } from '@/@types'
22
import { Module } from 'vuex'
3-
3+
import ModuleTeam from './modules/team/state'
44
interface Token {
55
[propertys: string]: any
66
}
@@ -25,7 +25,10 @@ type UserStateType = typeof state
2525

2626
const user: Module<UserStateType, StateType> = {
2727
namespaced: true,
28-
...state
28+
...state,
29+
modules: {
30+
team: ModuleTeam
31+
}
2932
}
3033

3134
export { UserStateType, state }

Diff for: src/views/test/Test.vue

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export default defineComponent({
9999
},
100100
setup() {
101101
const store = useStore<StateType>()
102+
console.log('-------------')
103+
console.log(store.state)
104+
// vuex三级module
105+
console.log(store.state.user)
102106
103107
onMounted(() => {
104108
console.log(store.state.app.language)

Diff for: vue.config.js

+158-105
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,158 @@
1-
const IS_DEV = process.env.NODE_ENV !== 'production'
2-
/**
3-
* @todo 开发环境配置
4-
* 某些实用工具, plugins 和 loaders 都只能在构建生产环境时才有用
5-
* 在开发时使用 UglifyJsPlugin 来压缩和修改代码是没有意义的,不压缩
6-
*/
7-
8-
const DEVELOPMENT = webpackConfig => {
9-
/**
10-
* @todo 启用 eval-source-map 更好的测试
11-
* 每个模块使用 eval() 执行,并且 source map 转换为 DataUrl 后添加到 eval() 中。
12-
* 初始化 source map 时比较慢,但是会在重新构建时提供比较快的速度,并且生成实际的文件。
13-
* 行数能够正确映射,因为会映射到原始代码中。它会生成用于开发环境的最佳品质的 source map。
14-
*/
15-
16-
webpackConfig.store.set('devtool', 'eval-source-map')
17-
webpackConfig.plugin('html').tap(([options]) => [
18-
Object.assign(options, {
19-
minify: false,
20-
chunksSortMode: 'none'
21-
})
22-
])
23-
return webpackConfig
24-
}
25-
26-
/**
27-
* @todo 生产环境配置
28-
* 每个额外的 loader/plugin 都有启动时间。尽量少使用不同的工具
29-
*/
30-
31-
const PRODUCTION = webpackConfig => {
32-
/**
33-
* @todo 不需要启用 source-map,去除 console 的情况下 source-map 根本没用,还浪费大量时间和空间
34-
* 详情见:https://webpack.js.org/configuration/devtool/#devtool
35-
*/
36-
webpackConfig.store.set('devtool', '')
37-
webpackConfig.plugin('html').tap(([options]) => [
38-
Object.assign(options, {
39-
minify: {
40-
removeComments: true,
41-
removeCommentsFromCDATA: true,
42-
collapseWhitespace: true,
43-
conservativeCollapse: false,
44-
collapseInlineTagWhitespace: true,
45-
collapseBooleanAttributes: true,
46-
removeRedundantAttributes: true,
47-
removeAttributeQuotes: false,
48-
removeEmptyAttributes: true,
49-
removeScriptTypeAttributes: true,
50-
removeStyleLinkTypeAttributes: true,
51-
useShortDoctype: true,
52-
minifyJS: true,
53-
minifyCSS: true
54-
},
55-
cache: true, // 仅在文件被更改时发出文件
56-
hash: true, // true则将唯一的webpack编译哈希值附加到所有包含的脚本和CSS文件中,这对于清除缓存很有用
57-
scriptLoading: 'defer', // 现代浏览器支持非阻塞javascript加载('defer'),以提高页面启动性能。
58-
inject: true, // true所有javascript资源都将放置在body元素的底部
59-
chunksSortMode: 'none'
60-
})
61-
])
62-
63-
return webpackConfig
64-
}
65-
66-
module.exports = {
67-
publicPath: IS_DEV ? '/' : '/vue3-ts-base',
68-
css: {
69-
loaderOptions: {
70-
less: {
71-
globalVars: {},
72-
srouceMap: IS_DEV,
73-
lessOptions: {
74-
javascriptEnabled: true
75-
}
76-
}
77-
}
78-
},
79-
devServer: {
80-
proxy: 'http://10.10.10.115:8002'
81-
},
82-
pluginOptions: {
83-
/** 全局加载less 的 webpack 插件 */
84-
'style-resources-loader': {
85-
preProcessor: 'less',
86-
patterns: ['./src/styles/index.less']
87-
}
88-
},
89-
/**
90-
* @description 去掉 console信息
91-
* config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
92-
* html-webpack-plugin插件配置详情见 https://github.com/jantimon/html-webpack-plugin#options
93-
*/
94-
configureWebpack: config => {
95-
if (!IS_DEV) {
96-
config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
97-
config.optimization.minimizer[0].options.terserOptions.sourceMap = false
98-
}
99-
},
100-
chainWebpack: config => {
101-
IS_DEV ? DEVELOPMENT(config) : PRODUCTION(config)
102-
},
103-
productionSourceMap: false,
104-
lintOnSave: true
105-
}
1+
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
2+
.BundleAnalyzerPlugin
3+
4+
const IS_DEV = process.env.NODE_ENV !== 'production'
5+
/**
6+
* @todo 开发环境配置
7+
* 某些实用工具, plugins 和 loaders 都只能在构建生产环境时才有用
8+
* 在开发时使用 UglifyJsPlugin 来压缩和修改代码是没有意义的,不压缩
9+
*/
10+
11+
const DEVELOPMENT = webpackConfig => {
12+
/**
13+
* @todo 启用 eval-source-map 更好的测试
14+
* 每个模块使用 eval() 执行,并且 source map 转换为 DataUrl 后添加到 eval() 中。
15+
* 初始化 source map 时比较慢,但是会在重新构建时提供比较快的速度,并且生成实际的文件。
16+
* 行数能够正确映射,因为会映射到原始代码中。它会生成用于开发环境的最佳品质的 source map。
17+
*/
18+
19+
webpackConfig.store.set('devtool', 'eval-source-map')
20+
webpackConfig.plugin('html').tap(([options]) => [
21+
Object.assign(options, {
22+
minify: false,
23+
chunksSortMode: 'none'
24+
})
25+
])
26+
27+
// webpackConfig.plugin('BundleAnalyzerPlugin').use(BundleAnalyzerPlugin)
28+
29+
return webpackConfig
30+
}
31+
32+
/**
33+
* @todo 生产环境配置
34+
* 每个额外的 loader/plugin 都有启动时间。尽量少使用不同的工具
35+
*/
36+
37+
const PRODUCTION = webpackConfig => {
38+
/**
39+
* @todo 不需要启用 source-map,去除 console 的情况下 source-map 根本没用,还浪费大量时间和空间
40+
* 详情见:https://webpack.js.org/configuration/devtool/#devtool
41+
*/
42+
webpackConfig.store.set('devtool', '')
43+
webpackConfig.plugin('html').tap(([options]) => [
44+
Object.assign(options, {
45+
minify: {
46+
removeComments: true,
47+
removeCommentsFromCDATA: true,
48+
collapseWhitespace: true,
49+
conservativeCollapse: false,
50+
collapseInlineTagWhitespace: true,
51+
collapseBooleanAttributes: true,
52+
removeRedundantAttributes: true,
53+
removeAttributeQuotes: false,
54+
removeEmptyAttributes: true,
55+
removeScriptTypeAttributes: true,
56+
removeStyleLinkTypeAttributes: true,
57+
useShortDoctype: true,
58+
minifyJS: true,
59+
minifyCSS: true
60+
},
61+
cache: true, // 仅在文件被更改时发出文件
62+
hash: true, // true则将唯一的webpack编译哈希值附加到所有包含的脚本和CSS文件中,这对于清除缓存很有用
63+
scriptLoading: 'defer', // 现代浏览器支持非阻塞javascript加载('defer'),以提高页面启动性能。
64+
inject: true, // true所有javascript资源都将放置在body元素的底部
65+
chunksSortMode: 'none'
66+
})
67+
])
68+
69+
return webpackConfig
70+
}
71+
72+
module.exports = {
73+
publicPath: IS_DEV ? '/' : '/vue3-ts-base',
74+
css: {
75+
loaderOptions: {
76+
less: {
77+
globalVars: {},
78+
srouceMap: IS_DEV,
79+
lessOptions: {
80+
javascriptEnabled: true
81+
}
82+
}
83+
}
84+
},
85+
devServer: {
86+
proxy: 'http://10.10.10.115:8002'
87+
},
88+
pluginOptions: {
89+
/** 全局加载less 的 webpack 插件 */
90+
'style-resources-loader': {
91+
preProcessor: 'less',
92+
patterns: ['./src/styles/index.less']
93+
}
94+
},
95+
/**
96+
* @description 去掉 console信息
97+
* config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
98+
* html-webpack-plugin插件配置详情见 https://github.com/jantimon/html-webpack-plugin#options
99+
*/
100+
configureWebpack: config => {
101+
config.optimization = {
102+
splitChunks: {
103+
chunks: 'all',
104+
minSize: 3000, // (默认值:30000)块的最小大小。
105+
minChunks: 1, //(默认值:1)在拆分之前共享模块的最小块数
106+
maxAsyncRequests: 5, //(默认值为5)按需加载时并行请求的最大数量
107+
maxInitialRequests: 6, // (默认值为3)入口点的最大并行请求数
108+
automaticNameDelimiter: '-',
109+
name: true,
110+
cacheGroups: {
111+
lodash: {
112+
name: 'lodash',
113+
test: /[\\/]node_modules[\\/]lodash[\\/]/,
114+
priority: 20
115+
},
116+
vue: {
117+
name: 'vue',
118+
test: /[\\/]node_modules[\\/]vue[\\/]/
119+
},
120+
vuex: {
121+
name: 'vuex',
122+
test: /[\\/]node_modules[\\/]vuex[\\/]/
123+
},
124+
'vuex-presistedstate': {
125+
name: 'vuex-presistedstate',
126+
test: /[\\/]node_modules[\\/]vuex-presistedstate[\\/]/
127+
},
128+
'vue-router': {
129+
name: 'vue-router',
130+
test: /[\\/]node_modules[\\/]vue-router[\\/]/
131+
},
132+
'ant-design-vue': {
133+
name: 'ant-design-vue',
134+
test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/
135+
},
136+
moment: {
137+
name: 'moment',
138+
test: /[\\/]node_modules[\\/]moment[\\/]/,
139+
priority: 40
140+
}
141+
}
142+
}
143+
}
144+
},
145+
chainWebpack: config => {
146+
config.resolve.symlinks(true)
147+
148+
config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
149+
{
150+
analyzerMode: 'static'
151+
}
152+
])
153+
154+
IS_DEV ? DEVELOPMENT(config) : PRODUCTION(config)
155+
},
156+
productionSourceMap: false,
157+
lintOnSave: true
158+
}

0 commit comments

Comments
 (0)