-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
executable file
·98 lines (86 loc) · 2.48 KB
/
build.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
import { createRequire } from 'module';
import fs from 'fs';
const require = createRequire(import.meta.url);
const Juke = require('./.yarn/juke');
Juke.setup({ file: import.meta.url, singleTarget: true });
const yarn = (...args) => Juke.exec('node', [
Juke.glob('.yarn/releases/*.cjs')[0],
...args,
]);
export const YarnTarget = new Juke.Target({
executes: async () => {
await yarn('install');
const pnpApi = require('./.pnp.cjs');
pnpApi.setup();
},
});
export const DtsTarget = new Juke.Target({
dependsOn: [YarnTarget],
executes: () => yarn(
'dts-bundle-generator',
'-o', 'dist/index.d.ts',
'src/index.ts'
),
});
export const BundleTarget = new Juke.Target({
dependsOn: [YarnTarget],
executes: async () => {
const { build } = require('esbuild');
const { pnpPlugin } = require('@yarnpkg/esbuild-plugin-pnp');
await build({
bundle: true,
format: 'cjs',
platform: 'node',
target: 'node14',
external: ['module'],
plugins: [pnpPlugin()],
entryPoints: ['src/index.ts'],
outfile: 'dist/index.js',
});
// A very crude implementation of replaceAll
let content = fs.readFileSync('dist/index.js', 'utf-8');
while (true) {
const nextContent = content.replace(process.cwd() + '/.yarn/cache/', '');
if (content === nextContent) {
break;
}
content = nextContent;
}
fs.writeFileSync('dist/index.js', content);
},
});
export const TscTarget = new Juke.Target({
dependsOn: [YarnTarget],
executes: () => yarn('tsc'),
});
export const UpdateLocalParameter = new Juke.Parameter({
type: 'boolean',
alias: 'u',
});
export const BuildTarget = new Juke.Target({
dependsOn: [TscTarget, DtsTarget, BundleTarget],
executes: async ({ get }) => {
if (get(UpdateLocalParameter)) {
Juke.logger.info('Updating local Juke version');
for (const file of ['index.js', 'index.d.ts']) {
fs.copyFileSync(`dist/${file}`, `.yarn/juke/${file}`);
}
}
},
});
export const CleanTarget = new Juke.Target({
executes: async () => {
await Juke.rm('.yarn/cache', { recursive: true });
await Juke.rm('.yarn/install-state.gz', { recursive: true });
await Juke.rm('.pnp.js');
await Juke.rm('dist', { recursive: true });
await Juke.rm('node_modules', { recursive: true });
},
});
export const PrintTarget = new Juke.Target({
executes: async ({ args }) => {
console.log({ args });
},
});
export default BuildTarget;