Skip to content

Commit f9dab95

Browse files
authored
fix register (#68)
1 parent 8020677 commit f9dab95

File tree

3 files changed

+22
-48
lines changed

3 files changed

+22
-48
lines changed

bin/build.mjs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,34 @@
11
import esbuild from 'esbuild';
2-
import {readFile} from 'fs/promises';
32
import minimist from 'minimist';
43

54
const argv = minimist(process.argv.slice(2));
65

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']);
97

10-
const preactCompatPlugin = {
11-
name: 'preact-compat',
8+
const externalResolverPlugin = {
9+
name: 'external-resolver',
1210
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+
}
2415
});
2516
},
2617
};
2718

2819
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+
});
5932
}
6033

6134
build();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"author": "The AMP HTML Authors",
1313
"license": "Apache-2.0",
1414
"files": [
15-
"dist"
15+
"dist",
16+
"register.js"
1617
],
1718
"peerDependencies": {
1819
"@storybook/addons": "^6.4.18",

register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import './esm/register/index';
1+
import './dist/register';

0 commit comments

Comments
 (0)