|
1 |
| - |
2 | 1 | var loaderUtils = require("loader-utils");
|
| 2 | +var ts = require("typescript"); |
3 | 3 |
|
4 |
| -// using: regex, capture groups, and capture group variables. |
5 |
| -var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm; |
6 |
| -var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g; |
7 |
| -var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g; |
| 4 | +function calculateTemplateUrlReplacement(propertyAssignment, replacedPropertyName) { |
| 5 | + return [ |
| 6 | + { |
| 7 | + start: propertyAssignment.name.getStart(), |
| 8 | + end: propertyAssignment.name.getEnd(), |
| 9 | + replacement: replacedPropertyName |
| 10 | + }, |
| 11 | + constructUrlReplacement(propertyAssignment.initializer) |
| 12 | + ]; |
| 13 | +} |
8 | 14 |
|
9 |
| -function replaceStringsWithRequires(string) { |
10 |
| - return string.replace(stringRegex, function (match, quote, url) { |
11 |
| - if (url.charAt(0) !== ".") { |
12 |
| - url = "./" + url; |
| 15 | +function calculateStyleUrlsReplacement(propertyAssignment, replacedPropertyName) { |
| 16 | + var styleUrlsReplacements = []; |
| 17 | + for (var i = 0; i < propertyAssignment.initializer.elements.length; i++) { |
| 18 | + styleUrlsReplacements.push(constructUrlReplacement(propertyAssignment.initializer.elements[i])); |
| 19 | + } |
| 20 | + return [ |
| 21 | + { |
| 22 | + start: propertyAssignment.name.getStart(), |
| 23 | + end: propertyAssignment.name.getEnd(), |
| 24 | + replacement: replacedPropertyName |
13 | 25 | }
|
14 |
| - return "require('" + url + "')"; |
15 |
| - }); |
| 26 | + ].concat(styleUrlsReplacements); |
16 | 27 | }
|
17 | 28 |
|
18 |
| -module.exports = function(source, sourcemap) { |
| 29 | +function constructUrlReplacement(element) { |
| 30 | + var delimiter = "'"; |
| 31 | + if (element.kind === ts.SyntaxKind.FirstTemplateToken) { |
| 32 | + delimiter = "`"; |
| 33 | + } |
| 34 | + var url = element.text.replace(/(['"\\])/g, "\\$&"); |
| 35 | + if (url.charAt(0) !== ".") { |
| 36 | + url = "./" + url; |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + start: element.getStart(), |
| 41 | + end: element.getEnd(), |
| 42 | + replacement: "require(" + delimiter + url + delimiter + ")"; |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function isTemplateUrlProperty(propertyAssignment) { |
| 47 | + return ( |
| 48 | + propertyAssignment.name.kind === ts.SyntaxKind.Identifier && |
| 49 | + propertyAssignment.name.text === "templateUrl" && |
| 50 | + (propertyAssignment.initializer.kind === ts.SyntaxKind.StringLiteral || |
| 51 | + propertyAssignment.initializer.kind === ts.SyntaxKind.FirstTemplateToken) |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function isStyleUrlsPropery(propertyAssignment) { |
| 56 | + return ( |
| 57 | + propertyAssignment.name.kind === ts.SyntaxKind.Identifier && |
| 58 | + propertyAssignment.name.text === "styleUrls" && |
| 59 | + propertyAssignment.initializer.kind === ts.SyntaxKind.ArrayLiteralExpression && |
| 60 | + propertyAssignment.initializer.elements.every(function(element) { |
| 61 | + return ( |
| 62 | + element.kind === ts.SyntaxKind.StringLiteral || |
| 63 | + element.kind === ts.SyntaxKind.FirstTemplateToken |
| 64 | + ); |
| 65 | + }) |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +function flatten(prev, current) { |
| 70 | + return prev.concat(current); |
| 71 | +} |
19 | 72 |
|
| 73 | +function findeReplacements(node, templateReplacement, styleReplacement) { |
| 74 | + var positions = []; |
| 75 | + |
| 76 | + calculateReplacements(node); |
| 77 | + |
| 78 | + function calculateReplacements(node) { |
| 79 | + switch (node.kind) { |
| 80 | + // search only for class declarations |
| 81 | + case ts.SyntaxKind.ClassDeclaration: |
| 82 | + if (node.decorators && node.decorators.length > 0) { |
| 83 | + // filter for all decorators containing a component decorator |
| 84 | + var replacementPositions = node.decorators |
| 85 | + .filter(function(decorator) { |
| 86 | + return ( |
| 87 | + decorator.expression.kind === ts.SyntaxKind.CallExpression && |
| 88 | + decorator.expression.expression.kind === ts.SyntaxKind.Identifier && |
| 89 | + decorator.expression.expression.text === "Component" && |
| 90 | + decorator.expression.arguments.length > 0 |
| 91 | + ); |
| 92 | + }) |
| 93 | + .map(function(decorator) { |
| 94 | + return decorator.expression.arguments[0]; |
| 95 | + }) |
| 96 | + // the argument must be a literal expression |
| 97 | + .filter(function(argument) { |
| 98 | + return (argument.kind = ts.SyntaxKind.ObjectLiteralExpression); |
| 99 | + }) |
| 100 | + .map(function(literalExpression) { |
| 101 | + return literalExpression.properties; |
| 102 | + }) |
| 103 | + .reduce(flatten, []) |
| 104 | + // filter for property assignments |
| 105 | + .filter(function(properties) { |
| 106 | + return properties.kind === ts.SyntaxKind.PropertyAssignment; |
| 107 | + }) |
| 108 | + // only filter for property assignments with the text templateUrl or styleUrls |
| 109 | + .filter(function(propertyAssignment) { |
| 110 | + return ( |
| 111 | + isTemplateUrlProperty(propertyAssignment) || isStyleUrlsPropery(propertyAssignment) |
| 112 | + ); |
| 113 | + }) |
| 114 | + .map(function(propertyAssignment) { |
| 115 | + if (propertyAssignment.name.text === "templateUrl") { |
| 116 | + return calculateTemplateUrlReplacement(propertyAssignment, templateReplacement); |
| 117 | + } else { |
| 118 | + return calculateStyleUrlsReplacement(propertyAssignment, styleReplacement); |
| 119 | + } |
| 120 | + }) |
| 121 | + .reduce(flatten, []); |
| 122 | + positions = positions.concat(replacementPositions); |
| 123 | + } |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + return ts.forEachChild(node, calculateReplacements); |
| 128 | + } |
| 129 | + |
| 130 | + return positions; |
| 131 | +} |
| 132 | + |
| 133 | +module.exports = function(source, sourcemap) { |
20 | 134 | var config = {};
|
21 | 135 | var query = loaderUtils.parseQuery(this.query);
|
22 |
| - var styleProperty = 'styles'; |
23 |
| - var templateProperty = 'template'; |
| 136 | + var styleProperty = "styles"; |
| 137 | + var templateProperty = "template"; |
24 | 138 |
|
25 | 139 | if (this.options != null) {
|
26 |
| - Object.assign(config, this.options['angular2TemplateLoader']); |
| 140 | + Object.assign(config, this.options["angular2TemplateLoader"]); |
27 | 141 | }
|
28 | 142 |
|
29 | 143 | Object.assign(config, query);
|
30 | 144 |
|
31 | 145 | if (config.keepUrl === true) {
|
32 |
| - styleProperty = 'styleUrls'; |
33 |
| - templateProperty = 'templateUrl'; |
| 146 | + styleProperty = "styleUrls"; |
| 147 | + templateProperty = "templateUrl"; |
34 | 148 | }
|
35 | 149 |
|
36 |
| - // Not cacheable during unit tests; |
| 150 | + // Not cacheable during unit tests; |
37 | 151 | this.cacheable && this.cacheable();
|
38 | 152 |
|
39 |
| - var newSource = source.replace(templateUrlRegex, function (match, url) { |
40 |
| - // replace: templateUrl: './path/to/template.html' |
41 |
| - // with: template: require('./path/to/template.html') |
42 |
| - // or: templateUrl: require('./path/to/template.html') |
43 |
| - // if `keepUrl` query parameter is set to true. |
44 |
| - return templateProperty + ":" + replaceStringsWithRequires(url); |
45 |
| - }) |
46 |
| - .replace(stylesRegex, function (match, urls) { |
47 |
| - // replace: stylesUrl: ['./foo.css', "./baz.css", "./index.component.css"] |
48 |
| - // with: styles: [require('./foo.css'), require("./baz.css"), require("./index.component.css")] |
49 |
| - // or: styleUrls: [require('./foo.css'), require("./baz.css"), require("./index.component.css")] |
50 |
| - // if `keepUrl` query parameter is set to true. |
51 |
| - return styleProperty + ":" + replaceStringsWithRequires(urls); |
52 |
| - }); |
| 153 | + var fileName = this.resourcePath; |
| 154 | + |
| 155 | + var sourceFile = ts.createSourceFile( |
| 156 | + fileName, |
| 157 | + source, |
| 158 | + ts.ScriptTarget.ES6, |
| 159 | + /*setParentNodes */ true |
| 160 | + ); |
| 161 | + |
| 162 | + var positions = findeReplacements(sourceFile, templateProperty, styleProperty); |
| 163 | + |
| 164 | + var newSource = source; |
| 165 | + |
| 166 | + for (var i = positions.length - 1; i >= 0; i--) { |
| 167 | + var pos = positions[i]; |
| 168 | + var prefix = newSource.substring(0, pos.start); |
| 169 | + var postfix = newSource.substring(pos.end); |
| 170 | + newSource = prefix + pos.replacement + postfix; |
| 171 | + } |
53 | 172 |
|
54 | 173 | // Support for tests
|
55 | 174 | if (this.callback) {
|
56 |
| - this.callback(null, newSource, sourcemap) |
| 175 | + this.callback(null, newSource, sourcemap); |
57 | 176 | } else {
|
58 | 177 | return newSource;
|
59 | 178 | }
|
|
0 commit comments