1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.AureliaDependenciesPlugin = void 0;
|
4 | const IncludeDependency_1 = require("./IncludeDependency");
|
5 | const webpack = require("webpack");
|
6 | const PreserveExportsPlugin_1 = require("./PreserveExportsPlugin");
|
7 | const BasicEvaluatedExpression = require("webpack/lib/javascript/BasicEvaluatedExpression");
|
8 | const TAP_NAME = "Aurelia:Dependencies";
|
9 | class AureliaDependency extends IncludeDependency_1.IncludeDependency {
|
10 | constructor(request, range, options) {
|
11 | super(request, options);
|
12 | this.range = range;
|
13 | }
|
14 | get type() {
|
15 | return `${super.type}/AureliaDependency`;
|
16 | }
|
17 | get [PreserveExportsPlugin_1.dependencyImports]() {
|
18 | return webpack.Dependency.EXPORTS_OBJECT_REFERENCED;
|
19 | }
|
20 | }
|
21 | class Template {
|
22 | apply(dep, source) {
|
23 | source.replace(dep.range[0], dep.range[1] - 1, "'" + dep.request.replace(/^async(?:\?[^!]*)?!/, "") + "'");
|
24 | }
|
25 | ;
|
26 | }
|
27 | class ParserPlugin {
|
28 | constructor(methods) {
|
29 | this.methods = methods;
|
30 | }
|
31 | apply(parser) {
|
32 | function addDependency(module, range, options) {
|
33 | let dep = new AureliaDependency(module, range, options);
|
34 | parser.state.current.addDependency(dep);
|
35 | return true;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 | const hooks = parser.hooks;
|
41 | hooks.evaluate.for('MemberExpression').tap(TAP_NAME, (expr) => {
|
42 | if (expr.property.type === 'Identifier'
|
43 | && expr.property.name === 'moduleName'
|
44 |
|
45 | && (expr.object.type === 'Identifier' && expr.object.name === 'PLATFORM'
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | || expr.object.type === 'MemberExpression'
|
51 | && expr.object.property.type === 'Identifier'
|
52 | && expr.object.property.name === 'PLATFORM')) {
|
53 | return new BasicEvaluatedExpression()
|
54 | .setIdentifier('PLATFORM.moduleName', undefined, () => [])
|
55 | .setRange(expr.range);
|
56 | }
|
57 | return undefined;
|
58 | });
|
59 | for (let method of this.methods) {
|
60 | hooks.call.for(method).tap(TAP_NAME, (expr) => {
|
61 | if (expr.arguments.length === 0 || expr.arguments.length > 2) {
|
62 | return;
|
63 | }
|
64 | let [arg1, arg2] = expr.arguments;
|
65 | let param1 = parser.evaluateExpression(arg1);
|
66 | if (!param1.isString())
|
67 | return;
|
68 | if (expr.arguments.length === 1) {
|
69 |
|
70 |
|
71 | return addDependency(param1.string, expr.range);
|
72 | }
|
73 | let options;
|
74 | let param2 = parser.evaluateExpression(arg2);
|
75 | if (param2.isString()) {
|
76 |
|
77 |
|
78 | options = { chunk: param2.string };
|
79 | }
|
80 | else if (arg2.type === "ObjectExpression") {
|
81 |
|
82 |
|
83 | options = {};
|
84 | for (let prop of arg2.properties) {
|
85 | if (prop.type !== 'Property' || prop.key.type !== "Identifier")
|
86 | continue;
|
87 | let value = parser.evaluateExpression(prop.value);
|
88 | switch (prop.key.name) {
|
89 | case "chunk":
|
90 | if (value.isString())
|
91 | options.chunk = value.string;
|
92 | break;
|
93 | case "exports":
|
94 | if (value.isArray() && value.items.every(v => v.isString()))
|
95 | options.exports = value.items.map(v => v.string);
|
96 | break;
|
97 | }
|
98 | }
|
99 | }
|
100 | else {
|
101 |
|
102 | return;
|
103 | }
|
104 | return addDependency(param1.string, expr.range, options);
|
105 | });
|
106 | }
|
107 | }
|
108 | }
|
109 | class AureliaDependenciesPlugin {
|
110 | constructor(...methods) {
|
111 |
|
112 | if (!methods.includes("PLATFORM.moduleName")) {
|
113 | methods.push("PLATFORM.moduleName");
|
114 | }
|
115 | this.parserPlugin = new ParserPlugin(methods);
|
116 | }
|
117 | apply(compiler) {
|
118 | compiler.hooks.compilation.tap(TAP_NAME, (compilation, params) => {
|
119 | const normalModuleFactory = params.normalModuleFactory;
|
120 | compilation.dependencyFactories.set(AureliaDependency, normalModuleFactory);
|
121 | compilation.dependencyTemplates.set(AureliaDependency, new Template());
|
122 | const handler = (parser) => {
|
123 | this.parserPlugin.apply(parser);
|
124 | };
|
125 | normalModuleFactory.hooks.parser.for("javascript/dynamic").tap(TAP_NAME, handler);
|
126 | normalModuleFactory.hooks.parser.for("javascript/auto").tap(TAP_NAME, handler);
|
127 | normalModuleFactory.hooks.parser.for("javascript/esm").tap(TAP_NAME, handler);
|
128 | });
|
129 | }
|
130 | }
|
131 | exports.AureliaDependenciesPlugin = AureliaDependenciesPlugin;
|