UNPKG

2.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.InlineViewDependenciesPlugin = void 0;
4// This plugins tries to detect @inlineView('<template>...</template>') and process its dependencies
5// like HtmlDependenciesPlugin does.
6const BaseIncludePlugin_1 = require("./BaseIncludePlugin");
7const htmlLoader = require("./html-requires-loader");
8const BasicEvaluatedExpression = require("webpack/lib/javascript/BasicEvaluatedExpression");
9const TAP_NAME = "Aurelia:InlineViewDependencies";
10// const JavaScriptParser = webpack.javascript.JavascriptParser;
11class InlineViewDependenciesPlugin extends BaseIncludePlugin_1.BaseIncludePlugin {
12 parser(compilation, parser, add) {
13 // The parser will only apply "call inlineView" on free variables.
14 // So we must first trick it into thinking inlineView is an unbound identifier
15 // in the various situations where it is not.
16 // This covers native ES module, for example:
17 // import { inlineView } from "aurelia-framework";
18 // inlineView("<template>");
19 parser.hooks.evaluateIdentifier.for('javascript/auto').tap(TAP_NAME, (expr) => {
20 if (expr.name === "inlineView") {
21 return new BasicEvaluatedExpression().setIdentifier("inlineView").setRange(expr.range);
22 }
23 return undefined;
24 });
25 // This covers commonjs modules, for example:
26 // const _aurelia = require("aurelia-framework");
27 // _aurelia.inlineView("<template>");
28 // Or (note: no renaming supported):
29 // const inlineView = require("aurelia-framework").inlineView;
30 // inlineView("<template>");
31 parser.hooks.evaluate.for('javascript/auto').tap(TAP_NAME, expr => {
32 // PLATFORM.moduleName
33 // -> MemberExpression [object: Identifier(PLATFORM)] [property: Identifier(moduleName)]
34 if (expr.type === 'MemberExpression' && expr.property.name === "inlineView") {
35 return new BasicEvaluatedExpression().setIdentifier("inlineView").setRange(expr.range);
36 }
37 return undefined;
38 });
39 parser.hooks.call.for('javascript/auto').tap(TAP_NAME, $expr => {
40 const expr = $expr;
41 if (expr.arguments.length !== 1)
42 return;
43 let arg1 = expr.arguments[0];
44 let param1 = parser.evaluateExpression(arg1);
45 if (!(param1 === null || param1 === void 0 ? void 0 : param1.isString())) {
46 return;
47 }
48 let modules;
49 try {
50 modules = htmlLoader.modules(param1.string);
51 }
52 catch (e) {
53 return;
54 }
55 modules.forEach(add);
56 });
57 }
58}
59exports.InlineViewDependenciesPlugin = InlineViewDependenciesPlugin;