Skip to content

Commit 4e7b397

Browse files
filipesilvahansl
authored andcommitted
feat(@ngtools/webpack): convert dashless resource urls (angular#3842)
1 parent 1555c2b commit 4e7b397

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ function _removeModuleId(refactor: TypeScriptFileRefactor) {
108108
});
109109
}
110110

111+
function _getResourceRequest(element: ts.Expression, sourceFile: ts.SourceFile) {
112+
if (element.kind == ts.SyntaxKind.StringLiteral) {
113+
// if string, assume relative path unless it start with /
114+
return `'${loaderUtils.urlToRequest((element as ts.StringLiteral).text, '')}'`;
115+
} else {
116+
// if not string, just use expression directly
117+
return element.getFullText(sourceFile);
118+
}
119+
}
120+
111121
function _replaceResources(refactor: TypeScriptFileRefactor): void {
112122
const sourceFile = refactor.sourceFile;
113123

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

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

143153
const initializer = arr[0].elements.map((element: ts.Expression) => {
144-
return element.getFullText(sourceFile);
154+
return _getResourceRequest(element, sourceFile);
145155
});
146156
refactor.replaceNode(node, `styles: [require(${initializer.join('), require(')})]`);
147157
}
+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {normalize} from 'path';
22
import {createProjectFromAsset} from '../../../utils/assets';
33
import {exec} from '../../../utils/process';
4-
import {expectFileSizeToBeUnder} from '../../../utils/fs';
4+
import {expectFileSizeToBeUnder, replaceInFile} from '../../../utils/fs';
55

66

77
export default function(skipCleaning: () => void) {
@@ -10,5 +10,11 @@ export default function(skipCleaning: () => void) {
1010
.then(() => exec(normalize('node_modules/.bin/webpack'), '-p'))
1111
.then(() => expectFileSizeToBeUnder('dist/app.main.js', 420000))
1212
.then(() => expectFileSizeToBeUnder('dist/0.app.main.js', 40000))
13+
// test resource urls without ./
14+
.then(() => replaceInFile('app/app.component.ts',
15+
'./app.component.html', 'app.component.html'))
16+
.then(() => replaceInFile('app/app.component.ts',
17+
'./app.component.scss', 'app.component.scss'))
18+
.then(() => exec(normalize('node_modules/.bin/webpack'), '-p'))
1319
.then(() => skipCleaning());
1420
}

0 commit comments

Comments
 (0)