|
1 | 1 | import esbuild from 'esbuild';
|
2 |
| -import {readFile} from 'fs/promises'; |
3 | 2 | import minimist from 'minimist';
|
4 | 3 |
|
5 | 4 | const argv = minimist(process.argv.slice(2));
|
6 | 5 |
|
7 |
| -const readJson = async (path) => |
8 |
| - JSON.parse(await readFile(new URL(path, import.meta.url))); |
| 6 | +const bundleNodeModules = new Set(['preact', 'preact-render-to-string']); |
9 | 7 |
|
10 |
| -const preactCompatPlugin = { |
11 |
| - name: 'preact-compat', |
| 8 | +const externalResolverPlugin = { |
| 9 | + name: 'external-resolver', |
12 | 10 | async setup(build) {
|
13 |
| - const path = await import('path'); |
14 |
| - const preact = path.join( |
15 |
| - process.cwd(), |
16 |
| - 'node_modules', |
17 |
| - 'preact', |
18 |
| - 'compat', |
19 |
| - 'dist', |
20 |
| - 'compat.module.js' |
21 |
| - ); |
22 |
| - build.onResolve({filter: /^(react-dom|react)$/}, (args) => { |
23 |
| - return {path: preact}; |
| 11 | + build.onResolve({filter: /.*/}, (args) => { |
| 12 | + if (!args.path.startsWith('.') && !bundleNodeModules.has(args.path)) { |
| 13 | + return {external: true, path: args.path}; |
| 14 | + } |
24 | 15 | });
|
25 | 16 | },
|
26 | 17 | };
|
27 | 18 |
|
28 | 19 | async function build() {
|
29 |
| - const pkg = await readJson('../package.json'); |
30 |
| - |
31 |
| - const external = [ |
32 |
| - [ |
33 |
| - ...Object.keys(pkg.devDependencies), |
34 |
| - ...Object.keys(pkg.dependencies), |
35 |
| - ].filter((name) => name.startsWith('@storybook/')), |
36 |
| - Object.keys(pkg.peerDependencies), |
37 |
| - ].flat(); |
38 |
| - |
39 |
| - const builds = { |
40 |
| - 'dist/esm': {format: 'esm', target: 'es2017'}, |
41 |
| - 'dist/cjs': {format: 'cjs'}, |
42 |
| - }; |
43 |
| - |
44 |
| - await Promise.all( |
45 |
| - Object.entries(builds).map(async ([outdir, options]) => { |
46 |
| - await esbuild.build({ |
47 |
| - entryPoints: ['src/index.ts'], |
48 |
| - bundle: true, |
49 |
| - platform: 'node', |
50 |
| - logLevel: 'info', |
51 |
| - plugins: [preactCompatPlugin], |
52 |
| - watch: argv.watch, |
53 |
| - external, |
54 |
| - outdir, |
55 |
| - ...options, |
56 |
| - }); |
57 |
| - }) |
58 |
| - ); |
| 20 | + await esbuild.build({ |
| 21 | + entryPoints: ['src/index.ts', 'src/register/index.tsx'], |
| 22 | + outdir: 'dist', |
| 23 | + bundle: true, |
| 24 | + platform: 'node', |
| 25 | + logLevel: 'info', |
| 26 | + watch: argv.watch, |
| 27 | + minifySyntax: true, |
| 28 | + plugins: [externalResolverPlugin], |
| 29 | + format: 'esm', |
| 30 | + target: 'es2017', |
| 31 | + }); |
59 | 32 | }
|
60 | 33 |
|
61 | 34 | build();
|
0 commit comments