|
1 | | -"use strict"; |
2 | | -Object.defineProperty(exports, "__esModule", { value: true }); |
3 | | -const IncludeDependency_1 = require("./IncludeDependency"); |
4 | | -const BasicEvaluatedExpression = require("webpack/lib/BasicEvaluatedExpression"); |
5 | | -class AureliaDependency extends IncludeDependency_1.IncludeDependency { |
6 | | - constructor(request, range, options) { |
7 | | - super(request, options); |
8 | | - this.range = range; |
9 | | - } |
10 | | -} |
11 | | -class Template { |
12 | | - apply(dep, source) { |
13 | | - source.replace(dep.range[0], dep.range[1] - 1, "'" + dep.request.replace(/^async(?:\?[^!]*)?!/, "") + "'"); |
14 | | - } |
15 | | - ; |
16 | | -} |
17 | | -class ParserPlugin { |
18 | | - constructor(methods) { |
19 | | - this.methods = methods; |
20 | | - } |
21 | | - apply(parser) { |
22 | | - function addDependency(module, range, options) { |
23 | | - let dep = new AureliaDependency(module, range, options); |
24 | | - parser.state.current.addDependency(dep); |
25 | | - return true; |
26 | | - } |
27 | | - // The parser will only apply "call PLATFORM.moduleName" on free variables. |
28 | | - // So we must first trick it into thinking PLATFORM.moduleName is an unbound identifier |
29 | | - // in the various situations where it is not. |
30 | | - // This covers native ES module, for example: |
31 | | - // import { PLATFORM } from "aurelia-pal"; |
32 | | - // PLATFORM.moduleName("id"); |
33 | | - parser.plugin("evaluate Identifier imported var.moduleName", (expr) => { |
34 | | - if (expr.property.name === "moduleName" && |
35 | | - expr.object.name === "PLATFORM" && |
36 | | - expr.object.type === "Identifier") { |
37 | | - return new BasicEvaluatedExpression().setIdentifier("PLATFORM.moduleName").setRange(expr.range); |
38 | | - } |
39 | | - return undefined; |
40 | | - }); |
41 | | - // This covers commonjs modules, for example: |
42 | | - // const _aureliaPal = require("aurelia-pal"); |
43 | | - // _aureliaPal.PLATFORM.moduleName("id"); |
44 | | - parser.plugin("evaluate MemberExpression", (expr) => { |
45 | | - if (expr.property.name === "moduleName" && |
46 | | - expr.object.type === "MemberExpression" && |
47 | | - expr.object.property.name === "PLATFORM") { |
48 | | - return new BasicEvaluatedExpression().setIdentifier("PLATFORM.moduleName").setRange(expr.range); |
49 | | - } |
50 | | - return undefined; |
51 | | - }); |
52 | | - for (let method of this.methods) { |
53 | | - parser.plugin("call " + method, (expr) => { |
54 | | - if (expr.arguments.length === 0 || expr.arguments.length > 2) |
55 | | - return; |
56 | | - let [arg1, arg2] = expr.arguments; |
57 | | - let param1 = parser.evaluateExpression(arg1); |
58 | | - if (!param1.isString()) |
59 | | - return; |
60 | | - if (expr.arguments.length === 1) { |
61 | | - // Normal module dependency |
62 | | - // PLATFORM.moduleName('some-module') |
63 | | - return addDependency(param1.string, expr.range); |
64 | | - } |
65 | | - let chunk; |
66 | | - let options; |
67 | | - let param2 = parser.evaluateExpression(arg2); |
68 | | - if (param2.isString()) { |
69 | | - // Async module dependency |
70 | | - // PLATFORM.moduleName('some-module', 'chunk name'); |
71 | | - chunk = param2.string; |
72 | | - } |
73 | | - else if (arg2.type === "ObjectExpression") { |
74 | | - // Module dependency with extended options |
75 | | - // PLATFORM.moduleName('some-module', { option: value }); |
76 | | - options = {}; |
77 | | - for (let prop of arg2.properties) { |
78 | | - if (prop.key.type !== "Identifier") |
79 | | - continue; |
80 | | - let value = parser.evaluateExpression(prop.value); |
81 | | - switch (prop.key.name) { |
82 | | - case "chunk": |
83 | | - if (value.isString()) |
84 | | - chunk = value.string; |
85 | | - break; |
86 | | - case "exports": |
87 | | - if (value.isArray() && value.items.every(v => v.isString())) |
88 | | - options.exports = value.items.map(v => v.string); |
89 | | - break; |
90 | | - } |
91 | | - } |
92 | | - } |
93 | | - else { |
94 | | - // Unknown PLATFORM.moduleName() signature |
95 | | - return; |
96 | | - } |
97 | | - return addDependency(chunk ? `async?lazy&name=${chunk}!${param1.string}` : param1.string, expr.range, options); |
98 | | - }); |
99 | | - } |
100 | | - } |
101 | | -} |
102 | | -class AureliaDependenciesPlugin { |
103 | | - constructor(...methods) { |
104 | | - // Always include PLATFORM.moduleName as it's what used in libs. |
105 | | - if (!methods.includes("PLATFORM.moduleName")) |
106 | | - methods.push("PLATFORM.moduleName"); |
107 | | - this.parserPlugin = new ParserPlugin(methods); |
108 | | - } |
109 | | - apply(compiler) { |
110 | | - compiler.plugin("compilation", (compilation, params) => { |
111 | | - const normalModuleFactory = params.normalModuleFactory; |
112 | | - compilation.dependencyFactories.set(AureliaDependency, normalModuleFactory); |
113 | | - compilation.dependencyTemplates.set(AureliaDependency, new Template()); |
114 | | - normalModuleFactory.plugin("parser", parser => { |
115 | | - parser.apply(this.parserPlugin); |
116 | | - }); |
117 | | - }); |
118 | | - } |
119 | | -} |
120 | | -exports.AureliaDependenciesPlugin = AureliaDependenciesPlugin; |
121 | | -; |
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const IncludeDependency_1 = require("./IncludeDependency"); |
| 4 | +const BasicEvaluatedExpression = require("webpack/lib/BasicEvaluatedExpression"); |
| 5 | +class AureliaDependency extends IncludeDependency_1.IncludeDependency { |
| 6 | + constructor(request, range, options) { |
| 7 | + super(request, options); |
| 8 | + this.range = range; |
| 9 | + } |
| 10 | +} |
| 11 | +class Template { |
| 12 | + apply(dep, source) { |
| 13 | + source.replace(dep.range[0], dep.range[1] - 1, "'" + dep.request.replace(/^async(?:\?[^!]*)?!/, "") + "'"); |
| 14 | + } |
| 15 | + ; |
| 16 | +} |
| 17 | +class ParserPlugin { |
| 18 | + constructor(methods) { |
| 19 | + this.methods = methods; |
| 20 | + } |
| 21 | + apply(parser) { |
| 22 | + function addDependency(module, range, options) { |
| 23 | + let dep = new AureliaDependency(module, range, options); |
| 24 | + parser.state.current.addDependency(dep); |
| 25 | + return true; |
| 26 | + } |
| 27 | + // The parser will only apply "call PLATFORM.moduleName" on free variables. |
| 28 | + // So we must first trick it into thinking PLATFORM.moduleName is an unbound identifier |
| 29 | + // in the various situations where it is not. |
| 30 | + // This covers native ES module, for example: |
| 31 | + // import { PLATFORM } from "aurelia-pal"; |
| 32 | + // PLATFORM.moduleName("id"); |
| 33 | + parser.plugin("evaluate Identifier imported var.moduleName", (expr) => { |
| 34 | + if (expr.property.name === "moduleName" && |
| 35 | + expr.object.name === "PLATFORM" && |
| 36 | + expr.object.type === "Identifier") { |
| 37 | + return new BasicEvaluatedExpression().setIdentifier("PLATFORM.moduleName").setRange(expr.range); |
| 38 | + } |
| 39 | + return undefined; |
| 40 | + }); |
| 41 | + // This covers commonjs modules, for example: |
| 42 | + // const _aureliaPal = require("aurelia-pal"); |
| 43 | + // _aureliaPal.PLATFORM.moduleName("id"); |
| 44 | + parser.plugin("evaluate MemberExpression", (expr) => { |
| 45 | + if (expr.property.name === "moduleName" && |
| 46 | + expr.object.type === "MemberExpression" && |
| 47 | + expr.object.property.name === "PLATFORM") { |
| 48 | + return new BasicEvaluatedExpression().setIdentifier("PLATFORM.moduleName").setRange(expr.range); |
| 49 | + } |
| 50 | + return undefined; |
| 51 | + }); |
| 52 | + for (let method of this.methods) { |
| 53 | + parser.plugin("call " + method, (expr) => { |
| 54 | + if (expr.arguments.length === 0 || expr.arguments.length > 2) |
| 55 | + return; |
| 56 | + let [arg1, arg2] = expr.arguments; |
| 57 | + let param1 = parser.evaluateExpression(arg1); |
| 58 | + if (!param1.isString()) |
| 59 | + return; |
| 60 | + if (expr.arguments.length === 1) { |
| 61 | + // Normal module dependency |
| 62 | + // PLATFORM.moduleName('some-module') |
| 63 | + return addDependency(param1.string, expr.range); |
| 64 | + } |
| 65 | + let chunk; |
| 66 | + let options; |
| 67 | + let param2 = parser.evaluateExpression(arg2); |
| 68 | + if (param2.isString()) { |
| 69 | + // Async module dependency |
| 70 | + // PLATFORM.moduleName('some-module', 'chunk name'); |
| 71 | + chunk = param2.string; |
| 72 | + } |
| 73 | + else if (arg2.type === "ObjectExpression") { |
| 74 | + // Module dependency with extended options |
| 75 | + // PLATFORM.moduleName('some-module', { option: value }); |
| 76 | + options = {}; |
| 77 | + for (let prop of arg2.properties) { |
| 78 | + if (prop.key.type !== "Identifier") |
| 79 | + continue; |
| 80 | + let value = parser.evaluateExpression(prop.value); |
| 81 | + switch (prop.key.name) { |
| 82 | + case "chunk": |
| 83 | + if (value.isString()) |
| 84 | + chunk = value.string; |
| 85 | + break; |
| 86 | + case "exports": |
| 87 | + if (value.isArray() && value.items.every(v => v.isString())) |
| 88 | + options.exports = value.items.map(v => v.string); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + else { |
| 94 | + // Unknown PLATFORM.moduleName() signature |
| 95 | + return; |
| 96 | + } |
| 97 | + return addDependency(chunk ? `async?lazy&name=${chunk}!${param1.string}` : param1.string, expr.range, options); |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | +class AureliaDependenciesPlugin { |
| 103 | + constructor(...methods) { |
| 104 | + // Always include PLATFORM.moduleName as it's what used in libs. |
| 105 | + if (!methods.includes("PLATFORM.moduleName")) |
| 106 | + methods.push("PLATFORM.moduleName"); |
| 107 | + this.parserPlugin = new ParserPlugin(methods); |
| 108 | + } |
| 109 | + apply(compiler) { |
| 110 | + compiler.plugin("compilation", (compilation, params) => { |
| 111 | + const normalModuleFactory = params.normalModuleFactory; |
| 112 | + compilation.dependencyFactories.set(AureliaDependency, normalModuleFactory); |
| 113 | + compilation.dependencyTemplates.set(AureliaDependency, new Template()); |
| 114 | + normalModuleFactory.plugin("parser", parser => { |
| 115 | + parser.apply(this.parserPlugin); |
| 116 | + }); |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
| 120 | +exports.AureliaDependenciesPlugin = AureliaDependenciesPlugin; |
| 121 | +; |
0 commit comments