-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-enhancers.mjs
79 lines (70 loc) · 1.96 KB
/
build-enhancers.mjs
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
import path from 'path';
import fs from 'fs/promises';
import esbuild from 'esbuild';
const enhancersDir = path.resolve('src', 'enhancers');
async function build() {
const nameList = await fs.readdir(enhancersDir);
const enhancerPathList = nameList
.filter((name) => {
const arr = name.split('.');
return name !== 'index.ts' && arr[arr.length - 1] === 'ts';
})
.map((name) => path.resolve(enhancersDir, name));
const formatBuild = (format) => {
return Promise.all([
esbuild.build({
entryPoints: enhancerPathList,
entryNames: `[dir]/[name].${format}`,
format,
outdir: path.resolve('dist', 'enhancers'),
write: true,
bundle: true,
treeShaking: true,
external: ['rxjs', 'immer'],
define: {
'process.env.NODE_ENV': 'process.env.NODE_ENV',
},
}),
esbuild
.transform(
enhancerPathList
.map((path) => {
const arr = path.split('/');
return arr[arr.length - 1].slice(0, -3);
})
.map((fileName) => `export { ${fileName} } from './${fileName}.${format}';`)
.join(''),
{ format },
)
.then((result) => {
return fs.writeFile(path.resolve('dist', 'enhancers', `index.${format}.js`), result.code);
}),
]);
};
await formatBuild('cjs');
await formatBuild('esm');
try {
await fs.access(path.resolve('enhancers'));
} catch (error) {
if (error.code === 'ENOENT') {
await fs.mkdir(path.resolve('enhancers'));
} else {
throw error;
}
}
fs.writeFile(
path.resolve('enhancers', 'package.json'),
JSON.stringify(
{
name: 'nice-store/enhancers',
types: '../dist/enhancers/index.d.ts',
main: '../dist/enhancers/index.cjs.js',
module: '../dist/enhancers/index.esm.js',
sideEffects: false,
},
null,
' ',
),
);
}
build();