Skip to content

Commit

Permalink
bug(build): Remove moduleId from components when building (#3664)
Browse files Browse the repository at this point in the history
Fixes #3576
  • Loading branch information
Brocco authored Dec 22, 2016
1 parent 142c518 commit 50d73d2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/@ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(() => {
Expand Down
4 changes: 4 additions & 0 deletions packages/@ngtools/webpack/src/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
33 changes: 33 additions & 0 deletions tests/acceptance/ngtools-webpack-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 50d73d2

Please sign in to comment.