Skip to content

Commit

Permalink
fix(bundling): update esbuild executor to emit correct events
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored and Jack Hsu committed Jan 6, 2023
1 parent bd305fd commit 9c5f000
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
52 changes: 33 additions & 19 deletions packages/esbuild/src/executors/esbuild/esbuild.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import { normalizeOptions } from './lib/normalize';
import { EsBuildExecutorOptions } from './schema';
import { removeSync, writeJsonSync } from 'fs-extra';
import { createAsyncIterable } from '@nrwl/devkit/src/utils/async-iterable';
import { buildEsbuildOptions } from './lib/build-esbuild-options';
import { buildEsbuildOptions, getOutfile } from './lib/build-esbuild-options';
import { getExtraDependencies } from './lib/get-extra-dependencies';
import { DependentBuildableProjectNode } from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { join } from 'path';

const CJS_FILE_EXTENSION = '.cjs' as const;

Expand Down Expand Up @@ -115,7 +116,11 @@ export async function* esbuildExecutor(

next({
success: !!error && !hasTypeErrors,
outfile: esbuildOptions.outfile,
// Need to call getOutfile directly in the case of bundle=false and outfile is not set for esbuild.
outfile: join(
context.root,
getOutfile(format, options, context)
),
});
},
}
Expand All @@ -125,7 +130,11 @@ export async function* esbuildExecutor(

next({
success: true,
outfile: esbuildOptions.outfile,
// Need to call getOutfile directly in the case of bundle=false and outfile is not set for esbuild.
outfile: join(
context.root,
getOutfile(format, options, context)
),
});

return result;
Expand Down Expand Up @@ -164,33 +173,38 @@ export async function* esbuildExecutor(
}
);
} else {
const buildResults = await Promise.all(
options.format.map((format) =>
esbuild.build(buildEsbuildOptions(format, options, context))
)
);
const buildSuccess = buildResults.every((r) => r.errors?.length === 0);

let hasTypeErrors = false;
// Run type-checks first and bail if they don't pass.
if (!options.skipTypeCheck) {
const { errors } = await runTypeCheck(options, context);
hasTypeErrors = errors.length > 0;
if (errors.length > 0) {
yield { success: false };
return;
}
}

if (options.metafile) {
buildResults.forEach((r, idx) => {
// Emit a build event for each file format.
for (let i = 0; i < options.format.length; i++) {
const format = options.format[i];
const esbuildOptions = buildEsbuildOptions(format, options, context);
const buildResult = await esbuild.build(esbuildOptions);

if (options.metafile) {
const filename =
options.format.length === 1
? 'meta.json'
: `meta.${options.format[idx]}.json`;
: `meta.${options.format[i]}.json`;
writeJsonSync(
joinPathFragments(options.outputPath, filename),
r.metafile
buildResult.metafile
);
});
}
}

return { success: buildSuccess && !hasTypeErrors };
yield {
success: buildResult.errors.length === 0,
// Need to call getOutfile directly in the case of bundle=false and outfile is not set for esbuild.
outfile: join(context.root, getOutfile(format, options, context)),
};
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function getOutExtension(
: CJS_FILE_EXTENSION;
}

function getOutfile(
export function getOutfile(
format: 'cjs' | 'esm',
options: EsBuildExecutorOptions,
context: ExecutorContext
Expand Down

0 comments on commit 9c5f000

Please sign in to comment.