Skip to content

Commit 67f1ff5

Browse files
fix: fix ESM build and types
Fixes #1317
1 parent 201f7d6 commit 67f1ff5

3 files changed

Lines changed: 39 additions & 20 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"scripts": {
3030
"build": "run-s build:*",
3131
"build:cjs": "tsc",
32-
"build:esm": "rollup --config --failAfterWarnings --environment ESM:true",
32+
"build:esm": "rollup --config --failAfterWarnings --environment ESM:true && scripts/fix-esm.sh",
3333
"build:umd": "rollup --config --failAfterWarnings --environment UMD:true",
34-
"clean": "rm -rf .nyc_output coverage dist lib esm",
34+
"clean": "rm -rf .nyc_output coverage dist esm lib",
3535
"lint": "eslint .",
3636
"lint:fix": "npm run lint -- --fix",
3737
"lint:package": "publint",

rollup.config.mjs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ const getPlugins = ({ browser = false, minify = false, outDir }) =>
2020
],
2121
}),
2222
typescript({
23-
declaration: false,
24-
declarationMap: false,
25-
module: 'esnext',
2623
compilerOptions: {
24+
module: 'esnext',
2725
outDir,
2826
},
2927
}),
@@ -42,41 +40,49 @@ const getUMDConfig = (minify = false) => {
4240
name: 'HTMLDOMParser',
4341
sourcemap: true,
4442
},
45-
plugins: getPlugins({ browser: true, minify, outDir: 'dist' }),
43+
plugins: getPlugins({
44+
browser: true,
45+
minify,
46+
outDir: 'dist',
47+
}),
4648
};
4749
};
4850

4951
const esmConfigs = [
52+
// ESM server
5053
{
5154
input: 'src/index.ts',
5255
output: {
53-
file: 'esm/index.mjs',
56+
dir: 'esm',
57+
entryFileNames: '[name].mjs',
5458
format: 'es',
59+
preserveModules: true,
60+
preserveModulesRoot: 'src',
5561
sourcemap: true,
5662
},
57-
plugins: getPlugins({ browser: false, minify: false, outDir: 'esm' }),
63+
plugins: getPlugins({
64+
browser: false,
65+
minify: false,
66+
outDir: 'esm',
67+
}),
5868
},
59-
// Client build: use preserveModules for proper module structure
69+
70+
// ESM client
6071
{
6172
input: 'src/client/html-to-dom.ts',
6273
output: {
6374
dir: 'esm/client',
64-
format: 'es',
6575
entryFileNames: '[name].mjs',
76+
format: 'es',
6677
preserveModules: true,
6778
preserveModulesRoot: 'src/client',
6879
sourcemap: true,
6980
},
70-
plugins: getPlugins({ browser: true, minify: false, outDir: 'esm/client' }),
71-
},
72-
{
73-
input: 'src/server/html-to-dom.ts',
74-
output: {
75-
file: 'esm/server/html-to-dom.mjs',
76-
format: 'es',
77-
sourcemap: true,
78-
},
79-
plugins: getPlugins({ browser: false, minify: false, outDir: 'esm' }),
81+
plugins: getPlugins({
82+
browser: true,
83+
minify: false,
84+
outDir: 'esm/client',
85+
}),
8086
},
8187
];
8288

scripts/fix-esm.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# replace `.d.ts` -> `.d.mts`
4+
find esm -type f -exec perl -i -pe 's/\.d\.ts/\.d\.mts/g' {} +
5+
6+
# rename `.d.ts` -> `.d.mts`
7+
find esm -name '*.d.ts' -exec bash -c 'mv "$1" "${1%.d.ts}.d.mts"' _ {} \;
8+
9+
# rename `.d.ts.map` -> `.d.mts.map`
10+
find esm -name '*.d.ts.map' -exec bash -c 'mv "$1" "${1%.d.ts.map}.d.mts.map"' _ {} \;
11+
12+
# delete extraneous files
13+
rm -rf esm/client/{client,server,index.*,types.*}

0 commit comments

Comments
 (0)