|
| 1 | +const ts = require("typescript"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +const UrlResolvePlugin = (function() { |
| 6 | + function UrlResolvePlugin(options) { |
| 7 | + if (!options || !options.platform) { |
| 8 | + throw new Error(`Target platform must be specified!`); |
| 9 | + } |
| 10 | + |
| 11 | + this.platform = options.platform; |
| 12 | + |
| 13 | + // these are true by default |
| 14 | + this.resolveStylesUrls = options.resolveStylesUrls === undefined || options.resolveStylesUrls; |
| 15 | + this.resolveTemplateUrl = options.resolveTemplateUrl === undefined || options.resolveTemplateUrl; |
| 16 | + |
| 17 | + if (!this.resolveStylesUrls && !this.resolveTemplateUrl) { |
| 18 | + throw new Error(`resolveStylesUrls and resolveTemplateUrl mustn't both be false`); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + UrlResolvePlugin.prototype.apply = function (compiler) { |
| 23 | + compiler.plugin("make", (compilation, callback) => { |
| 24 | + const aotPlugin = getAotPlugin(compilation); |
| 25 | + aotPlugin._program.getSourceFiles() |
| 26 | + .forEach(sf => this.usePlatformUrl(sf)); |
| 27 | + |
| 28 | + callback(); |
| 29 | + }) |
| 30 | + }; |
| 31 | + |
| 32 | + function getAotPlugin(compilation) { |
| 33 | + let maybeAotPlugin = compilation._ngToolsWebpackPluginInstance; |
| 34 | + if (!maybeAotPlugin) { |
| 35 | + throw new Error(`This plugin must be used with the AotPlugin!`); |
| 36 | + } |
| 37 | + |
| 38 | + return maybeAotPlugin; |
| 39 | + } |
| 40 | + |
| 41 | + UrlResolvePlugin.prototype.usePlatformUrl = function(sourceFile) { |
| 42 | + this.setCurrentDirectory(sourceFile); |
| 43 | + ts.forEachChild(sourceFile, node => this.traverseDecorators(node)); |
| 44 | + } |
| 45 | + |
| 46 | + UrlResolvePlugin.prototype.setCurrentDirectory = function(sourceFile) { |
| 47 | + this.currentDirectory = path.resolve(sourceFile.path, ".."); |
| 48 | + } |
| 49 | + |
| 50 | + UrlResolvePlugin.prototype.traverseDecorators = function(node) { |
| 51 | + if (node.kind !== ts.SyntaxKind.ClassDeclaration || !node.decorators) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + node.decorators.forEach(decorator => { |
| 56 | + this.traverseDecoratorArguments(decorator.expression.arguments); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + UrlResolvePlugin.prototype.traverseDecoratorArguments = function(args) { |
| 61 | + args.forEach(arg => arg.properties && this.traverseProperties(arg.properties)); |
| 62 | + } |
| 63 | + |
| 64 | + UrlResolvePlugin.prototype.traverseProperties = function(properties) { |
| 65 | + properties |
| 66 | + .filter(prop => this.isRelevantNode(prop)) |
| 67 | + .forEach(prop => this.traversePropertyElements(prop)); |
| 68 | + } |
| 69 | + |
| 70 | + UrlResolvePlugin.prototype.isRelevantNode = function(property) { |
| 71 | + return this.resolveStylesUrls && property.name.text === "styleUrls" || |
| 72 | + this.resolveTemplateUrl && property.name.text === "templateUrl" |
| 73 | + } |
| 74 | + |
| 75 | + UrlResolvePlugin.prototype.traversePropertyElements = function(property) { |
| 76 | + const elements = property.initializer.elements === undefined ? [property.initializer] : property.initializer.elements; |
| 77 | + |
| 78 | + elements |
| 79 | + .filter(el => !!el.text) |
| 80 | + .filter(el => this.notPlatformUrl(el.text)) |
| 81 | + .filter(el => this.noMultiplatformFile(el.text)) |
| 82 | + .forEach(el => this.replaceUrlsValue(el)); |
| 83 | + } |
| 84 | + |
| 85 | + UrlResolvePlugin.prototype.notPlatformUrl = function(url) { |
| 86 | + let extensionStartIndex = url.lastIndexOf("."); |
| 87 | + let extension = url.slice(extensionStartIndex); |
| 88 | + |
| 89 | + return !url.endsWith(`.${this.platform}${extension}`); |
| 90 | + } |
| 91 | + |
| 92 | + UrlResolvePlugin.prototype.noMultiplatformFile = function(url) { |
| 93 | + let filePath = path.resolve(this.currentDirectory, url); |
| 94 | + |
| 95 | + return !fs.existsSync(filePath); |
| 96 | + } |
| 97 | + |
| 98 | + UrlResolvePlugin.prototype.replaceUrlsValue = function(element) { |
| 99 | + const extensionStartIndex = element.text.lastIndexOf("."); |
| 100 | + const prefix = element.text.slice(0, extensionStartIndex); |
| 101 | + const currentExtension = element.text.slice(extensionStartIndex); |
| 102 | + |
| 103 | + element.text = `${prefix}.${this.platform}${currentExtension}`; |
| 104 | + } |
| 105 | + |
| 106 | + return UrlResolvePlugin; |
| 107 | +})(); |
| 108 | + |
| 109 | +module.exports = UrlResolvePlugin; |
0 commit comments