webpack 5.59.0 → 5.76.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of webpack might be problematic. Click here for more details.
- package/README.md +22 -24
- package/bin/webpack.js +0 -0
- package/hot/dev-server.js +17 -4
- package/hot/lazy-compilation-node.js +3 -1
- package/hot/poll.js +1 -1
- package/hot/signal.js +1 -1
- package/lib/APIPlugin.js +33 -0
- package/lib/BannerPlugin.js +13 -5
- package/lib/Cache.js +1 -1
- package/lib/CacheFacade.js +4 -11
- package/lib/Chunk.js +6 -3
- package/lib/ChunkGraph.js +94 -8
- package/lib/ChunkGroup.js +1 -1
- package/lib/CleanPlugin.js +81 -20
- package/lib/Compilation.js +188 -93
- package/lib/Compiler.js +87 -18
- package/lib/ConstPlugin.js +2 -2
- package/lib/ContextModule.js +142 -51
- package/lib/ContextModuleFactory.js +65 -25
- package/lib/DelegatedModule.js +1 -1
- package/lib/DelegatedModuleFactoryPlugin.js +1 -1
- package/lib/Dependency.js +17 -0
- package/lib/DependencyTemplate.js +9 -0
- package/lib/DependencyTemplates.js +1 -1
- package/lib/DllModule.js +1 -1
- package/lib/DllReferencePlugin.js +1 -1
- package/lib/EntryOptionPlugin.js +2 -0
- package/lib/ErrorHelpers.js +2 -2
- package/lib/EvalDevToolModulePlugin.js +16 -1
- package/lib/EvalSourceMapDevToolPlugin.js +25 -4
- package/lib/ExportsInfo.js +5 -5
- package/lib/ExternalModule.js +94 -54
- package/lib/ExternalModuleFactoryPlugin.js +5 -5
- package/lib/FileSystemInfo.js +124 -58
- package/lib/Generator.js +3 -0
- package/lib/HookWebpackError.js +1 -1
- package/lib/HotModuleReplacementPlugin.js +3 -1
- package/lib/LoaderOptionsPlugin.js +1 -1
- package/lib/Module.js +28 -4
- package/lib/ModuleFilenameHelpers.js +8 -4
- package/lib/ModuleHashingError.js +29 -0
- package/lib/MultiCompiler.js +1 -1
- package/lib/MultiWatching.js +1 -1
- package/lib/NodeStuffPlugin.js +13 -3
- package/lib/NormalModule.js +51 -33
- package/lib/NormalModuleFactory.js +42 -37
- package/lib/ProgressPlugin.js +4 -5
- package/lib/RawModule.js +1 -1
- package/lib/RuntimeGlobals.js +29 -1
- package/lib/RuntimeModule.js +1 -1
- package/lib/RuntimePlugin.js +84 -1
- package/lib/RuntimeTemplate.js +114 -2
- package/lib/Template.js +3 -2
- package/lib/TemplatedPathPlugin.js +48 -23
- package/lib/WatchIgnorePlugin.js +19 -7
- package/lib/Watching.js +33 -19
- package/lib/WebpackOptionsApply.js +79 -11
- package/lib/asset/AssetGenerator.js +228 -71
- package/lib/asset/AssetModulesPlugin.js +3 -0
- package/lib/asset/AssetParser.js +1 -0
- package/lib/asset/AssetSourceGenerator.js +31 -6
- package/lib/asset/AssetSourceParser.js +1 -0
- package/lib/asset/RawDataUrlModule.js +148 -0
- package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
- package/lib/buildChunkGraph.js +38 -7
- package/lib/cache/PackFileCacheStrategy.js +15 -8
- package/lib/cache/ResolverCachePlugin.js +90 -29
- package/lib/cli.js +44 -3
- package/lib/config/browserslistTargetHandler.js +41 -6
- package/lib/config/defaults.js +123 -19
- package/lib/config/normalization.js +10 -2
- package/lib/config/target.js +10 -0
- package/lib/container/ContainerEntryModule.js +8 -5
- package/lib/container/FallbackModule.js +4 -4
- package/lib/container/ModuleFederationPlugin.js +2 -0
- package/lib/container/RemoteModule.js +4 -2
- package/lib/container/RemoteRuntimeModule.js +8 -7
- package/lib/css/CssExportsGenerator.js +139 -0
- package/lib/css/CssGenerator.js +109 -0
- package/lib/css/CssLoadingRuntimeModule.js +442 -0
- package/lib/css/CssModulesPlugin.js +462 -0
- package/lib/css/CssParser.js +618 -0
- package/lib/css/walkCssTokens.js +659 -0
- package/lib/debug/ProfilingPlugin.js +24 -21
- package/lib/dependencies/AMDRequireDependency.js +6 -6
- package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
- package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
- package/lib/dependencies/CommonJsImportsParserPlugin.js +344 -61
- package/lib/dependencies/CommonJsRequireContextDependency.js +6 -2
- package/lib/dependencies/CommonJsRequireDependency.js +2 -1
- package/lib/dependencies/ContextDependency.js +16 -2
- package/lib/dependencies/ContextDependencyHelpers.js +21 -8
- package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
- package/lib/dependencies/ContextElementDependency.js +25 -3
- package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
- package/lib/dependencies/CssExportDependency.js +85 -0
- package/lib/dependencies/CssImportDependency.js +75 -0
- package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
- package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
- package/lib/dependencies/CssUrlDependency.js +132 -0
- package/lib/dependencies/ExportsInfoDependency.js +6 -0
- package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
- package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +127 -0
- package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
- package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
- package/lib/dependencies/HarmonyImportDependency.js +23 -0
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
- package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
- package/lib/dependencies/ImportContextDependency.js +0 -2
- package/lib/dependencies/ImportMetaContextDependency.js +35 -0
- package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
- package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
- package/lib/dependencies/ImportMetaPlugin.js +22 -3
- package/lib/dependencies/ImportParserPlugin.js +35 -29
- package/lib/dependencies/JsonExportsDependency.js +17 -21
- package/lib/dependencies/LoaderDependency.js +13 -0
- package/lib/dependencies/LoaderImportDependency.js +13 -0
- package/lib/dependencies/LoaderPlugin.js +4 -2
- package/lib/dependencies/ModuleDependency.js +11 -1
- package/lib/dependencies/ProvidedDependency.js +31 -8
- package/lib/dependencies/RequireContextDependency.js +0 -16
- package/lib/dependencies/RequireEnsureDependency.js +2 -2
- package/lib/dependencies/RequireResolveContextDependency.js +2 -2
- package/lib/dependencies/RequireResolveDependency.js +2 -1
- package/lib/dependencies/URLDependency.js +3 -8
- package/lib/dependencies/URLPlugin.js +22 -1
- package/lib/dependencies/WorkerPlugin.js +2 -0
- package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
- package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
- package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
- package/lib/hmr/LazyCompilationPlugin.js +54 -26
- package/lib/hmr/lazyCompilationBackend.js +51 -12
- package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
- package/lib/ids/HashedModuleIdsPlugin.js +11 -14
- package/lib/ids/IdHelpers.js +26 -12
- package/lib/ids/NamedModuleIdsPlugin.js +6 -9
- package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
- package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
- package/lib/ids/SyncModuleIdsPlugin.js +140 -0
- package/lib/index.js +20 -0
- package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
- package/lib/javascript/BasicEvaluatedExpression.js +5 -2
- package/lib/javascript/ChunkHelpers.js +33 -0
- package/lib/javascript/JavascriptGenerator.js +1 -0
- package/lib/javascript/JavascriptModulesPlugin.js +27 -2
- package/lib/javascript/JavascriptParser.js +143 -73
- package/lib/javascript/StartupHelpers.js +7 -30
- package/lib/json/JsonData.js +8 -0
- package/lib/json/JsonParser.js +4 -6
- package/lib/library/AssignLibraryPlugin.js +39 -15
- package/lib/library/EnableLibraryPlugin.js +11 -0
- package/lib/library/UmdLibraryPlugin.js +5 -3
- package/lib/node/NodeTargetPlugin.js +3 -0
- package/lib/node/NodeWatchFileSystem.js +85 -31
- package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
- package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
- package/lib/optimize/ConcatenatedModule.js +62 -26
- package/lib/optimize/ModuleConcatenationPlugin.js +26 -4
- package/lib/optimize/RealContentHashPlugin.js +45 -15
- package/lib/optimize/SplitChunksPlugin.js +8 -1
- package/lib/runtime/AsyncModuleRuntimeModule.js +50 -66
- package/lib/runtime/BaseUriRuntimeModule.js +31 -0
- package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
- package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
- package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
- package/lib/runtime/LoadScriptRuntimeModule.js +11 -9
- package/lib/runtime/NonceRuntimeModule.js +24 -0
- package/lib/schemes/HttpUriPlugin.js +77 -14
- package/lib/serialization/FileMiddleware.js +44 -9
- package/lib/sharing/ConsumeSharedModule.js +8 -2
- package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
- package/lib/sharing/ProvideSharedModule.js +4 -2
- package/lib/sharing/ProvideSharedPlugin.js +1 -2
- package/lib/sharing/ShareRuntimeModule.js +1 -1
- package/lib/sharing/utils.js +1 -1
- package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
- package/lib/stats/DefaultStatsPrinterPlugin.js +90 -25
- package/lib/util/ArrayHelpers.js +30 -0
- package/lib/util/AsyncQueue.js +1 -1
- package/lib/util/compileBooleanMatcher.js +1 -1
- package/lib/util/create-schema-validation.js +9 -2
- package/lib/util/createHash.js +12 -0
- package/lib/util/deprecation.js +10 -2
- package/lib/util/deterministicGrouping.js +1 -1
- package/lib/util/extractUrlAndGlobal.js +3 -0
- package/lib/util/fs.js +11 -0
- package/lib/util/hash/BatchedHash.js +7 -4
- package/lib/util/hash/md4.js +20 -0
- package/lib/util/hash/wasm-hash.js +163 -0
- package/lib/util/hash/xxhash64.js +5 -139
- package/lib/util/identifier.js +65 -44
- package/lib/util/internalSerializables.js +15 -0
- package/lib/util/nonNumericOnlyHash.js +22 -0
- package/lib/util/semver.js +17 -10
- package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
- package/lib/wasm-sync/WebAssemblyParser.js +1 -1
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +31 -18
- package/lib/webpack.js +10 -3
- package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
- package/module.d.ts +215 -0
- package/package.json +28 -32
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +321 -30
- package/schemas/plugins/BannerPlugin.check.js +1 -1
- package/schemas/plugins/BannerPlugin.json +4 -0
- package/schemas/plugins/DllReferencePlugin.check.js +1 -1
- package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
- package/schemas/plugins/ProgressPlugin.check.js +1 -1
- package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
- package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
- package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
- package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
- package/schemas/plugins/container/ContainerPlugin.json +2 -1
- package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
- package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
- package/schemas/plugins/container/ExternalsType.check.js +1 -1
- package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
- package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
- package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
- package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
- package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
- package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
- package/schemas/plugins/css/CssParserOptions.check.js +6 -0
- package/schemas/plugins/css/CssParserOptions.json +3 -0
- package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
- package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
- package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
- package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
- package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
- package/types.d.ts +869 -296
@@ -5,9 +5,12 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
+
const { fileURLToPath } = require("url");
|
8
9
|
const CommentCompilationWarning = require("../CommentCompilationWarning");
|
9
10
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
10
11
|
const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
|
12
|
+
const WebpackError = require("../WebpackError");
|
13
|
+
const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
|
11
14
|
const {
|
12
15
|
evaluateToIdentifier,
|
13
16
|
evaluateToString,
|
@@ -26,8 +29,12 @@ const RequireResolveContextDependency = require("./RequireResolveContextDependen
|
|
26
29
|
const RequireResolveDependency = require("./RequireResolveDependency");
|
27
30
|
const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency");
|
28
31
|
|
32
|
+
/** @typedef {import("estree").CallExpression} CallExpressionNode */
|
29
33
|
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
30
34
|
|
35
|
+
const createRequireSpecifierTag = Symbol("createRequire");
|
36
|
+
const createdRequireIdentifierTag = Symbol("createRequire()");
|
37
|
+
|
31
38
|
class CommonJsImportsParserPlugin {
|
32
39
|
/**
|
33
40
|
* @param {JavascriptParserOptions} options parser options
|
@@ -39,51 +46,72 @@ class CommonJsImportsParserPlugin {
|
|
39
46
|
apply(parser) {
|
40
47
|
const options = this.options;
|
41
48
|
|
42
|
-
|
49
|
+
const getContext = () => {
|
50
|
+
if (parser.currentTagData) {
|
51
|
+
const { context } = parser.currentTagData;
|
52
|
+
return context;
|
53
|
+
}
|
54
|
+
};
|
55
|
+
|
56
|
+
//#region metadata
|
43
57
|
const tapRequireExpression = (expression, getMembers) => {
|
44
58
|
parser.hooks.typeof
|
45
59
|
.for(expression)
|
46
60
|
.tap(
|
47
|
-
"
|
61
|
+
"CommonJsImportsParserPlugin",
|
48
62
|
toConstantDependency(parser, JSON.stringify("function"))
|
49
63
|
);
|
50
64
|
parser.hooks.evaluateTypeof
|
51
65
|
.for(expression)
|
52
|
-
.tap("
|
66
|
+
.tap("CommonJsImportsParserPlugin", evaluateToString("function"));
|
53
67
|
parser.hooks.evaluateIdentifier
|
54
68
|
.for(expression)
|
55
69
|
.tap(
|
56
|
-
"
|
70
|
+
"CommonJsImportsParserPlugin",
|
57
71
|
evaluateToIdentifier(expression, "require", getMembers, true)
|
58
72
|
);
|
59
73
|
};
|
74
|
+
const tapRequireExpressionTag = tag => {
|
75
|
+
parser.hooks.typeof
|
76
|
+
.for(tag)
|
77
|
+
.tap(
|
78
|
+
"CommonJsImportsParserPlugin",
|
79
|
+
toConstantDependency(parser, JSON.stringify("function"))
|
80
|
+
);
|
81
|
+
parser.hooks.evaluateTypeof
|
82
|
+
.for(tag)
|
83
|
+
.tap("CommonJsImportsParserPlugin", evaluateToString("function"));
|
84
|
+
};
|
60
85
|
tapRequireExpression("require", () => []);
|
61
86
|
tapRequireExpression("require.resolve", () => ["resolve"]);
|
62
87
|
tapRequireExpression("require.resolveWeak", () => ["resolveWeak"]);
|
88
|
+
//#endregion
|
63
89
|
|
64
90
|
// Weird stuff //
|
65
|
-
parser.hooks.assign
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
91
|
+
parser.hooks.assign
|
92
|
+
.for("require")
|
93
|
+
.tap("CommonJsImportsParserPlugin", expr => {
|
94
|
+
// to not leak to global "require", we need to define a local require here.
|
95
|
+
const dep = new ConstDependency("var require;", 0);
|
96
|
+
dep.loc = expr.loc;
|
97
|
+
parser.state.module.addPresentationalDependency(dep);
|
98
|
+
return true;
|
99
|
+
});
|
72
100
|
|
73
|
-
|
101
|
+
//#region Unsupported
|
74
102
|
parser.hooks.expression
|
75
|
-
.for("require.main
|
103
|
+
.for("require.main")
|
76
104
|
.tap(
|
77
|
-
"
|
105
|
+
"CommonJsImportsParserPlugin",
|
78
106
|
expressionIsUnsupported(
|
79
107
|
parser,
|
80
|
-
"require.main
|
108
|
+
"require.main is not supported by webpack."
|
81
109
|
)
|
82
110
|
);
|
83
111
|
parser.hooks.call
|
84
112
|
.for("require.main.require")
|
85
113
|
.tap(
|
86
|
-
"
|
114
|
+
"CommonJsImportsParserPlugin",
|
87
115
|
expressionIsUnsupported(
|
88
116
|
parser,
|
89
117
|
"require.main.require is not supported by webpack."
|
@@ -92,7 +120,7 @@ class CommonJsImportsParserPlugin {
|
|
92
120
|
parser.hooks.expression
|
93
121
|
.for("module.parent.require")
|
94
122
|
.tap(
|
95
|
-
"
|
123
|
+
"CommonJsImportsParserPlugin",
|
96
124
|
expressionIsUnsupported(
|
97
125
|
parser,
|
98
126
|
"module.parent.require is not supported by webpack."
|
@@ -101,61 +129,81 @@ class CommonJsImportsParserPlugin {
|
|
101
129
|
parser.hooks.call
|
102
130
|
.for("module.parent.require")
|
103
131
|
.tap(
|
104
|
-
"
|
132
|
+
"CommonJsImportsParserPlugin",
|
105
133
|
expressionIsUnsupported(
|
106
134
|
parser,
|
107
135
|
"module.parent.require is not supported by webpack."
|
108
136
|
)
|
109
137
|
);
|
138
|
+
//#endregion
|
110
139
|
|
111
|
-
|
112
|
-
|
113
|
-
parser.hooks.rename.for("require").tap("CommonJsPlugin", expr => {
|
140
|
+
//#region Renaming
|
141
|
+
const defineUndefined = expr => {
|
114
142
|
// To avoid "not defined" error, replace the value with undefined
|
115
143
|
const dep = new ConstDependency("undefined", expr.range);
|
116
144
|
dep.loc = expr.loc;
|
117
145
|
parser.state.module.addPresentationalDependency(dep);
|
118
146
|
return false;
|
119
|
-
}
|
147
|
+
};
|
148
|
+
parser.hooks.canRename
|
149
|
+
.for("require")
|
150
|
+
.tap("CommonJsImportsParserPlugin", () => true);
|
151
|
+
parser.hooks.rename
|
152
|
+
.for("require")
|
153
|
+
.tap("CommonJsImportsParserPlugin", defineUndefined);
|
154
|
+
//#endregion
|
155
|
+
|
156
|
+
//#region Inspection
|
157
|
+
const requireCache = toConstantDependency(
|
158
|
+
parser,
|
159
|
+
RuntimeGlobals.moduleCache,
|
160
|
+
[
|
161
|
+
RuntimeGlobals.moduleCache,
|
162
|
+
RuntimeGlobals.moduleId,
|
163
|
+
RuntimeGlobals.moduleLoaded
|
164
|
+
]
|
165
|
+
);
|
120
166
|
|
121
|
-
// inspection //
|
122
167
|
parser.hooks.expression
|
123
168
|
.for("require.cache")
|
124
|
-
.tap(
|
125
|
-
|
126
|
-
toConstantDependency(parser, RuntimeGlobals.moduleCache, [
|
127
|
-
RuntimeGlobals.moduleCache,
|
128
|
-
RuntimeGlobals.moduleId,
|
129
|
-
RuntimeGlobals.moduleLoaded
|
130
|
-
])
|
131
|
-
);
|
169
|
+
.tap("CommonJsImportsParserPlugin", requireCache);
|
170
|
+
//#endregion
|
132
171
|
|
133
|
-
|
172
|
+
//#region Require as expression
|
173
|
+
const requireAsExpressionHandler = expr => {
|
174
|
+
const dep = new CommonJsRequireContextDependency(
|
175
|
+
{
|
176
|
+
request: options.unknownContextRequest,
|
177
|
+
recursive: options.unknownContextRecursive,
|
178
|
+
regExp: options.unknownContextRegExp,
|
179
|
+
mode: "sync"
|
180
|
+
},
|
181
|
+
expr.range,
|
182
|
+
undefined,
|
183
|
+
parser.scope.inShorthand,
|
184
|
+
getContext()
|
185
|
+
);
|
186
|
+
dep.critical =
|
187
|
+
options.unknownContextCritical &&
|
188
|
+
"require function is used in a way in which dependencies cannot be statically extracted";
|
189
|
+
dep.loc = expr.loc;
|
190
|
+
dep.optional = !!parser.scope.inTry;
|
191
|
+
parser.state.current.addDependency(dep);
|
192
|
+
return true;
|
193
|
+
};
|
134
194
|
parser.hooks.expression
|
135
195
|
.for("require")
|
136
|
-
.tap("CommonJsImportsParserPlugin",
|
137
|
-
|
138
|
-
{
|
139
|
-
request: options.unknownContextRequest,
|
140
|
-
recursive: options.unknownContextRecursive,
|
141
|
-
regExp: options.unknownContextRegExp,
|
142
|
-
mode: "sync"
|
143
|
-
},
|
144
|
-
expr.range
|
145
|
-
);
|
146
|
-
dep.critical =
|
147
|
-
options.unknownContextCritical &&
|
148
|
-
"require function is used in a way in which dependencies cannot be statically extracted";
|
149
|
-
dep.loc = expr.loc;
|
150
|
-
dep.optional = !!parser.scope.inTry;
|
151
|
-
parser.state.current.addDependency(dep);
|
152
|
-
return true;
|
153
|
-
});
|
196
|
+
.tap("CommonJsImportsParserPlugin", requireAsExpressionHandler);
|
197
|
+
//#endregion
|
154
198
|
|
155
|
-
|
199
|
+
//#region Require
|
156
200
|
const processRequireItem = (expr, param) => {
|
157
201
|
if (param.isString()) {
|
158
|
-
const dep = new CommonJsRequireDependency(
|
202
|
+
const dep = new CommonJsRequireDependency(
|
203
|
+
param.string,
|
204
|
+
param.range,
|
205
|
+
getContext()
|
206
|
+
);
|
159
207
|
dep.loc = expr.loc;
|
160
208
|
dep.optional = !!parser.scope.inTry;
|
161
209
|
parser.state.current.addDependency(dep);
|
@@ -172,7 +220,9 @@ class CommonJsImportsParserPlugin {
|
|
172
220
|
{
|
173
221
|
category: "commonjs"
|
174
222
|
},
|
175
|
-
parser
|
223
|
+
parser,
|
224
|
+
undefined,
|
225
|
+
getContext()
|
176
226
|
);
|
177
227
|
if (!dep) return;
|
178
228
|
dep.loc = expr.loc;
|
@@ -266,8 +316,9 @@ class CommonJsImportsParserPlugin {
|
|
266
316
|
parser.hooks.new
|
267
317
|
.for("module.require")
|
268
318
|
.tap("CommonJsImportsParserPlugin", createRequireHandler(true));
|
319
|
+
//#endregion
|
269
320
|
|
270
|
-
|
321
|
+
//#region Require with property access
|
271
322
|
const chainHandler = (expr, calleeMembers, callExpr, members) => {
|
272
323
|
if (callExpr.arguments.length !== 1) return;
|
273
324
|
const param = parser.evaluateExpression(callExpr.arguments[0]);
|
@@ -280,7 +331,7 @@ class CommonJsImportsParserPlugin {
|
|
280
331
|
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
|
281
332
|
dep.optional = !!parser.scope.inTry;
|
282
333
|
dep.loc = expr.loc;
|
283
|
-
parser.state.
|
334
|
+
parser.state.current.addDependency(dep);
|
284
335
|
return true;
|
285
336
|
}
|
286
337
|
};
|
@@ -297,7 +348,7 @@ class CommonJsImportsParserPlugin {
|
|
297
348
|
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
|
298
349
|
dep.optional = !!parser.scope.inTry;
|
299
350
|
dep.loc = expr.callee.loc;
|
300
|
-
parser.state.
|
351
|
+
parser.state.current.addDependency(dep);
|
301
352
|
parser.walkExpressions(expr.arguments);
|
302
353
|
return true;
|
303
354
|
}
|
@@ -314,8 +365,9 @@ class CommonJsImportsParserPlugin {
|
|
314
365
|
parser.hooks.callMemberChainOfCallMemberChain
|
315
366
|
.for("module.require")
|
316
367
|
.tap("CommonJsImportsParserPlugin", callChainHandler);
|
368
|
+
//#endregion
|
317
369
|
|
318
|
-
|
370
|
+
//#region Require.resolve
|
319
371
|
const processResolve = (expr, weak) => {
|
320
372
|
if (expr.arguments.length !== 1) return;
|
321
373
|
const param = parser.evaluateExpression(expr.arguments[0]);
|
@@ -343,7 +395,11 @@ class CommonJsImportsParserPlugin {
|
|
343
395
|
};
|
344
396
|
const processResolveItem = (expr, param, weak) => {
|
345
397
|
if (param.isString()) {
|
346
|
-
const dep = new RequireResolveDependency(
|
398
|
+
const dep = new RequireResolveDependency(
|
399
|
+
param.string,
|
400
|
+
param.range,
|
401
|
+
getContext()
|
402
|
+
);
|
347
403
|
dep.loc = expr.loc;
|
348
404
|
dep.optional = !!parser.scope.inTry;
|
349
405
|
dep.weak = weak;
|
@@ -362,7 +418,8 @@ class CommonJsImportsParserPlugin {
|
|
362
418
|
category: "commonjs",
|
363
419
|
mode: weak ? "weak" : "sync"
|
364
420
|
},
|
365
|
-
parser
|
421
|
+
parser,
|
422
|
+
getContext()
|
366
423
|
);
|
367
424
|
if (!dep) return;
|
368
425
|
dep.loc = expr.loc;
|
@@ -373,14 +430,240 @@ class CommonJsImportsParserPlugin {
|
|
373
430
|
|
374
431
|
parser.hooks.call
|
375
432
|
.for("require.resolve")
|
376
|
-
.tap("
|
433
|
+
.tap("CommonJsImportsParserPlugin", expr => {
|
377
434
|
return processResolve(expr, false);
|
378
435
|
});
|
379
436
|
parser.hooks.call
|
380
437
|
.for("require.resolveWeak")
|
381
|
-
.tap("
|
438
|
+
.tap("CommonJsImportsParserPlugin", expr => {
|
382
439
|
return processResolve(expr, true);
|
383
440
|
});
|
441
|
+
//#endregion
|
442
|
+
|
443
|
+
//#region Create require
|
444
|
+
|
445
|
+
if (!options.createRequire) return;
|
446
|
+
|
447
|
+
let moduleName;
|
448
|
+
let specifierName;
|
449
|
+
|
450
|
+
if (options.createRequire === true) {
|
451
|
+
moduleName = "module";
|
452
|
+
specifierName = "createRequire";
|
453
|
+
} else {
|
454
|
+
const match = /^(.*) from (.*)$/.exec(options.createRequire);
|
455
|
+
if (match) {
|
456
|
+
[, specifierName, moduleName] = match;
|
457
|
+
}
|
458
|
+
if (!specifierName || !moduleName) {
|
459
|
+
const err = new WebpackError(
|
460
|
+
`Parsing javascript parser option "createRequire" failed, got ${JSON.stringify(
|
461
|
+
options.createRequire
|
462
|
+
)}`
|
463
|
+
);
|
464
|
+
err.details =
|
465
|
+
'Expected string in format "createRequire from module", where "createRequire" is specifier name and "module" name of the module';
|
466
|
+
throw err;
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
tapRequireExpressionTag(createdRequireIdentifierTag);
|
471
|
+
tapRequireExpressionTag(createRequireSpecifierTag);
|
472
|
+
parser.hooks.evaluateCallExpression
|
473
|
+
.for(createRequireSpecifierTag)
|
474
|
+
.tap("CommonJsImportsParserPlugin", expr => {
|
475
|
+
const context = parseCreateRequireArguments(expr);
|
476
|
+
if (context === undefined) return;
|
477
|
+
const ident = parser.evaluatedVariable({
|
478
|
+
tag: createdRequireIdentifierTag,
|
479
|
+
data: { context },
|
480
|
+
next: undefined
|
481
|
+
});
|
482
|
+
return new BasicEvaluatedExpression()
|
483
|
+
.setIdentifier(ident, ident, () => [])
|
484
|
+
.setSideEffects(false)
|
485
|
+
.setRange(expr.range);
|
486
|
+
});
|
487
|
+
parser.hooks.unhandledExpressionMemberChain
|
488
|
+
.for(createdRequireIdentifierTag)
|
489
|
+
.tap("CommonJsImportsParserPlugin", (expr, members) => {
|
490
|
+
return expressionIsUnsupported(
|
491
|
+
parser,
|
492
|
+
`createRequire().${members.join(".")} is not supported by webpack.`
|
493
|
+
)(expr);
|
494
|
+
});
|
495
|
+
parser.hooks.canRename
|
496
|
+
.for(createdRequireIdentifierTag)
|
497
|
+
.tap("CommonJsImportsParserPlugin", () => true);
|
498
|
+
parser.hooks.canRename
|
499
|
+
.for(createRequireSpecifierTag)
|
500
|
+
.tap("CommonJsImportsParserPlugin", () => true);
|
501
|
+
parser.hooks.rename
|
502
|
+
.for(createRequireSpecifierTag)
|
503
|
+
.tap("CommonJsImportsParserPlugin", defineUndefined);
|
504
|
+
parser.hooks.expression
|
505
|
+
.for(createdRequireIdentifierTag)
|
506
|
+
.tap("CommonJsImportsParserPlugin", requireAsExpressionHandler);
|
507
|
+
parser.hooks.call
|
508
|
+
.for(createdRequireIdentifierTag)
|
509
|
+
.tap("CommonJsImportsParserPlugin", createRequireHandler(false));
|
510
|
+
/**
|
511
|
+
* @param {CallExpressionNode} expr call expression
|
512
|
+
* @returns {string} context
|
513
|
+
*/
|
514
|
+
const parseCreateRequireArguments = expr => {
|
515
|
+
const args = expr.arguments;
|
516
|
+
if (args.length !== 1) {
|
517
|
+
const err = new WebpackError(
|
518
|
+
"module.createRequire supports only one argument."
|
519
|
+
);
|
520
|
+
err.loc = expr.loc;
|
521
|
+
parser.state.module.addWarning(err);
|
522
|
+
return;
|
523
|
+
}
|
524
|
+
const arg = args[0];
|
525
|
+
const evaluated = parser.evaluateExpression(arg);
|
526
|
+
if (!evaluated.isString()) {
|
527
|
+
const err = new WebpackError(
|
528
|
+
"module.createRequire failed parsing argument."
|
529
|
+
);
|
530
|
+
err.loc = arg.loc;
|
531
|
+
parser.state.module.addWarning(err);
|
532
|
+
return;
|
533
|
+
}
|
534
|
+
const ctx = evaluated.string.startsWith("file://")
|
535
|
+
? fileURLToPath(evaluated.string)
|
536
|
+
: evaluated.string;
|
537
|
+
// argument always should be a filename
|
538
|
+
return ctx.slice(0, ctx.lastIndexOf(ctx.startsWith("/") ? "/" : "\\"));
|
539
|
+
};
|
540
|
+
|
541
|
+
parser.hooks.import.tap(
|
542
|
+
{
|
543
|
+
name: "CommonJsImportsParserPlugin",
|
544
|
+
stage: -10
|
545
|
+
},
|
546
|
+
(statement, source) => {
|
547
|
+
if (
|
548
|
+
source !== moduleName ||
|
549
|
+
statement.specifiers.length !== 1 ||
|
550
|
+
statement.specifiers[0].type !== "ImportSpecifier" ||
|
551
|
+
statement.specifiers[0].imported.type !== "Identifier" ||
|
552
|
+
statement.specifiers[0].imported.name !== specifierName
|
553
|
+
)
|
554
|
+
return;
|
555
|
+
// clear for 'import { createRequire as x } from "module"'
|
556
|
+
// if any other specifier was used import module
|
557
|
+
const clearDep = new ConstDependency(
|
558
|
+
parser.isAsiPosition(statement.range[0]) ? ";" : "",
|
559
|
+
statement.range
|
560
|
+
);
|
561
|
+
clearDep.loc = statement.loc;
|
562
|
+
parser.state.module.addPresentationalDependency(clearDep);
|
563
|
+
parser.unsetAsiPosition(statement.range[1]);
|
564
|
+
return true;
|
565
|
+
}
|
566
|
+
);
|
567
|
+
parser.hooks.importSpecifier.tap(
|
568
|
+
{
|
569
|
+
name: "CommonJsImportsParserPlugin",
|
570
|
+
stage: -10
|
571
|
+
},
|
572
|
+
(statement, source, id, name) => {
|
573
|
+
if (source !== moduleName || id !== specifierName) return;
|
574
|
+
parser.tagVariable(name, createRequireSpecifierTag);
|
575
|
+
return true;
|
576
|
+
}
|
577
|
+
);
|
578
|
+
parser.hooks.preDeclarator.tap(
|
579
|
+
"CommonJsImportsParserPlugin",
|
580
|
+
declarator => {
|
581
|
+
if (
|
582
|
+
declarator.id.type !== "Identifier" ||
|
583
|
+
!declarator.init ||
|
584
|
+
declarator.init.type !== "CallExpression" ||
|
585
|
+
declarator.init.callee.type !== "Identifier"
|
586
|
+
)
|
587
|
+
return;
|
588
|
+
const variableInfo = parser.getVariableInfo(
|
589
|
+
declarator.init.callee.name
|
590
|
+
);
|
591
|
+
if (
|
592
|
+
variableInfo &&
|
593
|
+
variableInfo.tagInfo &&
|
594
|
+
variableInfo.tagInfo.tag === createRequireSpecifierTag
|
595
|
+
) {
|
596
|
+
const context = parseCreateRequireArguments(declarator.init);
|
597
|
+
if (context === undefined) return;
|
598
|
+
parser.tagVariable(declarator.id.name, createdRequireIdentifierTag, {
|
599
|
+
name: declarator.id.name,
|
600
|
+
context
|
601
|
+
});
|
602
|
+
return true;
|
603
|
+
}
|
604
|
+
}
|
605
|
+
);
|
606
|
+
|
607
|
+
parser.hooks.memberChainOfCallMemberChain
|
608
|
+
.for(createRequireSpecifierTag)
|
609
|
+
.tap(
|
610
|
+
"CommonJsImportsParserPlugin",
|
611
|
+
(expr, calleeMembers, callExpr, members) => {
|
612
|
+
if (
|
613
|
+
calleeMembers.length !== 0 ||
|
614
|
+
members.length !== 1 ||
|
615
|
+
members[0] !== "cache"
|
616
|
+
)
|
617
|
+
return;
|
618
|
+
// createRequire().cache
|
619
|
+
const context = parseCreateRequireArguments(callExpr);
|
620
|
+
if (context === undefined) return;
|
621
|
+
return requireCache(expr);
|
622
|
+
}
|
623
|
+
);
|
624
|
+
parser.hooks.callMemberChainOfCallMemberChain
|
625
|
+
.for(createRequireSpecifierTag)
|
626
|
+
.tap(
|
627
|
+
"CommonJsImportsParserPlugin",
|
628
|
+
(expr, calleeMembers, innerCallExpression, members) => {
|
629
|
+
if (
|
630
|
+
calleeMembers.length !== 0 ||
|
631
|
+
members.length !== 1 ||
|
632
|
+
members[0] !== "resolve"
|
633
|
+
)
|
634
|
+
return;
|
635
|
+
// createRequire().resolve()
|
636
|
+
return processResolve(expr, false);
|
637
|
+
}
|
638
|
+
);
|
639
|
+
parser.hooks.expressionMemberChain
|
640
|
+
.for(createdRequireIdentifierTag)
|
641
|
+
.tap("CommonJsImportsParserPlugin", (expr, members) => {
|
642
|
+
// require.cache
|
643
|
+
if (members.length === 1 && members[0] === "cache") {
|
644
|
+
return requireCache(expr);
|
645
|
+
}
|
646
|
+
});
|
647
|
+
parser.hooks.callMemberChain
|
648
|
+
.for(createdRequireIdentifierTag)
|
649
|
+
.tap("CommonJsImportsParserPlugin", (expr, members) => {
|
650
|
+
// require.resolve()
|
651
|
+
if (members.length === 1 && members[0] === "resolve") {
|
652
|
+
return processResolve(expr, false);
|
653
|
+
}
|
654
|
+
});
|
655
|
+
parser.hooks.call
|
656
|
+
.for(createRequireSpecifierTag)
|
657
|
+
.tap("CommonJsImportsParserPlugin", expr => {
|
658
|
+
const clearDep = new ConstDependency(
|
659
|
+
"/* createRequire() */ undefined",
|
660
|
+
expr.range
|
661
|
+
);
|
662
|
+
clearDep.loc = expr.loc;
|
663
|
+
parser.state.module.addPresentationalDependency(clearDep);
|
664
|
+
return true;
|
665
|
+
});
|
666
|
+
//#endregion
|
384
667
|
}
|
385
668
|
}
|
386
669
|
module.exports = CommonJsImportsParserPlugin;
|
@@ -10,11 +10,13 @@ const ContextDependency = require("./ContextDependency");
|
|
10
10
|
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
|
11
11
|
|
12
12
|
class CommonJsRequireContextDependency extends ContextDependency {
|
13
|
-
constructor(options, range, valueRange) {
|
14
|
-
super(options);
|
13
|
+
constructor(options, range, valueRange, inShorthand, context) {
|
14
|
+
super(options, context);
|
15
15
|
|
16
16
|
this.range = range;
|
17
17
|
this.valueRange = valueRange;
|
18
|
+
// inShorthand must be serialized by subclasses that use it
|
19
|
+
this.inShorthand = inShorthand;
|
18
20
|
}
|
19
21
|
|
20
22
|
get type() {
|
@@ -26,6 +28,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|
26
28
|
|
27
29
|
write(this.range);
|
28
30
|
write(this.valueRange);
|
31
|
+
write(this.inShorthand);
|
29
32
|
|
30
33
|
super.serialize(context);
|
31
34
|
}
|
@@ -35,6 +38,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|
35
38
|
|
36
39
|
this.range = read();
|
37
40
|
this.valueRange = read();
|
41
|
+
this.inShorthand = read();
|
38
42
|
|
39
43
|
super.deserialize(context);
|
40
44
|
}
|
@@ -10,9 +10,10 @@ const ModuleDependency = require("./ModuleDependency");
|
|
10
10
|
const ModuleDependencyTemplateAsId = require("./ModuleDependencyTemplateAsId");
|
11
11
|
|
12
12
|
class CommonJsRequireDependency extends ModuleDependency {
|
13
|
-
constructor(request, range) {
|
13
|
+
constructor(request, range, context) {
|
14
14
|
super(request);
|
15
15
|
this.range = range;
|
16
|
+
this._context = context;
|
16
17
|
}
|
17
18
|
|
18
19
|
get type() {
|
@@ -26,8 +26,9 @@ const regExpToString = r => (r ? r + "" : "");
|
|
26
26
|
class ContextDependency extends Dependency {
|
27
27
|
/**
|
28
28
|
* @param {ContextDependencyOptions} options options for the context module
|
29
|
+
* @param {string=} context request context
|
29
30
|
*/
|
30
|
-
constructor(options) {
|
31
|
+
constructor(options, context) {
|
31
32
|
super();
|
32
33
|
|
33
34
|
this.options = options;
|
@@ -47,8 +48,17 @@ class ContextDependency extends Dependency {
|
|
47
48
|
this.request = undefined;
|
48
49
|
this.range = undefined;
|
49
50
|
this.valueRange = undefined;
|
51
|
+
this.inShorthand = undefined;
|
50
52
|
// TODO refactor this
|
51
53
|
this.replaces = undefined;
|
54
|
+
this._requestContext = context;
|
55
|
+
}
|
56
|
+
|
57
|
+
/**
|
58
|
+
* @returns {string | undefined} a request context
|
59
|
+
*/
|
60
|
+
getContext() {
|
61
|
+
return this._requestContext;
|
52
62
|
}
|
53
63
|
|
54
64
|
get category() {
|
@@ -67,7 +77,9 @@ class ContextDependency extends Dependency {
|
|
67
77
|
*/
|
68
78
|
getResourceIdentifier() {
|
69
79
|
return (
|
70
|
-
`context${this.
|
80
|
+
`context${this._requestContext || ""}|ctx request${
|
81
|
+
this.options.request
|
82
|
+
} ${this.options.recursive} ` +
|
71
83
|
`${regExpToString(this.options.regExp)} ${regExpToString(
|
72
84
|
this.options.include
|
73
85
|
)} ${regExpToString(this.options.exclude)} ` +
|
@@ -111,6 +123,7 @@ class ContextDependency extends Dependency {
|
|
111
123
|
write(this.critical);
|
112
124
|
write(this.hadGlobalOrStickyRegExp);
|
113
125
|
write(this.request);
|
126
|
+
write(this._requestContext);
|
114
127
|
write(this.range);
|
115
128
|
write(this.valueRange);
|
116
129
|
write(this.prepend);
|
@@ -127,6 +140,7 @@ class ContextDependency extends Dependency {
|
|
127
140
|
this.critical = read();
|
128
141
|
this.hadGlobalOrStickyRegExp = read();
|
129
142
|
this.request = read();
|
143
|
+
this._requestContext = read();
|
130
144
|
this.range = read();
|
131
145
|
this.valueRange = read();
|
132
146
|
this.prepend = read();
|