Skip to content

Commit d3c5d35

Browse files
committed
add ImportMetaContextPlugin
1 parent b89f397 commit d3c5d35

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

lib/WebpackOptionsApply.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const ResolverCachePlugin = require("./cache/ResolverCachePlugin");
3535

3636
const CommonJsPlugin = require("./dependencies/CommonJsPlugin");
3737
const HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
38+
const ImportMetaContextPlugin = require("./dependencies/ImportMetaContextPlugin");
3839
const ImportMetaPlugin = require("./dependencies/ImportMetaPlugin");
3940
const ImportPlugin = require("./dependencies/ImportPlugin");
4041
const LoaderPlugin = require("./dependencies/LoaderPlugin");
@@ -361,6 +362,7 @@ class WebpackOptionsApply extends OptionsApply {
361362
new RequireEnsurePlugin().apply(compiler);
362363
new RequireContextPlugin().apply(compiler);
363364
new ImportPlugin().apply(compiler);
365+
new ImportMetaContextPlugin().apply(compiler);
364366
new SystemPlugin().apply(compiler);
365367
new ImportMetaPlugin().apply(compiler);
366368
new URLPlugin().apply(compiler);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 ContextElementDependency = require("./ContextElementDependency");
9+
const ImportMetaContextDependency = require("./ImportMetaContextDependency");
10+
const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin");
11+
12+
/** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
13+
/** @typedef {import("../Compiler")} Compiler */
14+
15+
class ImportMetaContextPlugin {
16+
/**
17+
* Apply the plugin
18+
* @param {Compiler} compiler the compiler instance
19+
* @returns {void}
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;

lib/dependencies/RequireContextPlugin.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
const { cachedSetProperty } = require("../util/cleverMerge");
99
const ContextElementDependency = require("./ContextElementDependency");
10-
const ImportMetaContextDependency = require("./ImportMetaContextDependency");
11-
const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin");
1210
const RequireContextDependency = require("./RequireContextDependency");
1311
const RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin");
1412

@@ -36,14 +34,6 @@ class RequireContextPlugin {
3634
RequireContextDependency,
3735
new RequireContextDependency.Template()
3836
);
39-
compilation.dependencyFactories.set(
40-
ImportMetaContextDependency,
41-
contextModuleFactory
42-
);
43-
compilation.dependencyTemplates.set(
44-
ImportMetaContextDependency,
45-
new ImportMetaContextDependency.Template()
46-
);
4737

4838
compilation.dependencyFactories.set(
4939
ContextElementDependency,
@@ -60,16 +50,6 @@ class RequireContextPlugin {
6050
new RequireContextDependencyParserPlugin().apply(parser);
6151
};
6252

63-
const handlerImportMeta = (parser, parserOptions) => {
64-
new ImportMetaContextDependencyParserPlugin().apply(parser);
65-
};
66-
67-
normalModuleFactory.hooks.parser
68-
.for("javascript/auto")
69-
.tap("RequireContextPlugin", handlerImportMeta);
70-
normalModuleFactory.hooks.parser
71-
.for("javascript/esm")
72-
.tap("RequireContextPlugin", handlerImportMeta);
7353
normalModuleFactory.hooks.parser
7454
.for("javascript/auto")
7555
.tap("RequireContextPlugin", handler);

test/cases/chunks/context-weak/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
it("should not bundle context requires with asyncMode === 'weak'", function() {
2+
var contextRequire = require.context(".", false, /two/, "weak");
3+
expect(function() {
4+
contextRequire("./two")
5+
}).toThrowError(/not available/);
6+
});
7+
8+
it("should not bundle context requires with asyncMode === 'weak' using import.meta.webpackContext", function() {
29
const contextRequire = import.meta.webpackContext(".", {
310
recursive: false,
411
regExp: /two/,
@@ -10,13 +17,13 @@ it("should not bundle context requires with asyncMode === 'weak'", function() {
1017
});
1118

1219
it("should find module with asyncMode === 'weak' when required elsewhere", function() {
13-
const contextRequire = require.context(".", false, /.+/, "weak");
20+
var contextRequire = require.context(".", false, /.+/, "weak");
1421
expect(contextRequire("./three")).toBe(3);
1522
require("./three"); // in a real app would be served as a separate chunk
1623
});
1724

1825
it("should find module with asyncMode === 'weak' when required elsewhere (recursive)", function() {
19-
const contextRequire = require.context(".", true, /.+/, "weak");
26+
var contextRequire = require.context(".", true, /.+/, "weak");
2027
expect(contextRequire("./dir/four")).toBe(4);
2128
require("./dir/four"); // in a real app would be served as a separate chunk
2229
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy