Skip to content

Commit 3c2b911

Browse files
CopilotTyriar
andcommitted
Set up code splitting for ESM build
Co-authored-by: Tyriar <2193314+Tyriar@users.noreply.github.com>
1 parent 5ee8760 commit 3c2b911

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

bin/esbuild.mjs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const commonOptions = {
2626
target: 'es2021',
2727
sourcemap: true,
2828
treeShaking: true,
29+
splitting: true,
30+
chunkNames: 'chunks/[name]-[hash]',
2931
logLevel: 'warning',
32+
outExtension: { '.js': '.mjs' },
3033
};
3134

3235
/** @type {esbuild.BuildOptions} */
@@ -103,7 +106,8 @@ if (config.addon) {
103106
bundleConfig = {
104107
...bundleConfig,
105108
entryPoints: [`addons/addon-${config.addon}/src/${getAddonEntryPoint(config.addon)}.ts`],
106-
outfile: `addons/addon-${config.addon}/lib/addon-${config.addon}.mjs`,
109+
outdir: `addons/addon-${config.addon}/lib`,
110+
entryNames: `addon-${config.addon}`,
107111
};
108112
outConfig = {
109113
...outConfig,
@@ -128,11 +132,12 @@ if (config.addon) {
128132
bundleConfig = {
129133
...bundleConfig,
130134
entryPoints: [`demo/client/client.ts`],
131-
outfile: 'demo/dist/client-bundle.js',
135+
outdir: 'demo/dist',
136+
entryNames: 'client-bundle',
132137
external: ['util', 'os', 'fs', 'path', 'stream', 'Terminal'],
133138
alias: {
134139
// Library ESM imports
135-
"@xterm/xterm": ".",
140+
"@xterm/xterm": "./lib/xterm.mjs",
136141
"@xterm/addon-attach": "./addons/addon-attach/lib/addon-attach.mjs",
137142
"@xterm/addon-clipboard": "./addons/addon-clipboard/lib/addon-clipboard.mjs",
138143
"@xterm/addon-fit": "./addons/addon-fit/lib/addon-fit.mjs",
@@ -164,14 +169,19 @@ if (config.addon) {
164169
format: 'cjs',
165170
platform: 'node',
166171
external: ['node-pty'],
172+
// Splitting is not compatible with cjs format
173+
splitting: false,
174+
// Override the .mjs extension from commonOptions
175+
outExtension: {},
167176
}
168177
skipOut = true;
169178
skipOutTest = true;
170179
} else if (config.isHeadless) {
171180
bundleConfig = {
172181
...bundleConfig,
173182
entryPoints: [`src/headless/public/Terminal.ts`],
174-
outfile: `headless/lib-headless/xterm-headless.mjs`
183+
outdir: `headless/lib-headless`,
184+
entryNames: 'xterm-headless',
175185
};
176186
outConfig = {
177187
...outConfig,
@@ -183,7 +193,8 @@ if (config.addon) {
183193
bundleConfig = {
184194
...bundleConfig,
185195
entryPoints: [`src/browser/public/Terminal.ts`],
186-
outfile: `lib/xterm.mjs`
196+
outdir: `lib`,
197+
entryNames: 'xterm',
187198
};
188199
outConfig = {
189200
...outConfig,

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
<div id="sidebar" class="grid">
2424
</div>
2525
</div>
26-
<script src="dist/client-bundle.js" defer ></script>
26+
<script src="dist/client-bundle.mjs" type="module" defer ></script>
2727
</body>
2828
</html>

demo/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
</head>
1919
<body id="test">
2020
<div id="terminal-container"></div>
21-
<script src="/dist/client-bundle.js" defer></script>
21+
<script src="/dist/client-bundle.mjs" type="module" defer></script>
2222
</body>
2323
</html>

0 commit comments

Comments
 (0)