-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.js
112 lines (97 loc) · 3.44 KB
/
esbuild.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const esbuild = require('esbuild');
const sassPlugin = require('esbuild-sass-plugin').sassPlugin;
const postcss = require('postcss');
const autoprefixer = require('autoprefixer');
const postcssPresetEnv = require('postcss-preset-env');
const path = require('path');
const fs = require('fs');
function fromDir(startPath, filter, callback) {
if (!fs.existsSync(startPath)) {
console.log('no dir ', startPath);
return;
}
const files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter, callback); // recurse
} else if (
filter.test(filename) &&
!/((stories)|(typings.d)|(types)).ts/.test(filename)
)
callback(filename.replace(/\\/g, '/'));
}
}
const sassEntryPoints = [];
fromDir('src', /[a-z\-]+\.scss$/, (filename) => {
sassEntryPoints.push(filename);
});
const typeScriptEntryPoints = [];
fromDir('src', /[a-z\-]+\.ts$/, (filename) => {
typeScriptEntryPoints.push(filename);
});
const typeScriptFormats = ['cjs', 'esm'];
const watchChange = process.argv.slice(2).includes('--watch');
async function build(watchChange) {
for (let index = 0; index < sassEntryPoints.length; index++) {
const entryPoint = sassEntryPoints[index];
const path = entryPoint.replace(/[a-z\-]{1,}\.scss$/, '');
const outfile =
path + entryPoint.replace(path, '').replace(/\.scss$/, '.css');
await esbuild.build({
entryPoints: [entryPoint],
minify: true,
sourcemap: false,
outfile,
watch: watchChange,
plugins: [
sassPlugin({
async transform(source) {
const { css } = await postcss([
autoprefixer,
postcssPresetEnv({ stage: 0 }),
]).process(source, { from: undefined });
return css;
},
}),
],
});
}
typeScriptEntryPoints.forEach((entryPoint) => {
typeScriptFormats.forEach((format) => {
if (/\/models\//.test(entryPoint)) return;
const extension = format == 'cjs' ? '.js' : `.${format}.js`;
const outfile = entryPoint
.replace(/src\//, 'lib/')
.replace(/\.ts$/, extension);
esbuild
.build({
entryPoints: [entryPoint],
bundle: true,
minify: true,
platform: 'node',
sourcemap: true,
target: 'es6',
format,
outfile,
watch: watchChange,
plugins: [
sassPlugin({
type: 'css-text',
}),
],
})
.then((r) => {
if (!watchChange) return;
console.clear();
console.log('\x1b[32m%s\x1b[0m', 'esbuild watch start');
})
.catch((e) => {
console.error('esbuild error', e);
process.exit(1);
});
});
});
}
build(watchChange);