Skip to content

Commit 16782f8

Browse files
committed
fix: add feedback for dev & build
1 parent 47d5b22 commit 16782f8

File tree

3 files changed

+89
-80
lines changed

3 files changed

+89
-80
lines changed

template/base/package.json.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<%_ } _%>
6969
<%_ } _%>
7070
"fs-extra": "^11.2.0",
71+
"kolorist": "^1.8.0",
7172
"postcss": "^8.4.39",
7273
"postcss-load-config": "^6.0.1",
7374
"postcss-pxtorpx-pro": "^2.0.0",

template/javascript/build.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import replace from '@rollup/plugin-replace';
1313
import terser from '@rollup/plugin-terser';
1414
import resolve from '@rollup/plugin-node-resolve';
1515
import commonjs from '@rollup/plugin-commonjs';
16+
import { green, bold } from 'kolorist';
1617

18+
let waitList = [];
19+
const startTime = Date.now();
1720
const NODE_ENV = process.env.NODE_ENV || 'production';
1821
const __PROD__ = NODE_ENV === 'production';
1922
const terserOptions = {
@@ -42,7 +45,7 @@ async function bundleModule(module) {
4245
__PROD__ && terser(terserOptions),
4346
].filter(Boolean),
4447
});
45-
bundle.write({
48+
await bundle.write({
4649
exports: 'named',
4750
file: `dist/miniprogram_npm/${module}/index.js`,
4851
format: 'cjs',
@@ -78,7 +81,8 @@ async function processScript(filePath) {
7881
'"use strict";',
7982
'"use strict";\n\nvar PromisePolyfill = require("promise-polyfill");\nPromise = PromisePolyfill.default;',
8083
);
81-
bundleModule('promise-polyfill');
84+
const promise = bundleModule('promise-polyfill');
85+
waitList.push(promise);
8286
}
8387

8488
traverse.default(ast, {
@@ -91,7 +95,8 @@ async function processScript(filePath) {
9195
return;
9296
}
9397

94-
bundleModule(node.arguments[0].value);
98+
const promise = bundleModule(node.arguments[0].value);
99+
waitList.push(promise);
95100
},
96101
});
97102

@@ -102,7 +107,7 @@ async function processScript(filePath) {
102107
const destination = filePath.replace('src', 'dist');
103108
// Make sure the directory already exists when write file
104109
await fs.copy(filePath, destination);
105-
fs.writeFile(destination, code);
110+
await fs.writeFile(destination, code);
106111
}
107112

108113
async function processTemplate(filePath) {
@@ -134,39 +139,47 @@ async function processStyle(filePath) {
134139
.replace(/\.css$/, '.wxss');
135140
// Make sure the directory already exists when write file
136141
await fs.copy(filePath, destination);
137-
fs.writeFile(destination, css);
142+
await fs.writeFile(destination, css);
138143
}
139144

140-
async function dev() {
141-
await fs.remove('dist');
142-
const cb = (filePath) => {
143-
if (/\.js$/.test(filePath)) {
144-
processScript(filePath);
145-
return;
146-
}
145+
const cb = async (filePath) => {
146+
if (/\.js$/.test(filePath)) {
147+
await processScript(filePath);
148+
return;
149+
}
147150

148-
if (/\.html$/.test(filePath)) {
149-
processTemplate(filePath);
150-
return;
151-
}
151+
if (/\.html$/.test(filePath)) {
152+
await processTemplate(filePath);
153+
return;
154+
}
152155

153-
if (/\.css$/.test(filePath)) {
154-
processStyle(filePath);
155-
return;
156-
}
156+
if (/\.css$/.test(filePath)) {
157+
await processStyle(filePath);
158+
return;
159+
}
157160

158-
fs.copy(filePath, filePath.replace('src', 'dist'));
159-
};
161+
await fs.copy(filePath, filePath.replace('src', 'dist'));
162+
};
160163

164+
async function dev() {
165+
await fs.remove('dist');
161166
chokidar
162167
.watch(['src'], {
163168
ignored: ['**/.{gitkeep,DS_Store}'],
164169
})
165170
.on('add', (filePath) => {
166-
cb(filePath);
171+
const promise = cb(filePath);
172+
waitList.push(promise);
167173
})
168174
.on('change', (filePath) => {
169175
cb(filePath);
176+
})
177+
.on('ready', async () => {
178+
await Promise.all(waitList);
179+
console.log(bold(green(`启动完成,耗时:${Date.now() - startTime}ms`)));
180+
console.log(bold(green('监听文件变化中...')));
181+
// Release memory.
182+
waitList = null;
170183
});
171184
}
172185

@@ -176,24 +189,15 @@ async function prod() {
176189
ignored: ['**/.{gitkeep,DS_Store}'],
177190
});
178191
watcher.on('add', (filePath) => {
179-
if (/\.js$/.test(filePath)) {
180-
processScript(filePath);
181-
return;
182-
}
183-
184-
if (/\.html$/.test(filePath)) {
185-
processTemplate(filePath);
186-
return;
187-
}
188-
189-
if (/\.css$/.test(filePath)) {
190-
processStyle(filePath);
191-
return;
192-
}
193-
194-
fs.copy(filePath, filePath.replace('src', 'dist'));
192+
const promise = cb(filePath);
193+
waitList.push(promise);
194+
});
195+
watcher.on('ready', async () => {
196+
const promise = watcher.close();
197+
waitList.push(promise);
198+
await Promise.all(waitList);
199+
console.log(bold(green(`构建完成,耗时:${Date.now() - startTime}ms`)));
195200
});
196-
watcher.on('ready', () => watcher.close());
197201
}
198202

199203
if (__PROD__) {

template/typescript/build.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import replace from '@rollup/plugin-replace';
1313
import terser from '@rollup/plugin-terser';
1414
import resolve from '@rollup/plugin-node-resolve';
1515
import commonjs from '@rollup/plugin-commonjs';
16+
import { green, bold } from 'kolorist';
1617

18+
let waitList = [];
19+
const startTime = Date.now();
1720
const NODE_ENV = process.env.NODE_ENV || 'production';
1821
const __PROD__ = NODE_ENV === 'production';
1922
const terserOptions = {
@@ -42,7 +45,7 @@ async function bundleModule(module) {
4245
__PROD__ && terser(terserOptions),
4346
].filter(Boolean),
4447
});
45-
bundle.write({
48+
await bundle.write({
4649
exports: 'named',
4750
file: `dist/miniprogram_npm/${module}/index.js`,
4851
format: 'cjs',
@@ -78,7 +81,8 @@ async function processScript(filePath) {
7881
'"use strict";',
7982
'"use strict";\n\nvar PromisePolyfill = require("promise-polyfill");\nPromise = PromisePolyfill.default;',
8083
);
81-
bundleModule('promise-polyfill');
84+
const promise = bundleModule('promise-polyfill');
85+
waitList.push(promise);
8286
}
8387

8488
traverse.default(ast, {
@@ -91,7 +95,8 @@ async function processScript(filePath) {
9195
return;
9296
}
9397

94-
bundleModule(node.arguments[0].value);
98+
const promise = bundleModule(node.arguments[0].value);
99+
waitList.push(promise);
95100
},
96101
});
97102

@@ -102,7 +107,7 @@ async function processScript(filePath) {
102107
const destination = filePath.replace('src', 'dist').replace(/\.ts$/, '.js');
103108
// Make sure the directory already exists when write file
104109
await fs.copy(filePath, destination);
105-
fs.writeFile(destination, code);
110+
await fs.writeFile(destination, code);
106111
}
107112

108113
async function processTemplate(filePath) {
@@ -134,39 +139,47 @@ async function processStyle(filePath) {
134139
.replace(/\.css$/, '.wxss');
135140
// Make sure the directory already exists when write file
136141
await fs.copy(filePath, destination);
137-
fs.writeFile(destination, css);
142+
await fs.writeFile(destination, css);
138143
}
139144

140-
async function dev() {
141-
await fs.remove('dist');
142-
const cb = (filePath) => {
143-
if (/\.ts$/.test(filePath)) {
144-
processScript(filePath);
145-
return;
146-
}
145+
const cb = async (filePath) => {
146+
if (/\.ts$/.test(filePath)) {
147+
await processScript(filePath);
148+
return;
149+
}
147150

148-
if (/\.html$/.test(filePath)) {
149-
processTemplate(filePath);
150-
return;
151-
}
151+
if (/\.html$/.test(filePath)) {
152+
await processTemplate(filePath);
153+
return;
154+
}
152155

153-
if (/\.css$/.test(filePath)) {
154-
processStyle(filePath);
155-
return;
156-
}
156+
if (/\.css$/.test(filePath)) {
157+
await processStyle(filePath);
158+
return;
159+
}
157160

158-
fs.copy(filePath, filePath.replace('src', 'dist'));
159-
};
161+
await fs.copy(filePath, filePath.replace('src', 'dist'));
162+
};
160163

164+
async function dev() {
165+
await fs.remove('dist');
161166
chokidar
162167
.watch(['src'], {
163168
ignored: ['**/.{gitkeep,DS_Store}'],
164169
})
165170
.on('add', (filePath) => {
166-
cb(filePath);
171+
const promise = cb(filePath);
172+
waitList.push(promise);
167173
})
168174
.on('change', (filePath) => {
169175
cb(filePath);
176+
})
177+
.on('ready', async () => {
178+
await Promise.all(waitList);
179+
console.log(bold(green(`启动完成,耗时:${Date.now() - startTime}ms`)));
180+
console.log(bold(green('监听文件变化中...')));
181+
// Release memory.
182+
waitList = null;
170183
});
171184
}
172185

@@ -176,24 +189,15 @@ async function prod() {
176189
ignored: ['**/.{gitkeep,DS_Store}'],
177190
});
178191
watcher.on('add', (filePath) => {
179-
if (/\.ts$/.test(filePath)) {
180-
processScript(filePath);
181-
return;
182-
}
183-
184-
if (/\.html$/.test(filePath)) {
185-
processTemplate(filePath);
186-
return;
187-
}
188-
189-
if (/\.css$/.test(filePath)) {
190-
processStyle(filePath);
191-
return;
192-
}
193-
194-
fs.copy(filePath, filePath.replace('src', 'dist'));
192+
const promise = cb(filePath);
193+
waitList.push(promise);
194+
});
195+
watcher.on('ready', async () => {
196+
const promise = watcher.close();
197+
waitList.push(promise);
198+
await Promise.all(waitList);
199+
console.log(bold(green(`构建完成,耗时:${Date.now() - startTime}ms`)));
195200
});
196-
watcher.on('ready', () => watcher.close());
197201
}
198202

199203
if (__PROD__) {

0 commit comments

Comments
 (0)