2013-01-31 01:49:25 +08:00
/ *
2017-01-24 03:31:53 +08:00
MIT License http : //www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @ sokra
2018-07-30 23:08:51 +08:00
* /
2017-01-06 01:00:36 +08:00
"use strict" ;
2018-02-11 12:27:09 +08:00
const asyncLib = require ( "neo-async" ) ;
2018-03-22 19:05:58 +08:00
const {
2018-11-06 02:03:12 +08:00
HookMap ,
2018-03-22 19:05:58 +08:00
SyncHook ,
SyncBailHook ,
SyncWaterfallHook ,
2019-12-11 05:58:26 +08:00
AsyncSeriesHook ,
AsyncSeriesBailHook
2018-03-22 19:05:58 +08:00
} = require ( "tapable" ) ;
2018-11-13 21:45:26 +08:00
const util = require ( "util" ) ;
2018-07-30 23:08:51 +08:00
const { CachedSource } = require ( "webpack-sources" ) ;
2020-07-28 00:09:48 +08:00
const { MultiItemCache } = require ( "./CacheFacade" ) ;
2017-01-06 01:00:36 +08:00
const Chunk = require ( "./Chunk" ) ;
2018-08-14 17:18:22 +08:00
const ChunkGraph = require ( "./ChunkGraph" ) ;
2018-07-30 23:08:51 +08:00
const ChunkGroup = require ( "./ChunkGroup" ) ;
2017-01-06 01:00:36 +08:00
const ChunkRenderError = require ( "./ChunkRenderError" ) ;
2018-07-30 23:08:51 +08:00
const ChunkTemplate = require ( "./ChunkTemplate" ) ;
2019-10-09 04:29:46 +08:00
const CodeGenerationError = require ( "./CodeGenerationError" ) ;
2020-07-28 00:09:48 +08:00
const CodeGenerationResults = require ( "./CodeGenerationResults" ) ;
2018-07-30 23:08:51 +08:00
const DependencyTemplates = require ( "./DependencyTemplates" ) ;
const Entrypoint = require ( "./Entrypoint" ) ;
2019-07-24 16:51:04 +08:00
const ErrorHelpers = require ( "./ErrorHelpers" ) ;
2018-09-27 13:22:19 +08:00
const FileSystemInfo = require ( "./FileSystemInfo" ) ;
2020-02-07 17:05:51 +08:00
const {
connectChunkGroupAndChunk ,
connectChunkGroupParentAndChild
} = require ( "./GraphHelpers" ) ;
2019-05-10 17:06:25 +08:00
const { makeWebpackError } = require ( "./HookWebpackError" ) ;
2018-07-30 23:08:51 +08:00
const MainTemplate = require ( "./MainTemplate" ) ;
2019-09-26 21:51:40 +08:00
const Module = require ( "./Module" ) ;
2018-07-30 23:08:51 +08:00
const ModuleDependencyError = require ( "./ModuleDependencyError" ) ;
const ModuleDependencyWarning = require ( "./ModuleDependencyWarning" ) ;
2018-07-17 22:41:07 +08:00
const ModuleGraph = require ( "./ModuleGraph" ) ;
2018-07-30 23:08:51 +08:00
const ModuleNotFoundError = require ( "./ModuleNotFoundError" ) ;
2018-08-22 20:54:28 +08:00
const ModuleProfile = require ( "./ModuleProfile" ) ;
2018-10-18 04:54:07 +08:00
const ModuleRestoreError = require ( "./ModuleRestoreError" ) ;
2019-12-16 21:50:10 +08:00
const ModuleStoreError = require ( "./ModuleStoreError" ) ;
2018-07-30 23:08:51 +08:00
const ModuleTemplate = require ( "./ModuleTemplate" ) ;
2018-11-17 01:18:44 +08:00
const RuntimeGlobals = require ( "./RuntimeGlobals" ) ;
2018-07-30 23:08:51 +08:00
const RuntimeTemplate = require ( "./RuntimeTemplate" ) ;
const Stats = require ( "./Stats" ) ;
2018-12-03 00:00:16 +08:00
const WebpackError = require ( "./WebpackError" ) ;
2019-07-25 14:07:55 +08:00
const buildChunkGraph = require ( "./buildChunkGraph" ) ;
2019-12-20 19:45:55 +08:00
const BuildCycleError = require ( "./errors/BuildCycleError" ) ;
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
const { Logger , LogType } = require ( "./logging/Logger" ) ;
2018-12-19 01:29:12 +08:00
const StatsFactory = require ( "./stats/StatsFactory" ) ;
const StatsPrinter = require ( "./stats/StatsPrinter" ) ;
2018-09-12 00:47:55 +08:00
const AsyncQueue = require ( "./util/AsyncQueue" ) ;
2019-08-07 15:54:43 +08:00
const LazySet = require ( "./util/LazySet" ) ;
2018-09-07 20:11:48 +08:00
const {
2018-12-20 15:51:54 +08:00
compareLocations ,
2018-09-07 20:11:48 +08:00
concatComparators ,
compareSelect ,
2018-10-21 17:11:21 +08:00
compareIds ,
2019-10-09 15:26:33 +08:00
compareStringsNumeric ,
2019-10-16 22:38:04 +08:00
compareModulesByIdentifier
2018-09-07 20:11:48 +08:00
} = require ( "./util/comparators" ) ;
2018-07-30 23:08:51 +08:00
const createHash = require ( "./util/createHash" ) ;
2020-05-12 18:16:51 +08:00
const {
arrayToSetDeprecation ,
soonFrozenObjectDeprecation ,
2020-07-14 18:02:32 +08:00
createFakeHook
2020-05-12 18:16:51 +08:00
} = require ( "./util/deprecation" ) ;
2020-07-28 01:36:06 +08:00
const { getRuntimeKey } = require ( "./util/runtime" ) ;
2013-01-31 01:49:25 +08:00
2020-05-12 18:16:51 +08:00
/** @template T @typedef {import("tapable").AsArray<T>} AsArray<T> */
2018-05-04 00:57:02 +08:00
/** @typedef {import("webpack-sources").Source} Source */
2020-02-26 20:08:05 +08:00
/** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
2020-02-20 03:25:49 +08:00
/** @typedef {import("../declarations/WebpackOptions").OutputNormalized} OutputOptions */
2018-05-12 00:12:30 +08:00
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
2020-07-15 17:14:28 +08:00
/** @typedef {import("./Cache")} Cache */
/** @typedef {import("./CacheFacade")} CacheFacade */
2019-06-14 17:44:54 +08:00
/** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
2018-06-30 01:02:35 +08:00
/** @typedef {import("./Dependency")} Dependency */
2018-06-25 16:43:59 +08:00
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
2020-05-28 02:34:55 +08:00
/** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
2018-07-23 23:33:29 +08:00
/** @typedef {import("./DependencyTemplate")} DependencyTemplate */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./Module")} Module */
2019-10-09 04:29:46 +08:00
/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
2019-01-05 02:17:37 +08:00
/** @typedef {import("./ModuleFactory")} ModuleFactory */
2018-11-06 02:03:12 +08:00
/** @typedef {import("./RuntimeModule")} RuntimeModule */
2018-09-27 13:22:19 +08:00
/** @typedef {import("./Template").RenderManifestEntry} RenderManifestEntry */
2019-10-02 14:54:21 +08:00
/** @typedef {import("./Template").RenderManifestOptions} RenderManifestOptions */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./WebpackError")} WebpackError */
2018-12-19 01:29:12 +08:00
/** @typedef {import("./stats/StatsFactory")} StatsFactory */
/** @typedef {import("./stats/StatsPrinter")} StatsPrinter */
2019-07-17 22:02:33 +08:00
/** @typedef {import("./util/Hash")} Hash */
2020-07-14 18:02:32 +08:00
/** @template T @typedef {import("./util/deprecation").FakeHook<T>} FakeHook<T> */
2020-07-28 00:09:48 +08:00
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
2018-05-04 00:57:02 +08:00
2018-12-10 18:34:59 +08:00
/ * *
* @ callback Callback
* @ param { WebpackError = } err
* @ returns { void }
* /
/ * *
* @ callback ModuleCallback
* @ param { WebpackError = } err
* @ param { Module = } result
* @ returns { void }
* /
/ * *
* @ callback DepBlockVarDependenciesCallback
* @ param { Dependency } dependency
* @ returns { any }
* /
/ * *
* @ typedef { Object } Plugin
* @ property { ( ) => void } apply
* /
2018-05-04 00:57:02 +08:00
/** @typedef {new (...args: any[]) => Dependency} DepConstructor */
2018-12-10 18:34:59 +08:00
/** @typedef {Record<string, Source>} CompilationAssets */
2018-05-04 00:57:02 +08:00
/ * *
* @ typedef { Object } AvailableModulesChunkGroupMapping
* @ property { ChunkGroup } chunkGroup
* @ property { Set < Module > } availableModules
2018-10-05 07:10:12 +08:00
* @ property { boolean } needCopy
2018-05-04 00:57:02 +08:00
* /
2018-05-07 05:01:47 +08:00
/ * *
* @ typedef { Object } DependenciesBlockLike
* @ property { Dependency [ ] } dependencies
* @ property { AsyncDependenciesBlock [ ] } blocks
* /
2018-08-23 01:23:48 +08:00
/ * *
* @ typedef { Object } ChunkPathData
* @ property { string | number } id
* @ property { string = } name
* @ property { string } hash
* @ property { function ( number ) : string = } hashWithLength
* @ property { ( Record < string , string > ) = } contentHash
* @ property { ( Record < string , ( length : number ) => string > ) = } contentHashWithLength
* /
2019-10-02 14:54:21 +08:00
/ * *
* @ typedef { Object } ChunkHashContext
* @ property { RuntimeTemplate } runtimeTemplate the runtime template
* @ property { ModuleGraph } moduleGraph the module graph
* @ property { ChunkGraph } chunkGraph the chunk graph
* /
2020-02-26 20:08:05 +08:00
/** @typedef {{ name: string } & Omit<EntryDescription, "import">} EntryOptions */
2020-02-05 04:21:42 +08:00
2020-02-05 04:35:01 +08:00
/ * *
* @ typedef { Object } EntryData
2020-05-23 22:08:51 +08:00
* @ property { Dependency [ ] } dependencies dependencies of the entrypoint that should be evaluated at startup
2020-06-05 12:42:33 +08:00
* @ property { Dependency [ ] } includeDependencies dependencies of the entrypoint that should be included but not evaluated
2020-02-05 04:35:01 +08:00
* @ property { EntryOptions } options options of the entrypoint
* /
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
/ * *
* @ typedef { Object } LogEntry
* @ property { string } type
* @ property { any [ ] } args
2019-07-23 15:28:06 +08:00
* @ property { number } time
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
* @ property { string [ ] = } trace
* /
2019-09-11 17:13:46 +08:00
/ * *
* @ typedef { Object } AssetInfo
* @ property { boolean = } immutable true , if the asset can be long term cached forever ( contains a hash )
* @ property { number = } size size in bytes , only set after asset has been emitted
* @ property { boolean = } development true , when asset is only used for development and doesn ' t count towards user - facing assets
* @ property { boolean = } hotModuleReplacement true , when asset ships data for updating an existing application ( HMR )
* /
/ * *
* @ typedef { Object } Asset
* @ property { string } name the filename of the asset
* @ property { Source } source source of the asset
* @ property { AssetInfo } info info about the asset
* /
2018-08-23 01:23:48 +08:00
/ * *
* @ typedef { Object } ModulePathData
* @ property { string | number } id
* @ property { string } hash
* @ property { function ( number ) : string = } hashWithLength
* /
/ * *
* @ typedef { Object } PathData
* @ property { ChunkGraph = } chunkGraph
* @ property { string = } hash
* @ property { function ( number ) : string = } hashWithLength
* @ property { ( Chunk | ChunkPathData ) = } chunk
* @ property { ( Module | ModulePathData ) = } module
2020-07-28 00:09:48 +08:00
* @ property { RuntimeSpec = } runtime
2018-08-23 01:23:48 +08:00
* @ property { string = } filename
* @ property { string = } basename
* @ property { string = } query
* @ property { string = } contentHashType
* @ property { string = } contentHash
* @ property { function ( number ) : string = } contentHashWithLength
* @ property { boolean = } noChunkHash
2019-07-17 22:02:33 +08:00
* @ property { string = } url
2018-08-23 01:23:48 +08:00
* /
2020-05-28 06:23:54 +08:00
const esmDependencyCategory = "esm" ;
2018-11-14 20:14:05 +08:00
// TODO webpack 6: remove
2019-11-14 22:01:25 +08:00
const deprecatedNormalModuleLoaderHook = util . deprecate (
compilation => {
return require ( "./NormalModule" ) . getCompilationHooks ( compilation ) . loader ;
} ,
"Compilation.hooks.normalModuleLoader was moved to NormalModule.getCompilationHooks(compilation).loader" ,
"DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK"
) ;
2018-11-13 21:45:26 +08:00
2018-10-21 17:11:21 +08:00
const byId = compareSelect (
/ * *
* @ param { Chunk } c chunk
* @ returns { number | string } id
* / c = > c . i d ,
compareIds
) ;
2015-08-18 19:35:57 +08:00
2018-10-21 17:11:21 +08:00
const byNameOrHash = concatComparators (
compareSelect (
/ * *
* @ param { Compilation } c compilation
* @ returns { string } name
* /
c => c . name ,
compareIds
) ,
compareSelect (
/ * *
* @ param { Compilation } c compilation
* @ returns { string } hash
* / c = > c . f u l l H a s h ,
compareIds
)
) ;
2018-04-03 23:23:41 +08:00
2019-10-28 21:02:37 +08:00
const byMessage = compareSelect ( err => ` ${ err . message } ` , compareStringsNumeric ) ;
const byModule = compareSelect (
err => ( err . module && err . module . identifier ( ) ) || "" ,
compareStringsNumeric
) ;
const byLocation = compareSelect ( err => err . loc , compareLocations ) ;
const compareErrors = concatComparators ( byModule , byLocation , byMessage ) ;
2019-09-13 14:42:36 +08:00
/ * *
* @ param { Source } a a source
* @ param { Source } b another source
* @ returns { boolean } true , when both sources are equal
* /
const isSourceEqual = ( a , b ) => {
if ( a === b ) return true ;
// TODO webpack 5: check .buffer() instead, it's called anyway during emit
/** @type {Buffer|string} */
let aSource = a . source ( ) ;
/** @type {Buffer|string} */
let bSource = b . source ( ) ;
if ( aSource === bSource ) return true ;
if ( typeof aSource === "string" && typeof bSource === "string" ) return false ;
if ( ! Buffer . isBuffer ( aSource ) ) aSource = Buffer . from ( aSource , "utf-8" ) ;
if ( ! Buffer . isBuffer ( bSource ) ) bSource = Buffer . from ( bSource , "utf-8" ) ;
return aSource . equals ( bSource ) ;
} ;
2018-06-26 14:27:44 +08:00
class Compilation {
2018-05-04 00:57:02 +08:00
/ * *
* Creates an instance of Compilation .
* @ param { Compiler } compiler the compiler which created the compilation
* /
2017-01-06 01:00:36 +08:00
constructor ( compiler ) {
2018-11-13 21:45:26 +08:00
const getNormalModuleLoader = ( ) => deprecatedNormalModuleLoaderHook ( this ) ;
2020-05-12 18:16:51 +08:00
/** @type {AsyncSeriesHook<[CompilationAssets]>} */
const processAssetsHook = new AsyncSeriesHook ( [ "assets" ] ) ;
/** @type {SyncHook<[CompilationAssets]>} */
const afterProcessAssetsHook = new SyncHook ( [ "assets" ] ) ;
/ * *
* @ template T
* @ param { string } name name of the hook
* @ param { number } stage new stage
* @ param { function ( ) : AsArray < T > } getArgs get old hook function args
* @ param { string = } code deprecation code ( not deprecated when unset )
2020-07-14 18:02:32 +08:00
* @ returns { FakeHook < Pick < AsyncSeriesHook < T > , "tap" | "tapAsync" | "tapPromise" | "name" >> } fake hook which redirects
2020-05-12 18:16:51 +08:00
* /
const createProcessAssetsHook = ( name , stage , getArgs , code ) => {
const errorMessage = reason => ` Can't automatically convert plugin using Compilation.hooks. ${ name } to Compilation.hooks.processAssets because ${ reason } .
BREAKING CHANGE : Asset processing hooks in Compilation has been merged into a single Compilation . hooks . processAssets hook . ` ;
const getOptions = options => {
if ( typeof options === "string" ) options = { name : options } ;
if ( options . stage ) {
throw new Error ( errorMessage ( "it's using the 'stage' option" ) ) ;
}
return { ... options , stage : stage } ;
} ;
2020-07-14 18:02:32 +08:00
return createFakeHook (
{
name ,
/** @type {AsyncSeriesHook<T>["intercept"]} */
intercept ( interceptor ) {
throw new Error ( errorMessage ( "it's using 'intercept'" ) ) ;
} ,
/** @type {AsyncSeriesHook<T>["tap"]} */
tap : ( options , fn ) => {
processAssetsHook . tap ( getOptions ( options ) , ( ) => fn ( ... getArgs ( ) ) ) ;
} ,
/** @type {AsyncSeriesHook<T>["tapAsync"]} */
tapAsync : ( options , fn ) => {
processAssetsHook . tapAsync (
getOptions ( options ) ,
( assets , callback ) =>
/** @type {any} */ ( fn ) ( ... getArgs ( ) , callback )
) ;
} ,
/** @type {AsyncSeriesHook<T>["tapPromise"]} */
tapPromise : ( options , fn ) => {
processAssetsHook . tapPromise ( getOptions ( options ) , ( ) =>
fn ( ... getArgs ( ) )
) ;
}
2020-05-12 18:16:51 +08:00
} ,
2020-07-14 18:02:32 +08:00
` ${ name } is deprecated (use Compilation.hook.processAssets instead and use one of Compilation.PROCESS_ASSETS_STAGE_* as stage option) ` ,
code
2020-05-12 18:16:51 +08:00
) ;
} ;
2018-07-30 20:25:40 +08:00
this . hooks = Object . freeze ( {
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Module]>} */
2017-11-27 22:27:30 +08:00
buildModule : new SyncHook ( [ "module" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Module]>} */
2017-12-07 00:39:42 +08:00
rebuildModule : new SyncHook ( [ "module" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Module, WebpackError]>} */
2017-11-27 22:27:30 +08:00
failedModule : new SyncHook ( [ "module" , "error" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Module]>} */
2017-11-27 22:27:30 +08:00
succeedModule : new SyncHook ( [ "module" ] ) ,
2019-07-25 21:47:31 +08:00
/** @type {SyncHook<[Module]>} */
stillValidModule : new SyncHook ( [ "module" ] ) ,
2017-11-27 22:27:30 +08:00
2020-02-05 04:21:42 +08:00
/** @type {SyncHook<[Dependency, EntryOptions]>} */
addEntry : new SyncHook ( [ "entry" , "options" ] ) ,
/** @type {SyncHook<[Dependency, EntryOptions, Error]>} */
failedEntry : new SyncHook ( [ "entry" , "options" , "error" ] ) ,
/** @type {SyncHook<[Dependency, EntryOptions, Module]>} */
succeedEntry : new SyncHook ( [ "entry" , "options" , "module" ] ) ,
2018-10-29 20:49:31 +08:00
2020-07-28 00:09:48 +08:00
/** @type {SyncWaterfallHook<[(string[] | ReferencedExport)[], Dependency, RuntimeSpec]>} */
2019-10-30 05:28:42 +08:00
dependencyReferencedExports : new SyncWaterfallHook ( [
"referencedExports" ,
2020-07-28 00:09:48 +08:00
"dependency" ,
"runtime"
2018-06-08 16:34:38 +08:00
] ) ,
2018-12-09 19:54:17 +08:00
/** @type {AsyncSeriesHook<[Iterable<Module>]>} */
2018-12-30 21:54:47 +08:00
finishModules : new AsyncSeriesHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {AsyncSeriesHook<[Module]>} */
2018-12-30 21:54:47 +08:00
finishRebuildingModule : new AsyncSeriesHook ( [ "module" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
unseal : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
seal : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2018-07-02 22:18:49 +08:00
beforeChunks : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>]>} */
2018-07-02 22:18:49 +08:00
afterChunks : new SyncHook ( [ "chunks" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
optimizeDependencies : new SyncBailHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
afterOptimizeDependencies : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
optimize : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
optimizeModules : new SyncBailHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
afterOptimizeModules : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[Iterable<Chunk>, ChunkGroup[]]>} */
2018-01-20 00:06:59 +08:00
optimizeChunks : new SyncBailHook ( [ "chunks" , "chunkGroups" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>, ChunkGroup[]]>} */
2018-01-20 00:06:59 +08:00
afterOptimizeChunks : new SyncHook ( [ "chunks" , "chunkGroups" ] ) ,
2017-11-27 22:27:30 +08:00
2018-12-09 19:54:17 +08:00
/** @type {AsyncSeriesHook<[Iterable<Chunk>, Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
optimizeTree : new AsyncSeriesHook ( [ "chunks" , "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>, Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
afterOptimizeTree : new SyncHook ( [ "chunks" , "modules" ] ) ,
2019-12-11 05:58:26 +08:00
/** @type {AsyncSeriesBailHook<[Iterable<Chunk>, Iterable<Module>]>} */
optimizeChunkModules : new AsyncSeriesBailHook ( [ "chunks" , "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>, Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
afterOptimizeChunkModules : new SyncHook ( [ "chunks" , "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[], boolean>} */
2017-11-27 22:27:30 +08:00
shouldRecord : new SyncBailHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Chunk, Set<string>]>} */
2018-11-06 02:03:12 +08:00
additionalChunkRuntimeRequirements : new SyncHook ( [
"chunk" ,
"runtimeRequirements"
] ) ,
2018-12-09 19:54:17 +08:00
/** @type {HookMap<SyncBailHook<[Chunk, Set<string>]>>} */
2018-11-06 02:03:12 +08:00
runtimeRequirementInChunk : new HookMap (
( ) => new SyncBailHook ( [ "chunk" , "runtimeRequirements" ] )
) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Module, Set<string>]>} */
2018-11-17 01:18:44 +08:00
additionalModuleRuntimeRequirements : new SyncHook ( [
"module" ,
"runtimeRequirements"
] ) ,
2018-12-09 19:54:17 +08:00
/** @type {HookMap<SyncBailHook<[Module, Set<string>]>>} */
2018-11-17 01:18:44 +08:00
runtimeRequirementInModule : new HookMap (
( ) => new SyncBailHook ( [ "module" , "runtimeRequirements" ] )
) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Chunk, Set<string>]>} */
2018-11-06 02:03:12 +08:00
additionalTreeRuntimeRequirements : new SyncHook ( [
"chunk" ,
"runtimeRequirements"
] ) ,
2018-12-09 19:54:17 +08:00
/** @type {HookMap<SyncBailHook<[Chunk, Set<string>]>>} */
2018-11-06 02:03:12 +08:00
runtimeRequirementInTree : new HookMap (
( ) => new SyncBailHook ( [ "chunk" , "runtimeRequirements" ] )
) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[RuntimeModule, Chunk]>} */
2018-11-15 00:31:32 +08:00
runtimeModule : new SyncHook ( [ "module" , "chunk" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>, any]>} */
2017-11-27 22:27:30 +08:00
reviveModules : new SyncHook ( [ "modules" , "records" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
beforeModuleIds : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
moduleIds : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
optimizeModuleIds : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2017-11-27 22:27:30 +08:00
afterOptimizeModuleIds : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>, any]>} */
2017-11-27 22:27:30 +08:00
reviveChunks : new SyncHook ( [ "chunks" , "records" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>]>} */
2017-11-27 22:27:30 +08:00
beforeChunkIds : new SyncHook ( [ "chunks" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>]>} */
2018-09-05 20:22:10 +08:00
chunkIds : new SyncHook ( [ "chunks" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>]>} */
2017-11-27 22:27:30 +08:00
optimizeChunkIds : new SyncHook ( [ "chunks" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>]>} */
2017-11-27 22:27:30 +08:00
afterOptimizeChunkIds : new SyncHook ( [ "chunks" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>, any]>} */
2017-11-27 22:27:30 +08:00
recordModules : new SyncHook ( [ "modules" , "records" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Chunk>, any]>} */
2017-11-27 22:27:30 +08:00
recordChunks : new SyncHook ( [ "chunks" , "records" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Iterable<Module>]>} */
2019-01-28 17:40:32 +08:00
optimizeCodeGeneration : new SyncHook ( [ "modules" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2018-11-15 00:31:32 +08:00
beforeModuleHash : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2018-11-15 00:31:32 +08:00
afterModuleHash : new SyncHook ( [ ] ) ,
2019-10-09 04:29:46 +08:00
/** @type {SyncHook<[]>} */
beforeCodeGeneration : new SyncHook ( [ ] ) ,
/** @type {SyncHook<[]>} */
afterCodeGeneration : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2018-11-15 00:31:32 +08:00
beforeRuntimeRequirements : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2018-11-15 00:31:32 +08:00
afterRuntimeRequirements : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
beforeHash : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Chunk]>} */
2018-03-23 02:52:11 +08:00
contentHash : new SyncHook ( [ "chunk" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
afterHash : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[any]>} */
2017-11-27 22:27:30 +08:00
recordHash : new SyncHook ( [ "records" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Compilation, any]>} */
2017-11-27 22:27:30 +08:00
record : new SyncHook ( [ "compilation" , "records" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
beforeModuleAssets : new SyncHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[], boolean>} */
2017-11-27 22:27:30 +08:00
shouldGenerateChunkAssets : new SyncBailHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[]>} */
2017-11-27 22:27:30 +08:00
beforeChunkAssets : new SyncHook ( [ ] ) ,
2020-05-12 18:16:51 +08:00
// TODO webpack 6 remove
/** @deprecated */
additionalChunkAssets : createProcessAssetsHook (
"additionalChunkAssets" ,
Compilation . PROCESS _ASSETS _STAGE _ADDITIONAL ,
( ) => [ this . chunks ] ,
"DEP_WEBPACK_COMPILATION_ADDITIONAL_CHUNK_ASSETS"
) ,
2017-11-27 22:27:30 +08:00
2020-05-12 18:16:51 +08:00
// TODO webpack 6 deprecate
/** @deprecated */
additionalAssets : createProcessAssetsHook (
"additionalAssets" ,
Compilation . PROCESS _ASSETS _STAGE _ADDITIONAL ,
( ) => [ ]
) ,
// TODO webpack 6 remove
/** @deprecated */
optimizeChunkAssets : createProcessAssetsHook (
"optimizeChunkAssets" ,
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE ,
( ) => [ this . chunks ] ,
"DEP_WEBPACK_COMPILATION_OPTIMIZE_CHUNK_ASSETS"
) ,
// TODO webpack 6 remove
/** @deprecated */
afterOptimizeChunkAssets : createProcessAssetsHook (
"afterOptimizeChunkAssets" ,
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE + 1 ,
( ) => [ this . chunks ] ,
"DEP_WEBPACK_COMPILATION_AFTER_OPTIMIZE_CHUNK_ASSETS"
) ,
// TODO webpack 6 deprecate
/** @deprecated */
optimizeAssets : processAssetsHook ,
// TODO webpack 6 deprecate
/** @deprecated */
afterOptimizeAssets : afterProcessAssetsHook ,
2017-11-27 22:27:30 +08:00
2020-05-12 18:16:51 +08:00
processAssets : processAssetsHook ,
afterProcessAssets : afterProcessAssetsHook ,
2019-10-31 03:47:17 +08:00
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[], boolean>} */
2017-11-27 22:27:30 +08:00
needAdditionalSeal : new SyncBailHook ( [ ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {AsyncSeriesHook<[]>} */
2017-11-27 22:27:30 +08:00
afterSeal : new AsyncSeriesHook ( [ ] ) ,
2019-10-02 14:54:21 +08:00
/** @type {SyncWaterfallHook<[RenderManifestEntry[], RenderManifestOptions]>} */
renderManifest : new SyncWaterfallHook ( [ "result" , "options" ] ) ,
/** @type {SyncHook<[Hash]>} */
fullHash : new SyncHook ( [ "hash" ] ) ,
/** @type {SyncHook<[Chunk, Hash, ChunkHashContext]>} */
chunkHash : new SyncHook ( [ "chunk" , "chunkHash" , "ChunkHashContext" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Module, string]>} */
2017-11-27 22:27:30 +08:00
moduleAsset : new SyncHook ( [ "module" , "filename" ] ) ,
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Chunk, string]>} */
2017-11-27 22:27:30 +08:00
chunkAsset : new SyncHook ( [ "chunk" , "filename" ] ) ,
2019-10-02 14:54:21 +08:00
/** @type {SyncWaterfallHook<[string, object, AssetInfo]>} */
assetPath : new SyncWaterfallHook ( [ "path" , "options" , "assetInfo" ] ) ,
2017-11-27 22:27:30 +08:00
2018-12-09 19:54:17 +08:00
/** @type {SyncBailHook<[], boolean>} */
2017-11-27 22:27:30 +08:00
needAdditionalPass : new SyncBailHook ( [ ] ) ,
2018-05-04 00:57:02 +08:00
2018-12-09 19:54:17 +08:00
/** @type {SyncHook<[Compiler, string, number]>} */
2018-02-25 09:00:20 +08:00
childCompiler : new SyncHook ( [
"childCompiler" ,
"compilerName" ,
"compilerIndex"
2018-11-13 21:45:26 +08:00
] ) ,
2019-07-24 16:51:04 +08:00
/** @type {SyncBailHook<[string, LogEntry], true>} */
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
log : new SyncBailHook ( [ "origin" , "logEntry" ] ) ,
2018-12-31 02:39:11 +08:00
/** @type {HookMap<SyncHook<[Object, Object]>>} */
2018-12-19 01:29:12 +08:00
statsPreset : new HookMap ( ( ) => new SyncHook ( [ "options" , "context" ] ) ) ,
2018-12-31 02:39:11 +08:00
/** @type {SyncHook<[Object, Object]>} */
2018-12-19 01:29:12 +08:00
statsNormalize : new SyncHook ( [ "options" , "context" ] ) ,
2018-12-31 02:39:11 +08:00
/** @type {SyncHook<[StatsFactory, Object]>} */
2018-12-19 01:29:12 +08:00
statsFactory : new SyncHook ( [ "statsFactory" , "options" ] ) ,
2018-12-31 02:39:11 +08:00
/** @type {SyncHook<[StatsPrinter, Object]>} */
2018-12-19 01:29:12 +08:00
statsPrinter : new SyncHook ( [ "statsPrinter" , "options" ] ) ,
2018-11-13 21:45:26 +08:00
get normalModuleLoader ( ) {
return getNormalModuleLoader ( ) ;
}
2018-07-30 20:25:40 +08:00
} ) ;
2018-05-04 00:57:02 +08:00
/** @type {string=} */
2018-03-30 08:33:19 +08:00
this . name = undefined ;
2018-05-04 00:57:02 +08:00
/** @type {Compiler} */
2017-01-06 01:00:36 +08:00
this . compiler = compiler ;
2017-11-17 21:26:23 +08:00
this . resolverFactory = compiler . resolverFactory ;
2017-01-06 01:00:36 +08:00
this . inputFileSystem = compiler . inputFileSystem ;
2019-08-13 04:59:09 +08:00
this . fileSystemInfo = new FileSystemInfo ( this . inputFileSystem , {
2019-08-16 17:55:10 +08:00
managedPaths : compiler . managedPaths ,
2019-11-04 17:06:53 +08:00
immutablePaths : compiler . immutablePaths ,
logger : this . getLogger ( "webpack.FileSystemInfo" )
2019-08-13 04:59:09 +08:00
} ) ;
2018-09-28 03:28:07 +08:00
if ( compiler . fileTimestamps ) {
this . fileSystemInfo . addFileTimestamps ( compiler . fileTimestamps ) ;
}
if ( compiler . contextTimestamps ) {
this . fileSystemInfo . addContextTimestamps ( compiler . contextTimestamps ) ;
}
2017-12-01 17:44:22 +08:00
this . requestShortener = compiler . requestShortener ;
2018-09-26 16:28:16 +08:00
this . compilerPath = compiler . compilerPath ;
2017-01-06 01:00:36 +08:00
2020-01-30 18:34:33 +08:00
this . logger = this . getLogger ( "webpack.Compilation" ) ;
2018-09-19 19:05:22 +08:00
const options = compiler . options ;
this . options = options ;
2017-01-06 01:00:36 +08:00
this . outputOptions = options && options . output ;
2018-08-22 20:54:28 +08:00
/** @type {boolean} */
this . bail = ( options && options . bail ) || false ;
/** @type {boolean} */
this . profile = ( options && options . profile ) || false ;
2017-01-06 01:00:36 +08:00
2019-10-02 14:54:21 +08:00
this . mainTemplate = new MainTemplate ( this . outputOptions , this ) ;
this . chunkTemplate = new ChunkTemplate ( this . outputOptions , this ) ;
2018-02-25 09:00:20 +08:00
this . runtimeTemplate = new RuntimeTemplate (
this . outputOptions ,
this . requestShortener
) ;
2019-10-02 14:54:21 +08:00
/** @type {{javascript: ModuleTemplate}} */
2017-10-30 20:56:57 +08:00
this . moduleTemplates = {
2019-10-02 14:54:21 +08:00
javascript : new ModuleTemplate ( this . runtimeTemplate , this )
2017-10-30 20:56:57 +08:00
} ;
2019-10-02 14:54:21 +08:00
Object . defineProperties ( this . moduleTemplates , {
asset : {
enumerable : false ,
configurable : false ,
get ( ) {
throw new WebpackError (
"Compilation.moduleTemplates.asset has been removed"
) ;
}
} ,
webassembly : {
enumerable : false ,
configurable : false ,
get ( ) {
throw new WebpackError (
"Compilation.moduleTemplates.webassembly has been removed"
) ;
}
}
} ) ;
2017-01-06 01:00:36 +08:00
2018-07-17 22:41:07 +08:00
this . moduleGraph = new ModuleGraph ( ) ;
2018-08-14 17:18:22 +08:00
this . chunkGraph = undefined ;
2020-07-28 00:09:48 +08:00
/** @type {CodeGenerationResults} */
2019-10-09 04:29:46 +08:00
this . codeGenerationResults = undefined ;
2018-07-17 22:41:07 +08:00
2019-11-08 18:13:16 +08:00
/** @type {AsyncQueue<FactorizeModuleOptions, string, Module>} */
2018-09-12 00:47:55 +08:00
this . factorizeQueue = new AsyncQueue ( {
name : "factorize" ,
parallelism : options . parallelism || 100 ,
processor : this . _factorizeModule . bind ( this )
} ) ;
2018-09-26 16:27:09 +08:00
/** @type {AsyncQueue<Module, string, Module>} */
this . addModuleQueue = new AsyncQueue ( {
name : "addModule" ,
parallelism : options . parallelism || 100 ,
getKey : module => module . identifier ( ) ,
processor : this . _addModule . bind ( this )
} ) ;
/** @type {AsyncQueue<Module, Module, Module>} */
2018-09-12 00:47:55 +08:00
this . buildQueue = new AsyncQueue ( {
name : "build" ,
parallelism : options . parallelism || 100 ,
processor : this . _buildModule . bind ( this )
} ) ;
2018-09-26 16:27:09 +08:00
/** @type {AsyncQueue<Module, Module, Module>} */
2018-09-12 00:47:55 +08:00
this . rebuildQueue = new AsyncQueue ( {
name : "rebuild" ,
parallelism : options . parallelism || 100 ,
processor : this . _rebuildModule . bind ( this )
} ) ;
2018-09-26 16:27:09 +08:00
/** @type {AsyncQueue<Module, Module, Module>} */
2018-09-12 00:47:55 +08:00
this . processDependenciesQueue = new AsyncQueue ( {
name : "processDependencies" ,
parallelism : options . parallelism || 100 ,
processor : this . _processModuleDependencies . bind ( this )
} ) ;
2017-08-11 22:11:17 +08:00
2019-12-20 19:45:55 +08:00
/ * *
* Modules in value are building during the build of Module in key .
* Means value blocking key from finishing .
* Needed to detect build cycles .
* @ type { WeakMap < Module , Set < Module >> }
* /
this . creatingModuleDuringBuild = new WeakMap ( ) ;
2020-02-05 04:35:01 +08:00
/** @type {Map<string, EntryData>} */
this . entries = new Map ( ) ;
2020-05-23 22:08:51 +08:00
/** @type {EntryData} */
this . globalEntry = {
dependencies : [ ] ,
includeDependencies : [ ] ,
options : {
name : undefined
}
} ;
2018-08-14 22:40:37 +08:00
/** @type {Map<string, Entrypoint>} */
2018-01-20 00:06:59 +08:00
this . entrypoints = new Map ( ) ;
2018-09-06 22:59:11 +08:00
/** @type {Set<Chunk>} */
this . chunks = new Set ( ) ;
arrayToSetDeprecation ( this . chunks , "Compilation.chunks" ) ;
2018-05-04 00:57:02 +08:00
/** @type {ChunkGroup[]} */
2018-01-20 00:06:59 +08:00
this . chunkGroups = [ ] ;
2018-05-04 00:57:02 +08:00
/** @type {Map<string, ChunkGroup>} */
2018-01-20 00:06:59 +08:00
this . namedChunkGroups = new Map ( ) ;
2018-05-04 00:57:02 +08:00
/** @type {Map<string, Chunk>} */
2018-01-20 00:06:59 +08:00
this . namedChunks = new Map ( ) ;
2018-09-05 22:12:48 +08:00
/** @type {Set<Module>} */
this . modules = new Set ( ) ;
arrayToSetDeprecation ( this . modules , "Compilation.modules" ) ;
2018-05-04 00:57:02 +08:00
/** @private @type {Map<string, Module>} */
2017-11-06 20:02:35 +08:00
this . _modules = new Map ( ) ;
2017-01-06 01:00:36 +08:00
this . records = null ;
2018-05-04 00:57:02 +08:00
/** @type {string[]} */
2017-01-06 01:00:36 +08:00
this . additionalChunkAssets = [ ] ;
2018-05-04 00:57:02 +08:00
/** @type {CompilationAssets} */
2017-01-06 01:00:36 +08:00
this . assets = { } ;
2019-09-11 17:13:46 +08:00
/** @type {Map<string, AssetInfo>} */
this . assetsInfo = new Map ( ) ;
2018-05-04 00:57:02 +08:00
/** @type {WebpackError[]} */
2017-01-06 01:00:36 +08:00
this . errors = [ ] ;
2018-05-04 00:57:02 +08:00
/** @type {WebpackError[]} */
2017-01-06 01:00:36 +08:00
this . warnings = [ ] ;
2018-05-04 00:57:02 +08:00
/** @type {Compilation[]} */
2017-01-06 01:00:36 +08:00
this . children = [ ] ;
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
/** @type {Map<string, LogEntry[]>} */
this . logging = new Map ( ) ;
2018-05-04 00:57:02 +08:00
/** @type {Map<DepConstructor, ModuleFactory>} */
2017-01-06 01:00:36 +08:00
this . dependencyFactories = new Map ( ) ;
2018-07-12 23:12:00 +08:00
/** @type {DependencyTemplates} */
this . dependencyTemplates = new DependencyTemplates ( ) ;
2017-04-13 19:43:51 +08:00
this . childrenCounters = { } ;
2018-05-15 18:20:17 +08:00
/** @type {Set<number|string>} */
2018-01-05 14:41:09 +08:00
this . usedChunkIds = null ;
2018-05-04 00:57:02 +08:00
/** @type {Set<number>} */
2018-01-05 14:41:09 +08:00
this . usedModuleIds = null ;
2018-07-20 19:35:01 +08:00
/** @type {boolean} */
this . needAdditionalPass = false ;
2018-08-22 21:33:16 +08:00
/** @type {WeakSet<Module>} */
this . builtModules = new WeakSet ( ) ;
2018-05-15 18:20:17 +08:00
/** @private @type {Map<Module, Callback[]>} */
2017-11-06 20:02:35 +08:00
this . _rebuildingModules = new Map ( ) ;
2019-01-19 03:38:34 +08:00
/** @type {Set<string>} */
this . emittedAssets = new Set ( ) ;
2019-11-04 16:23:18 +08:00
/** @type {Set<string>} */
this . comparedForEmitAssets = new Set ( ) ;
2019-08-07 15:54:43 +08:00
/** @type {LazySet<string>} */
this . fileDependencies = new LazySet ( ) ;
/** @type {LazySet<string>} */
this . contextDependencies = new LazySet ( ) ;
/** @type {LazySet<string>} */
this . missingDependencies = new LazySet ( ) ;
2019-08-09 20:46:42 +08:00
/** @type {LazySet<string>} */
this . buildDependencies = new LazySet ( ) ;
2019-10-10 19:48:56 +08:00
// TODO webpack 6 remove
this . compilationDependencies = {
add : util . deprecate (
item => this . fileDependencies . add ( item ) ,
"Compilation.compilationDependencies is deprecated (used Compilation.fileDependencies instead)" ,
"DEP_WEBPACK_COMPILATION_COMPILATION_DEPENDENCIES"
)
} ;
2020-07-15 17:14:28 +08:00
this . _modulesCache = this . getCache ( "Compilation/modules" ) ;
this . _assetsCache = this . getCache ( "Compilation/assets" ) ;
2020-07-28 00:09:48 +08:00
this . _codeGenerationCache = this . getCache ( "Compilation/codeGeneration" ) ;
2017-01-06 01:00:36 +08:00
}
2014-12-22 23:10:23 +08:00
2017-04-06 19:52:06 +08:00
getStats ( ) {
return new Stats ( this ) ;
2017-01-06 01:00:36 +08:00
}
2014-12-22 23:10:23 +08:00
2018-12-19 01:29:12 +08:00
createStatsOptions ( optionsOrPreset , context = { } ) {
if (
typeof optionsOrPreset === "boolean" ||
typeof optionsOrPreset === "string"
) {
optionsOrPreset = { preset : optionsOrPreset } ;
}
if ( typeof optionsOrPreset === "object" && optionsOrPreset !== null ) {
2019-07-25 21:51:11 +08:00
// We use this method of shallow cloning this object to include
// properties in the prototype chain
const options = { } ;
for ( const key in optionsOrPreset ) {
options [ key ] = optionsOrPreset [ key ] ;
}
2018-12-19 01:29:12 +08:00
if ( options . preset !== undefined ) {
2019-05-11 03:50:13 +08:00
this . hooks . statsPreset . for ( options . preset ) . call ( options , context ) ;
2018-12-19 01:29:12 +08:00
}
this . hooks . statsNormalize . call ( options , context ) ;
return options ;
} else {
const options = { } ;
this . hooks . statsNormalize . call ( options , context ) ;
return options ;
}
}
createStatsFactory ( options ) {
const statsFactory = new StatsFactory ( ) ;
this . hooks . statsFactory . call ( statsFactory , options ) ;
return statsFactory ;
}
createStatsPrinter ( options ) {
const statsPrinter = new StatsPrinter ( ) ;
this . hooks . statsPrinter . call ( statsPrinter , options ) ;
return statsPrinter ;
}
2020-07-15 17:14:28 +08:00
/ * *
* @ param { string } name cache name
* @ returns { CacheFacade } the cache facade instance
* /
getCache ( name ) {
return this . compiler . getCache ( name ) ;
}
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
/ * *
* @ param { string | ( function ( ) : string ) } name name of the logger , or function called once to get the logger name
* @ returns { Logger } a logger with that name
* /
getLogger ( name ) {
if ( ! name ) {
throw new TypeError ( "Compilation.getLogger(name) called without a name" ) ;
}
2019-07-23 15:28:06 +08:00
/** @type {LogEntry[] | undefined} */
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
let logEntries ;
2019-11-05 23:47:45 +08:00
return new Logger (
( type , args ) => {
if ( typeof name === "function" ) {
name = name ( ) ;
if ( ! name ) {
throw new TypeError (
"Compilation.getLogger(name) called with a function not returning a name"
) ;
}
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
}
2019-11-05 23:47:45 +08:00
let trace ;
switch ( type ) {
case LogType . warn :
case LogType . error :
case LogType . trace :
trace = ErrorHelpers . cutOffLoaderExecution ( new Error ( "Trace" ) . stack )
. split ( "\n" )
. slice ( 3 ) ;
break ;
}
/** @type {LogEntry} */
const logEntry = {
time : Date . now ( ) ,
type ,
args ,
trace
} ;
if ( this . hooks . log . call ( name , logEntry ) === undefined ) {
if ( logEntry . type === LogType . profileEnd ) {
2019-07-22 14:19:30 +08:00
// eslint-disable-next-line node/no-unsupported-features/node-builtins
2019-11-05 23:47:45 +08:00
if ( typeof console . profileEnd === "function" ) {
// eslint-disable-next-line node/no-unsupported-features/node-builtins
console . profileEnd ( ` [ ${ name } ] ${ logEntry . args [ 0 ] } ` ) ;
}
2019-07-22 14:19:30 +08:00
}
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
if ( logEntries === undefined ) {
2019-11-05 23:47:45 +08:00
logEntries = this . logging . get ( name ) ;
if ( logEntries === undefined ) {
logEntries = [ ] ;
this . logging . set ( name , logEntries ) ;
}
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
}
2019-11-05 23:47:45 +08:00
logEntries . push ( logEntry ) ;
if ( logEntry . type === LogType . profile ) {
2019-07-22 14:19:30 +08:00
// eslint-disable-next-line node/no-unsupported-features/node-builtins
2019-11-05 23:47:45 +08:00
if ( typeof console . profile === "function" ) {
// eslint-disable-next-line node/no-unsupported-features/node-builtins
console . profile ( ` [ ${ name } ] ${ logEntry . args [ 0 ] } ` ) ;
}
}
}
} ,
childName => {
if ( typeof name === "function" ) {
if ( typeof childName === "function" ) {
return this . getLogger ( ( ) => {
if ( typeof name === "function" ) {
name = name ( ) ;
if ( ! name ) {
throw new TypeError (
"Compilation.getLogger(name) called with a function not returning a name"
) ;
}
}
if ( typeof childName === "function" ) {
childName = childName ( ) ;
if ( ! childName ) {
throw new TypeError (
"Logger.getChildLogger(name) called with a function not returning a name"
) ;
}
}
return ` ${ name } / ${ childName } ` ;
} ) ;
} else {
return this . getLogger ( ( ) => {
if ( typeof name === "function" ) {
name = name ( ) ;
if ( ! name ) {
throw new TypeError (
"Compilation.getLogger(name) called with a function not returning a name"
) ;
}
}
return ` ${ name } / ${ childName } ` ;
} ) ;
}
} else {
if ( typeof childName === "function" ) {
return this . getLogger ( ( ) => {
if ( typeof childName === "function" ) {
childName = childName ( ) ;
if ( ! childName ) {
throw new TypeError (
"Logger.getChildLogger(name) called with a function not returning a name"
) ;
}
}
return ` ${ name } / ${ childName } ` ;
} ) ;
} else {
return this . getLogger ( ` ${ name } / ${ childName } ` ) ;
2019-07-22 14:19:30 +08:00
}
}
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
}
2019-11-05 23:47:45 +08:00
) ;
add logging API
Plugins:
Compiler.getInfrastructureLogger(name)
Compilation.getLogger(name)
Loader:
this.getLogger([name])
API equal to console API with these methods:
error, warn, info, log, debug,
time, timeLog, timeEnd,
group, groupCollapsed, groupEnd,
profile, profileEnd,
clear
2019-07-18 23:13:40 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
* @ param { Module } module module to be added that was created
2018-09-26 16:27:09 +08:00
* @ param { ModuleCallback } callback returns the module in the compilation ,
* it could be the passed one ( if new ) , or an already existing in the compilation
* @ returns { void }
* /
addModule ( module , callback ) {
this . addModuleQueue . add ( module , callback ) ;
}
/ * *
* @ param { Module } module module to be added that was created
* @ param { ModuleCallback } callback returns the module in the compilation ,
2018-09-12 00:47:55 +08:00
* it could be the passed one ( if new ) , or an already existing in the compilation
2018-09-26 16:27:09 +08:00
* @ returns { void }
2018-05-04 00:57:02 +08:00
* /
2018-09-26 16:27:09 +08:00
_addModule ( module , callback ) {
2017-01-06 01:00:36 +08:00
const identifier = module . identifier ( ) ;
2017-12-07 00:22:10 +08:00
const alreadyAddedModule = this . _modules . get ( identifier ) ;
2018-02-25 09:00:20 +08:00
if ( alreadyAddedModule ) {
2018-09-26 16:27:09 +08:00
return callback ( null , alreadyAddedModule ) ;
2013-01-31 01:49:25 +08:00
}
2013-02-01 01:00:22 +08:00
2018-10-09 20:30:59 +08:00
const currentProfile = this . profile
? this . moduleGraph . getProfile ( module )
: undefined ;
if ( currentProfile !== undefined ) {
currentProfile . markRestoringStart ( ) ;
}
2020-07-15 17:14:28 +08:00
this . _modulesCache . get ( identifier , null , ( err , cacheModule ) => {
2018-10-18 04:54:07 +08:00
if ( err ) return callback ( new ModuleRestoreError ( module , err ) ) ;
2018-03-28 22:19:15 +08:00
2018-10-09 20:30:59 +08:00
if ( currentProfile !== undefined ) {
currentProfile . markRestoringEnd ( ) ;
currentProfile . markIntegrationStart ( ) ;
}
2018-09-27 13:22:19 +08:00
if ( cacheModule ) {
cacheModule . updateCacheModule ( module ) ;
module = cacheModule ;
2018-09-26 16:27:09 +08:00
}
2018-09-27 13:22:19 +08:00
this . _modules . set ( identifier , module ) ;
this . modules . add ( module ) ;
ModuleGraph . setModuleGraphForModule ( module , this . moduleGraph ) ;
2018-10-09 20:30:59 +08:00
if ( currentProfile !== undefined ) {
currentProfile . markIntegrationEnd ( ) ;
}
2018-09-27 13:22:19 +08:00
callback ( null , module ) ;
} ) ;
2013-01-31 01:49:25 +08:00
}
2017-01-06 01:00:36 +08:00
2018-05-04 00:57:02 +08:00
/ * *
* Fetches a module from a compilation by its identifier
* @ param { Module } module the module provided
* @ returns { Module } the module requested
* /
2017-01-06 01:00:36 +08:00
getModule ( module ) {
const identifier = module . identifier ( ) ;
2017-11-06 20:02:35 +08:00
return this . _modules . get ( identifier ) ;
2013-01-31 01:49:25 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
* Attempts to search for a module by its identifier
* @ param { string } identifier identifier ( usually path ) for module
* @ returns { Module | undefined } attempt to search for module and return it , else undefined
* /
2017-01-06 01:00:36 +08:00
findModule ( identifier ) {
2017-11-06 20:02:35 +08:00
return this . _modules . get ( identifier ) ;
}
2018-05-04 00:57:02 +08:00
/ * *
2018-09-12 00:47:55 +08:00
* Schedules a build of the module object
*
* @ param { Module } module module to be built
* @ param { ModuleCallback } callback the callback
2018-05-04 00:57:02 +08:00
* @ returns { void }
* /
2018-09-12 00:47:55 +08:00
buildModule ( module , callback ) {
this . buildQueue . add ( module , callback ) ;
2017-01-06 01:00:36 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
* Builds the module object
*
* @ param { Module } module module to be built
2018-09-12 00:47:55 +08:00
* @ param { ModuleCallback } callback the callback
2018-12-10 18:34:59 +08:00
* @ returns { void }
2018-05-04 00:57:02 +08:00
* /
2018-09-12 00:47:55 +08:00
_buildModule ( module , callback ) {
const currentProfile = this . profile
? this . moduleGraph . getProfile ( module )
: undefined ;
if ( currentProfile !== undefined ) {
currentProfile . markBuildingStart ( ) ;
2017-01-06 01:00:36 +08:00
}
2017-11-06 20:02:35 +08:00
2018-09-26 15:14:44 +08:00
module . needBuild (
{
2018-09-27 13:22:19 +08:00
fileSystemInfo : this . fileSystemInfo
2018-09-26 15:14:44 +08:00
} ,
( err , needBuild ) => {
if ( err ) return callback ( err ) ;
if ( ! needBuild ) {
if ( currentProfile !== undefined ) {
currentProfile . markBuildingEnd ( ) ;
}
2019-07-25 21:47:31 +08:00
this . hooks . stillValidModule . call ( module ) ;
2018-09-26 15:14:44 +08:00
return callback ( ) ;
2018-09-12 00:47:55 +08:00
}
2018-09-26 15:14:44 +08:00
this . hooks . buildModule . call ( module ) ;
this . builtModules . add ( module ) ;
module . build (
this . options ,
this ,
this . resolverFactory . get ( "normal" , module . resolveOptions ) ,
this . inputFileSystem ,
err => {
if ( currentProfile !== undefined ) {
currentProfile . markBuildingEnd ( ) ;
}
if ( err ) {
this . hooks . failedModule . call ( module , err ) ;
return callback ( err ) ;
}
2018-09-27 13:22:19 +08:00
if ( currentProfile !== undefined ) {
currentProfile . markStoringStart ( ) ;
}
2020-07-15 17:14:28 +08:00
this . _modulesCache . store ( module . identifier ( ) , null , module , err => {
if ( currentProfile !== undefined ) {
currentProfile . markStoringEnd ( ) ;
2018-09-27 13:22:19 +08:00
}
2020-07-15 17:14:28 +08:00
if ( err ) {
this . hooks . failedModule . call ( module , err ) ;
return callback ( new ModuleStoreError ( module , err ) ) ;
}
this . hooks . succeedModule . call ( module ) ;
return callback ( ) ;
} ) ;
2018-09-26 15:14:44 +08:00
}
) ;
2013-01-31 01:49:25 +08:00
}
2018-02-25 09:00:20 +08:00
) ;
2017-01-06 01:00:36 +08:00
}
2015-07-08 20:15:21 +08:00
2018-05-04 00:57:02 +08:00
/ * *
* @ param { Module } module to be processed for deps
2018-05-07 05:01:47 +08:00
* @ param { ModuleCallback } callback callback to be triggered
2018-05-04 00:57:02 +08:00
* @ returns { void }
* /
2017-01-06 01:00:36 +08:00
processModuleDependencies ( module , callback ) {
2018-09-12 00:47:55 +08:00
this . processDependenciesQueue . add ( module , callback ) ;
}
2020-04-26 23:40:12 +08:00
/ * *
* @ param { Module } module to be processed for deps
* @ returns { void }
* /
processModuleDependenciesNonRecursive ( module ) {
const processDependenciesBlock = block => {
if ( block . dependencies ) {
for ( const dep of block . dependencies ) {
this . moduleGraph . setParents ( dep , block , module ) ;
}
}
if ( block . blocks ) {
for ( const b of block . blocks ) processDependenciesBlock ( b ) ;
}
} ;
processDependenciesBlock ( module ) ;
}
2018-09-12 00:47:55 +08:00
/ * *
* @ param { Module } module to be processed for deps
* @ param { ModuleCallback } callback callback to be triggered
* @ returns { void }
* /
_processModuleDependencies ( module , callback ) {
2017-11-19 07:22:38 +08:00
const dependencies = new Map ( ) ;
2017-01-06 01:00:36 +08:00
2020-05-27 07:27:52 +08:00
/ * *
* @ type { Array < { factory : ModuleFactory , dependencies : Dependency [ ] , originModule : Module | null } > }
* /
2019-08-07 17:57:11 +08:00
const sortedDependencies = [ ] ;
2018-08-07 20:20:53 +08:00
let currentBlock = module ;
2019-07-26 19:35:29 +08:00
let factoryCacheKey ;
let factoryCacheValue ;
2019-08-07 17:57:11 +08:00
let factoryCacheValue2 ;
2020-05-28 06:23:54 +08:00
let listCacheKey ;
2019-07-26 19:35:29 +08:00
let listCacheValue ;
2018-08-07 20:20:53 +08:00
const processDependency = dep => {
this . moduleGraph . setParents ( dep , currentBlock , module ) ;
2020-05-28 15:10:31 +08:00
const resourceIdent = dep . getResourceIdentifier ( ) ;
2018-02-25 09:00:20 +08:00
if ( resourceIdent ) {
2020-05-31 05:56:12 +08:00
// Here webpack is using heuristic that assumes
// mostly esm dependencies would be used
// so we don't allocate extra string for them
2020-06-18 05:01:18 +08:00
const category = dep . category ;
2020-05-28 15:10:31 +08:00
const cacheKey =
2020-06-18 05:01:18 +08:00
category === esmDependencyCategory
2020-05-28 15:10:31 +08:00
? resourceIdent
2020-06-18 05:01:18 +08:00
: ` ${ category } ${ resourceIdent } ` ;
2019-07-26 19:35:29 +08:00
const constructor = dep . constructor ;
2020-05-28 06:23:54 +08:00
let innerMap ;
2019-08-07 17:57:11 +08:00
let factory ;
2019-07-26 19:35:29 +08:00
if ( factoryCacheKey === constructor ) {
2020-05-28 06:23:54 +08:00
innerMap = factoryCacheValue ;
2020-05-28 15:10:31 +08:00
if ( listCacheKey === cacheKey ) {
2019-07-26 19:35:29 +08:00
listCacheValue . push ( dep ) ;
return ;
}
} else {
2019-08-07 17:57:11 +08:00
factory = this . dependencyFactories . get ( dep . constructor ) ;
2019-07-26 19:35:29 +08:00
if ( factory === undefined ) {
throw new Error (
` No module factory available for dependency type: ${ dep . constructor . name } `
) ;
}
2020-05-28 06:23:54 +08:00
innerMap = dependencies . get ( factory ) ;
if ( innerMap === undefined ) {
dependencies . set ( factory , ( innerMap = new Map ( ) ) ) ;
2019-07-26 19:35:29 +08:00
}
factoryCacheKey = constructor ;
2020-05-28 06:23:54 +08:00
factoryCacheValue = innerMap ;
2019-08-07 17:57:11 +08:00
factoryCacheValue2 = factory ;
2018-05-29 20:50:40 +08:00
}
2020-05-28 15:10:31 +08:00
let list = innerMap . get ( cacheKey ) ;
2019-08-07 17:57:11 +08:00
if ( list === undefined ) {
2020-05-28 15:10:31 +08:00
innerMap . set ( cacheKey , ( list = [ ] ) ) ;
2019-08-07 17:57:11 +08:00
sortedDependencies . push ( {
factory : factoryCacheValue2 ,
dependencies : list ,
originModule : module
} ) ;
}
2017-11-19 07:22:38 +08:00
list . push ( dep ) ;
2020-05-28 15:10:31 +08:00
listCacheKey = cacheKey ;
2019-07-26 19:35:29 +08:00
listCacheValue = list ;
2014-06-04 03:03:21 +08:00
}
2017-11-08 18:32:05 +08:00
} ;
2017-01-06 01:00:36 +08:00
2018-08-07 20:20:53 +08:00
const processDependenciesBlock = block => {
2018-02-25 09:00:20 +08:00
if ( block . dependencies ) {
2018-08-07 20:20:53 +08:00
currentBlock = block ;
2019-07-10 02:49:10 +08:00
for ( const dep of block . dependencies ) processDependency ( dep ) ;
2014-06-04 03:03:21 +08:00
}
2018-02-25 09:00:20 +08:00
if ( block . blocks ) {
2019-07-10 02:49:10 +08:00
for ( const b of block . blocks ) processDependenciesBlock ( b ) ;
2013-01-31 01:49:25 +08:00
}
2017-11-08 18:32:05 +08:00
} ;
2017-11-19 16:06:40 +08:00
try {
2018-08-07 20:20:53 +08:00
processDependenciesBlock ( module ) ;
2018-02-25 09:00:20 +08:00
} catch ( e ) {
2018-09-07 21:51:27 +08:00
return callback ( e ) ;
2017-11-19 16:06:40 +08:00
}
2017-11-19 07:22:38 +08:00
2019-01-05 18:19:30 +08:00
if ( sortedDependencies . length === 0 ) {
callback ( ) ;
return ;
}
2019-11-08 19:23:04 +08:00
// This is nested so we need to allow one additional task
this . processDependenciesQueue . increaseParallelism ( ) ;
asyncLib . forEach (
sortedDependencies ,
( item , callback ) => {
this . handleModuleCreation ( item , err => {
// In V8, the Error objects keep a reference to the functions on the stack. These warnings &
// errors are created inside closures that keep a reference to the Compilation, so errors are
// leaking the Compilation object.
if ( err && this . bail ) {
// eslint-disable-next-line no-self-assign
err . stack = err . stack ;
return callback ( err ) ;
}
callback ( ) ;
} ) ;
} ,
err => {
this . processDependenciesQueue . decreaseParallelism ( ) ;
2018-09-12 00:47:55 +08:00
2019-11-08 19:23:04 +08:00
return callback ( err ) ;
}
) ;
2017-01-06 01:00:36 +08:00
}
2013-05-08 19:28:54 +08:00
2018-05-04 00:57:02 +08:00
/ * *
2018-09-12 00:47:55 +08:00
* @ typedef { Object } HandleModuleCreationOptions
* @ property { ModuleFactory } factory
2018-09-07 21:51:27 +08:00
* @ property { Dependency [ ] } dependencies
2018-09-12 00:47:55 +08:00
* @ property { Module | null } originModule
* @ property { string = } context
2019-12-20 19:45:55 +08:00
* @ property { boolean = } recursive recurse into dependencies of the created module
2018-09-07 21:51:27 +08:00
* /
/ * *
2018-09-12 00:47:55 +08:00
* @ param { HandleModuleCreationOptions } options options object
2018-09-07 21:51:27 +08:00
* @ param { ModuleCallback } callback callback
2018-05-04 00:57:02 +08:00
* @ returns { void }
* /
2018-09-12 00:47:55 +08:00
handleModuleCreation (
2019-12-20 19:45:55 +08:00
{ factory , dependencies , originModule , context , recursive = true } ,
2018-09-07 21:51:27 +08:00
callback
) {
2018-09-12 00:47:55 +08:00
const moduleGraph = this . moduleGraph ;
2018-08-22 20:54:28 +08:00
2018-09-12 00:47:55 +08:00
const currentProfile = this . profile ? new ModuleProfile ( ) : undefined ;
2013-01-31 01:49:25 +08:00
2018-09-12 00:47:55 +08:00
this . factorizeModule (
{ currentProfile , factory , dependencies , originModule , context } ,
( err , newModule ) => {
if ( err ) {
if ( dependencies . every ( d => d . optional ) ) {
this . warnings . push ( err ) ;
} else {
this . errors . push ( err ) ;
}
return callback ( err ) ;
2018-09-07 21:51:27 +08:00
}
2018-09-12 00:47:55 +08:00
if ( ! newModule ) {
return callback ( ) ;
2018-09-07 21:51:27 +08:00
}
2018-09-12 00:47:55 +08:00
2018-10-09 20:30:59 +08:00
if ( currentProfile !== undefined ) {
moduleGraph . setProfile ( newModule , currentProfile ) ;
}
2018-09-26 16:27:09 +08:00
this . addModule ( newModule , ( err , module ) => {
if ( err ) {
2018-09-07 21:51:27 +08:00
if ( ! err . module ) {
err . module = module ;
}
this . errors . push ( err ) ;
2018-09-12 00:47:55 +08:00
return callback ( err ) ;
2018-09-07 20:11:48 +08:00
}
2018-09-07 21:51:27 +08:00
2018-09-26 16:27:09 +08:00
for ( let i = 0 ; i < dependencies . length ; i ++ ) {
const dependency = dependencies [ i ] ;
2019-10-30 07:11:26 +08:00
moduleGraph . setResolvedModule ( originModule , dependency , module ) ;
2018-09-26 16:27:09 +08:00
}
2019-08-07 17:56:26 +08:00
moduleGraph . setIssuerIfUnset (
module ,
originModule !== undefined ? originModule : null
) ;
2018-10-09 20:30:59 +08:00
if ( module !== newModule ) {
2018-09-26 16:27:09 +08:00
if ( currentProfile !== undefined ) {
2018-10-09 20:30:59 +08:00
const otherProfile = moduleGraph . getProfile ( module ) ;
if ( otherProfile !== undefined ) {
currentProfile . mergeInto ( otherProfile ) ;
} else {
moduleGraph . setProfile ( module , currentProfile ) ;
}
2018-09-26 16:27:09 +08:00
}
2018-02-25 09:00:20 +08:00
}
2018-09-07 21:51:27 +08:00
2019-12-20 19:45:55 +08:00
// Check for cycles when build is trigger inside another build
let creatingModuleDuringBuildSet = undefined ;
if ( ! recursive && this . buildQueue . isProcessing ( originModule ) ) {
// Track build dependency
creatingModuleDuringBuildSet = this . creatingModuleDuringBuild . get (
originModule
) ;
if ( creatingModuleDuringBuildSet === undefined ) {
creatingModuleDuringBuildSet = new Set ( ) ;
this . creatingModuleDuringBuild . set (
originModule ,
creatingModuleDuringBuildSet
) ;
}
creatingModuleDuringBuildSet . add ( originModule ) ;
// When building is blocked by another module
// search for a cycle, cancel the cycle by throwing
// an error (otherwise this would deadlock)
const blockReasons = this . creatingModuleDuringBuild . get ( module ) ;
if ( blockReasons !== undefined ) {
const set = new Set ( blockReasons ) ;
for ( const item of set ) {
const blockReasons = this . creatingModuleDuringBuild . get ( item ) ;
if ( blockReasons !== undefined ) {
for ( const m of blockReasons ) {
if ( m === module ) {
return callback ( new BuildCycleError ( module ) ) ;
}
set . add ( m ) ;
}
}
}
}
}
2018-09-26 16:27:09 +08:00
this . buildModule ( module , err => {
2019-12-20 19:45:55 +08:00
if ( creatingModuleDuringBuildSet !== undefined ) {
creatingModuleDuringBuildSet . delete ( module ) ;
}
2018-09-12 00:47:55 +08:00
if ( err ) {
2018-09-26 16:27:09 +08:00
if ( ! err . module ) {
err . module = module ;
}
this . errors . push ( err ) ;
2018-09-12 00:47:55 +08:00
return callback ( err ) ;
}
2018-09-26 16:27:09 +08:00
2019-12-20 19:45:55 +08:00
if ( ! recursive ) {
2020-04-26 23:40:12 +08:00
this . processModuleDependenciesNonRecursive ( module ) ;
2019-12-20 19:45:55 +08:00
callback ( null , module ) ;
return ;
}
2018-09-26 16:27:09 +08:00
// This avoids deadlocks for circular dependencies
if ( this . processDependenciesQueue . isProcessing ( module ) ) {
return callback ( ) ;
}
this . processModuleDependencies ( module , err => {
if ( err ) {
return callback ( err ) ;
}
callback ( null , module ) ;
} ) ;
2018-09-12 00:47:55 +08:00
} ) ;
2018-09-07 21:51:27 +08:00
} ) ;
}
2018-09-12 00:47:55 +08:00
) ;
2018-09-07 21:51:27 +08:00
}
/ * *
2018-09-12 00:47:55 +08:00
* @ typedef { Object } FactorizeModuleOptions
* @ property { ModuleProfile } currentProfile
2018-09-07 21:51:27 +08:00
* @ property { ModuleFactory } factory
* @ property { Dependency [ ] } dependencies
* @ property { Module | null } originModule
* @ property { string = } context
* /
/ * *
2018-09-12 00:47:55 +08:00
* @ param { FactorizeModuleOptions } options options object
2018-09-07 21:51:27 +08:00
* @ param { ModuleCallback } callback callback
* @ returns { void }
* /
2018-09-12 00:47:55 +08:00
factorizeModule ( options , callback ) {
this . factorizeQueue . add ( options , callback ) ;
2018-09-07 21:51:27 +08:00
}
/ * *
2018-09-12 00:47:55 +08:00
* @ param { FactorizeModuleOptions } options options object
* @ param { ModuleCallback } callback callback
2018-09-07 21:51:27 +08:00
* @ returns { void }
* /
2018-09-12 00:47:55 +08:00
_factorizeModule (
{ currentProfile , factory , dependencies , originModule , context } ,
callback
) {
if ( currentProfile !== undefined ) {
currentProfile . markFactoryStart ( ) ;
}
factory . create (
{
contextInfo : {
issuer : originModule ? originModule . nameForCondition ( ) : "" ,
compiler : this . compiler . name
} ,
resolveOptions : originModule ? originModule . resolveOptions : undefined ,
context : context
? context
: originModule
2019-02-06 22:37:11 +08:00
? originModule . context
: this . compiler . context ,
2018-09-12 00:47:55 +08:00
dependencies : dependencies
2018-02-25 09:00:20 +08:00
} ,
2019-01-05 02:17:37 +08:00
( err , result ) => {
if ( result ) {
2019-09-26 21:51:40 +08:00
// TODO webpack 6: remove
// For backward-compat
if ( result . module === undefined && result instanceof Module ) {
result = {
module : result
} ;
}
2019-01-05 02:17:37 +08:00
const {
fileDependencies ,
contextDependencies ,
missingDependencies
} = result ;
2020-01-29 17:28:33 +08:00
if ( fileDependencies && fileDependencies . size > 0 ) {
2019-08-07 15:54:43 +08:00
this . fileDependencies . addAll ( fileDependencies ) ;
2019-01-05 02:17:37 +08:00
}
2020-01-29 17:28:33 +08:00
if ( contextDependencies && contextDependencies . size > 0 ) {
2019-08-07 15:54:43 +08:00
this . contextDependencies . addAll ( contextDependencies ) ;
2019-01-05 02:17:37 +08:00
}
2020-01-29 17:28:33 +08:00
if ( missingDependencies && missingDependencies . size > 0 ) {
2019-08-07 15:54:43 +08:00
this . missingDependencies . addAll ( missingDependencies ) ;
2019-01-05 02:17:37 +08:00
}
}
2018-02-25 09:00:20 +08:00
if ( err ) {
2018-09-12 00:47:55 +08:00
const notFoundError = new ModuleNotFoundError (
originModule ,
err ,
dependencies . map ( d => d . loc ) . filter ( Boolean ) [ 0 ]
) ;
return callback ( notFoundError ) ;
}
2019-01-05 02:17:37 +08:00
if ( ! result ) {
return callback ( ) ;
}
const newModule = result . module ;
2018-09-12 00:47:55 +08:00
if ( ! newModule ) {
return callback ( ) ;
}
if ( currentProfile !== undefined ) {
currentProfile . markFactoryEnd ( ) ;
2018-02-25 09:00:20 +08:00
}
2013-01-31 01:49:25 +08:00
2018-09-12 00:47:55 +08:00
callback ( null , newModule ) ;
2017-01-06 01:00:36 +08:00
}
2018-02-25 09:00:20 +08:00
) ;
2014-06-04 03:03:21 +08:00
}
2013-01-31 01:49:25 +08:00
2018-05-04 00:57:02 +08:00
/ * *
*
* @ param { string } context context string path
* @ param { Dependency } dependency dependency used to create Module chain
2018-09-12 00:47:55 +08:00
* @ param { ModuleCallback } callback callback for when module chain is complete
2018-05-15 18:20:17 +08:00
* @ returns { void } will throw if dependency instance is not a valid Dependency
2018-05-04 00:57:02 +08:00
* /
2018-08-22 21:19:05 +08:00
addModuleChain ( context , dependency , callback ) {
2018-02-25 09:00:20 +08:00
if (
typeof dependency !== "object" ||
dependency === null ||
! dependency . constructor
) {
2018-12-03 00:00:16 +08:00
return callback (
new WebpackError ( "Parameter 'dependency' must be a Dependency" )
) ;
2014-06-04 03:03:21 +08:00
}
2018-06-25 22:23:40 +08:00
const Dep = /** @type {DepConstructor} */ ( dependency . constructor ) ;
const moduleFactory = this . dependencyFactories . get ( Dep ) ;
2018-02-25 09:00:20 +08:00
if ( ! moduleFactory ) {
2018-12-03 00:00:16 +08:00
return callback (
new WebpackError (
2019-06-13 16:51:12 +08:00
` No dependency factory available for this dependency type: ${ dependency . constructor . name } `
2018-12-03 00:00:16 +08:00
)
2018-02-25 09:00:20 +08:00
) ;
2013-05-13 19:34:00 +08:00
}
2018-09-07 21:51:27 +08:00
this . handleModuleCreation (
{
factory : moduleFactory ,
dependencies : [ dependency ] ,
originModule : null ,
2018-09-12 00:47:55 +08:00
context
2018-09-07 21:51:27 +08:00
} ,
2018-09-12 00:47:55 +08:00
err => {
2019-01-14 22:52:27 +08:00
if ( err && this . bail ) {
2018-11-23 18:04:03 +08:00
callback ( err ) ;
2018-09-12 00:47:55 +08:00
this . buildQueue . stop ( ) ;
this . rebuildQueue . stop ( ) ;
this . processDependenciesQueue . stop ( ) ;
this . factorizeQueue . stop ( ) ;
2019-01-14 22:52:27 +08:00
} else {
callback ( ) ;
2018-09-12 00:47:55 +08:00
}
}
2018-09-07 21:51:27 +08:00
) ;
2017-01-06 01:00:36 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
* @ param { string } context context path for entry
2020-05-23 22:08:51 +08:00
* @ param { Dependency } entry entry dependency that should be followed
2020-02-05 04:35:01 +08:00
* @ param { string | EntryOptions } optionsOrName options or deprecated name of entry
2018-05-04 00:57:02 +08:00
* @ param { ModuleCallback } callback callback function
* @ returns { void } returns
* /
2020-02-05 04:35:01 +08:00
addEntry ( context , entry , optionsOrName , callback ) {
2020-02-05 04:21:42 +08:00
// TODO webpack 6 remove
2020-02-05 04:35:01 +08:00
const options =
typeof optionsOrName === "object"
? optionsOrName
: { name : optionsOrName } ;
2018-10-29 20:49:31 +08:00
2020-05-23 22:08:51 +08:00
this . _addEntryItem ( context , entry , "dependencies" , options , callback ) ;
}
/ * *
* @ param { string } context context path for entry
* @ param { Dependency } dependency dependency that should be followed
* @ param { EntryOptions } options options
* @ param { ModuleCallback } callback callback function
* @ returns { void } returns
* /
addInclude ( context , dependency , options , callback ) {
this . _addEntryItem (
context ,
dependency ,
"includeDependencies" ,
options ,
callback
) ;
}
/ * *
* @ param { string } context context path for entry
* @ param { Dependency } entry entry dependency that should be followed
* @ param { "dependencies" | "includeDependencies" } target type of entry
* @ param { EntryOptions } options options
* @ param { ModuleCallback } callback callback function
* @ returns { void } returns
* /
_addEntryItem ( context , entry , target , options , callback ) {
2020-02-05 04:21:42 +08:00
const { name } = options ;
2020-05-27 00:08:16 +08:00
let entryData =
name !== undefined ? this . entries . get ( name ) : this . globalEntry ;
2020-02-05 04:35:01 +08:00
if ( entryData === undefined ) {
entryData = {
2020-05-23 22:08:51 +08:00
dependencies : [ ] ,
includeDependencies : [ ] ,
2020-02-05 04:35:01 +08:00
options : {
name : undefined ,
... options
}
} ;
2020-05-23 22:08:51 +08:00
entryData [ target ] . push ( entry ) ;
2020-02-05 04:35:01 +08:00
this . entries . set ( name , entryData ) ;
} else {
2020-05-23 22:08:51 +08:00
entryData [ target ] . push ( entry ) ;
2020-02-05 04:35:01 +08:00
for ( const key of Object . keys ( options ) ) {
2020-06-04 08:06:57 +08:00
if ( options [ key ] === undefined ) continue ;
2020-02-05 04:35:01 +08:00
if ( entryData . options [ key ] === options [ key ] ) continue ;
if ( entryData . options [ key ] === undefined ) {
entryData . options [ key ] = options [ key ] ;
} else {
return callback (
new WebpackError (
` Conflicting entry option ${ key } = ${ entryData . options [ key ] } vs ${ options [ key ] } `
)
) ;
}
}
2018-05-04 00:57:02 +08:00
}
2020-02-05 04:35:01 +08:00
this . hooks . addEntry . call ( entry , options ) ;
2018-05-04 00:57:02 +08:00
2018-11-05 17:19:09 +08:00
this . addModuleChain ( context , entry , ( err , module ) => {
if ( err ) {
2020-02-05 04:21:42 +08:00
this . hooks . failedEntry . call ( entry , options , err ) ;
2018-11-05 17:19:09 +08:00
return callback ( err ) ;
2015-05-11 00:43:47 +08:00
}
2020-02-05 04:21:42 +08:00
this . hooks . succeedEntry . call ( entry , options , module ) ;
2018-11-05 17:19:09 +08:00
return callback ( null , module ) ;
} ) ;
2017-01-06 01:00:36 +08:00
}
2018-05-07 05:01:47 +08:00
/ * *
* @ param { Module } module module to be rebuilt
2018-09-12 00:47:55 +08:00
* @ param { ModuleCallback } callback callback when module finishes rebuilding
2018-05-07 05:01:47 +08:00
* @ returns { void }
* /
2018-09-12 00:47:55 +08:00
rebuildModule ( module , callback ) {
this . rebuildQueue . add ( module , callback ) ;
}
2017-11-06 20:02:35 +08:00
2018-09-12 00:47:55 +08:00
/ * *
* @ param { Module } module module to be rebuilt
* @ param { ModuleCallback } callback callback when module finishes rebuilding
* @ returns { void }
* /
_rebuildModule ( module , callback ) {
2017-12-07 00:39:42 +08:00
this . hooks . rebuildModule . call ( module ) ;
2017-11-06 20:02:35 +08:00
const oldDependencies = module . dependencies . slice ( ) ;
const oldBlocks = module . blocks . slice ( ) ;
2018-09-12 00:47:55 +08:00
module . invalidateBuild ( ) ;
this . buildQueue . invalidate ( module ) ;
2018-09-07 20:11:48 +08:00
this . buildModule ( module , err => {
2018-02-25 09:00:20 +08:00
if ( err ) {
2018-12-30 21:54:47 +08:00
return this . hooks . finishRebuildingModule . callAsync ( module , err2 => {
2019-05-10 17:06:25 +08:00
if ( err2 ) {
callback (
makeWebpackError ( err2 , "Compilation.hooks.finishRebuildingModule" )
) ;
return ;
}
callback ( err ) ;
2018-12-30 21:54:47 +08:00
} ) ;
2017-12-07 00:39:42 +08:00
}
2013-05-13 19:34:00 +08:00
2018-02-25 09:00:20 +08:00
this . processModuleDependencies ( module , err => {
if ( err ) return callback ( err ) ;
2017-11-06 20:02:35 +08:00
this . removeReasonsOfDependencyBlock ( module , {
dependencies : oldDependencies ,
blocks : oldBlocks
2017-01-06 01:00:36 +08:00
} ) ;
2019-05-10 17:06:25 +08:00
this . hooks . finishRebuildingModule . callAsync ( module , err2 => {
if ( err2 ) {
callback (
makeWebpackError ( err2 , "Compilation.hooks.finishRebuildingModule" )
) ;
return ;
}
callback ( null , module ) ;
} ) ;
2017-01-06 01:00:36 +08:00
} ) ;
} ) ;
}
2013-05-13 19:34:00 +08:00
2018-12-30 21:54:47 +08:00
finish ( callback ) {
2020-01-30 18:34:33 +08:00
this . logger . time ( "finish modules" ) ;
2020-05-06 03:57:31 +08:00
const { modules } = this ;
2018-12-30 21:54:47 +08:00
this . hooks . finishModules . callAsync ( modules , err => {
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "finish modules" ) ;
2018-12-30 21:54:47 +08:00
if ( err ) return callback ( err ) ;
// extract warnings and errors from modules
2020-01-30 18:34:33 +08:00
this . logger . time ( "report dependency errors and warnings" ) ;
2018-12-30 21:54:47 +08:00
for ( const module of modules ) {
this . reportDependencyErrorsAndWarnings ( module , [ module ] ) ;
2019-11-08 19:43:05 +08:00
const errors = module . getErrors ( ) ;
if ( errors !== undefined ) {
2019-10-29 19:01:50 +08:00
if ( module . isOptional ( this . moduleGraph ) ) {
2019-11-08 19:43:05 +08:00
for ( const error of errors ) {
2019-10-29 19:01:50 +08:00
if ( ! error . module ) {
error . module = module ;
}
this . warnings . push ( error ) ;
2018-12-30 21:54:47 +08:00
}
2019-10-29 19:01:50 +08:00
} else {
2019-11-08 19:43:05 +08:00
for ( const error of errors ) {
2019-10-29 19:01:50 +08:00
if ( ! error . module ) {
error . module = module ;
}
this . errors . push ( error ) ;
2018-12-30 21:54:47 +08:00
}
2018-09-07 20:11:48 +08:00
}
}
2019-11-08 19:43:05 +08:00
const warnings = module . getWarnings ( ) ;
if ( warnings !== undefined ) {
for ( const warning of warnings ) {
if ( ! warning . module ) {
warning . module = module ;
}
this . warnings . push ( warning ) ;
2018-12-30 21:54:47 +08:00
}
2018-09-07 20:11:48 +08:00
}
}
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "report dependency errors and warnings" ) ;
2018-12-30 21:54:47 +08:00
callback ( ) ;
} ) ;
2017-01-06 01:00:36 +08:00
}
2013-05-13 19:34:00 +08:00
2017-01-06 01:00:36 +08:00
unseal ( ) {
2017-11-27 22:27:30 +08:00
this . hooks . unseal . call ( ) ;
2018-09-06 22:59:11 +08:00
this . chunks . clear ( ) ;
2018-01-20 00:06:59 +08:00
this . chunkGroups . length = 0 ;
this . namedChunks . clear ( ) ;
this . namedChunkGroups . clear ( ) ;
2018-09-05 20:22:10 +08:00
this . entrypoints . clear ( ) ;
2017-01-06 01:00:36 +08:00
this . additionalChunkAssets . length = 0 ;
this . assets = { } ;
2019-09-11 17:13:46 +08:00
this . assetsInfo . clear ( ) ;
2018-08-21 22:12:00 +08:00
this . moduleGraph . removeAllModuleAttributes ( ) ;
2014-07-29 06:13:25 +08:00
}
2015-07-08 20:15:21 +08:00
2018-05-04 00:57:02 +08:00
/ * *
2018-12-10 18:34:59 +08:00
* @ param { Callback } callback signals when the call finishes
2018-05-04 00:57:02 +08:00
* @ returns { void }
* /
2017-01-06 01:00:36 +08:00
seal ( callback ) {
2018-08-21 22:15:48 +08:00
const chunkGraph = new ChunkGraph ( this . moduleGraph ) ;
2018-08-14 17:18:22 +08:00
this . chunkGraph = chunkGraph ;
2018-08-21 22:15:48 +08:00
for ( const module of this . modules ) {
ChunkGraph . setChunkGraphForModule ( module , chunkGraph ) ;
}
2017-11-27 22:27:30 +08:00
this . hooks . seal . call ( ) ;
2017-08-08 15:40:17 +08:00
2020-01-30 18:34:33 +08:00
this . logger . time ( "optimize dependencies" ) ;
2018-07-31 04:30:27 +08:00
while ( this . hooks . optimizeDependencies . call ( this . modules ) ) {
2018-02-25 09:00:20 +08:00
/* empty */
}
2017-11-27 22:27:30 +08:00
this . hooks . afterOptimizeDependencies . call ( this . modules ) ;
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "optimize dependencies" ) ;
2017-08-08 15:40:17 +08:00
2020-01-30 18:34:33 +08:00
this . logger . time ( "create chunks" ) ;
2018-07-02 22:18:49 +08:00
this . hooks . beforeChunks . call ( ) ;
2020-05-23 22:08:51 +08:00
const chunkGraphInit = new Map ( ) ;
for ( const [ name , { dependencies , includeDependencies , options } ] of this
. entries ) {
2018-01-20 00:06:59 +08:00
const chunk = this . addChunk ( name ) ;
2018-08-14 22:40:37 +08:00
chunk . name = name ;
2020-02-07 18:00:25 +08:00
if ( options . filename ) {
chunk . filenameTemplate = options . filename ;
}
2020-02-10 20:04:28 +08:00
const entrypoint = new Entrypoint ( name ) ;
2020-07-21 16:22:10 +08:00
if ( ! options . dependOn && ! options . runtime ) {
entrypoint . setRuntimeChunk ( chunk ) ;
}
entrypoint . setEntrypointChunk ( chunk ) ;
2018-01-20 00:06:59 +08:00
this . namedChunkGroups . set ( name , entrypoint ) ;
this . entrypoints . set ( name , entrypoint ) ;
this . chunkGroups . push ( entrypoint ) ;
2018-07-27 04:43:20 +08:00
connectChunkGroupAndChunk ( entrypoint , chunk ) ;
2018-01-20 00:06:59 +08:00
2020-05-23 22:08:51 +08:00
for ( const dep of [ ... this . globalEntry . dependencies , ... dependencies ] ) {
2020-05-05 16:43:18 +08:00
entrypoint . addOrigin ( null , { name } , /** @type {any} */ ( dep ) . request ) ;
2018-01-20 00:06:59 +08:00
2018-08-14 22:40:37 +08:00
const module = this . moduleGraph . getModule ( dep ) ;
if ( module ) {
chunkGraph . connectChunkAndEntryModule ( chunk , module , entrypoint ) ;
this . assignDepth ( module ) ;
2020-05-23 22:08:51 +08:00
const modulesList = chunkGraphInit . get ( entrypoint ) ;
if ( modulesList === undefined ) {
chunkGraphInit . set ( entrypoint , [ module ] ) ;
} else {
modulesList . push ( module ) ;
}
}
}
2020-07-02 19:13:13 +08:00
const mapAndSort = deps =>
deps
. map ( dep => this . moduleGraph . getModule ( dep ) )
. filter ( Boolean )
. sort ( compareModulesByIdentifier ) ;
const includedModules = [
... mapAndSort ( this . globalEntry . includeDependencies ) ,
... mapAndSort ( includeDependencies )
] ;
for ( const module of includedModules ) {
this . assignDepth ( module ) ;
const modulesList = chunkGraphInit . get ( entrypoint ) ;
if ( modulesList === undefined ) {
chunkGraphInit . set ( entrypoint , [ module ] ) ;
} else {
modulesList . push ( module ) ;
2018-08-14 22:40:37 +08:00
}
}
2018-01-22 20:52:43 +08:00
}
2020-07-31 21:14:49 +08:00
const runtimeChunks = new Set ( ) ;
outer : for ( const [
2020-02-07 17:05:51 +08:00
name ,
{
2020-07-21 16:22:10 +08:00
options : { dependOn , runtime }
2020-02-07 17:05:51 +08:00
}
] of this . entries ) {
2020-07-31 21:14:49 +08:00
if ( dependOn && runtime ) {
const err = new WebpackError ( ` Entrypoint ' ${ name } ' has 'dependOn' and 'runtime' specified. This is not valid.
Entrypoints that depend on other entrypoints do not have their own runtime .
They will use the runtime ( s ) from referenced entrypoints instead .
Remove the 'runtime' option from the entrypoint . ` );
const entry = this . entrypoints . get ( name ) ;
err . chunk = entry . getEntrypointChunk ( ) ;
this . errors . push ( err ) ;
}
2020-02-07 17:05:51 +08:00
if ( dependOn ) {
const entry = this . entrypoints . get ( name ) ;
2020-07-31 21:14:49 +08:00
const referencedChunks = entry
. getEntrypointChunk ( )
. getAllReferencedChunks ( ) ;
const dependOnEntries = [ ] ;
2020-02-07 17:05:51 +08:00
for ( const dep of dependOn ) {
const dependency = this . entrypoints . get ( dep ) ;
if ( ! dependency ) {
throw new Error (
` Entry ${ name } depends on ${ dep } , but this entry was not found `
) ;
}
2020-07-31 21:14:49 +08:00
if ( referencedChunks . has ( dependency . getEntrypointChunk ( ) ) ) {
const err = new WebpackError (
` Entrypoints ' ${ name } ' and ' ${ dep } ' use 'dependOn' to depend on each other in a circular way. `
) ;
const entryChunk = entry . getEntrypointChunk ( ) ;
err . chunk = entryChunk ;
this . errors . push ( err ) ;
entry . setRuntimeChunk ( entryChunk ) ;
continue outer ;
}
dependOnEntries . push ( dependency ) ;
}
for ( const dependency of dependOnEntries ) {
2020-02-07 17:05:51 +08:00
connectChunkGroupParentAndChild ( dependency , entry ) ;
}
2020-07-21 16:22:10 +08:00
} else if ( runtime ) {
const entry = this . entrypoints . get ( name ) ;
2020-07-31 21:14:49 +08:00
let chunk = this . namedChunks . get ( runtime ) ;
if ( chunk ) {
if ( ! runtimeChunks . has ( chunk ) ) {
const err = new WebpackError ( ` Entrypoint ' ${ name } ' has a 'runtime' option which points to another entrypoint named ' ${ runtime } '.
It ' s not valid to use other entrypoints as runtime chunk .
Did you mean to use ' dependOn : $ { JSON . stringify (
runtime
) } ' instead to allow using entrypoint ' $ { name } ' within the runtime of entrypoint ' $ { runtime } '? For this ' $ { runtime } ' must always be loaded when ' $ { name } ' is used .
Or do you want to use the entrypoints '${name}' and '${runtime}' independently on the same page with a shared runtime ? In this case give them both the same value for the 'runtime' option . It must be a name not already used by an entrypoint . ` );
const entryChunk = entry . getEntrypointChunk ( ) ;
err . chunk = entryChunk ;
this . errors . push ( err ) ;
entry . setRuntimeChunk ( entryChunk ) ;
continue ;
}
} else {
chunk = this . addChunk ( runtime ) ;
chunk . preventIntegration = true ;
runtimeChunks . add ( chunk ) ;
}
2020-07-21 16:22:10 +08:00
entry . unshiftChunk ( chunk ) ;
chunk . addGroup ( entry ) ;
entry . setRuntimeChunk ( chunk ) ;
2020-02-07 17:05:51 +08:00
}
}
2020-05-23 22:08:51 +08:00
buildChunkGraph ( this , chunkGraphInit ) ;
2018-07-02 22:18:49 +08:00
this . hooks . afterChunks . call ( this . chunks ) ;
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "create chunks" ) ;
2018-07-02 22:18:49 +08:00
2020-01-30 18:34:33 +08:00
this . logger . time ( "optimize" ) ;
2017-11-27 22:27:30 +08:00
this . hooks . optimize . call ( ) ;
2014-07-19 20:32:48 +08:00
2018-07-31 04:30:27 +08:00
while ( this . hooks . optimizeModules . call ( this . modules ) ) {
2018-02-25 09:00:20 +08:00
/* empty */
}
2017-11-27 22:27:30 +08:00
this . hooks . afterOptimizeModules . call ( this . modules ) ;
2014-01-29 17:13:28 +08:00
2018-07-31 04:30:27 +08:00
while ( this . hooks . optimizeChunks . call ( this . chunks , this . chunkGroups ) ) {
2018-02-25 09:00:20 +08:00
/* empty */
}
2018-01-20 00:06:59 +08:00
this . hooks . afterOptimizeChunks . call ( this . chunks , this . chunkGroups ) ;
2015-04-21 01:39:02 +08:00
2017-11-27 22:27:30 +08:00
this . hooks . optimizeTree . callAsync ( this . chunks , this . modules , err => {
2018-02-25 09:00:20 +08:00
if ( err ) {
2019-05-11 03:37:35 +08:00
return callback (
makeWebpackError ( err , "Compilation.hooks.optimizeTree" )
) ;
2017-01-06 01:00:36 +08:00
}
2014-09-03 20:16:17 +08:00
2017-11-27 22:27:30 +08:00
this . hooks . afterOptimizeTree . call ( this . chunks , this . modules ) ;
2016-07-13 17:03:14 +08:00
2019-12-11 05:58:26 +08:00
this . hooks . optimizeChunkModules . callAsync (
this . chunks ,
this . modules ,
err => {
if ( err ) {
return callback (
makeWebpackError ( err , "Compilation.hooks.optimizeChunkModules" )
) ;
}
2017-05-10 19:15:14 +08:00
2019-12-11 05:58:26 +08:00
this . hooks . afterOptimizeChunkModules . call ( this . chunks , this . modules ) ;
2016-07-13 17:03:14 +08:00
2019-12-11 05:58:26 +08:00
const shouldRecord = this . hooks . shouldRecord . call ( ) !== false ;
2016-07-13 17:03:14 +08:00
2019-12-11 05:58:26 +08:00
this . hooks . reviveModules . call ( this . modules , this . records ) ;
this . hooks . beforeModuleIds . call ( this . modules ) ;
this . hooks . moduleIds . call ( this . modules ) ;
this . hooks . optimizeModuleIds . call ( this . modules ) ;
this . hooks . afterOptimizeModuleIds . call ( this . modules ) ;
2014-01-29 17:13:28 +08:00
2019-12-11 05:58:26 +08:00
this . hooks . reviveChunks . call ( this . chunks , this . records ) ;
this . hooks . beforeChunkIds . call ( this . chunks ) ;
this . hooks . chunkIds . call ( this . chunks ) ;
this . hooks . optimizeChunkIds . call ( this . chunks ) ;
this . hooks . afterOptimizeChunkIds . call ( this . chunks ) ;
2016-07-13 17:03:14 +08:00
2019-12-11 05:58:26 +08:00
this . sortItemsWithChunkIds ( ) ;
2016-07-13 17:03:14 +08:00
2019-12-11 05:58:26 +08:00
if ( shouldRecord ) {
this . hooks . recordModules . call ( this . modules , this . records ) ;
this . hooks . recordChunks . call ( this . chunks , this . records ) ;
}
2019-01-28 17:40:32 +08:00
2019-12-11 05:58:26 +08:00
this . hooks . optimizeCodeGeneration . call ( this . modules ) ;
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "optimize" ) ;
2018-11-15 00:31:32 +08:00
2020-01-30 18:34:33 +08:00
this . logger . time ( "module hashing" ) ;
2019-12-11 05:58:26 +08:00
this . hooks . beforeModuleHash . call ( ) ;
this . createModuleHashes ( ) ;
this . hooks . afterModuleHash . call ( ) ;
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "module hashing" ) ;
2019-10-09 04:29:46 +08:00
2020-01-30 18:34:33 +08:00
this . logger . time ( "code generation" ) ;
2019-12-11 05:58:26 +08:00
this . hooks . beforeCodeGeneration . call ( ) ;
2020-07-28 00:09:48 +08:00
this . codeGeneration ( err => {
if ( err ) {
return callback ( err ) ;
}
this . hooks . afterCodeGeneration . call ( ) ;
this . logger . timeEnd ( "code generation" ) ;
this . logger . time ( "runtime requirements" ) ;
this . hooks . beforeRuntimeRequirements . call ( ) ;
this . processRuntimeRequirements ( this . entrypoints . values ( ) ) ;
this . hooks . afterRuntimeRequirements . call ( ) ;
this . logger . timeEnd ( "runtime requirements" ) ;
this . logger . time ( "hashing" ) ;
this . hooks . beforeHash . call ( ) ;
this . createHash ( ) ;
this . hooks . afterHash . call ( ) ;
this . logger . timeEnd ( "hashing" ) ;
if ( shouldRecord ) {
this . logger . time ( "record hash" ) ;
this . hooks . recordHash . call ( this . records ) ;
this . logger . timeEnd ( "record hash" ) ;
}
2019-07-17 23:30:25 +08:00
2020-07-28 00:09:48 +08:00
this . logger . time ( "module assets" ) ;
this . clearAssets ( ) ;
2017-01-06 01:00:36 +08:00
2020-07-28 00:09:48 +08:00
this . hooks . beforeModuleAssets . call ( ) ;
this . createModuleAssets ( ) ;
this . logger . timeEnd ( "module assets" ) ;
2018-09-27 13:22:19 +08:00
2020-07-28 00:09:48 +08:00
const cont = ( ) => {
this . logger . time ( "process assets" ) ;
this . hooks . processAssets . callAsync ( this . assets , err => {
if ( err ) {
return callback (
makeWebpackError ( err , "Compilation.hooks.processAssets" )
) ;
}
this . hooks . afterProcessAssets . call ( this . assets ) ;
this . logger . timeEnd ( "process assets" ) ;
this . assets = soonFrozenObjectDeprecation (
this . assets ,
"Compilation.assets" ,
"DEP_WEBPACK_COMPILATION_ASSETS" ,
` BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
Do changes to assets earlier , e . g . in Compilation . hooks . processAssets .
Make sure to select an appropriate stage from Compilation . PROCESS _ASSETS _STAGE _ * . `
2019-05-11 03:37:35 +08:00
) ;
2020-05-12 18:16:51 +08:00
2020-07-28 00:09:48 +08:00
this . summarizeDependencies ( ) ;
if ( shouldRecord ) {
this . hooks . record . call ( this , this . records ) ;
}
2020-05-12 18:16:51 +08:00
2020-07-28 00:09:48 +08:00
if ( this . hooks . needAdditionalSeal . call ( ) ) {
this . unseal ( ) ;
return this . seal ( callback ) ;
2019-08-09 20:46:42 +08:00
}
2020-07-31 23:38:33 +08:00
return this . hooks . afterSeal . callAsync ( callback ) ;
2020-07-28 00:09:48 +08:00
} ) ;
} ;
2018-09-27 13:22:19 +08:00
2020-07-28 00:09:48 +08:00
this . logger . time ( "create chunk assets" ) ;
if ( this . hooks . shouldGenerateChunkAssets . call ( ) !== false ) {
this . hooks . beforeChunkAssets . call ( ) ;
this . createChunkAssets ( err => {
this . logger . timeEnd ( "create chunk assets" ) ;
if ( err ) {
return callback ( err ) ;
}
cont ( ) ;
} ) ;
} else {
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "create chunk assets" ) ;
2019-12-11 05:58:26 +08:00
cont ( ) ;
2020-07-28 00:09:48 +08:00
}
} ) ;
2019-12-11 05:58:26 +08:00
}
) ;
2016-10-18 02:26:22 +08:00
} ) ;
2017-01-06 01:00:36 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
2020-03-13 00:51:26 +08:00
* @ param { Module } module module to report from
2018-05-04 00:57:02 +08:00
* @ param { DependenciesBlock [ ] } blocks blocks to report from
* @ returns { void }
* /
2017-01-10 00:11:34 +08:00
reportDependencyErrorsAndWarnings ( module , blocks ) {
2018-02-25 09:00:20 +08:00
for ( let indexBlock = 0 ; indexBlock < blocks . length ; indexBlock ++ ) {
2017-03-02 22:09:09 +08:00
const block = blocks [ indexBlock ] ;
const dependencies = block . dependencies ;
2017-01-24 02:52:20 +08:00
2018-02-25 09:00:20 +08:00
for ( let indexDep = 0 ; indexDep < dependencies . length ; indexDep ++ ) {
2017-03-02 22:09:09 +08:00
const d = dependencies [ indexDep ] ;
2017-01-24 02:52:20 +08:00
2018-07-24 23:35:36 +08:00
const warnings = d . getWarnings ( this . moduleGraph ) ;
2018-02-25 09:00:20 +08:00
if ( warnings ) {
for ( let indexWar = 0 ; indexWar < warnings . length ; indexWar ++ ) {
2017-03-02 22:09:09 +08:00
const w = warnings [ indexWar ] ;
2017-01-24 02:52:20 +08:00
2017-03-02 22:09:09 +08:00
const warning = new ModuleDependencyWarning ( module , w , d . loc ) ;
2017-01-06 01:00:36 +08:00
this . warnings . push ( warning ) ;
2017-01-24 02:52:20 +08:00
}
2017-01-06 01:00:36 +08:00
}
2018-07-24 23:35:36 +08:00
const errors = d . getErrors ( this . moduleGraph ) ;
2018-02-25 09:00:20 +08:00
if ( errors ) {
for ( let indexErr = 0 ; indexErr < errors . length ; indexErr ++ ) {
2017-03-02 22:09:09 +08:00
const e = errors [ indexErr ] ;
2017-01-24 02:52:20 +08:00
2017-03-02 22:09:09 +08:00
const error = new ModuleDependencyError ( module , e , d . loc ) ;
2017-01-24 02:52:20 +08:00
this . errors . push ( error ) ;
}
2017-01-10 00:11:34 +08:00
}
2017-01-24 02:52:20 +08:00
}
2017-01-10 00:11:34 +08:00
this . reportDependencyErrorsAndWarnings ( module , block . blocks ) ;
2017-01-24 02:52:20 +08:00
}
2017-01-06 01:00:36 +08:00
}
2020-07-28 00:09:48 +08:00
codeGeneration ( callback ) {
2019-10-09 04:29:46 +08:00
const {
chunkGraph ,
moduleGraph ,
dependencyTemplates ,
runtimeTemplate
} = this ;
2020-07-28 00:09:48 +08:00
const results = ( this . codeGenerationResults = new CodeGenerationResults ( ) ) ;
2019-10-09 15:26:33 +08:00
const errors = [ ] ;
2020-07-28 00:09:48 +08:00
/** @type {{module: Module, hash: string, runtime: RuntimeSpec, runtimes: RuntimeSpec[]}[]} */
const jobs = [ ] ;
2019-10-09 04:29:46 +08:00
for ( const module of this . modules ) {
2020-07-28 00:09:48 +08:00
const runtimes = chunkGraph . getModuleRuntimes ( module ) ;
if ( runtimes . size === 1 ) {
for ( const runtime of runtimes ) {
const hash = chunkGraph . getModuleHash ( module , runtime ) ;
jobs . push ( { module , hash , runtime , runtimes : [ runtime ] } ) ;
}
} else if ( runtimes . size > 1 ) {
2020-07-29 04:05:13 +08:00
/** @type {Map<string, { runtimes: RuntimeSpec[] }>} */
2020-07-28 00:09:48 +08:00
const map = new Map ( ) ;
for ( const runtime of runtimes ) {
const hash = chunkGraph . getModuleHash ( module , runtime ) ;
2020-07-29 04:05:13 +08:00
const job = map . get ( hash ) ;
if ( job === undefined ) {
const newJob = { module , hash , runtime , runtimes : [ runtime ] } ;
jobs . push ( newJob ) ;
map . set ( hash , newJob ) ;
2020-07-28 00:09:48 +08:00
} else {
2020-07-29 04:05:13 +08:00
job . runtimes . push ( runtime ) ;
2020-07-28 00:09:48 +08:00
}
}
2019-10-09 04:29:46 +08:00
}
}
2020-07-28 00:09:48 +08:00
asyncLib . eachLimit (
jobs ,
this . options . parallelism ,
( { module , hash , runtime , runtimes } , callback ) => {
const cache = new MultiItemCache (
runtimes . map ( runtime =>
this . _codeGenerationCache . getItemCache (
` ${ module . identifier ( ) } | ${ getRuntimeKey ( runtime ) } ` ,
` ${ hash } | ${ dependencyTemplates . getHash ( ) } `
)
)
) ;
cache . get ( ( err , cachedResult ) => {
if ( err ) return callback ( err ) ;
let result ;
if ( ! cachedResult ) {
try {
result = module . codeGeneration ( {
chunkGraph ,
moduleGraph ,
dependencyTemplates ,
runtimeTemplate ,
runtime
} ) ;
} catch ( err ) {
errors . push ( new CodeGenerationError ( module , err ) ) ;
2020-07-29 04:09:31 +08:00
result = cachedResult = {
2020-07-28 00:09:48 +08:00
sources : new Map ( ) ,
runtimeRequirements : null
} ;
}
} else {
result = cachedResult ;
}
for ( const runtime of runtimes ) {
results . add ( module , runtime , result ) ;
}
if ( ! cachedResult ) {
cache . store ( result , callback ) ;
} else {
callback ( ) ;
}
} ) ;
} ,
err => {
if ( err ) return callback ( err ) ;
if ( errors . length > 0 ) {
errors . sort (
compareSelect ( err => err . module , compareModulesByIdentifier )
) ;
for ( const error of errors ) {
this . errors . push ( error ) ;
}
}
callback ( ) ;
2019-10-09 15:26:33 +08:00
}
2020-07-28 00:09:48 +08:00
) ;
2019-10-09 04:29:46 +08:00
}
2018-11-06 02:03:12 +08:00
/ * *
2018-11-28 20:07:40 +08:00
* @ param { Iterable < Entrypoint > } entrypoints the entrypoints
2018-11-06 02:03:12 +08:00
* @ returns { void }
* /
processRuntimeRequirements ( entrypoints ) {
2019-10-09 04:29:46 +08:00
const { chunkGraph } = this ;
2018-11-06 02:03:12 +08:00
2019-11-08 18:13:16 +08:00
const additionalModuleRuntimeRequirements = this . hooks
. additionalModuleRuntimeRequirements ;
2018-11-17 01:18:44 +08:00
const runtimeRequirementInModule = this . hooks . runtimeRequirementInModule ;
2018-11-06 02:03:12 +08:00
for ( const module of this . modules ) {
2018-11-15 00:31:32 +08:00
if ( chunkGraph . getNumberOfModuleChunks ( module ) > 0 ) {
2020-07-28 00:09:48 +08:00
for ( const runtime of chunkGraph . getModuleRuntimes ( module ) ) {
let set ;
const runtimeRequirements = this . codeGenerationResults . getRuntimeRequirements (
module ,
runtime
) ;
if ( runtimeRequirements && runtimeRequirements . size > 0 ) {
set = new Set ( runtimeRequirements ) ;
} else if ( additionalModuleRuntimeRequirements . isUsed ( ) ) {
set = new Set ( ) ;
} else {
continue ;
}
additionalModuleRuntimeRequirements . call ( module , set ) ;
2018-11-17 01:18:44 +08:00
2020-07-28 00:09:48 +08:00
for ( const r of set ) {
const hook = runtimeRequirementInModule . get ( r ) ;
if ( hook !== undefined ) hook . call ( module , set ) ;
}
chunkGraph . addModuleRuntimeRequirements ( module , runtime , set ) ;
2018-11-17 01:18:44 +08:00
}
2018-11-06 02:03:12 +08:00
}
}
for ( const chunk of this . chunks ) {
const set = new Set ( ) ;
2018-11-15 00:31:32 +08:00
for ( const module of chunkGraph . getChunkModulesIterable ( chunk ) ) {
2018-11-17 01:11:51 +08:00
const runtimeRequirements = chunkGraph . getModuleRuntimeRequirements (
2020-07-28 00:09:48 +08:00
module ,
chunk . runtime
2018-11-17 01:11:51 +08:00
) ;
2018-11-17 01:18:44 +08:00
for ( const r of runtimeRequirements ) set . add ( r ) ;
2018-11-06 02:03:12 +08:00
}
this . hooks . additionalChunkRuntimeRequirements . call ( chunk , set ) ;
for ( const r of set ) {
this . hooks . runtimeRequirementInChunk . for ( r ) . call ( chunk , set ) ;
}
2018-11-17 01:11:51 +08:00
chunkGraph . addChunkRuntimeRequirements ( chunk , set ) ;
2018-11-06 02:03:12 +08:00
}
2020-07-28 00:09:48 +08:00
/** @type {Set<Chunk>} */
2020-02-07 17:05:51 +08:00
const treeEntries = new Set ( ) ;
for ( const ep of entrypoints ) {
const chunk = ep . getRuntimeChunk ( ) ;
if ( chunk ) treeEntries . add ( chunk ) ;
}
2019-02-02 20:18:57 +08:00
2020-07-28 00:09:48 +08:00
for ( const treeEntry of treeEntries ) {
2018-11-06 02:03:12 +08:00
const set = new Set ( ) ;
2020-07-28 00:09:48 +08:00
for ( const chunk of treeEntry . getAllReferencedChunks ( ) ) {
2018-11-17 01:11:51 +08:00
const runtimeRequirements = chunkGraph . getChunkRuntimeRequirements (
chunk
) ;
2018-11-06 02:03:12 +08:00
for ( const r of runtimeRequirements ) set . add ( r ) ;
}
2020-07-28 00:09:48 +08:00
this . hooks . additionalTreeRuntimeRequirements . call ( treeEntry , set ) ;
2018-11-06 02:03:12 +08:00
for ( const r of set ) {
2020-07-28 00:09:48 +08:00
this . hooks . runtimeRequirementInTree . for ( r ) . call ( treeEntry , set ) ;
2018-11-06 02:03:12 +08:00
}
2018-11-23 16:37:33 +08:00
2020-07-28 00:09:48 +08:00
chunkGraph . addTreeRuntimeRequirements ( treeEntry , set ) ;
2018-11-06 02:03:12 +08:00
}
}
/ * *
* @ param { Chunk } chunk target chunk
* @ param { RuntimeModule } module runtime module
* @ returns { void }
* /
addRuntimeModule ( chunk , module ) {
2018-11-15 00:31:32 +08:00
// Deprecated ModuleGraph association
ModuleGraph . setModuleGraphForModule ( module , this . moduleGraph ) ;
// add it to the list
2018-11-06 02:03:12 +08:00
this . modules . add ( module ) ;
2018-11-23 17:02:47 +08:00
this . _modules . set ( module . identifier ( ) , module ) ;
2018-11-15 00:31:32 +08:00
// connect to the chunk graph
2018-11-06 02:03:12 +08:00
this . chunkGraph . connectChunkAndModule ( chunk , module ) ;
this . chunkGraph . connectChunkAndRuntimeModule ( chunk , module ) ;
2018-11-15 00:31:32 +08:00
2019-08-27 02:21:07 +08:00
// attach runtime module
module . attach ( this , chunk ) ;
2018-11-15 00:31:32 +08:00
// Setup internals
2019-02-06 22:37:11 +08:00
const exportsInfo = this . moduleGraph . getExportsInfo ( module ) ;
exportsInfo . setHasProvideInfo ( ) ;
2020-07-28 00:09:48 +08:00
if ( typeof chunk . runtime === "string" ) {
exportsInfo . setUsedForSideEffectsOnly ( chunk . runtime ) ;
} else if ( chunk . runtime === undefined ) {
exportsInfo . setUsedForSideEffectsOnly ( undefined ) ;
} else {
for ( const runtime of chunk . runtime ) {
exportsInfo . setUsedForSideEffectsOnly ( runtime ) ;
}
}
2020-01-29 17:28:33 +08:00
this . chunkGraph . addModuleRuntimeRequirements (
module ,
2020-07-28 00:09:48 +08:00
chunk . runtime ,
2020-01-29 17:28:33 +08:00
new Set ( [ RuntimeGlobals . requireScope ] )
) ;
2018-11-15 00:31:32 +08:00
2018-11-29 21:13:02 +08:00
// runtime modules don't need ids
this . chunkGraph . setModuleId ( module , "" ) ;
2018-11-15 00:31:32 +08:00
// Call hook
this . hooks . runtimeModule . call ( module , chunk ) ;
2018-11-06 02:03:12 +08:00
}
2018-05-12 00:12:30 +08:00
/ * *
2019-06-14 17:44:54 +08:00
* @ param { string | ChunkGroupOptions } groupOptions options for the chunk group
2018-05-12 00:12:30 +08:00
* @ param { Module } module the module the references the chunk group
2018-06-27 20:58:10 +08:00
* @ param { DependencyLocation } loc the location from with the chunk group is referenced ( inside of module )
2018-05-12 00:12:30 +08:00
* @ param { string } request the request from which the the chunk group is referenced
* @ returns { ChunkGroup } the new or existing chunk group
* /
2018-04-16 16:27:22 +08:00
addChunkInGroup ( groupOptions , module , loc , request ) {
if ( typeof groupOptions === "string" ) {
groupOptions = { name : groupOptions } ;
}
const name = groupOptions . name ;
2018-02-25 09:00:20 +08:00
if ( name ) {
2018-01-20 00:06:59 +08:00
const chunkGroup = this . namedChunkGroups . get ( name ) ;
2018-02-25 09:00:20 +08:00
if ( chunkGroup !== undefined ) {
2018-04-16 16:27:22 +08:00
chunkGroup . addOptions ( groupOptions ) ;
2018-02-25 09:00:20 +08:00
if ( module ) {
2018-01-20 00:06:59 +08:00
chunkGroup . addOrigin ( module , loc , request ) ;
2017-01-06 01:00:36 +08:00
}
2018-01-20 00:06:59 +08:00
return chunkGroup ;
}
}
2018-04-16 16:27:22 +08:00
const chunkGroup = new ChunkGroup ( groupOptions ) ;
2018-02-25 09:00:20 +08:00
if ( module ) chunkGroup . addOrigin ( module , loc , request ) ;
2018-01-20 00:06:59 +08:00
const chunk = this . addChunk ( name ) ;
2018-07-27 04:43:20 +08:00
connectChunkGroupAndChunk ( chunkGroup , chunk ) ;
2018-01-20 00:06:59 +08:00
this . chunkGroups . push ( chunkGroup ) ;
2018-02-25 09:00:20 +08:00
if ( name ) {
2018-01-20 00:06:59 +08:00
this . namedChunkGroups . set ( name , chunkGroup ) ;
}
return chunkGroup ;
}
2018-05-04 00:57:02 +08:00
/ * *
* This method first looks to see if a name is provided for a new chunk ,
* and first looks to see if any named chunks already exist and reuse that chunk instead .
*
* @ param { string = } name optional chunk name to be provided
* @ returns { Chunk } create a chunk ( invoked during seal event )
* /
2018-01-20 00:06:59 +08:00
addChunk ( name ) {
2018-02-25 09:00:20 +08:00
if ( name ) {
2018-01-20 00:06:59 +08:00
const chunk = this . namedChunks . get ( name ) ;
2018-02-25 09:00:20 +08:00
if ( chunk !== undefined ) {
2017-01-06 01:00:36 +08:00
return chunk ;
2014-06-04 03:03:21 +08:00
}
2014-01-24 20:32:58 +08:00
}
2018-01-20 00:06:59 +08:00
const chunk = new Chunk ( name ) ;
2018-09-06 22:59:11 +08:00
this . chunks . add ( chunk ) ;
2018-08-21 22:15:48 +08:00
ChunkGraph . setChunkGraphForChunk ( chunk , this . chunkGraph ) ;
2018-02-25 09:00:20 +08:00
if ( name ) {
2018-01-20 00:06:59 +08:00
this . namedChunks . set ( name , chunk ) ;
2017-01-06 01:00:36 +08:00
}
return chunk ;
2013-01-31 01:49:25 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
* @ param { Module } module module to assign depth
* @ returns { void }
* /
2017-01-06 01:00:36 +08:00
assignDepth ( module ) {
2018-08-22 01:16:02 +08:00
const moduleGraph = this . moduleGraph ;
2018-02-24 21:31:18 +08:00
const queue = new Set ( [ module ] ) ;
let depth ;
2018-08-22 01:16:02 +08:00
moduleGraph . setDepth ( module , 0 ) ;
2017-01-06 01:00:36 +08:00
2018-05-04 00:57:02 +08:00
/ * *
2020-03-13 00:51:26 +08:00
* @ param { Module } module module for processing
2018-05-04 00:57:02 +08:00
* @ returns { void }
* /
2019-10-28 21:02:23 +08:00
const processModule = module => {
2018-08-22 01:16:02 +08:00
if ( ! moduleGraph . setDepthIfLower ( module , depth ) ) return ;
2018-02-24 21:31:18 +08:00
queue . add ( module ) ;
2017-11-08 18:32:05 +08:00
} ;
2016-12-05 06:47:19 +08:00
2018-02-25 09:00:20 +08:00
for ( module of queue ) {
2018-02-24 21:31:18 +08:00
queue . delete ( module ) ;
2018-08-22 01:16:02 +08:00
depth = moduleGraph . getDepth ( module ) + 1 ;
2017-11-08 18:32:05 +08:00
2019-10-28 21:02:23 +08:00
for ( const connection of moduleGraph . getOutgoingConnections ( module ) ) {
const refModule = connection . module ;
if ( refModule ) {
processModule ( refModule ) ;
}
}
2016-12-14 19:03:24 +08:00
}
}
2018-06-08 16:34:38 +08:00
/ * *
* @ param { Dependency } dependency the dependency
2020-07-28 00:09:48 +08:00
* @ param { RuntimeSpec } runtime the runtime
2020-05-28 02:34:55 +08:00
* @ returns { ( string [ ] | ReferencedExport ) [ ] } referenced exports
2018-06-08 16:34:38 +08:00
* /
2020-07-28 00:09:48 +08:00
getDependencyReferencedExports ( dependency , runtime ) {
const referencedExports = dependency . getReferencedExports (
this . moduleGraph ,
runtime
) ;
2019-10-30 05:28:42 +08:00
return this . hooks . dependencyReferencedExports . call (
referencedExports ,
2020-07-28 00:09:48 +08:00
dependency ,
runtime
2019-10-30 05:28:42 +08:00
) ;
2018-06-08 16:34:38 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
*
* @ param { Module } module module relationship for removal
2018-05-07 05:01:47 +08:00
* @ param { DependenciesBlockLike } block //TODO: good description
2018-05-04 00:57:02 +08:00
* @ returns { void }
* /
2017-11-06 20:02:35 +08:00
removeReasonsOfDependencyBlock ( module , block ) {
2018-08-14 17:18:22 +08:00
const chunkGraph = this . chunkGraph ;
2017-01-24 02:52:20 +08:00
const iteratorDependency = d => {
2018-02-25 09:00:20 +08:00
if ( ! d . module ) {
2017-01-06 01:00:36 +08:00
return ;
}
2018-02-25 09:00:20 +08:00
if ( d . module . removeReason ( module , d ) ) {
2018-08-14 17:18:22 +08:00
for ( const chunk of chunkGraph . getModuleChunksIterable ( d . module ) ) {
2018-01-24 03:08:32 +08:00
this . patchChunksAfterReasonRemoval ( d . module , chunk ) ;
}
2017-11-06 20:02:35 +08:00
}
} ;
2018-02-25 09:00:20 +08:00
if ( block . blocks ) {
2019-07-10 02:49:10 +08:00
for ( const b of block . blocks ) {
this . removeReasonsOfDependencyBlock ( module , b ) ;
}
2017-11-06 20:02:35 +08:00
}
2018-02-25 09:00:20 +08:00
if ( block . dependencies ) {
2019-07-10 02:49:10 +08:00
for ( const dep of block . dependencies ) iteratorDependency ( dep ) ;
2017-11-06 20:02:35 +08:00
}
}
2018-05-04 00:57:02 +08:00
/ * *
* @ param { Module } module module to patch tie
* @ param { Chunk } chunk chunk to patch tie
* @ returns { void }
* /
2017-11-06 20:02:35 +08:00
patchChunksAfterReasonRemoval ( module , chunk ) {
2020-07-28 00:09:48 +08:00
if ( ! module . hasReasons ( this . moduleGraph , chunk . runtime ) ) {
2017-11-06 20:02:35 +08:00
this . removeReasonsOfDependencyBlock ( module , module ) ;
}
2018-08-14 17:18:22 +08:00
if ( ! module . hasReasonForChunk ( chunk , this . moduleGraph , this . chunkGraph ) ) {
if ( this . chunkGraph . isModuleInChunk ( module , chunk ) ) {
this . chunkGraph . disconnectChunkAndModule ( chunk , module ) ;
2017-11-06 20:02:35 +08:00
this . removeChunkFromDependencies ( module , chunk ) ;
}
}
}
2018-05-04 00:57:02 +08:00
/ * *
*
* @ param { DependenciesBlock } block block tie for Chunk
* @ param { Chunk } chunk chunk to remove from dep
* @ returns { void }
* /
2017-11-06 20:02:35 +08:00
removeChunkFromDependencies ( block , chunk ) {
const iteratorDependency = d => {
2018-02-25 09:00:20 +08:00
if ( ! d . module ) {
2017-11-06 20:02:35 +08:00
return ;
2017-01-06 01:00:36 +08:00
}
2017-11-06 20:02:35 +08:00
this . patchChunksAfterReasonRemoval ( d . module , chunk ) ;
2017-01-24 02:52:20 +08:00
} ;
2017-03-02 22:09:09 +08:00
const blocks = block . blocks ;
2018-02-25 09:00:20 +08:00
for ( let indexBlock = 0 ; indexBlock < blocks . length ; indexBlock ++ ) {
2018-05-15 18:20:17 +08:00
const asyncBlock = blocks [ indexBlock ] ;
2018-08-23 02:17:49 +08:00
const chunkGroup = this . chunkGraph . getBlockChunkGroup ( asyncBlock ) ;
2018-05-04 00:57:02 +08:00
// Grab all chunks from the first Block's AsyncDepBlock
2018-08-23 02:17:49 +08:00
const chunks = chunkGroup . chunks ;
2018-05-04 00:57:02 +08:00
// For each chunk in chunkGroup
2018-02-25 09:00:20 +08:00
for ( let indexChunk = 0 ; indexChunk < chunks . length ; indexChunk ++ ) {
2018-05-04 00:57:02 +08:00
const iteratedChunk = chunks [ indexChunk ] ;
2018-08-23 02:17:49 +08:00
chunkGroup . removeChunk ( iteratedChunk ) ;
2018-05-04 00:57:02 +08:00
// Recurse
this . removeChunkFromDependencies ( block , iteratedChunk ) ;
2017-01-24 02:52:20 +08:00
}
}
2018-02-25 09:00:20 +08:00
if ( block . dependencies ) {
2019-07-10 02:49:10 +08:00
for ( const dep of block . dependencies ) iteratorDependency ( dep ) ;
2017-01-24 02:52:20 +08:00
}
2014-11-02 19:17:05 +08:00
}
2015-07-08 20:15:21 +08:00
2017-01-06 01:00:36 +08:00
sortItemsWithChunkIds ( ) {
2018-02-25 09:00:20 +08:00
for ( const chunkGroup of this . chunkGroups ) {
2018-01-20 00:06:59 +08:00
chunkGroup . sortItems ( ) ;
}
2018-09-07 20:11:48 +08:00
this . errors . sort ( compareErrors ) ;
this . warnings . sort ( compareErrors ) ;
2018-04-03 23:23:41 +08:00
this . children . sort ( byNameOrHash ) ;
2017-01-06 01:00:36 +08:00
}
summarizeDependencies ( ) {
2018-02-25 09:00:20 +08:00
for (
let indexChildren = 0 ;
indexChildren < this . children . length ;
indexChildren ++
) {
2017-12-28 21:38:03 +08:00
const child = this . children [ indexChildren ] ;
2017-01-24 02:52:20 +08:00
2019-08-07 15:54:43 +08:00
this . fileDependencies . addAll ( child . fileDependencies ) ;
this . contextDependencies . addAll ( child . contextDependencies ) ;
this . missingDependencies . addAll ( child . missingDependencies ) ;
2019-08-09 20:46:42 +08:00
this . buildDependencies . addAll ( child . buildDependencies ) ;
2017-01-24 02:52:20 +08:00
}
2018-09-05 22:12:48 +08:00
for ( const module of this . modules ) {
2019-01-05 02:17:37 +08:00
const fileDependencies = module . buildInfo . fileDependencies ;
const contextDependencies = module . buildInfo . contextDependencies ;
2019-01-09 20:23:26 +08:00
const missingDependencies = module . buildInfo . missingDependencies ;
2019-01-05 02:17:37 +08:00
if ( fileDependencies ) {
2019-08-07 15:54:43 +08:00
this . fileDependencies . addAll ( fileDependencies ) ;
2017-01-06 01:00:36 +08:00
}
2019-01-05 02:17:37 +08:00
if ( contextDependencies ) {
2019-08-07 15:54:43 +08:00
this . contextDependencies . addAll ( contextDependencies ) ;
2017-01-06 01:00:36 +08:00
}
2019-01-09 20:23:26 +08:00
if ( missingDependencies ) {
2019-08-07 15:54:43 +08:00
this . missingDependencies . addAll ( missingDependencies ) ;
2019-01-09 20:23:26 +08:00
}
2017-01-24 02:52:20 +08:00
}
2017-01-06 01:00:36 +08:00
}
2018-11-15 00:31:32 +08:00
createModuleHashes ( ) {
const chunkGraph = this . chunkGraph ;
const { hashFunction , hashDigest , hashDigestLength } = this . outputOptions ;
for ( const module of this . modules ) {
2020-07-28 00:09:48 +08:00
for ( const runtime of chunkGraph . getModuleRuntimes ( module ) ) {
const moduleHash = createHash ( hashFunction ) ;
module . updateHash ( moduleHash , {
chunkGraph ,
runtime
} ) ;
const moduleHashDigest = /** @type {string} */ ( moduleHash . digest (
hashDigest
) ) ;
chunkGraph . setModuleHashes (
module ,
runtime ,
moduleHashDigest ,
moduleHashDigest . substr ( 0 , hashDigestLength )
) ;
}
2018-11-15 00:31:32 +08:00
}
}
2017-01-06 01:00:36 +08:00
createHash ( ) {
2020-01-30 18:34:33 +08:00
this . logger . time ( "hashing: initialize hash" ) ;
2018-08-23 01:23:48 +08:00
const chunkGraph = this . chunkGraph ;
2017-01-06 01:00:36 +08:00
const outputOptions = this . outputOptions ;
const hashFunction = outputOptions . hashFunction ;
const hashDigest = outputOptions . hashDigest ;
const hashDigestLength = outputOptions . hashDigestLength ;
2017-11-23 17:59:29 +08:00
const hash = createHash ( hashFunction ) ;
2018-05-29 20:50:40 +08:00
if ( outputOptions . hashSalt ) {
hash . update ( outputOptions . hashSalt ) ;
}
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "hashing: initialize hash" ) ;
if ( this . children . length > 0 ) {
this . logger . time ( "hashing: hash child compilations" ) ;
for ( const child of this . children ) {
hash . update ( child . hash ) ;
}
this . logger . timeEnd ( "hashing: hash child compilations" ) ;
2018-05-29 20:50:40 +08:00
}
2020-01-30 18:34:33 +08:00
if ( this . warnings . length > 0 ) {
this . logger . time ( "hashing: hash warnings" ) ;
for ( const warning of this . warnings ) {
hash . update ( ` ${ warning . message } ` ) ;
}
this . logger . timeEnd ( "hashing: hash warnings" ) ;
2018-05-29 20:50:40 +08:00
}
2020-01-30 18:34:33 +08:00
if ( this . errors . length > 0 ) {
this . logger . time ( "hashing: hash errors" ) ;
for ( const error of this . errors ) {
hash . update ( ` ${ error . message } ` ) ;
}
this . logger . timeEnd ( "hashing: hash errors" ) ;
2018-05-29 20:50:40 +08:00
}
2018-11-15 00:31:32 +08:00
2020-01-30 18:34:33 +08:00
this . logger . time ( "hashing: sort chunks" ) ;
2020-03-13 00:51:26 +08:00
// clone needed as sort below is in place mutation
2018-09-06 22:59:11 +08:00
const chunks = Array . from ( this . chunks ) ;
2017-01-10 20:20:54 +08:00
/ * *
* sort here will bring all "falsy" values to the beginning
* this is needed as the "hasRuntime()" chunks are dependent on the
* hashes of the non - runtime chunks .
* /
2017-01-06 01:00:36 +08:00
chunks . sort ( ( a , b ) => {
const aEntry = a . hasRuntime ( ) ;
const bEntry = b . hasRuntime ( ) ;
2018-02-25 09:00:20 +08:00
if ( aEntry && ! bEntry ) return 1 ;
if ( ! aEntry && bEntry ) return - 1 ;
2018-04-03 23:23:41 +08:00
return byId ( a , b ) ;
2017-01-06 01:00:36 +08:00
} ) ;
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "hashing: sort chunks" ) ;
2020-04-30 23:52:27 +08:00
const fullHashChunks = new Set ( ) ;
2018-02-25 09:00:20 +08:00
for ( let i = 0 ; i < chunks . length ; i ++ ) {
2017-02-05 08:18:40 +08:00
const chunk = chunks [ i ] ;
2018-11-20 19:55:17 +08:00
// Last minute module hash generation for modules that depend on chunk hashes
2020-01-30 18:34:33 +08:00
this . logger . time ( "hashing: hash runtime modules" ) ;
2018-11-20 19:55:17 +08:00
for ( const module of chunkGraph . getChunkModulesIterable ( chunk ) ) {
2020-07-28 00:09:48 +08:00
if ( ! chunkGraph . hasModuleHashes ( module , chunk . runtime ) ) {
2018-11-20 19:55:17 +08:00
const moduleHash = createHash ( hashFunction ) ;
2020-07-28 00:09:48 +08:00
module . updateHash ( moduleHash , {
chunkGraph ,
runtime : chunk . runtime
} ) ;
2019-07-17 22:02:33 +08:00
const moduleHashDigest = /** @type {string} */ ( moduleHash . digest (
hashDigest
) ) ;
2018-11-20 19:55:17 +08:00
chunkGraph . setModuleHashes (
module ,
2020-07-28 00:09:48 +08:00
chunk . runtime ,
2018-11-20 19:55:17 +08:00
moduleHashDigest ,
moduleHashDigest . substr ( 0 , hashDigestLength )
) ;
}
}
2020-01-30 18:34:33 +08:00
this . logger . timeAggregate ( "hashing: hash runtime modules" ) ;
2017-11-23 17:59:29 +08:00
const chunkHash = createHash ( hashFunction ) ;
2020-01-30 18:34:33 +08:00
this . logger . time ( "hashing: hash chunks" ) ;
2018-09-21 23:39:15 +08:00
try {
if ( outputOptions . hashSalt ) {
chunkHash . update ( outputOptions . hashSalt ) ;
}
2018-09-25 22:07:42 +08:00
chunk . updateHash ( chunkHash , chunkGraph ) ;
2019-10-02 14:54:21 +08:00
this . hooks . chunkHash . call ( chunk , chunkHash , {
2018-09-25 22:07:42 +08:00
chunkGraph ,
moduleGraph : this . moduleGraph ,
runtimeTemplate : this . runtimeTemplate
} ) ;
2020-04-30 23:52:27 +08:00
const chunkHashDigest = /** @type {string} */ ( chunkHash . digest (
hashDigest
) ) ;
hash . update ( chunkHashDigest ) ;
chunk . hash = chunkHashDigest ;
2018-09-21 23:39:15 +08:00
chunk . renderedHash = chunk . hash . substr ( 0 , hashDigestLength ) ;
2020-04-30 23:52:27 +08:00
const fullHashModules = chunkGraph . getChunkFullHashModulesIterable (
chunk
) ;
if ( fullHashModules ) {
fullHashChunks . add ( chunk ) ;
} else {
this . hooks . contentHash . call ( chunk ) ;
}
2018-09-21 23:39:15 +08:00
} catch ( err ) {
this . errors . push ( new ChunkRenderError ( chunk , "" , err ) ) ;
2018-05-29 20:50:40 +08:00
}
2020-01-30 18:34:33 +08:00
this . logger . timeAggregate ( "hashing: hash chunks" ) ;
2015-06-25 05:17:12 +08:00
}
2020-01-30 18:34:33 +08:00
this . logger . timeAggregateEnd ( "hashing: hash runtime modules" ) ;
this . logger . timeAggregateEnd ( "hashing: hash chunks" ) ;
this . logger . time ( "hashing: hash digest" ) ;
2020-07-08 23:20:14 +08:00
this . hooks . fullHash . call ( hash ) ;
2019-07-10 17:42:34 +08:00
this . fullHash = /** @type {string} */ ( hash . digest ( hashDigest ) ) ;
2017-01-06 01:00:36 +08:00
this . hash = this . fullHash . substr ( 0 , hashDigestLength ) ;
2020-01-30 18:34:33 +08:00
this . logger . timeEnd ( "hashing: hash digest" ) ;
2020-04-30 23:52:27 +08:00
this . logger . time ( "hashing: process full hash modules" ) ;
for ( const chunk of fullHashChunks ) {
for ( const module of chunkGraph . getChunkFullHashModulesIterable ( chunk ) ) {
const moduleHash = createHash ( hashFunction ) ;
2020-07-28 00:09:48 +08:00
module . updateHash ( moduleHash , {
chunkGraph ,
runtime : chunk . runtime
} ) ;
2020-04-30 23:52:27 +08:00
const moduleHashDigest = /** @type {string} */ ( moduleHash . digest (
hashDigest
) ) ;
chunkGraph . setModuleHashes (
module ,
2020-07-28 00:09:48 +08:00
chunk . runtime ,
2020-04-30 23:52:27 +08:00
moduleHashDigest ,
moduleHashDigest . substr ( 0 , hashDigestLength )
) ;
}
const chunkHash = createHash ( hashFunction ) ;
chunkHash . update ( chunk . hash ) ;
chunkHash . update ( this . hash ) ;
const chunkHashDigest = /** @type {string} */ ( chunkHash . digest (
hashDigest
) ) ;
chunk . hash = chunkHashDigest ;
chunk . renderedHash = chunk . hash . substr ( 0 , hashDigestLength ) ;
this . hooks . contentHash . call ( chunk ) ;
}
this . logger . timeEnd ( "hashing: process full hash modules" ) ;
2013-02-13 20:00:01 +08:00
}
2017-01-06 01:00:36 +08:00
2019-09-11 17:13:46 +08:00
/ * *
* @ param { string } file file name
* @ param { Source } source asset source
* @ param { AssetInfo } assetInfo extra asset information
* @ returns { void }
* /
emitAsset ( file , source , assetInfo = { } ) {
if ( this . assets [ file ] ) {
2019-09-13 14:42:36 +08:00
if ( ! isSourceEqual ( this . assets [ file ] , source ) ) {
2019-11-14 22:01:25 +08:00
this . errors . push (
2019-09-13 14:42:36 +08:00
new WebpackError (
` Conflict: Multiple assets emit different content to the same filename ${ file } `
)
2019-09-11 17:13:46 +08:00
) ;
2019-09-13 14:42:36 +08:00
this . assets [ file ] = source ;
this . assetsInfo . set ( file , assetInfo ) ;
return ;
2019-09-11 17:13:46 +08:00
}
const oldInfo = this . assetsInfo . get ( file ) ;
this . assetsInfo . set ( file , Object . assign ( { } , oldInfo , assetInfo ) ) ;
return ;
}
this . assets [ file ] = source ;
this . assetsInfo . set ( file , assetInfo ) ;
}
/ * *
* @ param { string } file file name
* @ param { Source | function ( Source ) : Source } newSourceOrFunction new asset source or function converting old to new
* @ param { AssetInfo | function ( AssetInfo | undefined ) : AssetInfo } assetInfoUpdateOrFunction new asset info or function converting old to new
* /
updateAsset (
file ,
newSourceOrFunction ,
assetInfoUpdateOrFunction = undefined
) {
if ( ! this . assets [ file ] ) {
throw new Error (
` Called Compilation.updateAsset for not existing filename ${ file } `
) ;
}
if ( typeof newSourceOrFunction === "function" ) {
this . assets [ file ] = newSourceOrFunction ( this . assets [ file ] ) ;
} else {
this . assets [ file ] = newSourceOrFunction ;
}
if ( assetInfoUpdateOrFunction !== undefined ) {
const oldInfo = this . assetsInfo . get ( file ) ;
if ( typeof assetInfoUpdateOrFunction === "function" ) {
this . assetsInfo . set ( file , assetInfoUpdateOrFunction ( oldInfo || { } ) ) ;
} else {
this . assetsInfo . set (
file ,
Object . assign ( { } , oldInfo , assetInfoUpdateOrFunction )
) ;
}
}
}
getAssets ( ) {
2020-05-12 18:16:51 +08:00
/** @type {Readonly<Asset>[]} */
2019-09-11 17:13:46 +08:00
const array = [ ] ;
for ( const assetName of Object . keys ( this . assets ) ) {
if ( Object . prototype . hasOwnProperty . call ( this . assets , assetName ) ) {
array . push ( {
name : assetName ,
source : this . assets [ assetName ] ,
info : this . assetsInfo . get ( assetName ) || { }
} ) ;
}
}
return array ;
}
/ * *
* @ param { string } name the name of the asset
2020-05-12 18:16:51 +08:00
* @ returns { Readonly < Asset > | undefined } the asset or undefined when not found
2019-09-11 17:13:46 +08:00
* /
getAsset ( name ) {
if ( ! Object . prototype . hasOwnProperty . call ( this . assets , name ) )
return undefined ;
return {
name ,
source : this . assets [ name ] ,
info : this . assetsInfo . get ( name ) || { }
} ;
}
2019-07-17 23:30:25 +08:00
clearAssets ( ) {
for ( const chunk of this . chunks ) {
chunk . files . clear ( ) ;
chunk . auxiliaryFiles . clear ( ) ;
}
}
2017-01-06 01:00:36 +08:00
createModuleAssets ( ) {
2019-07-17 23:30:25 +08:00
const { chunkGraph } = this ;
2018-09-05 22:12:48 +08:00
for ( const module of this . modules ) {
2018-02-25 09:00:20 +08:00
if ( module . buildInfo . assets ) {
2019-09-11 17:13:46 +08:00
const assetsInfo = module . buildInfo . assetsInfo ;
2018-02-25 09:00:20 +08:00
for ( const assetName of Object . keys ( module . buildInfo . assets ) ) {
2018-08-23 01:23:48 +08:00
const fileName = this . getPath ( assetName , {
chunkGraph : this . chunkGraph ,
module
} ) ;
2019-07-17 23:30:25 +08:00
for ( const chunk of chunkGraph . getModuleChunksIterable ( module ) ) {
chunk . auxiliaryFiles . add ( fileName ) ;
}
2019-09-11 17:13:46 +08:00
this . emitAsset (
fileName ,
module . buildInfo . assets [ assetName ] ,
assetsInfo ? assetsInfo . get ( assetName ) : undefined
) ;
2017-11-27 22:27:30 +08:00
this . hooks . moduleAsset . call ( module , fileName ) ;
2018-01-22 20:52:43 +08:00
}
2017-01-06 01:00:36 +08:00
}
2013-01-31 01:49:25 +08:00
}
}
2017-01-06 01:00:36 +08:00
2019-10-02 14:54:21 +08:00
/ * *
* @ param { RenderManifestOptions } options options object
* @ returns { RenderManifestEntry [ ] } manifest entries
* /
getRenderManifest ( options ) {
return this . hooks . renderManifest . call ( [ ] , options ) ;
}
2018-12-10 18:34:59 +08:00
/ * *
* @ param { Callback } callback signals when the call finishes
* @ returns { void }
* /
2018-09-27 13:22:19 +08:00
createChunkAssets ( callback ) {
2017-01-06 01:00:36 +08:00
const outputOptions = this . outputOptions ;
2018-09-27 13:22:19 +08:00
const cachedSourceMap = new WeakMap ( ) ;
2018-06-29 16:03:21 +08:00
/** @type {Map<string, {hash: string, source: Source, chunk: Chunk}>} */
const alreadyWrittenFiles = new Map ( ) ;
2018-09-27 13:22:19 +08:00
asyncLib . forEach (
this . chunks ,
2019-06-14 16:45:56 +08:00
( chunk , callback ) => {
2018-09-27 13:22:19 +08:00
/** @type {RenderManifestEntry[]} */
let manifest ;
try {
2019-10-02 14:54:21 +08:00
manifest = this . getRenderManifest ( {
2018-09-27 13:22:19 +08:00
chunk ,
hash : this . hash ,
fullHash : this . fullHash ,
outputOptions ,
2019-10-09 04:29:46 +08:00
codeGenerationResults : this . codeGenerationResults ,
2018-09-27 13:22:19 +08:00
moduleTemplates : this . moduleTemplates ,
dependencyTemplates : this . dependencyTemplates ,
chunkGraph : this . chunkGraph ,
moduleGraph : this . moduleGraph ,
runtimeTemplate : this . runtimeTemplate
2019-10-02 14:54:21 +08:00
} ) ;
2018-09-27 13:22:19 +08:00
} catch ( err ) {
this . errors . push ( new ChunkRenderError ( chunk , "" , err ) ) ;
return callback ( ) ;
}
asyncLib . forEach (
manifest ,
2019-06-14 16:45:56 +08:00
( fileManifest , callback ) => {
2018-10-11 16:46:48 +08:00
const ident = fileManifest . identifier ;
2018-09-27 13:22:19 +08:00
const usedHash = fileManifest . hash ;
2020-07-15 17:14:28 +08:00
const assetCacheItem = this . _assetsCache . getItemCache (
ident ,
usedHash
) ;
assetCacheItem . get ( ( err , sourceFromCache ) => {
2019-09-13 17:12:26 +08:00
/** @type {string | function(PathData, AssetInfo=): string} */
2019-07-23 14:45:14 +08:00
let filenameTemplate ;
/** @type {string} */
let file ;
2020-07-17 20:57:11 +08:00
/** @type {AssetInfo} */
let assetInfo ;
2019-07-23 14:45:14 +08:00
let inTry = true ;
const errorAndCallback = err => {
const filename =
file ||
2020-07-17 20:57:11 +08:00
( typeof file === "string"
? file
: typeof filenameTemplate === "string"
2019-07-23 14:45:14 +08:00
? filenameTemplate
: "" ) ;
this . errors . push ( new ChunkRenderError ( chunk , filename , err ) ) ;
inTry = false ;
return callback ( ) ;
} ;
2018-09-27 13:22:19 +08:00
try {
2020-07-17 20:57:11 +08:00
if ( "filename" in fileManifest ) {
file = fileManifest . filename ;
assetInfo = fileManifest . info ;
} else {
filenameTemplate = fileManifest . filenameTemplate ;
const pathAndInfo = this . getPathWithInfo (
filenameTemplate ,
fileManifest . pathOptions
) ;
file = pathAndInfo . path ;
assetInfo = pathAndInfo . info ;
}
2019-07-19 06:08:50 +08:00
2018-09-27 13:22:19 +08:00
if ( err ) {
2019-07-23 14:45:14 +08:00
return errorAndCallback ( err ) ;
2018-09-27 13:22:19 +08:00
}
let source = sourceFromCache ;
// check if the same filename was already written by another chunk
const alreadyWritten = alreadyWrittenFiles . get ( file ) ;
if ( alreadyWritten !== undefined ) {
if ( alreadyWritten . hash !== usedHash ) {
2019-07-23 14:45:14 +08:00
inTry = false ;
2018-09-27 13:22:19 +08:00
return callback (
2019-06-14 16:45:56 +08:00
new WebpackError (
2018-09-27 13:22:19 +08:00
` Conflict: Multiple chunks emit assets to the same filename ${ file } ` +
` (chunks ${ alreadyWritten . chunk . id } and ${ chunk . id } ) `
)
) ;
} else {
source = alreadyWritten . source ;
}
} else if ( ! source ) {
// render the asset
source = fileManifest . render ( ) ;
// Ensure that source is a cached source to avoid additional cost because of repeated access
if ( ! ( source instanceof CachedSource ) ) {
const cacheEntry = cachedSourceMap . get ( source ) ;
if ( cacheEntry ) {
source = cacheEntry ;
} else {
const cachedSource = new CachedSource ( source ) ;
cachedSourceMap . set ( source , cachedSource ) ;
source = cachedSource ;
}
}
}
2019-09-13 17:12:26 +08:00
this . emitAsset ( file , source , assetInfo ) ;
2019-07-17 23:30:25 +08:00
if ( fileManifest . auxiliary ) {
chunk . auxiliaryFiles . add ( file ) ;
} else {
chunk . files . add ( file ) ;
}
2018-09-27 13:22:19 +08:00
this . hooks . chunkAsset . call ( chunk , file ) ;
alreadyWrittenFiles . set ( file , {
2018-06-29 16:03:21 +08:00
hash : usedHash ,
2018-09-27 13:22:19 +08:00
source ,
chunk
} ) ;
2018-10-09 20:30:59 +08:00
if ( source !== sourceFromCache ) {
2020-07-15 17:14:28 +08:00
assetCacheItem . store ( source , err => {
2019-07-23 14:45:14 +08:00
if ( err ) return errorAndCallback ( err ) ;
2019-11-08 00:31:47 +08:00
inTry = false ;
2019-07-23 14:45:14 +08:00
return callback ( ) ;
} ) ;
2018-10-09 20:30:59 +08:00
} else {
2019-07-23 14:45:14 +08:00
inTry = false ;
2018-10-09 20:30:59 +08:00
callback ( ) ;
}
2018-09-27 13:22:19 +08:00
} catch ( err ) {
2019-07-23 14:45:14 +08:00
if ( ! inTry ) throw err ;
errorAndCallback ( err ) ;
2017-12-01 17:43:14 +08:00
}
2018-09-27 13:22:19 +08:00
} ) ;
} ,
callback
2018-02-25 09:00:20 +08:00
) ;
2018-09-27 13:22:19 +08:00
} ,
callback
) ;
2013-01-31 01:49:25 +08:00
}
2017-01-06 01:00:36 +08:00
2018-05-04 00:57:02 +08:00
/ * *
2019-09-13 17:12:26 +08:00
* @ param { string | function ( PathData , AssetInfo = ) : string } filename used to get asset path with hash
2018-08-23 01:23:48 +08:00
* @ param { PathData } data context data
2018-05-15 18:20:17 +08:00
* @ returns { string } interpolated path
2018-05-04 00:57:02 +08:00
* /
2019-10-02 14:54:21 +08:00
getPath ( filename , data = { } ) {
if ( ! data . hash ) {
data = {
hash : this . hash ,
... data
} ;
}
return this . getAssetPath ( filename , data ) ;
}
/ * *
* @ param { string | function ( PathData , AssetInfo = ) : string } filename used to get asset path with hash
* @ param { PathData } data context data
* @ returns { { path : string , info : AssetInfo } } interpolated path and asset info
* /
getPathWithInfo ( filename , data = { } ) {
2018-10-30 19:02:39 +08:00
if ( ! data . hash ) {
2019-06-19 19:16:05 +08:00
data = {
hash : this . hash ,
... data
} ;
2018-10-30 19:02:39 +08:00
}
2019-10-02 14:54:21 +08:00
return this . getAssetPathWithInfo ( filename , data ) ;
}
/ * *
* @ param { string | function ( PathData , AssetInfo = ) : string } filename used to get asset path with hash
* @ param { PathData } data context data
* @ returns { string } interpolated path
* /
getAssetPath ( filename , data ) {
return this . hooks . assetPath . call (
typeof filename === "function" ? filename ( data ) : filename ,
data ,
undefined
) ;
2017-01-06 01:00:36 +08:00
}
2019-09-11 17:13:46 +08:00
/ * *
2019-09-13 17:12:26 +08:00
* @ param { string | function ( PathData , AssetInfo = ) : string } filename used to get asset path with hash
* @ param { PathData } data context data
2019-09-11 17:13:46 +08:00
* @ returns { { path : string , info : AssetInfo } } interpolated path and asset info
* /
2019-10-02 14:54:21 +08:00
getAssetPathWithInfo ( filename , data ) {
const assetInfo = { } ;
// TODO webpack 5: refactor assetPath hook to receive { path, info } object
const newPath = this . hooks . assetPath . call (
typeof filename === "function" ? filename ( data , assetInfo ) : filename ,
data ,
assetInfo
) ;
return { path : newPath , info : assetInfo } ;
2019-09-11 17:13:46 +08:00
}
2018-05-04 00:57:02 +08:00
/ * *
* This function allows you to run another instance of webpack inside of webpack however as
* a child with different settings and configurations ( if desired ) applied . It copies all hooks , plugins
* from parent ( or top level compiler ) and creates a child Compilation
*
* @ param { string } name name of the child compiler
2018-12-18 04:35:39 +08:00
* @ param { OutputOptions } outputOptions // Need to convert config schema to types for this
2018-05-04 00:57:02 +08:00
* @ param { Plugin [ ] } plugins webpack plugins that will be applied
* @ returns { Compiler } creates a child Compiler instance
* /
2017-04-13 19:43:51 +08:00
createChildCompiler ( name , outputOptions , plugins ) {
2018-02-25 09:00:20 +08:00
const idx = this . childrenCounters [ name ] || 0 ;
2017-04-13 19:43:51 +08:00
this . childrenCounters [ name ] = idx + 1 ;
2018-02-25 09:00:20 +08:00
return this . compiler . createChildCompiler (
this ,
name ,
idx ,
outputOptions ,
plugins
) ;
2017-01-06 01:00:36 +08:00
}
checkConstraints ( ) {
2018-08-14 22:40:37 +08:00
const chunkGraph = this . chunkGraph ;
2018-05-04 00:57:02 +08:00
/** @type {Set<number|string>} */
2018-01-05 14:41:09 +08:00
const usedIds = new Set ( ) ;
2017-01-24 02:52:20 +08:00
2018-09-05 22:12:48 +08:00
for ( const module of this . modules ) {
2018-11-28 20:07:40 +08:00
if ( module . type === "runtime" ) continue ;
2018-09-05 22:12:48 +08:00
const moduleId = chunkGraph . getModuleId ( module ) ;
2018-02-25 09:00:20 +08:00
if ( moduleId === null ) continue ;
2018-05-29 20:50:40 +08:00
if ( usedIds . has ( moduleId ) ) {
2017-01-24 02:52:20 +08:00
throw new Error ( ` checkConstraints: duplicate module id ${ moduleId } ` ) ;
2018-05-29 20:50:40 +08:00
}
2018-01-05 14:41:09 +08:00
usedIds . add ( moduleId ) ;
2017-01-24 02:52:20 +08:00
}
2018-09-06 22:59:11 +08:00
for ( const chunk of this . chunks ) {
2018-08-14 22:40:37 +08:00
for ( const module of chunkGraph . getChunkModulesIterable ( chunk ) ) {
2018-09-05 22:12:48 +08:00
if ( ! this . modules . has ( module ) ) {
2018-08-14 22:40:37 +08:00
throw new Error (
"checkConstraints: module in chunk but not in compilation " +
` ${ chunk . debugId } ${ module . debugId } `
) ;
}
}
for ( const module of chunkGraph . getChunkEntryModulesIterable ( chunk ) ) {
2018-09-05 22:12:48 +08:00
if ( ! this . modules . has ( module ) ) {
2018-08-14 22:40:37 +08:00
throw new Error (
"checkConstraints: entry module in chunk but not in compilation " +
` ${ chunk . debugId } ${ module . debugId } `
) ;
}
}
2018-01-20 00:06:59 +08:00
}
2018-02-25 09:00:20 +08:00
for ( const chunkGroup of this . chunkGroups ) {
2018-01-20 00:06:59 +08:00
chunkGroup . checkConstraints ( ) ;
2017-01-24 02:52:20 +08:00
}
2017-01-06 01:00:36 +08:00
}
}
2020-07-15 17:14:28 +08:00
// Hide from typescript
const compilationPrototype = Compilation . prototype ;
2020-07-08 23:20:14 +08:00
// TODO webpack 6 remove
2020-07-15 17:14:28 +08:00
Object . defineProperty ( compilationPrototype , "modifyHash" , {
2020-07-08 23:20:14 +08:00
writable : false ,
enumerable : false ,
configurable : false ,
value : ( ) => {
throw new Error (
"Compilation.modifyHash was removed in favor of Compilation.hooks.fullHash"
) ;
}
} ) ;
2020-07-15 17:14:28 +08:00
// TODO webpack 6 remove
Object . defineProperty ( compilationPrototype , "cache" , {
enumerable : false ,
configurable : false ,
get : util . deprecate (
/ * *
* @ this { Compilation } the compilation
* @ returns { Cache } the cache
* /
function ( ) {
return this . compiler . cache ;
} ,
"Compilation.cache was removed in favor of Compilation.getCache()" ,
"DEP_WEBPACK_COMPILATION_CACHE"
) ,
set : util . deprecate (
v => { } ,
"Compilation.cache was removed in favor of Compilation.getCache()" ,
"DEP_WEBPACK_COMPILATION_CACHE"
)
} ) ;
2020-05-12 18:16:51 +08:00
/ * *
* Add additional assets to the compilation .
* /
Compilation . PROCESS _ASSETS _STAGE _ADDITIONAL = - 2000 ;
/ * *
* Basic preprocessing of assets .
* /
Compilation . PROCESS _ASSETS _STAGE _PRE _PROCESS = - 1000 ;
/ * *
* Derive new assets from existing assets .
* Existing assets should not be treated as complete .
* /
Compilation . PROCESS _ASSETS _STAGE _DERIVED = - 200 ;
/ * *
* Add additional sections to existing assets , like a banner or initialization code .
* /
Compilation . PROCESS _ASSETS _STAGE _ADDITIONS = - 100 ;
/ * *
* Optimize existing assets in a general way .
* /
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE = 100 ;
/ * *
* Optimize the count of existing assets , e . g . by merging them .
* /
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE _COUNT = 200 ;
/ * *
* Optimize the compatibility of existing assets , e . g . add polyfills or vendor - prefixes .
* /
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE _COMPATIBILITY = 300 ;
/ * *
* Optimize the size of existing assets , e . g . by minimizing or omitting whitespace .
* /
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE _SIZE = 400 ;
/ * *
* Summarize the list of existing assets .
* When creating new assets from this they should be fully optimized .
* e . g . creating an assets manifest of Service Workers .
* /
Compilation . PROCESS _ASSETS _STAGE _SUMMARIZE = 1000 ;
/ * *
* Add development tooling to assets , e . g . by extracting a SourceMap .
* /
Compilation . PROCESS _ASSETS _STAGE _DEV _TOOLING = 2000 ;
/ * *
* Optimize the transfer of existing assets , e . g . by preparing a compressed ( gzip ) file as separate asset .
* /
Compilation . PROCESS _ASSETS _STAGE _OPTIMIZE _TRANSFER = 3000 ;
/ * *
* Analyse existing assets .
* /
Compilation . PROCESS _ASSETS _STAGE _ANALYSE = 4000 ;
/ * *
* Creating assets for reporting purposes .
* /
Compilation . PROCESS _ASSETS _STAGE _REPORT = 5000 ;
2017-01-06 01:00:36 +08:00
module . exports = Compilation ;