Skip to content

Commit

Permalink
fix: Sourcemap for watch was different to build
Browse files Browse the repository at this point in the history
Common up base build / watch functions

Signed-off-by: Gordon Smith <[email protected]>
  • Loading branch information
GordonSmith committed Aug 28, 2024
1 parent 9e1dd99 commit 8811920
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 8811920

Please sign in to comment.