Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bundling): update esbuild executor to emit correct events #14193

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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