[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Ivan Kopeykin @vankop
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const {
|
---|
| 9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
| 10 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
| 11 | } = require("../ModuleTypeConstants");
|
---|
| 12 | const ContextElementDependency = require("./ContextElementDependency");
|
---|
| 13 | const ImportMetaContextDependency = require("./ImportMetaContextDependency");
|
---|
| 14 | const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin");
|
---|
| 15 |
|
---|
| 16 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
| 17 | /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
|
---|
| 18 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 19 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
| 20 |
|
---|
| 21 | const PLUGIN_NAME = "ImportMetaContextPlugin";
|
---|
| 22 |
|
---|
| 23 | class ImportMetaContextPlugin {
|
---|
| 24 | /**
|
---|
| 25 | * Apply the plugin
|
---|
| 26 | * @param {Compiler} compiler the compiler instance
|
---|
| 27 | * @returns {void}
|
---|
| 28 | */
|
---|
| 29 | apply(compiler) {
|
---|
| 30 | compiler.hooks.compilation.tap(
|
---|
| 31 | PLUGIN_NAME,
|
---|
| 32 | (compilation, { contextModuleFactory, normalModuleFactory }) => {
|
---|
| 33 | compilation.dependencyFactories.set(
|
---|
| 34 | ImportMetaContextDependency,
|
---|
| 35 | contextModuleFactory
|
---|
| 36 | );
|
---|
| 37 | compilation.dependencyTemplates.set(
|
---|
| 38 | ImportMetaContextDependency,
|
---|
| 39 | new ImportMetaContextDependency.Template()
|
---|
| 40 | );
|
---|
| 41 | compilation.dependencyFactories.set(
|
---|
| 42 | ContextElementDependency,
|
---|
| 43 | normalModuleFactory
|
---|
| 44 | );
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * @param {Parser} parser parser parser
|
---|
| 48 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
| 49 | * @returns {void}
|
---|
| 50 | */
|
---|
| 51 | const handler = (parser, parserOptions) => {
|
---|
| 52 | if (
|
---|
| 53 | parserOptions.importMetaContext !== undefined &&
|
---|
| 54 | !parserOptions.importMetaContext
|
---|
| 55 | )
|
---|
| 56 | return;
|
---|
| 57 |
|
---|
| 58 | new ImportMetaContextDependencyParserPlugin().apply(parser);
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | normalModuleFactory.hooks.parser
|
---|
| 62 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
| 63 | .tap(PLUGIN_NAME, handler);
|
---|
| 64 | normalModuleFactory.hooks.parser
|
---|
| 65 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
| 66 | .tap(PLUGIN_NAME, handler);
|
---|
| 67 | }
|
---|
| 68 | );
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | module.exports = ImportMetaContextPlugin;
|
---|