Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { SharedTSCompilationState, getSharedCompilationState } from './compilati
import { ComponentStylesheetBundler } from './component-stylesheets';
import { FileReferenceTracker } from './file-reference-tracker';
import { setupJitPluginCallbacks } from './jit-plugin-callbacks';
import { rewriteForBazel } from './rewrite-bazel-paths';
import { SourceFileCache } from './source-file-cache';

export interface CompilerPluginOptions {
Expand Down Expand Up @@ -411,8 +412,8 @@ export function createCompilerPlugin(
});

build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async (args) => {
const request = path.normalize(
pluginOptions.fileReplacements?.[path.normalize(args.path)] ?? args.path,
const request = rewriteForBazel(
path.normalize(pluginOptions.fileReplacements?.[path.normalize(args.path)] ?? args.path),
);
const isJS = /\.[cm]?js$/.test(request);

Expand Down Expand Up @@ -478,13 +479,14 @@ export function createCompilerPlugin(
return {
contents,
loader,
resolveDir: path.dirname(request),
};
});

build.onLoad(
{ filter: /\.[cm]?js$/ },
createCachedLoad(pluginOptions.loadResultCache, async (args) => {
let request = args.path;
let request = rewriteForBazel(args.path);
if (pluginOptions.fileReplacements) {
const replacement = pluginOptions.fileReplacements[path.normalize(args.path)];
if (replacement) {
Expand All @@ -505,6 +507,7 @@ export function createCompilerPlugin(
return {
contents,
loader: 'js',
resolveDir: path.dirname(request),
watchFiles: request !== args.path ? [request] : undefined,
};
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { join, relative } from 'node:path';

const bazelBinDirectory = process.env['BAZEL_BINDIR'];
const bazelExecRoot = process.env['JS_BINARY__EXECROOT'];

export function rewriteForBazel(path: string): string {
if (!bazelBinDirectory || !bazelExecRoot) {
return path;
}

const fromExecRoot = relative(bazelExecRoot, path);
if (!fromExecRoot.startsWith('..')) {
return path;
}

const fromBinDirectory = relative(bazelBinDirectory, path);
if (fromBinDirectory.startsWith('..')) {
return path;
}

return join(bazelExecRoot, fromBinDirectory);
}