UNPKG

2.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ConventionDependenciesPlugin = void 0;
4const BaseIncludePlugin_1 = require("./BaseIncludePlugin");
5const PreserveModuleNamePlugin_1 = require("./PreserveModuleNamePlugin");
6const minimatch = require("minimatch");
7const path = require("path");
8class ConventionDependenciesPlugin extends BaseIncludePlugin_1.BaseIncludePlugin {
9 /**
10 * glob: a pattern that filters which files are affected
11 */
12 constructor(glob, conventions = [".html", ".htm"]) {
13 super();
14 this.glob = glob;
15 if (!Array.isArray(conventions))
16 conventions = [conventions];
17 this.conventions = conventions.map(c => typeof c === "string"
18 ? swapExtension.bind(null, c)
19 : c);
20 }
21 parser(compilation, parser, addDependency) {
22 const root = path.resolve();
23 parser.hooks.program.tap("Aurelia:ConventionDependencies", () => {
24 const { resource: file, rawRequest } = parser.state.current;
25 if (!file) {
26 return;
27 }
28 // We don't want to bring in dependencies of the async! loader
29 if (/^async[!?]/.test(rawRequest)) {
30 return;
31 }
32 if (!minimatch(path.relative(root, file), this.glob, { dot: true })) {
33 return;
34 }
35 for (let c of this.conventions) {
36 try {
37 const probe = c(file);
38 // Check if file exists
39 compilation.inputFileSystem.statSync(probe);
40 let relative = path.relative(path.dirname(file), probe);
41 if (!relative.startsWith(".")) {
42 relative = "./" + relative;
43 }
44 addDependency(relative);
45 // If the module has a conventional dependency, make sure we preserve its name as well.
46 // This solves the pattern where a VM is statically loaded, e.g. `import { ViewModel } from "x"`
47 // and then passed to Aurelia, e.g. with `aurelia-dialog`.
48 // At this point Aurelia must determine the origin of the module to be able to look for
49 // a conventional view, and so the module name must be preserved although it's never loaded
50 // by `aurelia-loader`. See also aurelia/metadata#51.
51 parser.state.current[PreserveModuleNamePlugin_1.preserveModuleName] = true;
52 }
53 catch (ex) { }
54 }
55 });
56 }
57}
58exports.ConventionDependenciesPlugin = ConventionDependenciesPlugin;
59;
60function swapExtension(newExtension, file) {
61 return file.replace(/\.[^\\/.]*$/, "") + newExtension;
62}