@eriche/webpack 4.47.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.
- package/LICENSE +20 -0
- package/README.md +784 -0
- package/SECURITY.md +9 -0
- package/bin/webpack.js +171 -0
- package/buildin/amd-define.js +3 -0
- package/buildin/amd-options.js +2 -0
- package/buildin/global.js +20 -0
- package/buildin/harmony-module.js +24 -0
- package/buildin/module.js +22 -0
- package/buildin/system.js +7 -0
- package/declarations/WebpackOptions.d.ts +1466 -0
- package/declarations/plugins/BannerPlugin.d.ts +49 -0
- package/declarations/plugins/DllPlugin.d.ts +32 -0
- package/declarations/plugins/DllReferencePlugin.d.ts +126 -0
- package/declarations/plugins/HashedModuleIdsPlugin.d.ts +24 -0
- package/declarations/plugins/IgnorePlugin.d.ts +27 -0
- package/declarations/plugins/LoaderOptionsPlugin.d.ts +27 -0
- package/declarations/plugins/ProgressPlugin.d.ts +42 -0
- package/declarations/plugins/SourceMapDevToolPlugin.d.ts +94 -0
- package/declarations/plugins/WatchIgnorePlugin.d.ts +10 -0
- package/declarations/plugins/debug/ProfilingPlugin.d.ts +12 -0
- package/declarations/plugins/optimize/AggressiveSplittingPlugin.d.ts +24 -0
- package/declarations/plugins/optimize/LimitChunkCountPlugin.d.ts +16 -0
- package/declarations/plugins/optimize/MinChunkSizePlugin.d.ts +12 -0
- package/declarations/plugins/optimize/OccurrenceOrderChunkIdsPlugin.d.ts +12 -0
- package/declarations/plugins/optimize/OccurrenceOrderModuleIdsPlugin.d.ts +12 -0
- package/hot/dev-server.js +61 -0
- package/hot/emitter.js +2 -0
- package/hot/log-apply-result.js +44 -0
- package/hot/log.js +59 -0
- package/hot/only-dev-server.js +102 -0
- package/hot/poll.js +37 -0
- package/hot/signal.js +62 -0
- package/lib/APIPlugin.js +84 -0
- package/lib/AbstractMethodError.js +43 -0
- package/lib/AmdMainTemplatePlugin.js +106 -0
- package/lib/AsyncDependenciesBlock.js +110 -0
- package/lib/AsyncDependencyToInitialChunkError.js +31 -0
- package/lib/AutomaticPrefetchPlugin.js +57 -0
- package/lib/BannerPlugin.js +122 -0
- package/lib/BasicEvaluatedExpression.js +248 -0
- package/lib/CachePlugin.js +100 -0
- package/lib/CaseSensitiveModulesWarning.js +67 -0
- package/lib/Chunk.js +875 -0
- package/lib/ChunkGroup.js +513 -0
- package/lib/ChunkRenderError.js +32 -0
- package/lib/ChunkTemplate.js +87 -0
- package/lib/CommentCompilationWarning.js +32 -0
- package/lib/CommonJsStuffPlugin.js +116 -0
- package/lib/CompatibilityPlugin.js +70 -0
- package/lib/Compilation.js +2327 -0
- package/lib/Compiler.js +735 -0
- package/lib/ConcurrentCompilationError.js +19 -0
- package/lib/ConstPlugin.js +348 -0
- package/lib/ContextExclusionPlugin.js +28 -0
- package/lib/ContextModule.js +872 -0
- package/lib/ContextModuleFactory.js +262 -0
- package/lib/ContextReplacementPlugin.js +133 -0
- package/lib/DefinePlugin.js +289 -0
- package/lib/DelegatedModule.js +114 -0
- package/lib/DelegatedModuleFactoryPlugin.js +95 -0
- package/lib/DelegatedPlugin.js +39 -0
- package/lib/DependenciesBlock.js +124 -0
- package/lib/DependenciesBlockVariable.js +72 -0
- package/lib/Dependency.js +89 -0
- package/lib/DllEntryPlugin.js +54 -0
- package/lib/DllModule.js +60 -0
- package/lib/DllModuleFactory.js +29 -0
- package/lib/DllPlugin.js +49 -0
- package/lib/DllReferencePlugin.js +156 -0
- package/lib/DynamicEntryPlugin.js +94 -0
- package/lib/EntryModuleNotFoundError.js +21 -0
- package/lib/EntryOptionPlugin.js +46 -0
- package/lib/Entrypoint.js +64 -0
- package/lib/EnvironmentPlugin.js +72 -0
- package/lib/ErrorHelpers.js +60 -0
- package/lib/EvalDevToolModulePlugin.js +27 -0
- package/lib/EvalDevToolModuleTemplatePlugin.js +61 -0
- package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +120 -0
- package/lib/EvalSourceMapDevToolPlugin.js +41 -0
- package/lib/ExportPropertyMainTemplatePlugin.js +53 -0
- package/lib/ExtendedAPIPlugin.js +88 -0
- package/lib/ExternalModule.js +179 -0
- package/lib/ExternalModuleFactoryPlugin.js +110 -0
- package/lib/ExternalsPlugin.js +23 -0
- package/lib/FlagAllModulesAsUsedPlugin.js +38 -0
- package/lib/FlagDependencyExportsPlugin.js +174 -0
- package/lib/FlagDependencyUsagePlugin.js +116 -0
- package/lib/FlagInitialModulesAsUsedPlugin.js +36 -0
- package/lib/FunctionModulePlugin.js +19 -0
- package/lib/FunctionModuleTemplatePlugin.js +102 -0
- package/lib/Generator.js +60 -0
- package/lib/GraphHelpers.js +65 -0
- package/lib/HarmonyLinkingError.js +17 -0
- package/lib/HashedModuleIdsPlugin.js +63 -0
- package/lib/HotModuleReplacement.runtime.js +721 -0
- package/lib/HotModuleReplacementPlugin.js +425 -0
- package/lib/HotUpdateChunk.js +17 -0
- package/lib/HotUpdateChunkTemplate.js +78 -0
- package/lib/IgnorePlugin.js +91 -0
- package/lib/JavascriptGenerator.js +229 -0
- package/lib/JavascriptModulesPlugin.js +185 -0
- package/lib/JsonGenerator.js +57 -0
- package/lib/JsonModulesPlugin.js +30 -0
- package/lib/JsonParser.js +27 -0
- package/lib/LibManifestPlugin.js +90 -0
- package/lib/LibraryTemplatePlugin.js +186 -0
- package/lib/LoaderOptionsPlugin.js +58 -0
- package/lib/LoaderTargetPlugin.js +24 -0
- package/lib/MainTemplate.js +568 -0
- package/lib/MemoryOutputFileSystem.js +5 -0
- package/lib/Module.js +435 -0
- package/lib/ModuleBuildError.js +52 -0
- package/lib/ModuleDependencyError.js +35 -0
- package/lib/ModuleDependencyWarning.js +25 -0
- package/lib/ModuleError.js +36 -0
- package/lib/ModuleFilenameHelpers.js +179 -0
- package/lib/ModuleNotFoundError.js +23 -0
- package/lib/ModuleParseError.js +67 -0
- package/lib/ModuleReason.js +48 -0
- package/lib/ModuleTemplate.js +93 -0
- package/lib/ModuleWarning.js +36 -0
- package/lib/MultiCompiler.js +290 -0
- package/lib/MultiEntryPlugin.js +80 -0
- package/lib/MultiModule.js +87 -0
- package/lib/MultiModuleFactory.js +23 -0
- package/lib/MultiStats.js +92 -0
- package/lib/MultiWatching.js +50 -0
- package/lib/NamedChunksPlugin.js +29 -0
- package/lib/NamedModulesPlugin.js +58 -0
- package/lib/NoEmitOnErrorsPlugin.js +20 -0
- package/lib/NoModeWarning.js +23 -0
- package/lib/NodeStuffPlugin.js +118 -0
- package/lib/NormalModule.js +582 -0
- package/lib/NormalModuleFactory.js +528 -0
- package/lib/NormalModuleReplacementPlugin.js +51 -0
- package/lib/NullFactory.js +12 -0
- package/lib/OptionsApply.js +10 -0
- package/lib/OptionsDefaulter.js +141 -0
- package/lib/Parser.js +2454 -0
- package/lib/ParserHelpers.js +103 -0
- package/lib/PrefetchPlugin.js +37 -0
- package/lib/ProgressPlugin.js +307 -0
- package/lib/ProvidePlugin.js +86 -0
- package/lib/RawModule.js +56 -0
- package/lib/RecordIdsPlugin.js +236 -0
- package/lib/RemovedPluginError.js +11 -0
- package/lib/RequestShortener.js +83 -0
- package/lib/RequireJsStuffPlugin.js +66 -0
- package/lib/ResolverFactory.js +74 -0
- package/lib/RuleSet.js +567 -0
- package/lib/RuntimeTemplate.js +336 -0
- package/lib/SetVarMainTemplatePlugin.js +69 -0
- package/lib/SingleEntryPlugin.js +63 -0
- package/lib/SizeFormatHelpers.js +24 -0
- package/lib/SourceMapDevToolModuleOptionsPlugin.js +49 -0
- package/lib/SourceMapDevToolPlugin.js +418 -0
- package/lib/Stats.js +1685 -0
- package/lib/SystemMainTemplatePlugin.js +137 -0
- package/lib/Template.js +291 -0
- package/lib/TemplatedPathPlugin.js +188 -0
- package/lib/UmdMainTemplatePlugin.js +305 -0
- package/lib/UnsupportedFeatureWarning.js +30 -0
- package/lib/UseStrictPlugin.js +54 -0
- package/lib/WarnCaseSensitiveModulesPlugin.js +37 -0
- package/lib/WarnNoModeSetPlugin.js +17 -0
- package/lib/WatchIgnorePlugin.js +106 -0
- package/lib/Watching.js +211 -0
- package/lib/WebpackError.js +31 -0
- package/lib/WebpackOptionsApply.js +546 -0
- package/lib/WebpackOptionsDefaulter.js +391 -0
- package/lib/WebpackOptionsValidationError.js +390 -0
- package/lib/buildChunkGraph.js +712 -0
- package/lib/compareLocations.js +52 -0
- package/lib/debug/ProfilingPlugin.js +442 -0
- package/lib/dependencies/AMDDefineDependency.js +137 -0
- package/lib/dependencies/AMDDefineDependencyParserPlugin.js +336 -0
- package/lib/dependencies/AMDPlugin.js +249 -0
- package/lib/dependencies/AMDRequireArrayDependency.js +49 -0
- package/lib/dependencies/AMDRequireContextDependency.js +20 -0
- package/lib/dependencies/AMDRequireDependenciesBlock.js +47 -0
- package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +274 -0
- package/lib/dependencies/AMDRequireDependency.js +135 -0
- package/lib/dependencies/AMDRequireItemDependency.js +22 -0
- package/lib/dependencies/CommonJsPlugin.js +158 -0
- package/lib/dependencies/CommonJsRequireContextDependency.js +23 -0
- package/lib/dependencies/CommonJsRequireDependency.js +22 -0
- package/lib/dependencies/CommonJsRequireDependencyParserPlugin.js +138 -0
- package/lib/dependencies/ConstDependency.js +33 -0
- package/lib/dependencies/ContextDependency.js +68 -0
- package/lib/dependencies/ContextDependencyHelpers.js +232 -0
- package/lib/dependencies/ContextDependencyTemplateAsId.js +43 -0
- package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +39 -0
- package/lib/dependencies/ContextElementDependency.js +21 -0
- package/lib/dependencies/CriticalDependencyWarning.js +20 -0
- package/lib/dependencies/DelegatedExportsDependency.js +33 -0
- package/lib/dependencies/DelegatedSourceDependency.js +18 -0
- package/lib/dependencies/DependencyReference.js +64 -0
- package/lib/dependencies/DllEntryDependency.js +20 -0
- package/lib/dependencies/HarmonyAcceptDependency.js +48 -0
- package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -0
- package/lib/dependencies/HarmonyCompatibilityDependency.js +31 -0
- package/lib/dependencies/HarmonyDetectionParserPlugin.js +96 -0
- package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +154 -0
- package/lib/dependencies/HarmonyExportExpressionDependency.js +58 -0
- package/lib/dependencies/HarmonyExportHeaderDependency.js +30 -0
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +659 -0
- package/lib/dependencies/HarmonyExportSpecifierDependency.js +54 -0
- package/lib/dependencies/HarmonyImportDependency.js +109 -0
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +222 -0
- package/lib/dependencies/HarmonyImportSideEffectDependency.js +31 -0
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +163 -0
- package/lib/dependencies/HarmonyInitDependency.js +60 -0
- package/lib/dependencies/HarmonyModulesPlugin.js +143 -0
- package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +26 -0
- package/lib/dependencies/ImportContextDependency.js +23 -0
- package/lib/dependencies/ImportDependenciesBlock.js +18 -0
- package/lib/dependencies/ImportDependency.js +34 -0
- package/lib/dependencies/ImportEagerDependency.js +32 -0
- package/lib/dependencies/ImportParserPlugin.js +248 -0
- package/lib/dependencies/ImportPlugin.js +79 -0
- package/lib/dependencies/ImportWeakDependency.js +34 -0
- package/lib/dependencies/JsonExportsDependency.js +26 -0
- package/lib/dependencies/LoaderDependency.js +21 -0
- package/lib/dependencies/LoaderPlugin.js +115 -0
- package/lib/dependencies/LocalModule.js +23 -0
- package/lib/dependencies/LocalModuleDependency.js +28 -0
- package/lib/dependencies/LocalModulesHelpers.js +52 -0
- package/lib/dependencies/ModuleDependency.js +23 -0
- package/lib/dependencies/ModuleDependencyTemplateAsId.js +17 -0
- package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +17 -0
- package/lib/dependencies/ModuleHotAcceptDependency.js +23 -0
- package/lib/dependencies/ModuleHotDeclineDependency.js +23 -0
- package/lib/dependencies/MultiEntryDependency.js +25 -0
- package/lib/dependencies/NullDependency.js +20 -0
- package/lib/dependencies/PrefetchDependency.js +18 -0
- package/lib/dependencies/RequireContextDependency.js +22 -0
- package/lib/dependencies/RequireContextDependencyParserPlugin.js +56 -0
- package/lib/dependencies/RequireContextPlugin.js +143 -0
- package/lib/dependencies/RequireEnsureDependenciesBlock.js +33 -0
- package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +116 -0
- package/lib/dependencies/RequireEnsureDependency.js +58 -0
- package/lib/dependencies/RequireEnsureItemDependency.js +21 -0
- package/lib/dependencies/RequireEnsurePlugin.js +74 -0
- package/lib/dependencies/RequireHeaderDependency.js +26 -0
- package/lib/dependencies/RequireIncludeDependency.js +39 -0
- package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +23 -0
- package/lib/dependencies/RequireIncludePlugin.js +61 -0
- package/lib/dependencies/RequireResolveContextDependency.js +23 -0
- package/lib/dependencies/RequireResolveDependency.js +22 -0
- package/lib/dependencies/RequireResolveDependencyParserPlugin.js +86 -0
- package/lib/dependencies/RequireResolveHeaderDependency.js +26 -0
- package/lib/dependencies/SingleEntryDependency.js +21 -0
- package/lib/dependencies/SystemPlugin.js +122 -0
- package/lib/dependencies/UnsupportedDependency.js +27 -0
- package/lib/dependencies/WebAssemblyExportImportedDependency.js +31 -0
- package/lib/dependencies/WebAssemblyImportDependency.js +54 -0
- package/lib/dependencies/WebpackMissingModule.js +20 -0
- package/lib/dependencies/getFunctionExpression.js +52 -0
- package/lib/formatLocation.js +75 -0
- package/lib/logging/Logger.js +128 -0
- package/lib/logging/createConsoleLogger.js +210 -0
- package/lib/logging/runtime.js +36 -0
- package/lib/logging/truncateArgs.js +76 -0
- package/lib/node/NodeChunkTemplatePlugin.js +31 -0
- package/lib/node/NodeEnvironmentPlugin.js +44 -0
- package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -0
- package/lib/node/NodeMainTemplate.runtime.js +32 -0
- package/lib/node/NodeMainTemplateAsync.runtime.js +50 -0
- package/lib/node/NodeMainTemplatePlugin.js +321 -0
- package/lib/node/NodeOutputFileSystem.js +22 -0
- package/lib/node/NodeSourcePlugin.js +141 -0
- package/lib/node/NodeTargetPlugin.js +19 -0
- package/lib/node/NodeTemplatePlugin.js +31 -0
- package/lib/node/NodeWatchFileSystem.js +109 -0
- package/lib/node/ReadFileCompileWasmTemplatePlugin.js +61 -0
- package/lib/node/nodeConsole.js +135 -0
- package/lib/optimize/AggressiveMergingPlugin.js +87 -0
- package/lib/optimize/AggressiveSplittingPlugin.js +294 -0
- package/lib/optimize/ChunkModuleIdRangePlugin.js +66 -0
- package/lib/optimize/ConcatenatedModule.js +1477 -0
- package/lib/optimize/EnsureChunkConditionsPlugin.js +70 -0
- package/lib/optimize/FlagIncludedChunksPlugin.js +99 -0
- package/lib/optimize/LimitChunkCountPlugin.js +231 -0
- package/lib/optimize/MergeDuplicateChunksPlugin.js +78 -0
- package/lib/optimize/MinChunkSizePlugin.js +82 -0
- package/lib/optimize/MinMaxSizeWarning.js +29 -0
- package/lib/optimize/ModuleConcatenationPlugin.js +485 -0
- package/lib/optimize/NaturalChunkOrderPlugin.js +41 -0
- package/lib/optimize/OccurrenceChunkOrderPlugin.js +66 -0
- package/lib/optimize/OccurrenceModuleOrderPlugin.js +112 -0
- package/lib/optimize/OccurrenceOrderPlugin.js +135 -0
- package/lib/optimize/RemoveEmptyChunksPlugin.js +42 -0
- package/lib/optimize/RemoveParentModulesPlugin.js +127 -0
- package/lib/optimize/RuntimeChunkPlugin.js +41 -0
- package/lib/optimize/SideEffectsFlagPlugin.js +352 -0
- package/lib/optimize/SplitChunksPlugin.js +971 -0
- package/lib/performance/AssetsOverSizeLimitWarning.js +30 -0
- package/lib/performance/EntrypointsOverSizeLimitWarning.js +30 -0
- package/lib/performance/NoAsyncChunksWarning.js +21 -0
- package/lib/performance/SizeLimitsPlugin.js +125 -0
- package/lib/util/LazyBucketSortedSet.js +235 -0
- package/lib/util/Queue.js +46 -0
- package/lib/util/Semaphore.js +53 -0
- package/lib/util/SetHelpers.js +48 -0
- package/lib/util/SortableSet.js +140 -0
- package/lib/util/StackedSetMap.js +142 -0
- package/lib/util/TrackingSet.js +35 -0
- package/lib/util/cachedMerge.js +35 -0
- package/lib/util/cleverMerge.js +77 -0
- package/lib/util/createHash.js +174 -0
- package/lib/util/deterministicGrouping.js +274 -0
- package/lib/util/hash/BatchedHash.js +71 -0
- package/lib/util/hash/md4.js +25 -0
- package/lib/util/hash/wasm-hash.js +174 -0
- package/lib/util/identifier.js +127 -0
- package/lib/util/objectToMap.js +16 -0
- package/lib/validateSchema.js +67 -0
- package/lib/wasm/UnsupportedWebAssemblyFeatureError.js +17 -0
- package/lib/wasm/WasmFinalizeExportsPlugin.js +69 -0
- package/lib/wasm/WasmMainTemplatePlugin.js +341 -0
- package/lib/wasm/WebAssemblyGenerator.js +458 -0
- package/lib/wasm/WebAssemblyInInitialChunkError.js +88 -0
- package/lib/wasm/WebAssemblyJavascriptGenerator.js +152 -0
- package/lib/wasm/WebAssemblyModulesPlugin.js +128 -0
- package/lib/wasm/WebAssemblyParser.js +175 -0
- package/lib/wasm/WebAssemblyUtils.js +59 -0
- package/lib/web/FetchCompileWasmTemplatePlugin.js +37 -0
- package/lib/web/JsonpChunkTemplatePlugin.js +71 -0
- package/lib/web/JsonpExportMainTemplatePlugin.js +50 -0
- package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -0
- package/lib/web/JsonpMainTemplate.runtime.js +71 -0
- package/lib/web/JsonpMainTemplatePlugin.js +615 -0
- package/lib/web/JsonpTemplatePlugin.js +23 -0
- package/lib/web/WebEnvironmentPlugin.js +18 -0
- package/lib/webpack.js +206 -0
- package/lib/webpack.web.js +31 -0
- package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -0
- package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -0
- package/lib/webworker/WebWorkerMainTemplate.runtime.js +73 -0
- package/lib/webworker/WebWorkerMainTemplatePlugin.js +192 -0
- package/lib/webworker/WebWorkerTemplatePlugin.js +25 -0
- package/package.json +211 -0
- package/schemas/WebpackOptions.json +2285 -0
- package/schemas/ajv.absolutePath.js +57 -0
- package/schemas/plugins/BannerPlugin.json +101 -0
- package/schemas/plugins/DllPlugin.json +36 -0
- package/schemas/plugins/DllReferencePlugin.json +205 -0
- package/schemas/plugins/HashedModuleIdsPlugin.json +26 -0
- package/schemas/plugins/IgnorePlugin.json +37 -0
- package/schemas/plugins/LoaderOptionsPlugin.json +27 -0
- package/schemas/plugins/ProgressPlugin.json +52 -0
- package/schemas/plugins/SourceMapDevToolPlugin.json +185 -0
- package/schemas/plugins/WatchIgnorePlugin.json +18 -0
- package/schemas/plugins/debug/ProfilingPlugin.json +13 -0
- package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +23 -0
- package/schemas/plugins/optimize/LimitChunkCountPlugin.json +16 -0
- package/schemas/plugins/optimize/MinChunkSizePlugin.json +12 -0
- package/schemas/plugins/optimize/OccurrenceOrderChunkIdsPlugin.json +11 -0
- package/schemas/plugins/optimize/OccurrenceOrderModuleIdsPlugin.json +11 -0
- package/web_modules/node-libs-browser.js +0 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const asyncLib = require("neo-async");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
Tapable,
|
|
12
|
+
AsyncSeriesWaterfallHook,
|
|
13
|
+
SyncWaterfallHook
|
|
14
|
+
} = require("tapable");
|
|
15
|
+
const ContextModule = require("./ContextModule");
|
|
16
|
+
const ContextElementDependency = require("./dependencies/ContextElementDependency");
|
|
17
|
+
|
|
18
|
+
/** @typedef {import("./Module")} Module */
|
|
19
|
+
|
|
20
|
+
const EMPTY_RESOLVE_OPTIONS = {};
|
|
21
|
+
|
|
22
|
+
module.exports = class ContextModuleFactory extends Tapable {
|
|
23
|
+
constructor(resolverFactory) {
|
|
24
|
+
super();
|
|
25
|
+
this.hooks = {
|
|
26
|
+
/** @type {AsyncSeriesWaterfallHook<TODO>} */
|
|
27
|
+
beforeResolve: new AsyncSeriesWaterfallHook(["data"]),
|
|
28
|
+
/** @type {AsyncSeriesWaterfallHook<TODO>} */
|
|
29
|
+
afterResolve: new AsyncSeriesWaterfallHook(["data"]),
|
|
30
|
+
/** @type {SyncWaterfallHook<string[]>} */
|
|
31
|
+
contextModuleFiles: new SyncWaterfallHook(["files"]),
|
|
32
|
+
/** @type {SyncWaterfallHook<TODO[]>} */
|
|
33
|
+
alternatives: new AsyncSeriesWaterfallHook(["modules"])
|
|
34
|
+
};
|
|
35
|
+
this._pluginCompat.tap("ContextModuleFactory", options => {
|
|
36
|
+
switch (options.name) {
|
|
37
|
+
case "before-resolve":
|
|
38
|
+
case "after-resolve":
|
|
39
|
+
case "alternatives":
|
|
40
|
+
options.async = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
this.resolverFactory = resolverFactory;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
create(data, callback) {
|
|
48
|
+
const context = data.context;
|
|
49
|
+
const dependencies = data.dependencies;
|
|
50
|
+
const resolveOptions = data.resolveOptions;
|
|
51
|
+
const dependency = dependencies[0];
|
|
52
|
+
this.hooks.beforeResolve.callAsync(
|
|
53
|
+
Object.assign(
|
|
54
|
+
{
|
|
55
|
+
context: context,
|
|
56
|
+
dependencies: dependencies,
|
|
57
|
+
resolveOptions
|
|
58
|
+
},
|
|
59
|
+
dependency.options
|
|
60
|
+
),
|
|
61
|
+
(err, beforeResolveResult) => {
|
|
62
|
+
if (err) return callback(err);
|
|
63
|
+
|
|
64
|
+
// Ignored
|
|
65
|
+
if (!beforeResolveResult) return callback();
|
|
66
|
+
|
|
67
|
+
const context = beforeResolveResult.context;
|
|
68
|
+
const request = beforeResolveResult.request;
|
|
69
|
+
const resolveOptions = beforeResolveResult.resolveOptions;
|
|
70
|
+
|
|
71
|
+
let loaders,
|
|
72
|
+
resource,
|
|
73
|
+
loadersPrefix = "";
|
|
74
|
+
const idx = request.lastIndexOf("!");
|
|
75
|
+
if (idx >= 0) {
|
|
76
|
+
let loadersRequest = request.substr(0, idx + 1);
|
|
77
|
+
let i;
|
|
78
|
+
for (
|
|
79
|
+
i = 0;
|
|
80
|
+
i < loadersRequest.length && loadersRequest[i] === "!";
|
|
81
|
+
i++
|
|
82
|
+
) {
|
|
83
|
+
loadersPrefix += "!";
|
|
84
|
+
}
|
|
85
|
+
loadersRequest = loadersRequest
|
|
86
|
+
.substr(i)
|
|
87
|
+
.replace(/!+$/, "")
|
|
88
|
+
.replace(/!!+/g, "!");
|
|
89
|
+
if (loadersRequest === "") {
|
|
90
|
+
loaders = [];
|
|
91
|
+
} else {
|
|
92
|
+
loaders = loadersRequest.split("!");
|
|
93
|
+
}
|
|
94
|
+
resource = request.substr(idx + 1);
|
|
95
|
+
} else {
|
|
96
|
+
loaders = [];
|
|
97
|
+
resource = request;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const contextResolver = this.resolverFactory.get(
|
|
101
|
+
"context",
|
|
102
|
+
resolveOptions || EMPTY_RESOLVE_OPTIONS
|
|
103
|
+
);
|
|
104
|
+
const loaderResolver = this.resolverFactory.get(
|
|
105
|
+
"loader",
|
|
106
|
+
EMPTY_RESOLVE_OPTIONS
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
asyncLib.parallel(
|
|
110
|
+
[
|
|
111
|
+
callback => {
|
|
112
|
+
contextResolver.resolve(
|
|
113
|
+
{},
|
|
114
|
+
context,
|
|
115
|
+
resource,
|
|
116
|
+
{},
|
|
117
|
+
(err, result) => {
|
|
118
|
+
if (err) return callback(err);
|
|
119
|
+
callback(null, result);
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
},
|
|
123
|
+
callback => {
|
|
124
|
+
asyncLib.map(
|
|
125
|
+
loaders,
|
|
126
|
+
(loader, callback) => {
|
|
127
|
+
loaderResolver.resolve(
|
|
128
|
+
{},
|
|
129
|
+
context,
|
|
130
|
+
loader,
|
|
131
|
+
{},
|
|
132
|
+
(err, result) => {
|
|
133
|
+
if (err) return callback(err);
|
|
134
|
+
callback(null, result);
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
},
|
|
138
|
+
callback
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
],
|
|
142
|
+
(err, result) => {
|
|
143
|
+
if (err) return callback(err);
|
|
144
|
+
|
|
145
|
+
this.hooks.afterResolve.callAsync(
|
|
146
|
+
Object.assign(
|
|
147
|
+
{
|
|
148
|
+
addon:
|
|
149
|
+
loadersPrefix +
|
|
150
|
+
result[1].join("!") +
|
|
151
|
+
(result[1].length > 0 ? "!" : ""),
|
|
152
|
+
resource: result[0],
|
|
153
|
+
resolveDependencies: this.resolveDependencies.bind(this)
|
|
154
|
+
},
|
|
155
|
+
beforeResolveResult
|
|
156
|
+
),
|
|
157
|
+
(err, result) => {
|
|
158
|
+
if (err) return callback(err);
|
|
159
|
+
|
|
160
|
+
// Ignored
|
|
161
|
+
if (!result) return callback();
|
|
162
|
+
|
|
163
|
+
return callback(
|
|
164
|
+
null,
|
|
165
|
+
new ContextModule(result.resolveDependencies, result)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
resolveDependencies(fs, options, callback) {
|
|
176
|
+
const cmf = this;
|
|
177
|
+
let resource = options.resource;
|
|
178
|
+
let resourceQuery = options.resourceQuery;
|
|
179
|
+
let recursive = options.recursive;
|
|
180
|
+
let regExp = options.regExp;
|
|
181
|
+
let include = options.include;
|
|
182
|
+
let exclude = options.exclude;
|
|
183
|
+
if (!regExp || !resource) return callback(null, []);
|
|
184
|
+
|
|
185
|
+
const addDirectory = (directory, callback) => {
|
|
186
|
+
fs.readdir(directory, (err, files) => {
|
|
187
|
+
if (err) return callback(err);
|
|
188
|
+
files = cmf.hooks.contextModuleFiles.call(files);
|
|
189
|
+
if (!files || files.length === 0) return callback(null, []);
|
|
190
|
+
asyncLib.map(
|
|
191
|
+
files.filter(p => p.indexOf(".") !== 0),
|
|
192
|
+
(segment, callback) => {
|
|
193
|
+
const subResource = path.join(directory, segment);
|
|
194
|
+
|
|
195
|
+
if (!exclude || !subResource.match(exclude)) {
|
|
196
|
+
fs.stat(subResource, (err, stat) => {
|
|
197
|
+
if (err) {
|
|
198
|
+
if (err.code === "ENOENT") {
|
|
199
|
+
// ENOENT is ok here because the file may have been deleted between
|
|
200
|
+
// the readdir and stat calls.
|
|
201
|
+
return callback();
|
|
202
|
+
} else {
|
|
203
|
+
return callback(err);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (stat.isDirectory()) {
|
|
208
|
+
if (!recursive) return callback();
|
|
209
|
+
addDirectory.call(this, subResource, callback);
|
|
210
|
+
} else if (
|
|
211
|
+
stat.isFile() &&
|
|
212
|
+
(!include || subResource.match(include))
|
|
213
|
+
) {
|
|
214
|
+
const obj = {
|
|
215
|
+
context: resource,
|
|
216
|
+
request:
|
|
217
|
+
"." +
|
|
218
|
+
subResource.substr(resource.length).replace(/\\/g, "/")
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
this.hooks.alternatives.callAsync(
|
|
222
|
+
[obj],
|
|
223
|
+
(err, alternatives) => {
|
|
224
|
+
if (err) return callback(err);
|
|
225
|
+
alternatives = alternatives
|
|
226
|
+
.filter(obj => regExp.test(obj.request))
|
|
227
|
+
.map(obj => {
|
|
228
|
+
const dep = new ContextElementDependency(
|
|
229
|
+
obj.request + resourceQuery,
|
|
230
|
+
obj.request
|
|
231
|
+
);
|
|
232
|
+
dep.optional = true;
|
|
233
|
+
return dep;
|
|
234
|
+
});
|
|
235
|
+
callback(null, alternatives);
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
} else {
|
|
239
|
+
callback();
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
} else {
|
|
243
|
+
callback();
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
(err, result) => {
|
|
247
|
+
if (err) return callback(err);
|
|
248
|
+
|
|
249
|
+
if (!result) return callback(null, []);
|
|
250
|
+
|
|
251
|
+
callback(
|
|
252
|
+
null,
|
|
253
|
+
result.filter(Boolean).reduce((a, i) => a.concat(i), [])
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
addDirectory(resource, callback);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const ContextElementDependency = require("./dependencies/ContextElementDependency");
|
|
9
|
+
|
|
10
|
+
class ContextReplacementPlugin {
|
|
11
|
+
constructor(
|
|
12
|
+
resourceRegExp,
|
|
13
|
+
newContentResource,
|
|
14
|
+
newContentRecursive,
|
|
15
|
+
newContentRegExp
|
|
16
|
+
) {
|
|
17
|
+
this.resourceRegExp = resourceRegExp;
|
|
18
|
+
|
|
19
|
+
if (typeof newContentResource === "function") {
|
|
20
|
+
this.newContentCallback = newContentResource;
|
|
21
|
+
} else if (
|
|
22
|
+
typeof newContentResource === "string" &&
|
|
23
|
+
typeof newContentRecursive === "object"
|
|
24
|
+
) {
|
|
25
|
+
this.newContentResource = newContentResource;
|
|
26
|
+
this.newContentCreateContextMap = (fs, callback) => {
|
|
27
|
+
callback(null, newContentRecursive);
|
|
28
|
+
};
|
|
29
|
+
} else if (
|
|
30
|
+
typeof newContentResource === "string" &&
|
|
31
|
+
typeof newContentRecursive === "function"
|
|
32
|
+
) {
|
|
33
|
+
this.newContentResource = newContentResource;
|
|
34
|
+
this.newContentCreateContextMap = newContentRecursive;
|
|
35
|
+
} else {
|
|
36
|
+
if (typeof newContentResource !== "string") {
|
|
37
|
+
newContentRegExp = newContentRecursive;
|
|
38
|
+
newContentRecursive = newContentResource;
|
|
39
|
+
newContentResource = undefined;
|
|
40
|
+
}
|
|
41
|
+
if (typeof newContentRecursive !== "boolean") {
|
|
42
|
+
newContentRegExp = newContentRecursive;
|
|
43
|
+
newContentRecursive = undefined;
|
|
44
|
+
}
|
|
45
|
+
this.newContentResource = newContentResource;
|
|
46
|
+
this.newContentRecursive = newContentRecursive;
|
|
47
|
+
this.newContentRegExp = newContentRegExp;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
apply(compiler) {
|
|
52
|
+
const resourceRegExp = this.resourceRegExp;
|
|
53
|
+
const newContentCallback = this.newContentCallback;
|
|
54
|
+
const newContentResource = this.newContentResource;
|
|
55
|
+
const newContentRecursive = this.newContentRecursive;
|
|
56
|
+
const newContentRegExp = this.newContentRegExp;
|
|
57
|
+
const newContentCreateContextMap = this.newContentCreateContextMap;
|
|
58
|
+
|
|
59
|
+
compiler.hooks.contextModuleFactory.tap("ContextReplacementPlugin", cmf => {
|
|
60
|
+
cmf.hooks.beforeResolve.tap("ContextReplacementPlugin", result => {
|
|
61
|
+
if (!result) return;
|
|
62
|
+
if (resourceRegExp.test(result.request)) {
|
|
63
|
+
if (newContentResource !== undefined) {
|
|
64
|
+
result.request = newContentResource;
|
|
65
|
+
}
|
|
66
|
+
if (newContentRecursive !== undefined) {
|
|
67
|
+
result.recursive = newContentRecursive;
|
|
68
|
+
}
|
|
69
|
+
if (newContentRegExp !== undefined) {
|
|
70
|
+
result.regExp = newContentRegExp;
|
|
71
|
+
}
|
|
72
|
+
if (typeof newContentCallback === "function") {
|
|
73
|
+
newContentCallback(result);
|
|
74
|
+
} else {
|
|
75
|
+
for (const d of result.dependencies) {
|
|
76
|
+
if (d.critical) d.critical = false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
});
|
|
82
|
+
cmf.hooks.afterResolve.tap("ContextReplacementPlugin", result => {
|
|
83
|
+
if (!result) return;
|
|
84
|
+
if (resourceRegExp.test(result.resource)) {
|
|
85
|
+
if (newContentResource !== undefined) {
|
|
86
|
+
result.resource = path.resolve(result.resource, newContentResource);
|
|
87
|
+
}
|
|
88
|
+
if (newContentRecursive !== undefined) {
|
|
89
|
+
result.recursive = newContentRecursive;
|
|
90
|
+
}
|
|
91
|
+
if (newContentRegExp !== undefined) {
|
|
92
|
+
result.regExp = newContentRegExp;
|
|
93
|
+
}
|
|
94
|
+
if (typeof newContentCreateContextMap === "function") {
|
|
95
|
+
result.resolveDependencies = createResolveDependenciesFromContextMap(
|
|
96
|
+
newContentCreateContextMap
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
if (typeof newContentCallback === "function") {
|
|
100
|
+
const origResource = result.resource;
|
|
101
|
+
newContentCallback(result);
|
|
102
|
+
if (result.resource !== origResource) {
|
|
103
|
+
result.resource = path.resolve(origResource, result.resource);
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
for (const d of result.dependencies) {
|
|
107
|
+
if (d.critical) d.critical = false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const createResolveDependenciesFromContextMap = createContextMap => {
|
|
118
|
+
const resolveDependenciesFromContextMap = (fs, options, callback) => {
|
|
119
|
+
createContextMap(fs, (err, map) => {
|
|
120
|
+
if (err) return callback(err);
|
|
121
|
+
const dependencies = Object.keys(map).map(key => {
|
|
122
|
+
return new ContextElementDependency(
|
|
123
|
+
map[key] + options.resourceQuery,
|
|
124
|
+
key
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
callback(null, dependencies);
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
return resolveDependenciesFromContextMap;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
module.exports = ContextReplacementPlugin;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
3
|
+
Author Tobias Koppers @sokra
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const ConstDependency = require("./dependencies/ConstDependency");
|
|
8
|
+
const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
|
|
9
|
+
const ParserHelpers = require("./ParserHelpers");
|
|
10
|
+
const NullFactory = require("./NullFactory");
|
|
11
|
+
|
|
12
|
+
/** @typedef {import("./Compiler")} Compiler */
|
|
13
|
+
/** @typedef {import("./Parser")} Parser */
|
|
14
|
+
/** @typedef {null|undefined|RegExp|Function|string|number} CodeValuePrimitive */
|
|
15
|
+
/** @typedef {CodeValuePrimitive|Record<string, CodeValuePrimitive>|RuntimeValue} CodeValue */
|
|
16
|
+
|
|
17
|
+
class RuntimeValue {
|
|
18
|
+
constructor(fn, fileDependencies) {
|
|
19
|
+
this.fn = fn;
|
|
20
|
+
this.fileDependencies = fileDependencies || [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exec(parser) {
|
|
24
|
+
if (this.fileDependencies === true) {
|
|
25
|
+
parser.state.module.buildInfo.cacheable = false;
|
|
26
|
+
} else {
|
|
27
|
+
for (const fileDependency of this.fileDependencies) {
|
|
28
|
+
parser.state.module.buildInfo.fileDependencies.add(fileDependency);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return this.fn({ module: parser.state.module });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const stringifyObj = (obj, parser) => {
|
|
37
|
+
return (
|
|
38
|
+
"Object({" +
|
|
39
|
+
Object.keys(obj)
|
|
40
|
+
.map(key => {
|
|
41
|
+
const code = obj[key];
|
|
42
|
+
return JSON.stringify(key) + ":" + toCode(code, parser);
|
|
43
|
+
})
|
|
44
|
+
.join(",") +
|
|
45
|
+
"})"
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Convert code to a string that evaluates
|
|
51
|
+
* @param {CodeValue} code Code to evaluate
|
|
52
|
+
* @param {Parser} parser Parser
|
|
53
|
+
* @returns {string} code converted to string that evaluates
|
|
54
|
+
*/
|
|
55
|
+
const toCode = (code, parser) => {
|
|
56
|
+
if (code === null) {
|
|
57
|
+
return "null";
|
|
58
|
+
}
|
|
59
|
+
if (code === undefined) {
|
|
60
|
+
return "undefined";
|
|
61
|
+
}
|
|
62
|
+
if (code instanceof RuntimeValue) {
|
|
63
|
+
return toCode(code.exec(parser), parser);
|
|
64
|
+
}
|
|
65
|
+
if (code instanceof RegExp && code.toString) {
|
|
66
|
+
return code.toString();
|
|
67
|
+
}
|
|
68
|
+
if (typeof code === "function" && code.toString) {
|
|
69
|
+
return "(" + code.toString() + ")";
|
|
70
|
+
}
|
|
71
|
+
if (typeof code === "object") {
|
|
72
|
+
return stringifyObj(code, parser);
|
|
73
|
+
}
|
|
74
|
+
return code + "";
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
class DefinePlugin {
|
|
78
|
+
/**
|
|
79
|
+
* Create a new define plugin
|
|
80
|
+
* @param {Record<string, CodeValue>} definitions A map of global object definitions
|
|
81
|
+
*/
|
|
82
|
+
constructor(definitions) {
|
|
83
|
+
this.definitions = definitions;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static runtimeValue(fn, fileDependencies) {
|
|
87
|
+
return new RuntimeValue(fn, fileDependencies);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Apply the plugin
|
|
92
|
+
* @param {Compiler} compiler Webpack compiler
|
|
93
|
+
* @returns {void}
|
|
94
|
+
*/
|
|
95
|
+
apply(compiler) {
|
|
96
|
+
const definitions = this.definitions;
|
|
97
|
+
compiler.hooks.compilation.tap(
|
|
98
|
+
"DefinePlugin",
|
|
99
|
+
(compilation, { normalModuleFactory }) => {
|
|
100
|
+
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
|
101
|
+
compilation.dependencyTemplates.set(
|
|
102
|
+
ConstDependency,
|
|
103
|
+
new ConstDependency.Template()
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Handler
|
|
108
|
+
* @param {Parser} parser Parser
|
|
109
|
+
* @returns {void}
|
|
110
|
+
*/
|
|
111
|
+
const handler = parser => {
|
|
112
|
+
/**
|
|
113
|
+
* Walk definitions
|
|
114
|
+
* @param {Object} definitions Definitions map
|
|
115
|
+
* @param {string} prefix Prefix string
|
|
116
|
+
* @returns {void}
|
|
117
|
+
*/
|
|
118
|
+
const walkDefinitions = (definitions, prefix) => {
|
|
119
|
+
Object.keys(definitions).forEach(key => {
|
|
120
|
+
const code = definitions[key];
|
|
121
|
+
if (
|
|
122
|
+
code &&
|
|
123
|
+
typeof code === "object" &&
|
|
124
|
+
!(code instanceof RuntimeValue) &&
|
|
125
|
+
!(code instanceof RegExp)
|
|
126
|
+
) {
|
|
127
|
+
walkDefinitions(code, prefix + key + ".");
|
|
128
|
+
applyObjectDefine(prefix + key, code);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
applyDefineKey(prefix, key);
|
|
132
|
+
applyDefine(prefix + key, code);
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Apply define key
|
|
138
|
+
* @param {string} prefix Prefix
|
|
139
|
+
* @param {string} key Key
|
|
140
|
+
* @returns {void}
|
|
141
|
+
*/
|
|
142
|
+
const applyDefineKey = (prefix, key) => {
|
|
143
|
+
const splittedKey = key.split(".");
|
|
144
|
+
splittedKey.slice(1).forEach((_, i) => {
|
|
145
|
+
const fullKey = prefix + splittedKey.slice(0, i + 1).join(".");
|
|
146
|
+
parser.hooks.canRename
|
|
147
|
+
.for(fullKey)
|
|
148
|
+
.tap("DefinePlugin", ParserHelpers.approve);
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Apply Code
|
|
154
|
+
* @param {string} key Key
|
|
155
|
+
* @param {CodeValue} code Code
|
|
156
|
+
* @returns {void}
|
|
157
|
+
*/
|
|
158
|
+
const applyDefine = (key, code) => {
|
|
159
|
+
const isTypeof = /^typeof\s+/.test(key);
|
|
160
|
+
if (isTypeof) key = key.replace(/^typeof\s+/, "");
|
|
161
|
+
let recurse = false;
|
|
162
|
+
let recurseTypeof = false;
|
|
163
|
+
if (!isTypeof) {
|
|
164
|
+
parser.hooks.canRename
|
|
165
|
+
.for(key)
|
|
166
|
+
.tap("DefinePlugin", ParserHelpers.approve);
|
|
167
|
+
parser.hooks.evaluateIdentifier
|
|
168
|
+
.for(key)
|
|
169
|
+
.tap("DefinePlugin", expr => {
|
|
170
|
+
/**
|
|
171
|
+
* this is needed in case there is a recursion in the DefinePlugin
|
|
172
|
+
* to prevent an endless recursion
|
|
173
|
+
* e.g.: new DefinePlugin({
|
|
174
|
+
* "a": "b",
|
|
175
|
+
* "b": "a"
|
|
176
|
+
* });
|
|
177
|
+
*/
|
|
178
|
+
if (recurse) return;
|
|
179
|
+
recurse = true;
|
|
180
|
+
const res = parser.evaluate(toCode(code, parser));
|
|
181
|
+
recurse = false;
|
|
182
|
+
res.setRange(expr.range);
|
|
183
|
+
return res;
|
|
184
|
+
});
|
|
185
|
+
parser.hooks.expression.for(key).tap("DefinePlugin", expr => {
|
|
186
|
+
const strCode = toCode(code, parser);
|
|
187
|
+
if (/__webpack_require__/.test(strCode)) {
|
|
188
|
+
return ParserHelpers.toConstantDependencyWithWebpackRequire(
|
|
189
|
+
parser,
|
|
190
|
+
strCode
|
|
191
|
+
)(expr);
|
|
192
|
+
} else {
|
|
193
|
+
return ParserHelpers.toConstantDependency(
|
|
194
|
+
parser,
|
|
195
|
+
strCode
|
|
196
|
+
)(expr);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
parser.hooks.evaluateTypeof.for(key).tap("DefinePlugin", expr => {
|
|
201
|
+
/**
|
|
202
|
+
* this is needed in case there is a recursion in the DefinePlugin
|
|
203
|
+
* to prevent an endless recursion
|
|
204
|
+
* e.g.: new DefinePlugin({
|
|
205
|
+
* "typeof a": "typeof b",
|
|
206
|
+
* "typeof b": "typeof a"
|
|
207
|
+
* });
|
|
208
|
+
*/
|
|
209
|
+
if (recurseTypeof) return;
|
|
210
|
+
recurseTypeof = true;
|
|
211
|
+
const typeofCode = isTypeof
|
|
212
|
+
? toCode(code, parser)
|
|
213
|
+
: "typeof (" + toCode(code, parser) + ")";
|
|
214
|
+
const res = parser.evaluate(typeofCode);
|
|
215
|
+
recurseTypeof = false;
|
|
216
|
+
res.setRange(expr.range);
|
|
217
|
+
return res;
|
|
218
|
+
});
|
|
219
|
+
parser.hooks.typeof.for(key).tap("DefinePlugin", expr => {
|
|
220
|
+
const typeofCode = isTypeof
|
|
221
|
+
? toCode(code, parser)
|
|
222
|
+
: "typeof (" + toCode(code, parser) + ")";
|
|
223
|
+
const res = parser.evaluate(typeofCode);
|
|
224
|
+
if (!res.isString()) return;
|
|
225
|
+
return ParserHelpers.toConstantDependency(
|
|
226
|
+
parser,
|
|
227
|
+
JSON.stringify(res.string)
|
|
228
|
+
).bind(parser)(expr);
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Apply Object
|
|
234
|
+
* @param {string} key Key
|
|
235
|
+
* @param {Object} obj Object
|
|
236
|
+
* @returns {void}
|
|
237
|
+
*/
|
|
238
|
+
const applyObjectDefine = (key, obj) => {
|
|
239
|
+
parser.hooks.canRename
|
|
240
|
+
.for(key)
|
|
241
|
+
.tap("DefinePlugin", ParserHelpers.approve);
|
|
242
|
+
parser.hooks.evaluateIdentifier
|
|
243
|
+
.for(key)
|
|
244
|
+
.tap("DefinePlugin", expr =>
|
|
245
|
+
new BasicEvaluatedExpression().setTruthy().setRange(expr.range)
|
|
246
|
+
);
|
|
247
|
+
parser.hooks.evaluateTypeof.for(key).tap("DefinePlugin", expr => {
|
|
248
|
+
return ParserHelpers.evaluateToString("object")(expr);
|
|
249
|
+
});
|
|
250
|
+
parser.hooks.expression.for(key).tap("DefinePlugin", expr => {
|
|
251
|
+
const strCode = stringifyObj(obj, parser);
|
|
252
|
+
|
|
253
|
+
if (/__webpack_require__/.test(strCode)) {
|
|
254
|
+
return ParserHelpers.toConstantDependencyWithWebpackRequire(
|
|
255
|
+
parser,
|
|
256
|
+
strCode
|
|
257
|
+
)(expr);
|
|
258
|
+
} else {
|
|
259
|
+
return ParserHelpers.toConstantDependency(
|
|
260
|
+
parser,
|
|
261
|
+
strCode
|
|
262
|
+
)(expr);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
parser.hooks.typeof.for(key).tap("DefinePlugin", expr => {
|
|
266
|
+
return ParserHelpers.toConstantDependency(
|
|
267
|
+
parser,
|
|
268
|
+
JSON.stringify("object")
|
|
269
|
+
)(expr);
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
walkDefinitions(definitions, "");
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
normalModuleFactory.hooks.parser
|
|
277
|
+
.for("javascript/auto")
|
|
278
|
+
.tap("DefinePlugin", handler);
|
|
279
|
+
normalModuleFactory.hooks.parser
|
|
280
|
+
.for("javascript/dynamic")
|
|
281
|
+
.tap("DefinePlugin", handler);
|
|
282
|
+
normalModuleFactory.hooks.parser
|
|
283
|
+
.for("javascript/esm")
|
|
284
|
+
.tap("DefinePlugin", handler);
|
|
285
|
+
}
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
module.exports = DefinePlugin;
|