Skip to content

Commit 6c942a2

Browse files
committed
cleaner attempt to find the property and remove trailing comma if not last property
1 parent 70355b3 commit 6c942a2

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

packages/@ngtools/webpack/src/loader.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,44 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
9090
function _removeModuleId(refactor: TypeScriptFileRefactor) {
9191
const sourceFile = refactor.sourceFile;
9292

93-
// Find all object literals.
9493
refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
9594
// Get all their property assignments.
96-
.map(node => refactor.findAstNodes(node, ts.SyntaxKind.PropertyAssignment))
97-
// Flatten into a single array (from an array of array<property assignments>).
98-
.reduce((prev, curr) => curr ? prev.concat(curr) : prev, [])
99-
// Remove every property assignment that aren't 'loadChildren'.
100-
.filter((node: ts.PropertyAssignment) => {
101-
const key = _getContentOfKeyLiteral(sourceFile, node.name);
102-
if (!key) {
103-
// key is an expression, can't do anything.
104-
return false;
105-
}
106-
return key == 'moduleId';
107-
})
108-
.forEach((node: ts.PropertyAssignment) => {
109-
refactor.removeNode(node);
95+
.filter((node: ts.ObjectLiteralExpression) =>
96+
node.properties.some(prop => prop.name.getText() == 'moduleId'))
97+
.forEach((node: ts.ObjectLiteralExpression) => {
98+
let isLastProp: boolean = false;
99+
100+
let moduleIdProp = node.properties.filter((prop: ts.ObjectLiteralElement, idx) => {
101+
const isModuleId = prop.name.getText() == 'moduleId';
102+
if (isModuleId && idx == node.properties.length - 1) {
103+
isLastProp = true;
104+
}
105+
return isModuleId;
106+
})[0];
107+
108+
refactor.removeNode(moduleIdProp, isLastProp ? undefined : ',');
110109
});
110+
111+
// // Find all object literals.
112+
// refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
113+
// // Get all their property assignments.
114+
// .map((node: ts.ObjectLiteralExpression) => refactor.findAstNodes(node, ts.SyntaxKind.PropertyAssignment))
115+
// // Flatten into a single array (from an array of array<property assignments>).
116+
// .reduce((prev, curr) => curr ? prev.concat(curr) : prev, [])
117+
// // Remove every property assignment that aren't 'loadChildren'.
118+
// .filter((node: ts.PropertyAssignment) => {
119+
// const key = _getContentOfKeyLiteral(sourceFile, node.name);
120+
// if (!key) {
121+
// // key is an expression, can't do anything.
122+
// return false;
123+
// }
124+
// return key == 'moduleId';
125+
// })
126+
// .forEach((node: ts.PropertyAssignment) => {
127+
// console.log('');
128+
// console.log('removing node: ', node.getFullText());
129+
// refactor.removeNode(node);
130+
// });
111131
}
112132

113133
function _replaceResources(refactor: TypeScriptFileRefactor): void {

packages/@ngtools/webpack/src/refactor.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,14 @@ export class TypeScriptFileRefactor {
164164
}
165165
}
166166

167-
removeNode(node: ts.Node) {
168-
this._sourceString.remove(node.getStart(this._sourceFile), node.getEnd());
167+
removeNode(node: ts.Node, deleteThroughChar: string) {
168+
let end = node.getEnd();
169+
if (!!deleteThroughChar) {
170+
while(this._sourceText.charAt(end-1) != deleteThroughChar) {
171+
end++;
172+
}
173+
}
174+
this._sourceString.remove(node.getStart(this._sourceFile), end);
169175
this._changed = true;
170176
}
171177

0 commit comments

Comments
 (0)