Skip to content

Commit 9867c95

Browse files
filipesilvaKeen Yee Liau
authored andcommitted
refactor(@ngtools/webpack): add forwardSlashPath util
I figured one day I'd get tired of repeating this replace. Today was that day.
1 parent 8bf7fbe commit 9867c95

File tree

8 files changed

+21
-10
lines changed

8 files changed

+21
-10
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import {
6969
MESSAGE_KIND,
7070
UpdateMessage,
7171
} from './type_checker_messages';
72-
import { flattenArray, workaroundResolve } from './utils';
72+
import { flattenArray, forwardSlashPath, workaroundResolve } from './utils';
7373
import {
7474
VirtualFileSystemDecorator,
7575
VirtualWatchFileSystemDecorator,
@@ -163,7 +163,7 @@ export class AngularCompilerPlugin {
163163
throw new Error('Must specify "tsConfigPath" in the configuration of @ngtools/webpack.');
164164
}
165165
// TS represents paths internally with '/' and expects the tsconfig path to be in this format
166-
this._tsConfigPath = options.tsConfigPath.replace(/\\/g, '/');
166+
this._tsConfigPath = forwardSlashPath(options.tsConfigPath);
167167

168168
// Check the base path.
169169
const maybeBasePath = path.resolve(process.cwd(), this._tsConfigPath);
@@ -482,7 +482,7 @@ export class AngularCompilerPlugin {
482482
return;
483483
}
484484

485-
const lazyRouteTSFile = discoveredLazyRoutes[lazyRouteKey].replace(/\\/g, '/');
485+
const lazyRouteTSFile = forwardSlashPath(discoveredLazyRoutes[lazyRouteKey]);
486486
let modulePath: string, moduleKey: string;
487487

488488
if (this._useFactories) {

packages/ngtools/webpack/src/lazy_routes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { dirname, join } from 'path';
99
import * as ts from 'typescript';
1010
import { findAstNodes, resolve } from './refactor';
11+
import { forwardSlashPath } from './utils';
1112

1213

1314
function _getContentOfKeyLiteral(_source: ts.SourceFile, node: ts.Node): string | null {
@@ -38,7 +39,7 @@ export function findLazyRoutes(
3839
}
3940
compilerOptions = program.getCompilerOptions();
4041
}
41-
const fileName = resolve(filePath, host, compilerOptions).replace(/\\/g, '/');
42+
const fileName = forwardSlashPath(resolve(filePath, host, compilerOptions));
4243
let sourceFile: ts.SourceFile | undefined;
4344
if (program) {
4445
sourceFile = program.getSourceFile(fileName);

packages/ngtools/webpack/src/refactor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import * as path from 'path';
99
import * as ts from 'typescript';
10+
import { forwardSlashPath } from './utils';
1011

1112

1213
/**
@@ -95,7 +96,7 @@ export class TypeScriptFileRefactor {
9596
let sourceFile: ts.SourceFile | null = null;
9697

9798
if (_program) {
98-
fileName = resolve(fileName, _host, _program.getCompilerOptions()).replace(/\\/g, '/');
99+
fileName = forwardSlashPath(resolve(fileName, _host, _program.getCompilerOptions()));
99100
this._fileName = fileName;
100101

101102
if (source) {

packages/ngtools/webpack/src/transformers/export_lazy_module_map.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import * as path from 'path';
99
import * as ts from 'typescript';
1010
import { LazyRouteMap } from '../lazy_routes';
11+
import { forwardSlashPath } from '../utils';
1112
import { getFirstNode, getLastNode } from './ast_helpers';
1213
import { AddNodeOperation, StandardTransform, TransformOperation } from './interfaces';
1314
import { makeTransform } from './make_transform';
@@ -46,7 +47,7 @@ export function exportLazyModuleMap(
4647
return;
4748
}
4849

49-
let relativePath = path.relative(dirName, modulePath).replace(/\\/g, '/');
50+
let relativePath = forwardSlashPath(path.relative(dirName, modulePath));
5051
if (!(relativePath.startsWith('./') || relativePath.startsWith('../'))) {
5152
// 'a/b/c' is a relative path for Node but an absolute path for TS, so we must convert it.
5253
relativePath = `./${relativePath}`;

packages/ngtools/webpack/src/transformers/export_ngfactory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { dirname, relative } from 'path';
99
import * as ts from 'typescript';
10+
import { forwardSlashPath } from '../utils';
1011
import { collectDeepNodes, getFirstNode } from './ast_helpers';
1112
import { AddNodeOperation, StandardTransform, TransformOperation } from './interfaces';
1213
import { makeTransform } from './make_transform';
@@ -35,7 +36,7 @@ export function exportNgFactory(
3536
}
3637

3738
const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path);
38-
const normalizedEntryModulePath = `./${relativeEntryModulePath}`.replace(/\\/g, '/');
39+
const normalizedEntryModulePath = forwardSlashPath(`./${relativeEntryModulePath}`);
3940

4041
// Get the module path from the import.
4142
entryModuleIdentifiers.forEach((entryModuleIdentifier) => {

packages/ngtools/webpack/src/transformers/replace_bootstrap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { dirname, relative } from 'path';
99
import * as ts from 'typescript';
10+
import { forwardSlashPath } from '../utils';
1011
import { collectDeepNodes } from './ast_helpers';
1112
import { insertStarImport } from './insert_import';
1213
import { ReplaceNodeOperation, StandardTransform, TransformOperation } from './interfaces';
@@ -79,7 +80,7 @@ export function replaceBootstrap(
7980
// Add the transform operations.
8081
const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path);
8182
let className = entryModule.className;
82-
let modulePath = `./${relativeEntryModulePath}`.replace(/\\/g, '/');
83+
let modulePath = forwardSlashPath(`./${relativeEntryModulePath}`);
8384
let bootstrapIdentifier = 'bootstrapModule';
8485

8586
if (useFactories) {

packages/ngtools/webpack/src/transformers/replace_server_bootstrap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { dirname, relative } from 'path';
99
import * as ts from 'typescript';
10+
import { forwardSlashPath } from '../utils';
1011
import { collectDeepNodes } from './ast_helpers';
1112
import { insertStarImport } from './insert_import';
1213
import { ReplaceNodeOperation, StandardTransform, TransformOperation } from './interfaces';
@@ -37,7 +38,7 @@ export function replaceServerBootstrap(
3738
}
3839

3940
const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path);
40-
const normalizedEntryModulePath = `./${relativeEntryModulePath}`.replace(/\\/g, '/');
41+
const normalizedEntryModulePath = forwardSlashPath(`./${relativeEntryModulePath}`);
4142
const factoryClassName = entryModule.className + 'NgFactory';
4243
const factoryModulePath = normalizedEntryModulePath + '.ngfactory';
4344

packages/ngtools/webpack/src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ import { Path, getSystemPath, normalize } from '@angular-devkit/core';
1212
// To work around this we must provide the same path format as TS internally uses in
1313
// the SourceFile paths.
1414
export function workaroundResolve(path: Path | string) {
15-
return getSystemPath(normalize(path)).replace(/\\/g, '/');
15+
return forwardSlashPath(getSystemPath(normalize(path)));
1616
}
1717

1818
export function flattenArray<T>(value: Array<T | T[]>): T[] {
1919
return [].concat.apply([], value);
2020
}
21+
22+
// TS represents paths internally with '/' and expects paths to be in this format.
23+
export function forwardSlashPath(path: string) {
24+
return path.replace(/\\/g, '/');
25+
}

0 commit comments

Comments
 (0)