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(@angular-devkit/build-angular): show file replacement in TS missing file error in esbuild builder #24218

Merged
merged 1 commit into from Nov 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -453,36 +453,33 @@ export function createCompilerPlugin(
async () => {
assert.ok(fileEmitter, 'Invalid plugin execution order');

const request = pluginOptions.fileReplacements?.[args.path] ?? args.path;

// The filename is currently used as a cache key. Since the cache is memory only,
// the options cannot change and do not need to be represented in the key. If the
// cache is later stored to disk, then the options that affect transform output
// would need to be added to the key as well as a check for any change of content.
let contents = pluginOptions.sourceFileCache?.typeScriptFileCache.get(
pathToFileURL(args.path).href,
pathToFileURL(request).href,
);

if (contents === undefined) {
const typescriptResult = await fileEmitter(
pluginOptions.fileReplacements?.[args.path] ?? args.path,
);
const typescriptResult = await fileEmitter(request);
if (!typescriptResult) {
// No TS result indicates the file is not part of the TypeScript program.
// If allowJs is enabled and the file is JS then defer to the next load hook.
if (compilerOptions.allowJs && /\.[cm]?js$/.test(args.path)) {
if (compilerOptions.allowJs && /\.[cm]?js$/.test(request)) {
return undefined;
}

// Otherwise return an error
return {
errors: [
{
text: `File '${args.path}' is missing from the TypeScript compilation.`,
notes: [
{
text: `Ensure the file is part of the TypeScript program via the 'files' or 'include' property.`,
},
],
},
createMissingFileError(
request,
args.path,
build.initialOptions.absWorkingDir ?? '',
),
],
};
}
Expand All @@ -494,13 +491,13 @@ export function createCompilerPlugin(
// would need to be added to the key as well.
contents = babelDataCache.get(data);
if (contents === undefined) {
const transformedData = await transformWithBabel(args.path, data, pluginOptions);
const transformedData = await transformWithBabel(request, data, pluginOptions);
contents = Buffer.from(transformedData, 'utf-8');
babelDataCache.set(data, contents);
}

pluginOptions.sourceFileCache?.typeScriptFileCache.set(
pathToFileURL(args.path).href,
pathToFileURL(request).href,
contents,
);
}
Expand Down Expand Up @@ -687,3 +684,22 @@ function findAffectedFiles(

return affectedFiles;
}

function createMissingFileError(request: string, original: string, root: string): PartialMessage {
const error = {
text: `File '${path.relative(root, request)}' is missing from the TypeScript compilation.`,
notes: [
{
text: `Ensure the file is part of the TypeScript program via the 'files' or 'include' property.`,
},
],
};

if (request !== original) {
error.notes.push({
text: `File is requested from a file replacement of '${path.relative(root, original)}'.`,
});
}

return error;
}