Skip to content

Commit 2d545c6

Browse files
Amour1688tangjinzhou
authored andcommitted
feat: make run dev more faster and modify some dependencies (vueComponent#1671)
1 parent c8574d1 commit 2d545c6

12 files changed

+199
-79
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ package-lock.json
7070
# 备份文件
7171
/components/test/*
7272
list.txt
73+
74+
site/dev.js

antd-tools/getWebpackConfig.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const webpack = require('webpack');
33
const WebpackBar = require('webpackbar');
44
const webpackMerge = require('webpack-merge');
5-
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
5+
const TerserPlugin = require('terser-webpack-plugin');
66
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
77
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
88
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
@@ -182,13 +182,8 @@ All rights reserved.
182182
config.output.libraryTarget = 'umd';
183183
config.optimization = {
184184
minimizer: [
185-
new UglifyJsPlugin({
186-
cache: true,
187-
parallel: true,
185+
new TerserPlugin({
188186
sourceMap: true,
189-
uglifyOptions: {
190-
warnings: false,
191-
},
192187
}),
193188
],
194189
};

antd-tools/gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const esDir = path.join(cwd, 'es');
3434
function dist(done) {
3535
rimraf.sync(path.join(cwd, 'dist'));
3636
process.env.RUN_ENV = 'PRODUCTION';
37-
const webpackConfig = require(path.join(cwd, 'webpack.build.config.js'));
37+
const webpackConfig = require(path.join(cwd, 'build/webpack.build.conf.js'));
3838
webpack(webpackConfig, (err, stats) => {
3939
if (err) {
4040
console.error(err.stack || err);

build/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
devComponent: 'select',
3+
};

build/dev.js

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
process.env.ENTRY_INDEX = 'dev';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const chokidar = require('chokidar');
6+
const importFresh = require('import-fresh');
7+
const replace = require('json-templater/string');
8+
const webpack = require('webpack');
9+
const WebpackDevServer = require('webpack-dev-server');
10+
const devWebpack = require('./webpack.dev.conf');
11+
12+
const configPath = path.join(__dirname, './config.js');
13+
14+
let { devComponent } = require('./config');
15+
16+
const componentsInPrototype = ['Modal', 'message', 'notification'];
17+
18+
const MAIN_TEMPLATE = `import 'babel-polyfill';
19+
import './index.less';
20+
import 'highlight.js/styles/solarized-light.css';
21+
import Vue from 'vue';
22+
import Vuex from 'vuex';
23+
import VueI18n from 'vue-i18n';
24+
import VueRouter from 'vue-router';
25+
import VueClipboard from 'vue-clipboard2';
26+
import Md from './components/md';
27+
import Api from './components/api';
28+
import demoBox from './components/demoBox';
29+
import demoContainer from './components/demoContainer';
30+
import Modal from '../components/modal';
31+
import message from '../components/message';
32+
import notification from '../components/notification';
33+
{{importComponents}}
34+
{{importStyles}}
35+
import '../components/modal/style';
36+
import '../components/message/style';
37+
import '../components/notification/style';
38+
import Test from '../components/{{name}}/demo/index.vue';
39+
import zhCN from './theme/zh-CN';
40+
import enUS from './theme/en-US';
41+
42+
Vue.use(Vuex);
43+
Vue.use(VueClipboard);
44+
Vue.use(VueRouter);
45+
Vue.use(VueI18n);
46+
Vue.component(Md.name, Md);
47+
Vue.component(Api.name, Api);
48+
Vue.component('demo-box', demoBox);
49+
Vue.component('demo-container', demoContainer);
50+
51+
Vue.prototype.$message = message;
52+
Vue.prototype.$notification = notification;
53+
Vue.prototype.$info = Modal.info;
54+
Vue.prototype.$success = Modal.success;
55+
Vue.prototype.$error = Modal.error;
56+
Vue.prototype.$warning = Modal.warning;
57+
Vue.prototype.$confirm = Modal.confirm;
58+
Vue.prototype.$destroyAll = Modal.destroyAll;
59+
60+
Vue.use(Modal);
61+
{{install}}
62+
63+
const i18n = new VueI18n({
64+
locale: enUS.locale,
65+
messages: {
66+
[enUS.locale]: { message: enUS.messages },
67+
[zhCN.locale]: { message: zhCN.messages },
68+
},
69+
});
70+
71+
const router = new VueRouter({
72+
mode: 'history',
73+
routes: [{ path: '/*', component: Test }],
74+
});
75+
76+
const store = new Vuex.Store({
77+
state: {
78+
username: 'zeka',
79+
},
80+
mutations: {
81+
update(state, payload) {
82+
state.username = payload.username;
83+
},
84+
},
85+
});
86+
new Vue({
87+
el: '#app',
88+
i18n,
89+
router,
90+
store,
91+
});
92+
`;
93+
94+
const OUTPUT_PATH = path.join(__dirname, '../site/dev.js');
95+
96+
const generateEntry = components =>
97+
Object.keys(components)
98+
.map(component => `import ${component} from '../components/${components[component]}';`)
99+
.join('\n');
100+
101+
const generateStyles = components =>
102+
Object.keys(components)
103+
.map(component => `import '../components/${components[component]}/style';`)
104+
.join('\n');
105+
106+
const generateInstall = components =>
107+
Object.keys(components)
108+
.map(component => `Vue.use(${component});`)
109+
.join('\n');
110+
111+
const renderTemplate = name => {
112+
const components = {
113+
Tooltip: 'tooltip', // for DemoBox
114+
};
115+
116+
const demoPaths = fs.readdirSync(path.join(__dirname, `../components/${name}/demo`));
117+
118+
demoPaths.forEach(demoPath => {
119+
const demo = fs
120+
.readFileSync(path.join(__dirname, `../components/${name}/demo/${demoPath}`))
121+
.toString();
122+
123+
const componentsInDemo = demo.match(/a-(\w+(-\w+)*)/g) || [];
124+
componentsInDemo.forEach(name => {
125+
const componentName = name.replace(/-(\w)/g, ($, $1) => $1.toUpperCase()).replace(/^a/, '');
126+
127+
if (componentsInPrototype.includes(componentName)) {
128+
return;
129+
}
130+
131+
const componentPath = path.join(__dirname, `../components/${name.replace(/^a-/, '')}`);
132+
if (fs.existsSync(componentPath)) {
133+
components[componentName] = name.replace(/^a-/, '');
134+
}
135+
});
136+
});
137+
138+
const importComponents = generateEntry(components);
139+
const importStyles = generateStyles(components);
140+
const install = generateInstall(components);
141+
const template = replace(MAIN_TEMPLATE, {
142+
importComponents,
143+
importStyles,
144+
install,
145+
name,
146+
});
147+
fs.writeFileSync(OUTPUT_PATH, template);
148+
};
149+
150+
let demoWatcher;
151+
152+
chokidar.watch(configPath, { ignoreInitial: true }).on('change', async () => {
153+
devComponent = importFresh(configPath).devComponent;
154+
155+
demoWatcher && (await demoWatcher.close());
156+
157+
demoWatcher = chokidar.watch(path.join(__dirname, `../components/${devComponent}/demo`));
158+
demoWatcher.on('change', () => {
159+
renderTemplate(devComponent);
160+
});
161+
162+
renderTemplate(devComponent);
163+
});
164+
165+
renderTemplate(devComponent);
166+
167+
const compiler = webpack(devWebpack);
168+
169+
const configuration = devWebpack.devServer;
170+
171+
const server = new WebpackDevServer(compiler, configuration);
172+
server.listen(configuration.port);

webpack.base.config.js build/webpack.base.conf.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Token = require('markdown-it/lib/token');
44
const cheerio = require('cheerio');
55
const WebpackBar = require('webpackbar');
66
const VueLoaderPlugin = require('vue-loader/lib/plugin');
7-
const getBabelCommonConfig = require('./antd-tools/getBabelCommonConfig');
7+
const getBabelCommonConfig = require('../antd-tools/getBabelCommonConfig');
88
const babelConfig = getBabelCommonConfig(false);
99

1010
babelConfig.plugins.push(require.resolve('babel-plugin-syntax-dynamic-import'));
@@ -199,11 +199,11 @@ module.exports = {
199199
extensions: ['.js', '.jsx', '.vue', '.md'],
200200
alias: {
201201
vue$: 'vue/dist/vue.esm.js',
202-
antd: path.join(__dirname, 'components'),
203-
'ant-design-vue': path.join(__dirname, 'components'),
204-
'ant-design-vue/es': path.join(__dirname, 'components'),
205-
'ant-design-vue/lib': path.join(__dirname, 'components'),
206-
'@': path.join(__dirname, ''),
202+
antd: path.join(__dirname, '../components'),
203+
'ant-design-vue': path.join(__dirname, '../components'),
204+
'ant-design-vue/es': path.join(__dirname, '../components'),
205+
'ant-design-vue/lib': path.join(__dirname, '../components'),
206+
'@': path.join(__dirname, '../'),
207207
},
208208
},
209209
plugins: [new VueLoaderPlugin(), new WebpackBar()],

webpack.build.config.js build/webpack.build.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This config is for building dist files
22
const webpack = require('webpack');
3-
const getWebpackConfig = require('./antd-tools/getWebpackConfig');
3+
const getWebpackConfig = require('../antd-tools/getWebpackConfig');
44

55
// noParse still leave `require('./locale' + name)` in dist files
66
// ignore is better

webpack.config.js build/webpack.dev.conf.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const path = require('path');
22
const HtmlWebpackPlugin = require('html-webpack-plugin');
33
const merge = require('webpack-merge');
4-
const baseWebpackConfig = require('./webpack.base.config');
4+
const baseWebpackConfig = require('./webpack.base.conf');
55

66
module.exports = merge(baseWebpackConfig, {
77
mode: 'development',
88
output: {
9-
path: path.resolve(__dirname, './dist'),
9+
path: path.resolve(__dirname, '../dist'),
1010
publicPath: '/',
1111
filename: 'build.js',
1212
},
@@ -36,6 +36,8 @@ module.exports = merge(baseWebpackConfig, {
3636
rewrites: [{ from: /./, to: '/index.html' }],
3737
},
3838
disableHostCheck: true,
39+
hot: true,
40+
open: true,
3941
headers: { 'Access-Control-Allow-Origin': '*' },
4042
},
4143
performance: {

webpack.site.config.js build/webpack.site.conf.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const webpack = require('webpack');
33
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
44
const HtmlWebpackPlugin = require('html-webpack-plugin');
55
const merge = require('webpack-merge');
6-
const baseWebpackConfig = require('./webpack.base.config');
6+
const baseWebpackConfig = require('./webpack.base.conf');
77

88
module.exports = merge(baseWebpackConfig, {
99
output: {
10-
path: path.resolve(__dirname, './_site'),
10+
path: path.resolve(__dirname, '../_site'),
1111
publicPath: '/',
1212
filename: '[name].[contenthash:8].js',
1313
chunkFilename: '[contenthash:8].async.js',

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"scripts"
2727
],
2828
"scripts": {
29-
"dev": "cross-env NODE_ENV=development ENTRY_INDEX=dev ./node_modules/.bin/webpack-dev-server --open --hot --port 3001",
30-
"start": "cross-env NODE_ENV=development ./node_modules/.bin/webpack-dev-server --open --hot",
29+
"dev": "node build/dev.js",
30+
"start": "cross-env NODE_ENV=development webpack-dev-server --config build/webpack.dev.conf.js",
3131
"test": "cross-env NODE_ENV=test jest --config .jest.js",
3232
"site": "node scripts/run.js _site",
3333
"copy": "node scripts/run.js copy-html",
@@ -109,7 +109,7 @@
109109
"fs-extra": "^8.0.0",
110110
"glob": "^7.1.2",
111111
"gulp": "^4.0.1",
112-
"gulp-babel": "^8.0.0",
112+
"gulp-babel": "^7.0.0",
113113
"gulp-strip-code": "^0.1.4",
114114
"highlight.js": "^9.12.0",
115115
"html-webpack-plugin": "^3.2.0",
@@ -119,6 +119,7 @@
119119
"jest-serializer-vue": "^2.0.0",
120120
"jest-transform-stub": "^2.0.0",
121121
"js-base64": "^2.4.8",
122+
"json-templater": "^1.2.0",
122123
"jsonp": "^0.2.1",
123124
"less": "^3.9.0",
124125
"less-loader": "^5.0.0",
@@ -149,8 +150,8 @@
149150
"stylelint": "^12.0.0",
150151
"stylelint-config-prettier": "^8.0.0",
151152
"stylelint-config-standard": "^19.0.0",
153+
"terser-webpack-plugin": "^2.3.1",
152154
"through2": "^3.0.0",
153-
"uglifyjs-webpack-plugin": "^2.1.1",
154155
"url-loader": "^3.0.0",
155156
"vue": "^2.6.11",
156157
"vue-antd-md-loader": "^1.1.0",

scripts/gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const cwd = process.cwd();
1414
function dist(done) {
1515
rimraf.sync(path.join(cwd, '_site'));
1616
process.env.RUN_ENV = 'PRODUCTION';
17-
const webpackConfig = require(path.join(cwd, 'webpack.site.config.js'));
17+
const webpackConfig = require(path.join(cwd, 'build/webpack.site.conf.js'));
1818
webpack(webpackConfig, (err, stats) => {
1919
if (err) {
2020
console.error(err.stack || err);

site/dev.js

-55
This file was deleted.

0 commit comments

Comments
 (0)