1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ConventionDependenciesPlugin = void 0;
|
4 | const BaseIncludePlugin_1 = require("./BaseIncludePlugin");
|
5 | const PreserveModuleNamePlugin_1 = require("./PreserveModuleNamePlugin");
|
6 | const minimatch = require("minimatch");
|
7 | const path = require("path");
|
8 | class ConventionDependenciesPlugin extends BaseIncludePlugin_1.BaseIncludePlugin {
|
9 | |
10 |
|
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 |
|
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 |
|
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 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | parser.state.current[PreserveModuleNamePlugin_1.preserveModuleName] = true;
|
52 | }
|
53 | catch (ex) { }
|
54 | }
|
55 | });
|
56 | }
|
57 | }
|
58 | exports.ConventionDependenciesPlugin = ConventionDependenciesPlugin;
|
59 | ;
|
60 | function swapExtension(newExtension, file) {
|
61 | return file.replace(/\.[^\\/.]*$/, "") + newExtension;
|
62 | }
|