Skip to content

Commit 199c12c

Browse files
committed
refactor(@angular/build): use the toPosixPath util to convert windows file
Use the shared util instead of duplicated the code.
1 parent cad1642 commit 199c12c

File tree

7 files changed

+19
-21
lines changed

7 files changed

+19
-21
lines changed

packages/angular/build/src/builders/application/build-action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { logMessages, withNoProgress, withSpinner } from '../../tools/esbuild/ut
1616
import { ChangedFiles } from '../../tools/esbuild/watcher';
1717
import { shouldWatchRoot } from '../../utils/environment-options';
1818
import { NormalizedCachedOptions } from '../../utils/normalize-cache';
19+
import { toPosixPath } from '../../utils/path';
1920
import { NormalizedApplicationBuildOptions, NormalizedOutputOptions } from './options';
2021
import {
2122
ComponentUpdateResult,
@@ -105,7 +106,7 @@ export async function* runEsBuildBuildAction(
105106
// Ignore the output and cache paths to avoid infinite rebuild cycles
106107
outputOptions.base,
107108
cacheOptions.basePath,
108-
`${workspaceRoot.replace(/\\/g, '/')}/**/.*/**`,
109+
`${toPosixPath(workspaceRoot)}/**/.*/**`,
109110
];
110111

111112
// Setup a watcher

packages/angular/build/src/builders/karma/find-tests.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { PathLike, constants, promises as fs } from 'node:fs';
1010
import { basename, dirname, extname, join, relative } from 'node:path';
1111
import { glob, isDynamicPattern } from 'tinyglobby';
12+
import { toPosixPath } from '../../utils/path';
1213

1314
/* Go through all patterns and find unique list of files */
1415
export async function findTests(
@@ -59,8 +60,6 @@ export function getTestEntrypoints(
5960
);
6061
}
6162

62-
const normalizePath = (path: string): string => path.replace(/\\/g, '/');
63-
6463
const removeLeadingSlash = (pattern: string): string => {
6564
if (pattern.charAt(0) === '/') {
6665
return pattern.substring(1);
@@ -94,10 +93,10 @@ async function findMatchingTests(
9493
projectSourceRoot: string,
9594
): Promise<string[]> {
9695
// normalize pattern, glob lib only accepts forward slashes
97-
let normalizedPattern = normalizePath(pattern);
96+
let normalizedPattern = toPosixPath(pattern);
9897
normalizedPattern = removeLeadingSlash(normalizedPattern);
9998

100-
const relativeProjectRoot = normalizePath(relative(workspaceRoot, projectSourceRoot) + '/');
99+
const relativeProjectRoot = toPosixPath(relative(workspaceRoot, projectSourceRoot) + '/');
101100

102101
// remove relativeProjectRoot to support relative paths from root
103102
// such paths are easy to get when running scripts via IDEs
@@ -125,7 +124,7 @@ async function findMatchingTests(
125124

126125
// normalize the patterns in the ignore list
127126
const normalizedIgnorePatternList = ignore.map((pattern: string) =>
128-
removeRelativeRoot(removeLeadingSlash(normalizePath(pattern)), relativeProjectRoot),
127+
removeRelativeRoot(removeLeadingSlash(toPosixPath(pattern)), relativeProjectRoot),
129128
);
130129

131130
return glob(normalizedPattern, {

packages/angular/build/src/tools/esbuild/application-code-bundle.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { extname, relative } from 'node:path';
1313
import type { NormalizedApplicationBuildOptions } from '../../builders/application/options';
1414
import { ExperimentalPlatform } from '../../builders/application/schema';
1515
import { allowMangle } from '../../utils/environment-options';
16+
import { toPosixPath } from '../../utils/path';
1617
import {
1718
SERVER_APP_ENGINE_MANIFEST_FILENAME,
1819
SERVER_APP_MANIFEST_FILENAME,
@@ -719,9 +720,7 @@ function getEsBuildCommonPolyfillsOptions(
719720
}
720721

721722
// Generate module contents with an import statement per defined polyfill
722-
let contents = polyfillPaths
723-
.map((file) => `import '${file.replace(/\\/g, '/')}';`)
724-
.join('\n');
723+
let contents = polyfillPaths.map((file) => `import '${toPosixPath(file)}';`).join('\n');
725724

726725
// The below should be done after loading `$localize` as otherwise the locale will be overridden.
727726
if (i18nOptions.shouldInline) {
@@ -746,10 +745,5 @@ function getEsBuildCommonPolyfillsOptions(
746745
}
747746

748747
function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string): string {
749-
return (
750-
'./' +
751-
relative(workspaceRoot, entryFile)
752-
.replace(/.[mc]?ts$/, '')
753-
.replace(/\\/g, '/')
754-
);
748+
return './' + toPosixPath(relative(workspaceRoot, entryFile).replace(/.[mc]?ts$/, ''));
755749
}

packages/angular/build/src/tools/esbuild/global-styles.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import assert from 'node:assert';
1010
import { NormalizedApplicationBuildOptions } from '../../builders/application/options';
11+
import { toPosixPath } from '../../utils/path';
1112
import { BundlerOptionsFactory } from './bundler-context';
1213
import { createStylesheetBundleOptions } from './stylesheets/bundle-options';
1314
import { createVirtualModulePlugin } from './virtual-module-plugin';
@@ -91,7 +92,7 @@ export function createGlobalStylesBundleOptions(
9192
assert(files, `global style name should always be found [${args.path}]`);
9293

9394
return {
94-
contents: files.map((file) => `@import '${file.replace(/\\/g, '/')}';`).join('\n'),
95+
contents: files.map((file) => `@import '${toPosixPath(file)}';`).join('\n'),
9596
loader: 'css',
9697
resolveDir: workspaceRoot,
9798
};

packages/angular/build/src/tools/sass/rebasing-importer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { basename, dirname, extname, join, relative } from 'node:path';
1313
import { fileURLToPath, pathToFileURL } from 'node:url';
1414
import type { CanonicalizeContext, Importer, ImporterResult, Syntax } from 'sass';
1515
import { assertIsError } from '../../utils/error';
16+
import { toPosixPath } from '../../utils/path';
1617
import { findUrls } from './lexer';
1718

1819
/**
@@ -83,7 +84,7 @@ abstract class UrlRebasingImporter implements Importer<'sync'> {
8384

8485
// Normalize path separators and escape characters
8586
// https://developer.mozilla.org/en-US/docs/Web/CSS/url#syntax
86-
const rebasedUrl = rebasedPath.replace(/\\/g, '/').replace(/[()\s'"]/g, '\\$&');
87+
const rebasedUrl = toPosixPath(rebasedPath).replace(/[()\s'"]/g, '\\$&');
8788

8889
updatedContents ??= new MagicString(contents);
8990
// Always quote the URL to avoid potential downstream parsing problems

packages/angular/build/src/utils/server-rendering/prerender.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { OutputMode } from '../../builders/application/schema';
1313
import { BuildOutputFile, BuildOutputFileType } from '../../tools/esbuild/bundler-context';
1414
import { BuildOutputAsset } from '../../tools/esbuild/bundler-execution-result';
1515
import { assertIsError } from '../error';
16+
import { toPosixPath } from '../path';
1617
import { urlJoin } from '../url';
1718
import { WorkerPool } from '../worker-pool';
1819
import { IMPORT_EXEC_ARGV } from './esm-in-memory-loader/utils';
@@ -94,7 +95,7 @@ export async function prerenderPages(
9495

9596
const assetsReversed: Record</** Destination */ string, /** Source */ string> = {};
9697
for (const { source, destination } of assets) {
97-
assetsReversed[addLeadingSlash(destination.replace(/\\/g, posix.sep))] = source;
98+
assetsReversed[addLeadingSlash(toPosixPath(destination))] = source;
9899
}
99100

100101
// Get routes to prerender

packages/angular/build/src/utils/service-worker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { BuildOutputFile, BuildOutputFileType } from '../tools/esbuild/bundler-c
1414
import { BuildOutputAsset } from '../tools/esbuild/bundler-execution-result';
1515
import { assertIsError } from './error';
1616
import { loadEsmModule } from './load-esm';
17+
import { toPosixPath } from './path';
1718

1819
class CliFilesystem implements Filesystem {
1920
constructor(
@@ -52,7 +53,7 @@ class CliFilesystem implements Filesystem {
5253

5354
if (stats.isFile()) {
5455
// Uses posix paths since the service worker expects URLs
55-
items.push('/' + path.relative(this.base, entryPath).replace(/\\/g, '/'));
56+
items.push('/' + toPosixPath(path.relative(this.base, entryPath)));
5657
} else if (stats.isDirectory()) {
5758
subdirectories.push(entryPath);
5859
}
@@ -75,11 +76,11 @@ class ResultFilesystem implements Filesystem {
7576
) {
7677
for (const file of outputFiles) {
7778
if (file.type === BuildOutputFileType.Media || file.type === BuildOutputFileType.Browser) {
78-
this.fileReaders.set('/' + file.path.replace(/\\/g, '/'), async () => file.contents);
79+
this.fileReaders.set('/' + toPosixPath(file.path), async () => file.contents);
7980
}
8081
}
8182
for (const file of assetFiles) {
82-
this.fileReaders.set('/' + file.destination.replace(/\\/g, '/'), () =>
83+
this.fileReaders.set('/' + toPosixPath(file.destination), () =>
8384
fsPromises.readFile(file.source),
8485
);
8586
}

0 commit comments

Comments
 (0)