Skip to content

Commit 7172552

Browse files
authored
fix: resolve ModuleNotFoundError in ESM environments by generating native bundles (#1299)
* fix: resolve ModuleNotFoundError in ESM environments by generating native bundles * chore: modify build config * chore: modify build config * fix(package.json): add author and remove unused build:esm script * fix(rollup): use preserveModules for client ESM build * build(package.json): add build:umd script * fix(package.json): build all artifacts in test:esm script
1 parent c2ce150 commit 7172552

9 files changed

Lines changed: 75 additions & 57 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Build directory
22
dist/
3-
esm/types.ts
3+
esm/
44
lib/
55

66
# Logs

esm/client/html-to-dom.d.mts

Lines changed: 0 additions & 3 deletions
This file was deleted.

esm/client/html-to-dom.mjs

Lines changed: 0 additions & 3 deletions
This file was deleted.

esm/index.d.mts

Lines changed: 0 additions & 2 deletions
This file was deleted.

esm/index.mjs

Lines changed: 0 additions & 3 deletions
This file was deleted.

esm/server/html-to-dom.d.mts

Lines changed: 0 additions & 8 deletions
This file was deleted.

esm/server/html-to-dom.mjs

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 3 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": "awk '!/sourceMappingURL/' lib/types.d.ts > esm/types.ts",
32+
"build:esm": "rollup --config --failAfterWarnings --environment ESM:true",
3333
"build:umd": "rollup --config --failAfterWarnings",
34-
"clean": "rm -rf .nyc_output coverage dist lib",
34+
"clean": "rm -rf .nyc_output coverage dist lib esm",
3535
"lint": "eslint .",
3636
"lint:fix": "npm run lint -- --fix",
3737
"lint:package": "publint",
@@ -43,7 +43,7 @@
4343
"test:client": "npm run test:client:watch -- --single-run",
4444
"test:client:build": "NODE_ENV=test npm run build",
4545
"test:client:watch": "npm run test:client:build && karma start",
46-
"test:esm": "npm run build:cjs && node --test test/esm",
46+
"test:esm": "npm run build && node --test test/esm",
4747
"test:server": "npm run build:cjs && nyc mocha"
4848
},
4949
"repository": {

rollup.config.mjs

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,95 @@ import commonjs from '@rollup/plugin-commonjs';
33
import resolve from '@rollup/plugin-node-resolve';
44
import terser from '@rollup/plugin-terser';
55
import typescript from '@rollup/plugin-typescript';
6+
import { createRequire } from 'module';
7+
const require = createRequire(import.meta.url);
68

7-
const getConfig = (minify = false) => ({
8-
input: 'src/index.ts',
9-
10-
output: {
11-
file: `dist/html-dom-parser${minify ? '.min' : ''}.js`,
12-
format: 'umd',
13-
name: 'HTMLDOMParser',
14-
sourcemap: true,
15-
},
16-
17-
plugins: [
18-
alias({
19-
entries: [
20-
{
21-
find: './server/html-to-dom',
22-
replacement: './client/html-to-dom',
23-
},
24-
],
25-
}),
26-
9+
const getPlugins = (isBrowser = false, minify = false, outputDir) =>
10+
[
11+
isBrowser &&
12+
alias({
13+
entries: [
14+
{
15+
find: './server/html-to-dom',
16+
replacement: './client/html-to-dom',
17+
},
18+
],
19+
}),
2720
typescript({
2821
declaration: false,
2922
declarationMap: false,
3023
module: 'esnext',
3124
compilerOptions: {
32-
outDir: 'dist',
25+
outDir: outputDir,
3326
},
3427
}),
35-
3628
commonjs(),
37-
resolve({ browser: true }),
29+
resolve({ browser: isBrowser }),
3830
minify && terser(),
39-
],
40-
});
31+
].filter(Boolean);
4132

42-
const configs = [getConfig(), getConfig(true)];
33+
const getUMDConfig = (minify = false) => {
34+
const output = `dist/html-dom-parser${minify ? '.min' : ''}.js`;
35+
return {
36+
input: 'src/index.ts',
37+
output: {
38+
file: output,
39+
format: 'umd',
40+
name: 'HTMLDOMParser',
41+
sourcemap: true,
42+
},
43+
plugins: getPlugins(true, minify, 'dist'),
44+
};
45+
};
46+
47+
const esmConfigs = [
48+
{
49+
input: 'src/index.ts',
50+
output: {
51+
file: 'esm/index.mjs',
52+
format: 'es',
53+
sourcemap: true,
54+
},
55+
plugins: getPlugins(false, false, 'esm'),
56+
},
57+
// Client build: use preserveModules for proper module structure
58+
{
59+
input: 'src/client/html-to-dom.ts',
60+
output: {
61+
dir: 'esm/client',
62+
format: 'es',
63+
entryFileNames: '[name].mjs',
64+
preserveModules: true,
65+
preserveModulesRoot: 'src/client',
66+
sourcemap: true,
67+
},
68+
plugins: getPlugins(true, false, 'esm/client'),
69+
},
70+
{
71+
input: 'src/server/html-to-dom.ts',
72+
output: {
73+
file: 'esm/server/html-to-dom.mjs',
74+
format: 'es',
75+
sourcemap: true,
76+
},
77+
plugins: getPlugins(false, false, 'esm'),
78+
},
79+
];
4380

44-
if (process.env.NODE_ENV === 'test') {
45-
configs.push({
46-
input: 'node_modules/htmlparser2',
81+
const configs = [
82+
getUMDConfig(),
83+
getUMDConfig(true),
84+
...esmConfigs,
85+
{
86+
input: require.resolve('htmlparser2'),
4787
output: {
4888
file: 'dist/htmlparser2.js',
4989
format: 'umd',
5090
name: 'htmlparser2',
5191
sourcemap: true,
5292
},
5393
plugins: [commonjs(), resolve({ browser: true })],
54-
});
55-
}
94+
},
95+
];
5696

5797
export default configs;

0 commit comments

Comments
 (0)