1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const ContextElementDependency = require("./ContextElementDependency");
|
9 | const ImportMetaContextDependency = require("./ImportMetaContextDependency");
|
10 | const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class ImportMetaContextPlugin {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | apply(compiler) {
|
22 | compiler.hooks.compilation.tap(
|
23 | "RequireContextPlugin",
|
24 | (compilation, { contextModuleFactory, normalModuleFactory }) => {
|
25 | compilation.dependencyFactories.set(
|
26 | ImportMetaContextDependency,
|
27 | contextModuleFactory
|
28 | );
|
29 | compilation.dependencyTemplates.set(
|
30 | ImportMetaContextDependency,
|
31 | new ImportMetaContextDependency.Template()
|
32 | );
|
33 | compilation.dependencyFactories.set(
|
34 | ContextElementDependency,
|
35 | normalModuleFactory
|
36 | );
|
37 |
|
38 | const handler = (parser, parserOptions) => {
|
39 | if (
|
40 | parserOptions.importMetaContext !== undefined &&
|
41 | !parserOptions.importMetaContext
|
42 | )
|
43 | return;
|
44 |
|
45 | new ImportMetaContextDependencyParserPlugin().apply(parser);
|
46 | };
|
47 |
|
48 | normalModuleFactory.hooks.parser
|
49 | .for("javascript/auto")
|
50 | .tap("ImportMetaContextPlugin", handler);
|
51 | normalModuleFactory.hooks.parser
|
52 | .for("javascript/esm")
|
53 | .tap("ImportMetaContextPlugin", handler);
|
54 | }
|
55 | );
|
56 | }
|
57 | }
|
58 |
|
59 | module.exports = ImportMetaContextPlugin;
|