Skip to content

Commit

Permalink
feat(@ngtools/webpack): convert dashless resource urls (#3842)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva authored and hansl committed Jan 4, 2017
1 parent 1555c2b commit 4e7b397
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/@ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ function _removeModuleId(refactor: TypeScriptFileRefactor) {
});
}

function _getResourceRequest(element: ts.Expression, sourceFile: ts.SourceFile) {
if (element.kind == ts.SyntaxKind.StringLiteral) {
// if string, assume relative path unless it start with /
return `'${loaderUtils.urlToRequest((element as ts.StringLiteral).text, '')}'`;
} else {
// if not string, just use expression directly
return element.getFullText(sourceFile);
}
}

function _replaceResources(refactor: TypeScriptFileRefactor): void {
const sourceFile = refactor.sourceFile;

Expand All @@ -132,7 +142,7 @@ function _replaceResources(refactor: TypeScriptFileRefactor): void {

if (key == 'templateUrl') {
refactor.replaceNode(node,
`template: require(${node.initializer.getFullText(sourceFile)})`);
`template: require(${_getResourceRequest(node.initializer, sourceFile)})`);
} else if (key == 'styleUrls') {
const arr = <ts.ArrayLiteralExpression[]>(
refactor.findAstNodes(node, ts.SyntaxKind.ArrayLiteralExpression, false));
Expand All @@ -141,7 +151,7 @@ function _replaceResources(refactor: TypeScriptFileRefactor): void {
}

const initializer = arr[0].elements.map((element: ts.Expression) => {
return element.getFullText(sourceFile);
return _getResourceRequest(element, sourceFile);
});
refactor.replaceNode(node, `styles: [require(${initializer.join('), require(')})]`);
}
Expand Down
8 changes: 7 additions & 1 deletion tests/e2e/tests/packages/webpack/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {normalize} from 'path';
import {createProjectFromAsset} from '../../../utils/assets';
import {exec} from '../../../utils/process';
import {expectFileSizeToBeUnder} from '../../../utils/fs';
import {expectFileSizeToBeUnder, replaceInFile} from '../../../utils/fs';


export default function(skipCleaning: () => void) {
Expand All @@ -10,5 +10,11 @@ export default function(skipCleaning: () => void) {
.then(() => exec(normalize('node_modules/.bin/webpack'), '-p'))
.then(() => expectFileSizeToBeUnder('dist/app.main.js', 420000))
.then(() => expectFileSizeToBeUnder('dist/0.app.main.js', 40000))
// test resource urls without ./
.then(() => replaceInFile('app/app.component.ts',
'./app.component.html', 'app.component.html'))
.then(() => replaceInFile('app/app.component.ts',
'./app.component.scss', 'app.component.scss'))
.then(() => exec(normalize('node_modules/.bin/webpack'), '-p'))
.then(() => skipCleaning());
}

0 comments on commit 4e7b397

Please sign in to comment.