Skip to content

Commit c1c0af7

Browse files
committed
feat: transform component library js
1 parent a410da2 commit c1c0af7

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

template/javascript/build.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,33 @@ async function buildComponentLibrary(name) {
5454
const destination = path.resolve('dist', 'miniprogram_npm', name);
5555
await fs.copy(source, destination);
5656

57-
if (__PROD__) {
58-
return new Promise((resolve) => {
59-
const jobs = [];
60-
const compress = async (filePath) => {
61-
let code = await fs.readFile(filePath, 'utf8');
57+
return new Promise((resolve) => {
58+
const jobs = [];
59+
const tnm = async (filePath) => {
60+
let { code } = await babel.transformFileAsync(filePath);
61+
62+
if (__PROD__) {
6263
code = (await minify(code, terserOptions)).code;
63-
await fs.writeFile(filePath, code);
64-
};
65-
66-
const watcher = chokidar.watch([destination], {
67-
ignored: ['**/.{gitkeep,DS_Store}'],
68-
});
69-
watcher.on('add', (filePath) => {
70-
if (!/\.js$/.test(filePath)) return;
71-
const promise = compress(filePath);
72-
jobs.push(promise);
73-
});
74-
watcher.on('ready', async () => {
75-
const promise = watcher.close();
76-
jobs.push(promise);
77-
await Promise.all(jobs);
78-
resolve();
79-
});
64+
}
65+
66+
await fs.writeFile(filePath, code);
67+
};
68+
69+
const watcher = chokidar.watch([destination], {
70+
ignored: ['**/.{gitkeep,DS_Store}'],
8071
});
81-
}
72+
watcher.on('add', (filePath) => {
73+
if (!/\.js$/.test(filePath)) return;
74+
const promise = tnm(filePath);
75+
jobs.push(promise);
76+
});
77+
watcher.on('ready', async () => {
78+
const promise = watcher.close();
79+
jobs.push(promise);
80+
await Promise.all(jobs);
81+
resolve();
82+
});
83+
});
8284
}
8385

8486
async function scanDependencies() {

template/typescript/build.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,33 @@ async function buildComponentLibrary(name) {
5454
const destination = path.resolve('dist', 'miniprogram_npm', name);
5555
await fs.copy(source, destination);
5656

57-
if (__PROD__) {
58-
return new Promise((resolve) => {
59-
const jobs = [];
60-
const compress = async (filePath) => {
61-
let code = await fs.readFile(filePath, 'utf8');
57+
return new Promise((resolve) => {
58+
const jobs = [];
59+
const tnm = async (filePath) => {
60+
let { code } = await babel.transformFileAsync(filePath);
61+
62+
if (__PROD__) {
6263
code = (await minify(code, terserOptions)).code;
63-
await fs.writeFile(filePath, code);
64-
};
65-
66-
const watcher = chokidar.watch([destination], {
67-
ignored: ['**/.{gitkeep,DS_Store}'],
68-
});
69-
watcher.on('add', (filePath) => {
70-
if (!/\.js$/.test(filePath)) return;
71-
const promise = compress(filePath);
72-
jobs.push(promise);
73-
});
74-
watcher.on('ready', async () => {
75-
const promise = watcher.close();
76-
jobs.push(promise);
77-
await Promise.all(jobs);
78-
resolve();
79-
});
64+
}
65+
66+
await fs.writeFile(filePath, code);
67+
};
68+
69+
const watcher = chokidar.watch([destination], {
70+
ignored: ['**/.{gitkeep,DS_Store}'],
8071
});
81-
}
72+
watcher.on('add', (filePath) => {
73+
if (!/\.js$/.test(filePath)) return;
74+
const promise = tnm(filePath);
75+
jobs.push(promise);
76+
});
77+
watcher.on('ready', async () => {
78+
const promise = watcher.close();
79+
jobs.push(promise);
80+
await Promise.all(jobs);
81+
resolve();
82+
});
83+
});
8284
}
8385

8486
async function scanDependencies() {

0 commit comments

Comments
 (0)