webpack 5.107.2 → 5.108.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/hot/dev-server.js +2 -0
- package/lib/APIPlugin.js +8 -4
- package/lib/CacheFacade.js +7 -0
- package/lib/Chunk.js +4 -0
- package/lib/ChunkGraph.js +30 -9
- package/lib/CircularModulesPlugin.js +190 -0
- package/lib/CleanPlugin.js +2 -1
- package/lib/Compilation.js +396 -65
- package/lib/Compiler.js +25 -11
- package/lib/ConcatenationScope.js +10 -3
- package/lib/ContextExclusionPlugin.js +1 -0
- package/lib/ContextModule.js +92 -47
- package/lib/ContextReplacementPlugin.js +4 -0
- package/lib/DefinePlugin.js +89 -15
- package/lib/Dependency.js +111 -7
- package/lib/EntryOptionPlugin.js +39 -2
- package/lib/EntryPlugin.js +2 -0
- package/lib/ExportsInfo.js +314 -73
- package/lib/ExternalModule.js +82 -34
- package/lib/ExternalModuleFactoryPlugin.js +1 -0
- package/lib/ExternalsPlugin.js +1 -0
- package/lib/FileSystemInfo.js +173 -23
- package/lib/FlagAllModulesAsUsedPlugin.js +1 -5
- package/lib/FlagDependencyExportsPlugin.js +23 -0
- package/lib/FlagDependencyUsagePlugin.js +87 -5
- package/lib/FlagEntryExportAsUsedPlugin.js +2 -0
- package/lib/Generator.js +8 -0
- package/lib/HotModuleReplacementPlugin.js +65 -28
- package/lib/IgnorePlugin.js +1 -0
- package/lib/InitFragment.js +5 -0
- package/lib/JavascriptMetaInfoPlugin.js +7 -5
- package/lib/LazyBarrel.js +361 -0
- package/lib/LoaderTargetPlugin.js +1 -0
- package/lib/Module.js +21 -42
- package/lib/ModuleGraph.js +3 -2
- package/lib/ModuleProfile.js +27 -1
- package/lib/ModuleTemplate.js +2 -0
- package/lib/MultiCompiler.js +2 -0
- package/lib/MultiStats.js +1 -0
- package/lib/MultiWatching.js +2 -0
- package/lib/NodeStuffPlugin.js +22 -17
- package/lib/NormalModule.js +142 -80
- package/lib/NormalModuleReplacementPlugin.js +2 -0
- package/lib/PrefetchPlugin.js +2 -0
- package/lib/ProgressPlugin.js +8 -0
- package/lib/ProvidePlugin.js +1 -0
- package/lib/RawModule.js +20 -16
- package/lib/RecordIdsPlugin.js +1 -0
- package/lib/RuntimeGlobals.js +5 -0
- package/lib/RuntimePlugin.js +58 -26
- package/lib/RuntimeTemplate.js +278 -21
- package/lib/SelfModuleFactory.js +1 -0
- package/lib/SourceMapDevToolPlugin.js +3 -5
- package/lib/Stats.js +1 -0
- package/lib/Template.js +8 -2
- package/lib/TemplatedPathPlugin.js +473 -131
- package/lib/WarnCaseSensitiveModulesPlugin.js +1 -0
- package/lib/WarnDeprecatedOptionPlugin.js +4 -0
- package/lib/WarnNoModeSetPlugin.js +1 -0
- package/lib/WatchIgnorePlugin.js +1 -0
- package/lib/Watching.js +9 -0
- package/lib/WebpackOptionsApply.js +50 -5
- package/lib/asset/AssetBytesGenerator.js +11 -3
- package/lib/asset/AssetGenerator.js +47 -22
- package/lib/asset/AssetModule.js +47 -0
- package/lib/asset/AssetModulesPlugin.js +14 -14
- package/lib/asset/AssetParser.js +2 -2
- package/lib/asset/AssetSourceGenerator.js +8 -0
- package/lib/asset/RawDataUrlModule.js +12 -13
- package/lib/buildChunkGraph.js +64 -7
- package/lib/bun/BunTargetPlugin.js +48 -0
- package/lib/cache/AddBuildDependenciesPlugin.js +1 -0
- package/lib/cache/AddManagedPathsPlugin.js +3 -0
- package/lib/cache/IdleFileCachePlugin.js +4 -0
- package/lib/cache/MemoryWithGcCachePlugin.js +1 -0
- package/lib/cache/PackFileCacheStrategy.js +1 -0
- package/lib/cache/ResolverCachePlugin.js +2 -0
- package/lib/cache/mergeEtags.js +2 -0
- package/lib/cli.js +109 -19
- package/lib/config/browserslistTargetHandler.js +99 -0
- package/lib/config/defaults.js +147 -7
- package/lib/config/defineConfig.js +31 -0
- package/lib/config/normalization.js +5 -0
- package/lib/config/target.js +181 -2
- package/lib/container/ContainerEntryModule.js +15 -14
- package/lib/container/ContainerExposedDependency.js +2 -2
- package/lib/container/ContainerReferencePlugin.js +1 -1
- package/lib/container/FallbackDependency.js +5 -7
- package/lib/container/FallbackModule.js +10 -9
- package/lib/container/RemoteModule.js +20 -10
- package/lib/container/RemoteRuntimeModule.js +14 -12
- package/lib/css/CssGenerator.js +353 -327
- package/lib/css/CssInjectStyleRuntimeModule.js +36 -8
- package/lib/css/CssLoadingRuntimeModule.js +118 -47
- package/lib/css/CssModule.js +52 -23
- package/lib/css/CssModulesPlugin.js +107 -124
- package/lib/css/CssParser.js +2759 -2205
- package/lib/css/syntax.js +2859 -0
- package/lib/debug/ProfilingPlugin.js +2 -0
- package/lib/deno/DenoTargetPlugin.js +47 -0
- package/lib/dependencies/AMDDefineDependency.js +22 -17
- package/lib/dependencies/AMDDefineDependencyParserPlugin.js +1 -0
- package/lib/dependencies/AMDRequireArrayDependency.js +7 -11
- package/lib/dependencies/AMDRequireContextDependency.js +7 -11
- package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +1 -0
- package/lib/dependencies/AMDRequireDependency.js +24 -20
- package/lib/dependencies/CachedConstDependency.js +3 -0
- package/lib/dependencies/CommonJsDependencyHelpers.js +1 -0
- package/lib/dependencies/CommonJsExportRequireDependency.js +79 -31
- package/lib/dependencies/CommonJsExportsDependency.js +21 -16
- package/lib/dependencies/CommonJsExportsParserPlugin.js +230 -7
- package/lib/dependencies/CommonJsFullRequireDependency.js +27 -19
- package/lib/dependencies/CommonJsImportsParserPlugin.js +51 -16
- package/lib/dependencies/CommonJsPlugin.js +1 -1
- package/lib/dependencies/CommonJsRequireContextDependency.js +10 -13
- package/lib/dependencies/CommonJsRequireDependency.js +42 -11
- package/lib/dependencies/CommonJsSelfReferenceDependency.js +22 -14
- package/lib/dependencies/ConstDependency.js +16 -11
- package/lib/dependencies/ContextDependency.js +4 -0
- package/lib/dependencies/ContextDependencyTemplateAsId.js +1 -1
- package/lib/dependencies/ContextElementDependency.js +23 -14
- package/lib/dependencies/CreateRequireParserPlugin.js +1 -0
- package/lib/dependencies/CreateScriptUrlDependency.js +5 -7
- package/lib/dependencies/CriticalDependencyWarning.js +1 -0
- package/lib/dependencies/CssIcssExportDependency.js +512 -574
- package/lib/dependencies/CssIcssImportDependency.js +9 -9
- package/lib/dependencies/CssIcssSymbolDependency.js +22 -15
- package/lib/dependencies/CssImportDependency.js +25 -1
- package/lib/dependencies/CssUrlDependency.js +23 -14
- package/lib/dependencies/DllEntryDependency.js +9 -13
- package/lib/dependencies/ExportBindingInitFragment.js +164 -0
- package/lib/dependencies/ExportsInfoDependency.js +12 -12
- package/lib/dependencies/ExternalModuleDependency.js +8 -5
- package/lib/dependencies/ExternalModuleInitFragment.js +7 -5
- package/lib/dependencies/ExternalModuleInitFragmentDependency.js +13 -10
- package/lib/dependencies/HarmonyAcceptDependency.js +11 -11
- package/lib/dependencies/HarmonyAcceptImportDependency.js +1 -0
- package/lib/dependencies/HarmonyDetectionParserPlugin.js +47 -22
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +28 -2
- package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +46 -22
- package/lib/dependencies/HarmonyExportExpressionDependency.js +107 -30
- package/lib/dependencies/HarmonyExportHeaderDependency.js +7 -9
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +109 -31
- package/lib/dependencies/HarmonyExportInitFragment.js +18 -15
- package/lib/dependencies/HarmonyExportSpecifierDependency.js +83 -18
- package/lib/dependencies/HarmonyExports.js +4 -1
- package/lib/dependencies/HarmonyImportDependency.js +24 -0
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +110 -74
- package/lib/dependencies/HarmonyImportGuard.js +254 -0
- package/lib/dependencies/HarmonyImportSideEffectDependency.js +18 -1
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +118 -14
- package/lib/dependencies/HarmonyLinkingError.js +1 -0
- package/lib/dependencies/HarmonyModulesPlugin.js +1 -0
- package/lib/dependencies/{HtmlScriptSrcDependency.js → HtmlEntryDependency.js} +261 -109
- package/lib/dependencies/HtmlInlineHtmlDependency.js +107 -0
- package/lib/dependencies/HtmlInlineScriptDependency.js +17 -13
- package/lib/dependencies/HtmlInlineStyleDependency.js +45 -11
- package/lib/dependencies/ImportContextDependency.js +5 -9
- package/lib/dependencies/ImportDependency.js +44 -2
- package/lib/dependencies/ImportMetaHotAcceptDependency.js +1 -0
- package/lib/dependencies/ImportMetaHotDeclineDependency.js +1 -0
- package/lib/dependencies/ImportMetaPlugin.js +74 -30
- package/lib/dependencies/ImportParserPlugin.js +19 -0
- package/lib/dependencies/ImportWeakDependency.js +1 -0
- package/lib/dependencies/JsonExportsDependency.js +9 -9
- package/lib/dependencies/LoaderImportDependency.js +1 -0
- package/lib/dependencies/LocalModule.js +11 -12
- package/lib/dependencies/LocalModuleDependency.js +11 -13
- package/lib/dependencies/ModuleDecoratorDependency.js +9 -9
- package/lib/dependencies/ModuleDependency.js +7 -0
- package/lib/dependencies/ModuleHotAcceptDependency.js +1 -0
- package/lib/dependencies/ModuleHotDeclineDependency.js +1 -0
- package/lib/dependencies/ModuleInitFragmentDependency.js +15 -11
- package/lib/dependencies/ProvidedDependency.js +31 -27
- package/lib/dependencies/PureExpressionDependency.js +7 -9
- package/lib/dependencies/RequireEnsureDependency.js +12 -13
- package/lib/dependencies/RequireHeaderDependency.js +3 -4
- package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +3 -0
- package/lib/dependencies/RequireResolveContextDependency.js +7 -11
- package/lib/dependencies/RequireResolveDependency.js +1 -0
- package/lib/dependencies/RequireResolveHeaderDependency.js +3 -5
- package/lib/dependencies/RuntimeRequirementsDependency.js +6 -7
- package/lib/dependencies/StaticExportsDependency.js +10 -10
- package/lib/dependencies/SystemPlugin.js +2 -0
- package/lib/dependencies/URLContextDependency.js +5 -7
- package/lib/dependencies/URLDependency.js +14 -16
- package/lib/dependencies/UnsupportedDependency.js +8 -13
- package/lib/dependencies/WebAssemblyExportImportedDependency.js +11 -16
- package/lib/dependencies/WebAssemblyImportDependency.js +14 -16
- package/lib/dependencies/WebpackIsIncludedDependency.js +1 -0
- package/lib/dependencies/WorkerDependency.js +19 -8
- package/lib/dependencies/WorkerPlugin.js +22 -6
- package/lib/dependencies/processExportInfo.js +13 -11
- package/lib/dll/DelegatedModule.js +29 -15
- package/lib/dll/DelegatedModuleFactoryPlugin.js +1 -0
- package/lib/dll/DelegatedPlugin.js +1 -0
- package/lib/dll/DllEntryPlugin.js +3 -0
- package/lib/dll/DllModule.js +3 -2
- package/lib/dll/DllPlugin.js +16 -0
- package/lib/dll/DllReferencePlugin.js +3 -0
- package/lib/dll/LibManifestPlugin.js +1 -0
- package/lib/electron/ElectronTargetPlugin.js +22 -4
- package/lib/errors/ConcurrentCompilationError.js +1 -0
- package/lib/errors/HookWebpackError.js +11 -13
- package/lib/errors/IgnoreErrorModuleFactory.js +1 -0
- package/lib/errors/InvalidDependenciesModuleWarning.js +2 -0
- package/lib/errors/JSONParseError.js +9 -8
- package/lib/errors/ModuleBuildError.js +16 -13
- package/lib/errors/ModuleDependencyError.js +8 -1
- package/lib/errors/ModuleDependencyWarning.js +8 -1
- package/lib/errors/ModuleError.js +5 -11
- package/lib/errors/ModuleHashingError.js +4 -0
- package/lib/errors/ModuleNotFoundError.js +3 -0
- package/lib/errors/ModuleParseError.js +5 -11
- package/lib/errors/ModuleRestoreError.js +2 -0
- package/lib/errors/ModuleStoreError.js +3 -0
- package/lib/errors/ModuleWarning.js +7 -11
- package/lib/errors/NodeStuffInWebError.js +1 -0
- package/lib/errors/NonErrorEmittedError.js +2 -0
- package/lib/errors/UnhandledSchemeError.js +1 -0
- package/lib/esm/ModuleChunkLoadingPlugin.js +0 -1
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +28 -23
- package/lib/hmr/HotModuleReplacement.runtime.js +175 -30
- package/lib/hmr/HotModuleReplacementRuntimeModule.js +7 -0
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +173 -26
- package/lib/hmr/LazyCompilationPlugin.js +30 -6
- package/lib/html/HtmlGenerator.js +217 -23
- package/lib/html/HtmlModule.js +40 -0
- package/lib/html/HtmlModulesPlugin.js +219 -55
- package/lib/html/HtmlParser.js +1108 -1099
- package/lib/html/{walkHtmlTokens.js → syntax.js} +4701 -212
- package/lib/ids/IdHelpers.js +4 -2
- package/lib/index.js +19 -0
- package/lib/javascript/BasicEvaluatedExpression.js +1 -0
- package/lib/javascript/EnableChunkLoadingPlugin.js +1 -0
- package/lib/javascript/JavascriptGenerator.js +6 -2
- package/lib/javascript/JavascriptModule.js +54 -0
- package/lib/javascript/JavascriptModulesPlugin.js +257 -102
- package/lib/javascript/JavascriptParser.js +285 -158
- package/lib/json/JsonModule.js +40 -0
- package/lib/json/JsonModulesPlugin.js +7 -0
- package/lib/json/JsonParser.js +4 -2
- package/lib/library/AssignLibraryPlugin.js +5 -3
- package/lib/library/ExportPropertyLibraryPlugin.js +1 -0
- package/lib/library/FalseIIFEUmdWarning.js +1 -0
- package/lib/library/ModuleLibraryPlugin.js +24 -5
- package/lib/library/SystemLibraryPlugin.js +3 -3
- package/lib/node/NodeTargetPlugin.js +1 -0
- package/lib/node/NodeWatchFileSystem.js +1 -0
- package/lib/node/ReadFileChunkLoadingRuntimeModule.js +10 -9
- package/lib/node/RequireChunkLoadingRuntimeModule.js +18 -16
- package/lib/optimize/ConcatenatedModule.js +395 -77
- package/lib/optimize/ConstExportsPlugin.js +211 -0
- package/lib/optimize/InlineExports.js +178 -0
- package/lib/optimize/InnerGraph.js +372 -275
- package/lib/optimize/InnerGraphPlugin.js +196 -99
- package/lib/optimize/ModuleConcatenationPlugin.js +56 -25
- package/lib/optimize/RealContentHashPlugin.js +14 -1
- package/lib/optimize/SideEffectsFlagPlugin.js +234 -64
- package/lib/runtime/AsyncModuleRuntimeModule.js +32 -30
- package/lib/runtime/AutoPublicPathRuntimeModule.js +11 -5
- package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +1 -1
- package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +6 -4
- package/lib/runtime/DefinePropertyGettersRuntimeModule.js +33 -4
- package/lib/runtime/GetChunkFilenameRuntimeModule.js +82 -3
- package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
- package/lib/runtime/HasOwnPropertyRuntimeModule.js +1 -1
- package/lib/runtime/LoadScriptRuntimeModule.js +18 -13
- package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +39 -26
- package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +1 -1
- package/lib/runtime/OnChunksLoadedRuntimeModule.js +4 -4
- package/lib/runtime/RelativeUrlRuntimeModule.js +3 -2
- package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +1 -1
- package/lib/runtime/StartupEntrypointRuntimeModule.js +4 -3
- package/lib/runtime/WorkerRuntimeModule.js +33 -0
- package/lib/schemes/HttpUriPlugin.js +3 -2
- package/lib/serialization/AggregateErrorSerializer.js +7 -6
- package/lib/serialization/ArraySerializer.js +4 -8
- package/lib/serialization/BinaryMiddleware.js +538 -468
- package/lib/serialization/DateObjectSerializer.js +2 -2
- package/lib/serialization/ErrorObjectSerializer.js +7 -6
- package/lib/serialization/MapObjectSerializer.js +5 -9
- package/lib/serialization/NullPrototypeObjectSerializer.js +7 -9
- package/lib/serialization/ObjectMiddleware.js +50 -13
- package/lib/serialization/PlainObjectSerializer.js +9 -8
- package/lib/serialization/RegExpObjectSerializer.js +2 -2
- package/lib/serialization/Serializer.js +1 -0
- package/lib/serialization/SetObjectSerializer.js +4 -8
- package/lib/sharing/ConsumeSharedModule.js +6 -7
- package/lib/sharing/ConsumeSharedPlugin.js +1 -0
- package/lib/sharing/ConsumeSharedRuntimeModule.js +46 -41
- package/lib/sharing/ProvideSharedDependency.js +30 -15
- package/lib/sharing/ProvideSharedModule.js +30 -11
- package/lib/sharing/ProvideSharedPlugin.js +4 -2
- package/lib/sharing/SharePlugin.js +3 -0
- package/lib/sharing/ShareRuntimeModule.js +18 -16
- package/lib/sharing/resolveMatchedConfigs.js +2 -1
- package/lib/sharing/utils.js +1 -1
- package/lib/stats/DefaultStatsFactoryPlugin.js +4 -6
- package/lib/stats/DefaultStatsPrinterPlugin.js +14 -14
- package/lib/stats/StatsFactory.js +11 -9
- package/lib/stats/StatsPrinter.js +9 -2
- package/lib/url/URLParserPlugin.js +5 -2
- package/lib/util/AsyncQueue.js +10 -0
- package/lib/util/LazyBucketSortedSet.js +5 -0
- package/lib/util/LazySet.js +6 -4
- package/lib/util/LocConverter.js +11 -1
- package/lib/util/Semaphore.js +1 -0
- package/lib/util/SourceProcessor.js +106 -0
- package/lib/util/TupleSet.js +1 -0
- package/lib/util/WeakTupleMap.js +27 -1
- package/lib/util/chainedImports.js +3 -3
- package/lib/util/comparators.js +2 -2
- package/lib/util/concatenate.js +31 -7
- package/lib/util/createHash.js +0 -1
- package/lib/util/deterministicGrouping.js +19 -12
- package/lib/util/extractSourceMap.js +1 -1
- package/lib/util/findGraphRoots.js +2 -0
- package/lib/util/fs.js +10 -5
- package/lib/util/hash/DebugHash.js +1 -0
- package/lib/util/hash/hash-digest.js +24 -15
- package/lib/util/identifier.js +7 -0
- package/lib/util/internalSerializables.js +11 -2
- package/lib/util/magicComment.js +42 -0
- package/lib/util/makeSerializable.js +1 -0
- package/lib/util/nonNumericOnlyHash.js +30 -1
- package/lib/util/property.js +7 -1
- package/lib/util/publicPathPlaceholder.js +64 -0
- package/lib/util/registerExternalSerializer.js +4 -4
- package/lib/wasm-async/AsyncWasmModule.js +77 -0
- package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +1 -1
- package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +1 -96
- package/lib/wasm-async/AsyncWebAssemblyParser.js +1 -1
- package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +1 -1
- package/lib/wasm-sync/SyncWasmModule.js +39 -0
- package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +1 -0
- package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +6 -3
- package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +2 -1
- package/lib/wasm-sync/WebAssemblyGenerator.js +1 -0
- package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +2 -0
- package/lib/wasm-sync/WebAssemblyModulesPlugin.js +10 -0
- package/lib/wasm-sync/WebAssemblyParser.js +7 -3
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +45 -39
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +11 -10
- package/package.json +35 -27
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +212 -3
- package/schemas/plugins/HtmlGeneratorOptions.check.js +1 -1
- package/schemas/plugins/HtmlParserOptions.check.d.ts +7 -0
- package/schemas/plugins/HtmlParserOptions.check.js +6 -0
- package/schemas/plugins/HtmlParserOptions.json +3 -0
- package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
- package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
- package/schemas/plugins/css/CssParserOptions.check.js +1 -1
- package/types.d.ts +1456 -290
- package/lib/css/walkCssTokens.js +0 -2020
- package/lib/util/AppendOnlyStackedSet.js +0 -93
|
@@ -19,7 +19,9 @@ const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
|
|
|
19
19
|
/** @typedef {import("estree").AssignmentExpression} AssignmentExpression */
|
|
20
20
|
/** @typedef {import("estree").CallExpression} CallExpression */
|
|
21
21
|
/** @typedef {import("estree").Expression} Expression */
|
|
22
|
+
/** @typedef {import("estree").Node} Node */
|
|
22
23
|
/** @typedef {import("estree").Super} Super */
|
|
24
|
+
/** @typedef {import("estree").ThisExpression} ThisExpression */
|
|
23
25
|
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
|
24
26
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|
25
27
|
/** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
|
|
@@ -29,7 +31,7 @@ const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
|
|
|
29
31
|
/** @typedef {import("../javascript/JavascriptParser").Members} Members */
|
|
30
32
|
/** @typedef {import("../javascript/JavascriptParser").StatementPath} StatementPath */
|
|
31
33
|
/** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
|
|
32
|
-
/** @typedef {import("../
|
|
34
|
+
/** @typedef {import("../javascript/JavascriptModule").JavascriptModuleBuildMeta} JavascriptModuleBuildMeta */
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* This function takes a generic expression and detects whether it is an ObjectExpression.
|
|
@@ -59,6 +61,55 @@ const getValueOfPropertyDescription = (expr) => {
|
|
|
59
61
|
}
|
|
60
62
|
};
|
|
61
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Extracts the re-exportable expression from a property descriptor, handling
|
|
66
|
+
* both the eager `{ value: <expr> }` form and the lazy
|
|
67
|
+
* `{ get: () => <expr> }` / `{ get() { return <expr>; } }` accessor form used
|
|
68
|
+
* by barrel files (e.g. webpack's own `lib/index.js`). A `set` accessor cannot
|
|
69
|
+
* be reproduced by the rewritten descriptor, so descriptors with a setter are
|
|
70
|
+
* left to the generic handler (which keeps the descriptor verbatim).
|
|
71
|
+
* @param {Expression} expr property descriptor expression
|
|
72
|
+
* @returns {{ expr: Expression, getter: boolean } | undefined} the value expression and whether it is a lazy getter
|
|
73
|
+
*/
|
|
74
|
+
const getReexportOfPropertyDescriptor = (expr) => {
|
|
75
|
+
if (expr.type !== "ObjectExpression") return;
|
|
76
|
+
/** @type {Expression | undefined} */
|
|
77
|
+
let valueExpr;
|
|
78
|
+
/** @type {Expression | undefined} */
|
|
79
|
+
let getFn;
|
|
80
|
+
for (const property of expr.properties) {
|
|
81
|
+
if (property.type === "SpreadElement" || property.computed) continue;
|
|
82
|
+
const key = property.key;
|
|
83
|
+
if (key.type !== "Identifier") continue;
|
|
84
|
+
// A setter would be silently dropped by the rewrite — bail out.
|
|
85
|
+
if (key.name === "set") return;
|
|
86
|
+
if (key.name === "value") {
|
|
87
|
+
valueExpr = /** @type {Expression} */ (property.value);
|
|
88
|
+
} else if (key.name === "get") {
|
|
89
|
+
getFn = /** @type {Expression} */ (property.value);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (valueExpr) return { expr: valueExpr, getter: false };
|
|
93
|
+
if (
|
|
94
|
+
getFn &&
|
|
95
|
+
(getFn.type === "FunctionExpression" ||
|
|
96
|
+
getFn.type === "ArrowFunctionExpression")
|
|
97
|
+
) {
|
|
98
|
+
// Arrow with expression body: `() => require("./x")`.
|
|
99
|
+
if (getFn.body.type !== "BlockStatement") {
|
|
100
|
+
return { expr: /** @type {Expression} */ (getFn.body), getter: true };
|
|
101
|
+
}
|
|
102
|
+
// Function body must be exactly `return <expr>;`.
|
|
103
|
+
if (
|
|
104
|
+
getFn.body.body.length === 1 &&
|
|
105
|
+
getFn.body.body[0].type === "ReturnStatement" &&
|
|
106
|
+
getFn.body.body[0].argument
|
|
107
|
+
) {
|
|
108
|
+
return { expr: getFn.body.body[0].argument, getter: true };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
62
113
|
/**
|
|
63
114
|
* The purpose of this function is to check whether an expression is a truthy literal or not. This is
|
|
64
115
|
* useful when parsing CommonJS exports, because CommonJS modules can export any value, including falsy
|
|
@@ -129,6 +180,105 @@ const parseRequireCall = (parser, expr) => {
|
|
|
129
180
|
return { argument: argValue, ids: ids.reverse() };
|
|
130
181
|
};
|
|
131
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Checks if a value is an AST node.
|
|
185
|
+
* @param {unknown} value candidate
|
|
186
|
+
* @returns {value is Node} true when the value is an AST node
|
|
187
|
+
*/
|
|
188
|
+
const isNode = (value) =>
|
|
189
|
+
typeof value === "object" &&
|
|
190
|
+
value !== null &&
|
|
191
|
+
typeof (/** @type {{ type?: unknown }} */ (value).type) === "string";
|
|
192
|
+
|
|
193
|
+
/** Positional/metadata keys that never hold child AST nodes. */
|
|
194
|
+
const NON_CHILD_KEYS = new Set([
|
|
195
|
+
"type",
|
|
196
|
+
"start",
|
|
197
|
+
"end",
|
|
198
|
+
"loc",
|
|
199
|
+
"range",
|
|
200
|
+
"leadingComments",
|
|
201
|
+
"trailingComments"
|
|
202
|
+
]);
|
|
203
|
+
|
|
204
|
+
/** @type {Map<string, string[]>} per node type: own keys that may hold child nodes */
|
|
205
|
+
const childKeysByType = new Map();
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns the keys of a node that may hold child nodes, excluding positional
|
|
209
|
+
* metadata. Computed once per node type and cached: acorn initializes every
|
|
210
|
+
* field of a given type (to `null` when absent), so the key set is stable, and
|
|
211
|
+
* the cache stays allocation-free on the hot path after the first node of each
|
|
212
|
+
* type is seen.
|
|
213
|
+
* @param {Node} node node
|
|
214
|
+
* @returns {string[]} candidate child keys
|
|
215
|
+
*/
|
|
216
|
+
const getChildKeys = (node) => {
|
|
217
|
+
let keys = childKeysByType.get(node.type);
|
|
218
|
+
if (keys === undefined) {
|
|
219
|
+
keys = Object.keys(node).filter((key) => !NON_CHILD_KEYS.has(key));
|
|
220
|
+
childKeysByType.set(node.type, keys);
|
|
221
|
+
}
|
|
222
|
+
return keys;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Searches for a `this` belonging to the scanned function's own `this`-scope.
|
|
227
|
+
* Descends into arrow functions, but not into nested functions, static blocks
|
|
228
|
+
* and class field values, which have their own `this`. Class method bodies are
|
|
229
|
+
* function expressions and are skipped by the same rule, while computed keys
|
|
230
|
+
* and `extends` clauses evaluate in the outer scope and are searched. The
|
|
231
|
+
* `this`-scope boundary is enforced when a node is popped, so the generic
|
|
232
|
+
* branch may enumerate any child without crossing it.
|
|
233
|
+
* @param {Node[]} initialNodes nodes to search
|
|
234
|
+
* @returns {ThisExpression | undefined} first own `this` expression
|
|
235
|
+
*/
|
|
236
|
+
const findOwnThisExpression = (initialNodes) => {
|
|
237
|
+
const stack = [...initialNodes];
|
|
238
|
+
let node;
|
|
239
|
+
while ((node = stack.pop()) !== undefined) {
|
|
240
|
+
switch (node.type) {
|
|
241
|
+
case "ThisExpression":
|
|
242
|
+
return node;
|
|
243
|
+
case "FunctionExpression":
|
|
244
|
+
case "FunctionDeclaration":
|
|
245
|
+
case "StaticBlock":
|
|
246
|
+
break;
|
|
247
|
+
case "PropertyDefinition":
|
|
248
|
+
if (node.computed) stack.push(node.key);
|
|
249
|
+
break;
|
|
250
|
+
default: {
|
|
251
|
+
const children =
|
|
252
|
+
/** @type {Record<string, unknown>} */
|
|
253
|
+
(/** @type {unknown} */ (node));
|
|
254
|
+
const keys = getChildKeys(node);
|
|
255
|
+
for (let i = 0; i < keys.length; i++) {
|
|
256
|
+
const value = children[keys[i]];
|
|
257
|
+
if (Array.isArray(value)) {
|
|
258
|
+
for (const item of value) {
|
|
259
|
+
if (isNode(item)) stack.push(item);
|
|
260
|
+
}
|
|
261
|
+
} else if (isNode(value)) {
|
|
262
|
+
stack.push(value);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Returns the own `this` of a function-expression export value. When such a
|
|
272
|
+
* function is called as a method of the exports object, `this` is the exports
|
|
273
|
+
* object and may reach any sibling export (#21178).
|
|
274
|
+
* @param {Node} expr the exported value
|
|
275
|
+
* @returns {ThisExpression | undefined} first own `this` expression
|
|
276
|
+
*/
|
|
277
|
+
const getThisAccessInExportedValue = (expr) =>
|
|
278
|
+
expr.type === "FunctionExpression"
|
|
279
|
+
? findOwnThisExpression([...expr.params, expr.body])
|
|
280
|
+
: undefined;
|
|
281
|
+
|
|
132
282
|
const PLUGIN_NAME = "CommonJsExportsParserPlugin";
|
|
133
283
|
|
|
134
284
|
class CommonJsExportsParserPlugin {
|
|
@@ -137,6 +287,7 @@ class CommonJsExportsParserPlugin {
|
|
|
137
287
|
* @param {ModuleGraph} moduleGraph module graph
|
|
138
288
|
*/
|
|
139
289
|
constructor(moduleGraph) {
|
|
290
|
+
/** @type {ModuleGraph} */
|
|
140
291
|
this.moduleGraph = moduleGraph;
|
|
141
292
|
}
|
|
142
293
|
|
|
@@ -184,6 +335,36 @@ class CommonJsExportsParserPlugin {
|
|
|
184
335
|
.getOptimizationBailout(parser.state.module)
|
|
185
336
|
.push(`CommonJS bailout: ${reason}`);
|
|
186
337
|
};
|
|
338
|
+
/** @type {unknown} */
|
|
339
|
+
let keptAllExportsForState;
|
|
340
|
+
/**
|
|
341
|
+
* An exported function accessing its own `this` may reach any export
|
|
342
|
+
* through it when called as a method of the exports object, so the
|
|
343
|
+
* whole exports object must be kept (#21178).
|
|
344
|
+
* @param {Node} valueExpr the exported value
|
|
345
|
+
* @param {CommonJSDependencyBaseKeywords} base commonjs base keywords
|
|
346
|
+
* @returns {void}
|
|
347
|
+
*/
|
|
348
|
+
const keepAllExportsOnThisAccess = (valueExpr, base) => {
|
|
349
|
+
// One bailout per module is enough
|
|
350
|
+
if (keptAllExportsForState === parser.state) return;
|
|
351
|
+
const thisExpr = getThisAccessInExportedValue(valueExpr);
|
|
352
|
+
if (thisExpr === undefined) return;
|
|
353
|
+
keptAllExportsForState = parser.state;
|
|
354
|
+
bailoutHint(
|
|
355
|
+
`this in exported function may access any export, so all exports are kept, at ${formatLocation(
|
|
356
|
+
/** @type {DependencyLocation} */ (thisExpr.loc)
|
|
357
|
+
)}`
|
|
358
|
+
);
|
|
359
|
+
const dep = new CommonJsSelfReferenceDependency(
|
|
360
|
+
/** @type {Range} */ (thisExpr.range),
|
|
361
|
+
base,
|
|
362
|
+
[],
|
|
363
|
+
false
|
|
364
|
+
);
|
|
365
|
+
dep.loc = /** @type {DependencyLocation} */ (thisExpr.loc);
|
|
366
|
+
parser.state.module.addDependency(dep);
|
|
367
|
+
};
|
|
187
368
|
|
|
188
369
|
// metadata //
|
|
189
370
|
parser.hooks.evaluateTypeof
|
|
@@ -226,7 +407,7 @@ class CommonJsExportsParserPlugin {
|
|
|
226
407
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
227
408
|
dep.optional = Boolean(parser.scope.inTry);
|
|
228
409
|
parser.state.module.addDependency(dep);
|
|
229
|
-
/** @type {
|
|
410
|
+
/** @type {JavascriptModuleBuildMeta} */ (
|
|
230
411
|
parser.state.module.buildMeta
|
|
231
412
|
).treatAsCommonJs = true;
|
|
232
413
|
|
|
@@ -250,8 +431,10 @@ class CommonJsExportsParserPlugin {
|
|
|
250
431
|
);
|
|
251
432
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
252
433
|
parser.state.module.addDependency(dep);
|
|
253
|
-
/** @type {
|
|
254
|
-
|
|
434
|
+
/** @type {JavascriptModuleBuildMeta} */ (
|
|
435
|
+
parser.state.module.buildMeta
|
|
436
|
+
).treatAsCommonJs = true;
|
|
437
|
+
keepAllExportsOnThisAccess(expr.right, base);
|
|
255
438
|
parser.walkExpression(expr.right);
|
|
256
439
|
return true;
|
|
257
440
|
};
|
|
@@ -295,6 +478,33 @@ class CommonJsExportsParserPlugin {
|
|
|
295
478
|
if (typeof property !== "string") return;
|
|
296
479
|
enableStructuredExports();
|
|
297
480
|
const descArg = expr.arguments[2];
|
|
481
|
+
// Handle reexporting: `Object.defineProperty(exports, "x", { value: require("./y")[.z] })`
|
|
482
|
+
// and the lazy getter form `{ get: () => require("./y")[.z] }`.
|
|
483
|
+
if (property !== "__esModule") {
|
|
484
|
+
const reexport = getReexportOfPropertyDescriptor(descArg);
|
|
485
|
+
const requireCall =
|
|
486
|
+
reexport && parseRequireCall(parser, reexport.expr);
|
|
487
|
+
if (reexport && requireCall && requireCall.argument.isString()) {
|
|
488
|
+
const dep = new CommonJsExportRequireDependency(
|
|
489
|
+
/** @type {Range} */ (expr.range),
|
|
490
|
+
/** @type {Range} */ (reexport.expr.range),
|
|
491
|
+
`Object.defineProperty(${exportsArg.identifier})`,
|
|
492
|
+
[property],
|
|
493
|
+
/** @type {string} */ (requireCall.argument.string),
|
|
494
|
+
requireCall.ids,
|
|
495
|
+
false
|
|
496
|
+
);
|
|
497
|
+
dep.getter = reexport.getter;
|
|
498
|
+
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
499
|
+
dep.optional = Boolean(parser.scope.inTry);
|
|
500
|
+
parser.state.module.addDependency(dep);
|
|
501
|
+
/** @type {JavascriptModuleBuildMeta} */ (
|
|
502
|
+
parser.state.module.buildMeta
|
|
503
|
+
).treatAsCommonJs = true;
|
|
504
|
+
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
298
508
|
checkNamespace(
|
|
299
509
|
/** @type {StatementPath} */
|
|
300
510
|
(parser.statementPath).length === 1,
|
|
@@ -309,10 +519,22 @@ class CommonJsExportsParserPlugin {
|
|
|
309
519
|
);
|
|
310
520
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
311
521
|
parser.state.module.addDependency(dep);
|
|
312
|
-
/** @type {
|
|
522
|
+
/** @type {JavascriptModuleBuildMeta} */ (
|
|
313
523
|
parser.state.module.buildMeta
|
|
314
524
|
).treatAsCommonJs = true;
|
|
315
525
|
|
|
526
|
+
if (descArg.type === "ObjectExpression") {
|
|
527
|
+
// `value`, `get` and `set` descriptor functions are all called
|
|
528
|
+
// with the exports object as `this`
|
|
529
|
+
for (const descProperty of descArg.properties) {
|
|
530
|
+
if (descProperty.type === "SpreadElement") continue;
|
|
531
|
+
keepAllExportsOnThisAccess(
|
|
532
|
+
descProperty.value,
|
|
533
|
+
/** @type {CommonJSDependencyBaseKeywords} */
|
|
534
|
+
(exportsArg.identifier)
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
316
538
|
parser.walkExpression(expr.arguments[2]);
|
|
317
539
|
return true;
|
|
318
540
|
});
|
|
@@ -353,8 +575,9 @@ class CommonJsExportsParserPlugin {
|
|
|
353
575
|
);
|
|
354
576
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
355
577
|
parser.state.module.addDependency(dep);
|
|
356
|
-
/** @type {
|
|
357
|
-
|
|
578
|
+
/** @type {JavascriptModuleBuildMeta} */ (
|
|
579
|
+
parser.state.module.buildMeta
|
|
580
|
+
).treatAsCommonJs = true;
|
|
358
581
|
|
|
359
582
|
if (call) {
|
|
360
583
|
parser.walkExpressions(call.arguments);
|
|
@@ -24,10 +24,10 @@ const ModuleDependency = require("./ModuleDependency");
|
|
|
24
24
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|
25
25
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|
26
26
|
/** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
|
|
27
|
-
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
28
|
-
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|
29
27
|
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|
30
28
|
/** @typedef {import("../util/chainedImports").IdRanges} IdRanges */
|
|
29
|
+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[ExportInfoName[], IdRanges | undefined, boolean, undefined | boolean]>} ObjectDeserializerContext */
|
|
30
|
+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[ExportInfoName[], IdRanges | undefined, boolean, undefined | boolean]>} ObjectSerializerContext */
|
|
31
31
|
|
|
32
32
|
class CommonJsFullRequireDependency extends ModuleDependency {
|
|
33
33
|
/**
|
|
@@ -45,7 +45,9 @@ class CommonJsFullRequireDependency extends ModuleDependency {
|
|
|
45
45
|
) {
|
|
46
46
|
super(request);
|
|
47
47
|
this.range = range;
|
|
48
|
+
/** @type {string[]} */
|
|
48
49
|
this.names = names;
|
|
50
|
+
/** @type {IdRanges | undefined} */
|
|
49
51
|
this.idRanges = idRanges;
|
|
50
52
|
/** @type {boolean} */
|
|
51
53
|
this.call = false;
|
|
@@ -61,6 +63,7 @@ class CommonJsFullRequireDependency extends ModuleDependency {
|
|
|
61
63
|
*/
|
|
62
64
|
getReferencedExports(moduleGraph, runtime) {
|
|
63
65
|
const importedModule = moduleGraph.getModule(this);
|
|
66
|
+
// CommonJS property access is never rewritten to a literal, so it can't inline
|
|
64
67
|
if (
|
|
65
68
|
importedModule &&
|
|
66
69
|
isRequireEsmModuleExportsModule(importedModule, moduleGraph)
|
|
@@ -68,16 +71,16 @@ class CommonJsFullRequireDependency extends ModuleDependency {
|
|
|
68
71
|
// When `require(esm)` unwraps a `"module.exports"` named export, the
|
|
69
72
|
// user's property access lands on that value (which webpack does not
|
|
70
73
|
// model), so only the "module.exports" export itself is referenced.
|
|
71
|
-
return [[ESM_MODULE_EXPORTS_NAME]];
|
|
74
|
+
return [{ name: [ESM_MODULE_EXPORTS_NAME], canInline: false }];
|
|
72
75
|
}
|
|
73
76
|
if (
|
|
74
77
|
this.call &&
|
|
75
78
|
(!importedModule ||
|
|
76
79
|
importedModule.getExportsType(moduleGraph, false) !== "namespace")
|
|
77
80
|
) {
|
|
78
|
-
return [this.names.slice(0, -1)];
|
|
81
|
+
return [{ name: this.names.slice(0, -1), canInline: false }];
|
|
79
82
|
}
|
|
80
|
-
return [this.names];
|
|
83
|
+
return [{ name: this.names, canInline: false }];
|
|
81
84
|
}
|
|
82
85
|
|
|
83
86
|
/**
|
|
@@ -85,11 +88,11 @@ class CommonJsFullRequireDependency extends ModuleDependency {
|
|
|
85
88
|
* @param {ObjectSerializerContext} context context
|
|
86
89
|
*/
|
|
87
90
|
serialize(context) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
context
|
|
92
|
+
.write(this.names)
|
|
93
|
+
.write(this.idRanges)
|
|
94
|
+
.write(this.call)
|
|
95
|
+
.write(this.asiSafe);
|
|
93
96
|
super.serialize(context);
|
|
94
97
|
}
|
|
95
98
|
|
|
@@ -98,12 +101,14 @@ class CommonJsFullRequireDependency extends ModuleDependency {
|
|
|
98
101
|
* @param {ObjectDeserializerContext} context context
|
|
99
102
|
*/
|
|
100
103
|
deserialize(context) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.idRanges = read();
|
|
104
|
-
|
|
105
|
-
this.
|
|
106
|
-
|
|
104
|
+
this.names = context.read();
|
|
105
|
+
const c1 = context.rest;
|
|
106
|
+
this.idRanges = c1.read();
|
|
107
|
+
const c2 = c1.rest;
|
|
108
|
+
this.call = c2.read();
|
|
109
|
+
const c3 = c2.rest;
|
|
110
|
+
this.asiSafe = c3.read();
|
|
111
|
+
super.deserialize(c3.rest);
|
|
107
112
|
}
|
|
108
113
|
|
|
109
114
|
get type() {
|
|
@@ -163,9 +168,12 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
|
|
|
163
168
|
? `(${requireExpr}${access})`
|
|
164
169
|
: `${requireExpr}${access}`;
|
|
165
170
|
} else if (importedModule) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
// CJS exports are never inlined
|
|
172
|
+
const usedImported = /** @type {string | string[] | false} */ (
|
|
173
|
+
moduleGraph
|
|
174
|
+
.getExportsInfo(importedModule)
|
|
175
|
+
.getUsedName(trimmedIds, runtime)
|
|
176
|
+
);
|
|
169
177
|
if (usedImported) {
|
|
170
178
|
const comment = equals(usedImported, trimmedIds)
|
|
171
179
|
? ""
|
|
@@ -14,6 +14,7 @@ const {
|
|
|
14
14
|
expressionIsUnsupported,
|
|
15
15
|
toConstantDependency
|
|
16
16
|
} = require("../javascript/JavascriptParserHelpers");
|
|
17
|
+
const memoize = require("../util/memoize");
|
|
17
18
|
const traverseDestructuringAssignmentProperties = require("../util/traverseDestructuringAssignmentProperties");
|
|
18
19
|
const CommonJsFullRequireDependency = require("./CommonJsFullRequireDependency");
|
|
19
20
|
const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
|
|
@@ -53,12 +54,11 @@ const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency
|
|
|
53
54
|
* member-access references on `NAME` to the `CommonJsRequireDependency`
|
|
54
55
|
* created for the `require()` call.
|
|
55
56
|
* @typedef {object} RequireBindingData
|
|
56
|
-
* @property {RawReferencedExports} referencedExports mutable list
|
|
57
|
-
* @property {InstanceType<typeof import("./CommonJsRequireDependency")> | null} dep dependency for the `require()` call (
|
|
57
|
+
* @property {RawReferencedExports | null} referencedExports mutable list, pushed to as `NAME.x.y` accesses are walked; null means the whole namespace is used. Separate from `dep` so a deferred require (walked after accesses on `NAME`) still collects them; becomes the dep's shared list on creation.
|
|
58
|
+
* @property {InstanceType<typeof import("./CommonJsRequireDependency")> | null} dep dependency for the `require()` call, null until the `require()` is walked; kept so a later whole-namespace use can null its list directly
|
|
58
59
|
*/
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
const requireBindingData = new WeakMap();
|
|
61
|
+
const getHarmonyImportGuard = memoize(() => require("./HarmonyImportGuard"));
|
|
62
62
|
|
|
63
63
|
const REQUIRE_BINDING_TAG = Symbol(
|
|
64
64
|
"CommonJsImportsParserPlugin require binding"
|
|
@@ -157,9 +157,15 @@ const createRequireAsExpressionHandler =
|
|
|
157
157
|
* @param {JavascriptParser} parser parser
|
|
158
158
|
* @param {JavascriptParserOptions} options options
|
|
159
159
|
* @param {() => undefined | string} getContext context accessor
|
|
160
|
+
* @param {WeakMap<CallExpression, RequireBindingData>=} requireBindingData per-parser `const NAME = require(LITERAL)` binding state; only the free-`require` plugin populates it
|
|
160
161
|
* @returns {(callNew: boolean) => (expr: CallExpression | NewExpression) => (boolean | void)} handler factory
|
|
161
162
|
*/
|
|
162
|
-
const createRequireCallHandler = (
|
|
163
|
+
const createRequireCallHandler = (
|
|
164
|
+
parser,
|
|
165
|
+
options,
|
|
166
|
+
getContext,
|
|
167
|
+
requireBindingData
|
|
168
|
+
) => {
|
|
163
169
|
/**
|
|
164
170
|
* Process require item.
|
|
165
171
|
* @param {CallExpression | NewExpression} expr expression
|
|
@@ -172,9 +178,9 @@ const createRequireCallHandler = (parser, options, getContext) => {
|
|
|
172
178
|
parser,
|
|
173
179
|
expr
|
|
174
180
|
);
|
|
175
|
-
const binding =
|
|
176
|
-
|
|
177
|
-
|
|
181
|
+
const binding =
|
|
182
|
+
requireBindingData &&
|
|
183
|
+
requireBindingData.get(/** @type {CallExpression} */ (expr));
|
|
178
184
|
if (binding && !referencedExports) {
|
|
179
185
|
// `const NAME = require(LITERAL)` — let later member-access walks
|
|
180
186
|
// on `NAME` populate the dependency's referenced exports.
|
|
@@ -191,6 +197,7 @@ const createRequireCallHandler = (parser, options, getContext) => {
|
|
|
191
197
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
192
198
|
dep.optional = Boolean(parser.scope.inTry);
|
|
193
199
|
parser.state.current.addDependency(dep);
|
|
200
|
+
getHarmonyImportGuard().attachDependencyGuards(parser, dep);
|
|
194
201
|
return true;
|
|
195
202
|
}
|
|
196
203
|
};
|
|
@@ -436,6 +443,7 @@ class CommonJsImportsParserPlugin {
|
|
|
436
443
|
* @param {JavascriptParserOptions} options parser options
|
|
437
444
|
*/
|
|
438
445
|
constructor(options) {
|
|
446
|
+
/** @type {JavascriptParserOptions} */
|
|
439
447
|
this.options = options;
|
|
440
448
|
}
|
|
441
449
|
|
|
@@ -577,10 +585,13 @@ class CommonJsImportsParserPlugin {
|
|
|
577
585
|
* @param {boolean} callNew true, when require is called with new
|
|
578
586
|
* @returns {(expr: CallExpression | NewExpression) => (boolean | void)} handler
|
|
579
587
|
*/
|
|
588
|
+
/** @type {WeakMap<CallExpression, RequireBindingData>} */
|
|
589
|
+
const requireBindingData = new WeakMap();
|
|
580
590
|
const createRequireHandler = createRequireCallHandler(
|
|
581
591
|
parser,
|
|
582
592
|
options,
|
|
583
|
-
getContext
|
|
593
|
+
getContext,
|
|
594
|
+
requireBindingData
|
|
584
595
|
);
|
|
585
596
|
parser.hooks.call
|
|
586
597
|
.for("require")
|
|
@@ -727,11 +738,15 @@ class CommonJsImportsParserPlugin {
|
|
|
727
738
|
const binding =
|
|
728
739
|
/** @type {RequireBindingData} */
|
|
729
740
|
(parser.currentTagData);
|
|
730
|
-
if (binding
|
|
741
|
+
if (binding) {
|
|
731
742
|
// `NAME` is read as a value (not as the object of a static member
|
|
732
743
|
// chain), so we have to assume the whole exports object is used.
|
|
733
|
-
binding.
|
|
744
|
+
binding.referencedExports = null;
|
|
745
|
+
if (binding.dep) {
|
|
746
|
+
binding.dep.referencedExports = null;
|
|
747
|
+
}
|
|
734
748
|
}
|
|
749
|
+
return true;
|
|
735
750
|
});
|
|
736
751
|
|
|
737
752
|
parser.hooks.expressionMemberChain
|
|
@@ -740,8 +755,8 @@ class CommonJsImportsParserPlugin {
|
|
|
740
755
|
const binding =
|
|
741
756
|
/** @type {RequireBindingData} */
|
|
742
757
|
(parser.currentTagData);
|
|
743
|
-
if (binding && binding.
|
|
744
|
-
binding.
|
|
758
|
+
if (binding && binding.referencedExports) {
|
|
759
|
+
binding.referencedExports.push(members);
|
|
745
760
|
}
|
|
746
761
|
// Returning truthy suppresses the parser's fallback chain (which
|
|
747
762
|
// would otherwise walk `NAME` as a bare expression and trigger our
|
|
@@ -755,18 +770,38 @@ class CommonJsImportsParserPlugin {
|
|
|
755
770
|
const binding =
|
|
756
771
|
/** @type {RequireBindingData} */
|
|
757
772
|
(parser.currentTagData);
|
|
758
|
-
if (binding && binding.
|
|
773
|
+
if (binding && binding.referencedExports) {
|
|
759
774
|
if (members.length === 0) {
|
|
760
775
|
// `NAME(...)` — calling the require result directly; the
|
|
761
776
|
// whole exports object is observable.
|
|
762
|
-
binding.
|
|
777
|
+
binding.referencedExports = null;
|
|
778
|
+
if (binding.dep) {
|
|
779
|
+
binding.dep.referencedExports = null;
|
|
780
|
+
}
|
|
763
781
|
} else {
|
|
764
|
-
binding.
|
|
782
|
+
binding.referencedExports.push(members);
|
|
765
783
|
}
|
|
766
784
|
}
|
|
767
785
|
if (expr.arguments) parser.walkExpressions(expr.arguments);
|
|
768
786
|
return true;
|
|
769
787
|
});
|
|
788
|
+
|
|
789
|
+
// Re-exporting the binding lets the require result escape, so the whole
|
|
790
|
+
// exports object is observable. Stage -10 runs before the harmony bail tap.
|
|
791
|
+
parser.hooks.exportSpecifier.tap(
|
|
792
|
+
{ name: PLUGIN_NAME, stage: -10 },
|
|
793
|
+
(_statement, id) => {
|
|
794
|
+
const binding =
|
|
795
|
+
/** @type {RequireBindingData | undefined} */
|
|
796
|
+
(parser.getTagData(id, REQUIRE_BINDING_TAG));
|
|
797
|
+
if (binding) {
|
|
798
|
+
binding.referencedExports = null;
|
|
799
|
+
if (binding.dep) {
|
|
800
|
+
binding.dep.referencedExports = null;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
);
|
|
770
805
|
// #endregion
|
|
771
806
|
|
|
772
807
|
// #region Require.resolve
|
|
@@ -281,7 +281,7 @@ class HarmonyModuleDecoratorRuntimeModule extends RuntimeModule {
|
|
|
281
281
|
"Object.defineProperty(module, 'exports', {",
|
|
282
282
|
Template.indent([
|
|
283
283
|
"enumerable: true,",
|
|
284
|
-
|
|
284
|
+
`${runtimeTemplate.method("set", "", [
|
|
285
285
|
"throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);"
|
|
286
286
|
])}`
|
|
287
287
|
]),
|
|
@@ -11,8 +11,8 @@ const ContextDependency = require("./ContextDependency");
|
|
|
11
11
|
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
|
|
12
12
|
|
|
13
13
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|
14
|
-
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
15
|
-
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|
14
|
+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range | undefined, boolean | string | undefined]>} ObjectDeserializerContext */
|
|
15
|
+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range | undefined, boolean | string | undefined]>} ObjectSerializerContext */
|
|
16
16
|
/** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
|
|
17
17
|
/** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
|
|
18
18
|
/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|
@@ -34,6 +34,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|
|
34
34
|
this.range = range;
|
|
35
35
|
this.valueRange = valueRange;
|
|
36
36
|
// inShorthand must be serialized by subclasses that use it
|
|
37
|
+
/** @type {string | boolean | undefined} */
|
|
37
38
|
this.inShorthand = inShorthand;
|
|
38
39
|
}
|
|
39
40
|
|
|
@@ -62,11 +63,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|
|
62
63
|
* @param {ObjectSerializerContext} context context
|
|
63
64
|
*/
|
|
64
65
|
serialize(context) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
write(this.range);
|
|
68
|
-
write(this.valueRange);
|
|
69
|
-
write(this.inShorthand);
|
|
66
|
+
context.write(this.range).write(this.valueRange).write(this.inShorthand);
|
|
70
67
|
|
|
71
68
|
super.serialize(context);
|
|
72
69
|
}
|
|
@@ -76,13 +73,13 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|
|
76
73
|
* @param {ObjectDeserializerContext} context context
|
|
77
74
|
*/
|
|
78
75
|
deserialize(context) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.
|
|
82
|
-
|
|
83
|
-
this.inShorthand = read();
|
|
76
|
+
this.range = context.read();
|
|
77
|
+
const c1 = context.rest;
|
|
78
|
+
this.valueRange = c1.read();
|
|
79
|
+
const c2 = c1.rest;
|
|
80
|
+
this.inShorthand = c2.read();
|
|
84
81
|
|
|
85
|
-
super.deserialize(
|
|
82
|
+
super.deserialize(c2.rest);
|
|
86
83
|
}
|
|
87
84
|
}
|
|
88
85
|
|