mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-18 20:02:40 +08:00
In the CLI `UpdateRecorder` methods such as `insertLeft`, `remove` etc.. accepts positions which are not offset by a BOM. This is because when a file has a BOM a different recorder will be used https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/schematics/src/tree/recorder.ts#L72 which caters for an addition offset/delta. The main reason for this is that when a developer is writing a schematic they shouldn't need to compute the offset based if a file has a BOM or not and is handled out of the box. Example ```ts recorder.insertLeft(5, 'true'); ``` However this is unfortunate in the case if a ts SourceFile is used and one uses `getWidth` and `getStart` method they will already be offset by 1, which at the end it results in a double offset and hence the problem. Fixes #14551
77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. 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.io/license
|
|
*/
|
|
import { DirEntry, Rule, UpdateRecorder } from '@angular-devkit/schematics';
|
|
import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
|
|
|
|
function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> {
|
|
for (const path of directory.subfiles) {
|
|
if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
|
|
const entry = directory.file(path);
|
|
if (entry) {
|
|
const content = entry.content;
|
|
if (content.includes('loadChildren')) {
|
|
const source = ts.createSourceFile(
|
|
entry.path,
|
|
content.toString().replace(/^\uFEFF/, ''),
|
|
ts.ScriptTarget.Latest,
|
|
true,
|
|
);
|
|
|
|
yield source;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const path of directory.subdirs) {
|
|
if (path === 'node_modules') {
|
|
continue;
|
|
}
|
|
|
|
yield* visit(directory.dir(path));
|
|
}
|
|
}
|
|
|
|
export function updateLazyModulePaths(): Rule {
|
|
return tree => {
|
|
for (const sourceFile of visit(tree.root)) {
|
|
let recorder: UpdateRecorder | undefined;
|
|
|
|
ts.forEachChild(sourceFile, function analyze(node) {
|
|
if (ts.isPropertyAssignment(node) &&
|
|
(ts.isIdentifier(node.name) || ts.isStringLiteral(node.name)) &&
|
|
node.name.text === 'loadChildren' &&
|
|
ts.isStringLiteral(node.initializer)) {
|
|
const valueNode = node.initializer;
|
|
const parts = valueNode.text.split('#');
|
|
const path = parts[0];
|
|
const moduleName = parts[1] || 'default';
|
|
|
|
const fix = `() => import('${path}').then(m => m.${moduleName})`;
|
|
|
|
if (!recorder) {
|
|
recorder = tree.beginUpdate(sourceFile.fileName);
|
|
}
|
|
|
|
const index = valueNode.getStart();
|
|
const length = valueNode.getWidth();
|
|
recorder
|
|
.remove(index, length)
|
|
.insertLeft(index, fix);
|
|
}
|
|
|
|
ts.forEachChild(node, analyze);
|
|
});
|
|
|
|
if (recorder) {
|
|
tree.commitUpdate(recorder);
|
|
}
|
|
}
|
|
};
|
|
}
|