Skip to content

Commit 18b3612

Browse files
committed
feat: include polyfills and runtime globals into the transpiled source code instead of using ESBuild header
1 parent 6676e6b commit 18b3612

14 files changed

+1062
-1159
lines changed

src/esbuild-plugins/react-gtk/react-gtk-plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type ReactGtkEsbuildPluginOptions = {
2929

3030
export const reactGtkPlugin = (
3131
program: Program,
32-
options: ReactGtkEsbuildPluginOptions,
32+
options?: ReactGtkEsbuildPluginOptions,
3333
) => {
3434
const externalPackages = new Set(program.config.externalPackages ?? []);
3535
externalPackages.add("system");
@@ -41,7 +41,7 @@ export const reactGtkPlugin = (
4141
const gi = new GiImports(program.config.giVersions);
4242
const externalImports: ExternalImport[] = [];
4343

44-
for (const [name, version] of options.giRequirements ?? []) {
44+
for (const [name, version] of options?.giRequirements ?? []) {
4545
gi.add(name, version);
4646
}
4747

src/polyfills/queue-microtask.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ import { registerPolyfills } from "./shared/polyfill-global";
22

33
declare const __console_proxy: Console;
44

5-
const EOL = "\n";
6-
7-
function handleTimerCbError(e: unknown, type: string, stack?: string) {
8-
const stackFmtd = stack
9-
? __console_proxy.formatStackTrace(
10-
__console_proxy.mapStackTrace(stack),
11-
2,
12-
)
13-
: "Stack trace not available";
14-
__console_proxy.error(
15-
e,
16-
`${EOL}${EOL}The above error occured in a callback provided to ${type} in here:${EOL}${stackFmtd}`,
17-
);
18-
}
19-
20-
function runWithErrorHandler(cb: Function, type: string, stack?: string) {
21-
try {
22-
const res = cb();
23-
if (res instanceof Promise) {
24-
res.catch((e) => handleTimerCbError(e, type, stack));
5+
registerPolyfills("queueMicrotask")(() => {
6+
const EOL = "\n";
7+
8+
function handleTimerCbError(e: unknown, type: string, stack?: string) {
9+
const stackFmtd = stack
10+
? __console_proxy.formatStackTrace(
11+
__console_proxy.mapStackTrace(stack),
12+
2,
13+
)
14+
: "Stack trace not available";
15+
__console_proxy.error(
16+
e,
17+
`${EOL}${EOL}The above error occured in a callback provided to ${type} in here:${EOL}${stackFmtd}`,
18+
);
19+
}
20+
21+
function runWithErrorHandler(cb: Function, type: string, stack?: string) {
22+
try {
23+
const res = cb();
24+
if (res instanceof Promise) {
25+
res.catch((e) => handleTimerCbError(e, type, stack));
26+
}
27+
} catch (e) {
28+
handleTimerCbError(e, type, stack);
2529
}
26-
} catch (e) {
27-
handleTimerCbError(e, type, stack);
2830
}
29-
}
3031

31-
registerPolyfills("queueMicrotask")(() => {
3232
let onNextMicrotask: Array<() => void> | undefined;
3333

3434
function queueMicrotask(task: () => void) {

src/polyfills/shared/polyfill-global.ts

+10
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ export const registerPolyfills = <Names extends string>(
4646
}
4747
};
4848
};
49+
50+
registerPolyfills.fromModule = (
51+
module: Record<string, any>,
52+
) => {
53+
const entries = Object.entries(module);
54+
const names = entries.map(([name]) => name);
55+
registerPolyfills(...names)(() => {
56+
return module;
57+
});
58+
};

src/programs/build-program.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ import { getLinguas } from "../packaging/templates/po/linguas";
2222
import { getPostInstallScript } from "../packaging/templates/post-install-script";
2323
import { AppResources } from "../utils/app-resources";
2424
import { Command } from "../utils/command";
25+
import { getEntrypoint } from "../utils/get-entrypoint";
2526
import { getPlugins } from "../utils/get-plugins";
26-
import { getGlobalPolyfills } from "../utils/get-polyfills";
27-
import { getRuntimeInit } from "../utils/get-runtime-init";
2827
import { pascalToKebab } from "../utils/pascal-to-kebab";
2928
import { Program } from "./base";
3029
import { createBuildOptions } from "./default-build-options";
@@ -251,19 +250,17 @@ export class BuildProgram extends Program {
251250

252251
if (existsSync(buildDirPath)) await rimraf(buildDirPath, {});
253252

254-
const polyfills = await getGlobalPolyfills(this);
255-
const initScript = await getRuntimeInit(this);
253+
const entrypoint = getEntrypoint(this);
256254

257255
await this.esbuildCtx.init(
258-
createBuildOptions({
259-
banner: { js: `${polyfills.bundle}\n${initScript.bundle}` },
260-
entryPoints: [path.resolve(this.cwd, this.config.entrypoint)],
256+
createBuildOptions(this, {
257+
stdin: {
258+
contents: entrypoint,
259+
loader: "js",
260+
resolveDir: this.cwd,
261+
},
261262
outfile: path.resolve(buildDirPath, "src", "main.js"),
262-
plugins: getPlugins(this, {
263-
giRequirements: polyfills.requirements.concat(
264-
initScript.requirements,
265-
),
266-
}),
263+
plugins: getPlugins(this),
267264
minify: this.config.minify ?? (this.isDev ? false : true),
268265
treeShaking: this.config.treeShake ?? (this.isDev ? false : true),
269266
}),

src/programs/bundle-program.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import path from "path";
22
import { html, Output } from "termx-markup";
3+
import { getEntrypoint } from "../utils/get-entrypoint";
34
import { getPlugins } from "../utils/get-plugins";
4-
import { getGlobalPolyfills } from "../utils/get-polyfills";
5-
import { getRuntimeInit } from "../utils/get-runtime-init";
65
import { Program } from "./base";
76
import { createBuildOptions } from "./default-build-options";
87

@@ -25,19 +24,15 @@ export class BundleProgram extends Program {
2524
Output.print(html` <span color="lightBlue"> Building... </span> `);
2625
}
2726

28-
const polyfills = await getGlobalPolyfills(this);
29-
const initScript = await getRuntimeInit(this);
30-
3127
await this.esbuildCtx.init(
32-
createBuildOptions({
33-
banner: { js: `${polyfills.bundle}\n${initScript.bundle}` },
34-
entryPoints: [path.resolve(this.cwd, this.config.entrypoint)],
28+
createBuildOptions(this, {
29+
stdin: {
30+
contents: getEntrypoint(this),
31+
loader: "js",
32+
resolveDir: this.cwd,
33+
},
3534
outfile: path.resolve(this.cwd, this.config.outDir, "index.js"),
36-
plugins: getPlugins(this, {
37-
giRequirements: polyfills.requirements.concat(
38-
initScript.requirements,
39-
),
40-
}),
35+
plugins: getPlugins(this),
4136
minify: this.config.minify ?? (this.isDev ? false : true),
4237
treeShaking: this.config.treeShake ?? (this.isDev ? false : true),
4338
}),

src/programs/default-build-options.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { BuildOptions } from "esbuild";
2+
import { Program } from "./base";
23

34
const defaultBuildOptions = {
45
target: "es2022",
@@ -16,6 +17,7 @@ const defaultBuildOptions = {
1617
type DefaultKeys = keyof typeof defaultBuildOptions;
1718

1819
export const createBuildOptions = (
20+
program: Program,
1921
options:
2022
& Omit<BuildOptions, DefaultKeys>
2123
& Partial<Pick<BuildOptions, DefaultKeys>>,
@@ -25,6 +27,8 @@ export const createBuildOptions = (
2527
...defaultBuildOptions,
2628
...options,
2729
define: {
30+
__MODE__: JSON.stringify(program.isDev ? "development" : "production"),
31+
__SOURCE_MAPS_ENABLED__: String(!!program.config.sourcemap),
2832
...defaultBuildOptions.define,
2933
...options.define,
3034
},

src/programs/start-program.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { html, Output } from "termx-markup";
55
import { startAppPlugin } from "../esbuild-plugins/start-app/start-app-plugin";
66
import { AppResources } from "../utils/app-resources";
77
import { Command } from "../utils/command";
8+
import { getEntrypoint } from "../utils/get-entrypoint";
89
import type { AdditionalPlugins } from "../utils/get-plugins";
910
import { getPlugins } from "../utils/get-plugins";
10-
import { getGlobalPolyfills } from "../utils/get-polyfills";
11-
import { getRuntimeInit } from "../utils/get-runtime-init";
1211
import { BuildProgram } from "./build-program";
1312
import { createBuildOptions } from "./default-build-options";
1413

@@ -64,19 +63,15 @@ export class StartProgram extends BuildProgram {
6463

6564
if (existsSync(buildDirPath)) await rimraf(buildDirPath, {});
6665

67-
const polyfills = await getGlobalPolyfills(this);
68-
const initScript = await getRuntimeInit(this);
69-
7066
await this.esbuildCtx.init(
71-
createBuildOptions({
72-
banner: { js: `${polyfills.bundle}\n${initScript.bundle}` },
73-
entryPoints: [path.resolve(this.cwd, this.config.entrypoint)],
67+
createBuildOptions(this, {
68+
stdin: {
69+
contents: getEntrypoint(this),
70+
loader: "js",
71+
resolveDir: this.cwd,
72+
},
7473
outfile: path.resolve(buildDirPath, "src", "main.js"),
75-
plugins: getPlugins(this, {
76-
giRequirements: polyfills.requirements.concat(
77-
initScript.requirements,
78-
),
79-
}),
74+
plugins: getPlugins(this),
8075
minify: this.config.minify ?? (this.isDev ? false : true),
8176
treeShaking: this.config.treeShake ?? (this.isDev ? false : true),
8277
}),

0 commit comments

Comments
 (0)