From 50d73d29080f86ad66c6d4eb059c0dda21d46857 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Thu, 22 Dec 2016 15:30:16 -0500 Subject: [PATCH] bug(build): Remove moduleId from components when building (#3664) Fixes #3576 --- packages/@ngtools/webpack/src/loader.ts | 25 +++++++++++++- packages/@ngtools/webpack/src/refactor.ts | 4 +++ .../acceptance/ngtools-webpack-loader.spec.ts | 33 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/acceptance/ngtools-webpack-loader.spec.ts diff --git a/packages/@ngtools/webpack/src/loader.ts b/packages/@ngtools/webpack/src/loader.ts index b176e558202c..8f8b84957dff 100644 --- a/packages/@ngtools/webpack/src/loader.ts +++ b/packages/@ngtools/webpack/src/loader.ts @@ -87,6 +87,27 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor) refactor.insertImport(entryModule.className + 'NgFactory', ngFactoryPath); } +export function removeModuleIdOnlyForTesting(refactor: TypeScriptFileRefactor) { + _removeModuleId(refactor); +} + +function _removeModuleId(refactor: TypeScriptFileRefactor) { + const sourceFile = refactor.sourceFile; + + refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true) + // Get all their property assignments. + .filter((node: ts.ObjectLiteralExpression) => + node.properties.some(prop => prop.name.getText() == 'moduleId')) + .forEach((node: ts.ObjectLiteralExpression) => { + const moduleIdProp = node.properties.filter((prop: ts.ObjectLiteralElement, idx: number) => { + return prop.name.getText() == 'moduleId'; + })[0]; + // get the trailing comma + const moduleIdCommaProp = moduleIdProp.parent.getChildAt(1).getChildren()[1]; + refactor.removeNodes(moduleIdProp, moduleIdCommaProp); + }); +} + function _replaceResources(refactor: TypeScriptFileRefactor): void { const sourceFile = refactor.sourceFile; @@ -163,7 +184,9 @@ export function ngcLoader(source: string) { .then(() => _removeDecorators(refactor)) .then(() => _replaceBootstrap(plugin, refactor)); } else { - return _replaceResources(refactor); + return Promise.resolve() + .then(() => _replaceResources(refactor)) + .then(() => _removeModuleId(refactor)); } }) .then(() => { diff --git a/packages/@ngtools/webpack/src/refactor.ts b/packages/@ngtools/webpack/src/refactor.ts index 3ad8fc76d922..276c807a7fdd 100644 --- a/packages/@ngtools/webpack/src/refactor.ts +++ b/packages/@ngtools/webpack/src/refactor.ts @@ -169,6 +169,10 @@ export class TypeScriptFileRefactor { this._changed = true; } + removeNodes(...nodes: ts.Node[]) { + nodes.forEach(node => this.removeNode(node)); + } + replaceNode(node: ts.Node, replacement: string) { let replaceSymbolName: boolean = node.kind === ts.SyntaxKind.Identifier; this._sourceString.overwrite(node.getStart(this._sourceFile), diff --git a/tests/acceptance/ngtools-webpack-loader.spec.ts b/tests/acceptance/ngtools-webpack-loader.spec.ts new file mode 100644 index 000000000000..51381c6c19cb --- /dev/null +++ b/tests/acceptance/ngtools-webpack-loader.spec.ts @@ -0,0 +1,33 @@ +'use strict'; + +// This needs to be first so fs module can be mocked correctly. +import { expect } from 'chai'; + +import { removeModuleIdOnlyForTesting } from '../../packages/@ngtools/webpack/src/loader'; + +describe('@ngtools webpack loader: ', () => { + describe('removeModuleId', () => { + let refactor: any; + let moduleIdProp: any; + let commaProp: any; + let removeNodesArgs: any; + beforeEach(() => { + commaProp = { isCommaProp: true }; + moduleIdProp = { + name: { getText: () => 'moduleId' }, + parent: { getChildAt: () => ({ getChildren: (): any => [{}, commaProp] }) } + }; + refactor = { + sourceFile: 'sourceFile', + findAstNodes: (): any => [{ properties: [moduleIdProp] }], + removeNodes: (...args: any[]) => { removeNodesArgs = args; } + }; + }); + + it('should remove "moduleId: module.id"', () => { + removeModuleIdOnlyForTesting(refactor); + expect(removeNodesArgs[0]).to.equal(moduleIdProp); + expect(removeNodesArgs[1]).to.equal(commaProp); + }); + }); +});