Skip to content

Commit

Permalink
Merge pull request #4229 from GordonSmith/ESBUILD_PLUGINS
Browse files Browse the repository at this point in the history
fix:  Sourcemap for watch was different to build
  • Loading branch information
GordonSmith authored Aug 28, 2024
2 parents 9e1dd99 + 8811920 commit 2294d9c
Showing 1 changed file with 33 additions and 54 deletions.
87 changes: 33 additions & 54 deletions packages/esbuild-plugins/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,95 +39,74 @@ export const isDevelopment = argv.mode === "development";
export const isProduction = !isDevelopment;
export const isWatch = argv.watch;

export function build(config: BuildOptions) {
if (isDevelopment && Array.isArray(config.entryPoints)) {
// eslint-disable-next-line no-console
console.log("Start: ", config.entryPoints[0], config.outfile);
}
return esbuild.build({
sourcemap: "linked",
async function buildWatch(input: string, format: Format | "umd" = "esm", external: string[] = [], config: BuildOptions): Promise<void> {

const ctx = await esbuild.context({
entryPoints: [input],
format: format as Format,
bundle: true,
minify: isProduction,
sourcemap: isDevelopment,
external,
...config,
plugins: [
...(isWatch ? [rebuildLogger(config)] : []),
...(config.plugins ?? []),
sfxWasm()
]
}).finally(() => {
});

if (isWatch) {
await ctx.watch();
} else {
if (isDevelopment && Array.isArray(config.entryPoints)) {
// eslint-disable-next-line no-console
console.log("Start: ", config.entryPoints[0], config.outfile);
}
await ctx.rebuild();
await ctx.dispose();
if (isDevelopment && Array.isArray(config.entryPoints)) {
// eslint-disable-next-line no-console
console.log("Stop: ", config.entryPoints[0], config.outfile);
}
});
}

export async function watch(config: BuildOptions) {
await build(config);
return esbuild.context({
sourcemap: "external",
...config,
plugins: [
...(config.plugins ?? []),
rebuildLogger(config),
sfxWasm()
]
}).then(ctx => {
return ctx.watch();
});
}

export function buildWatch(config: BuildOptions) {
return isWatch ? watch(config) : build(config);
}
}

export function browserTpl(input: string, output: string, format: Format | "umd" = "esm", globalName?: string, libraryName?: string, external: string[] = []) {
return buildWatch({
entryPoints: [input],
return buildWatch(input, format, external, {
outfile: `${output}.${format === "esm" ? "js" : `${format}.js`}`,
platform: "browser",
target: "es2022",
format: format as Format,
globalName,
bundle: true,
minify: isProduction,
external,
plugins: format === "umd" ? [umdWrapper({ libraryName })] : []
});
}

export function browserBoth(input: string, output: string, globalName?: string, libraryName?: string, external: string[] = []) {
return Promise.all([
browserTpl(input, output, "esm", globalName, libraryName, external),
browserTpl(input, output, "umd", globalName, libraryName, external)
]);
}

export function nodeTpl(input: string, output: string, format: Format | "umd" = "esm", external: string[] = []) {
return buildWatch({
entryPoints: [input],
return buildWatch(input, format, external, {
outfile: `${output}.${format === "esm" ? NODE_MJS : NODE_CJS}`,
platform: "node",
target: "node20",
format: format as Format,
bundle: true,
minify: isProduction,
external
target: "node20"
});
}

export function neutralTpl(input: string, output: string, format: Format | "umd" = "esm", globalName?: string, libraryName?: string, external: string[] = []) {
return buildWatch({
entryPoints: [input],
return buildWatch(input, format, external, {
outfile: `${output}.${format === "esm" ? "js" : "umd.js"}`,
platform: "neutral",
target: "es2022",
format: format as Format,
globalName,
bundle: true,
minify: isProduction,
external,
plugins: format === "umd" ? [umdWrapper({ libraryName })] : []
});
}

export function browserBoth(input: string, output: string, globalName?: string, libraryName?: string, external: string[] = []) {
return Promise.all([
browserTpl(input, output, "esm", globalName, libraryName, external),
browserTpl(input, output, "umd", globalName, libraryName, external)
]);
}

export function nodeBoth(input: string, output: string, external: string[] = []) {
return Promise.all([
nodeTpl(input, output, "esm", external),
Expand Down

0 comments on commit 2294d9c

Please sign in to comment.