diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index 8c1e148c8f..97ab0d1324 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -27,6 +27,7 @@ /// +import { Hash as CryptoHash } from 'crypto'; import { Tapable, HookMap, @@ -923,12 +924,44 @@ declare namespace webpack { toString(): string; } + type GroupOptions = string | { name?: string; }; + class ChunkGroup { + chunks: Chunk[]; + childrenIterable: SortableSet; + parentsIterable: SortableSet; + insertChunk(chunk: Chunk, before: Chunk): boolean; + getNumberOfChildren(): number; + setModuleIndex(module: Module, index: number): void; + getModuleIndex(module: Module): number | undefined; + setModuleIndex2(module: Module, index: number): void; + getModuleIndex2(module: Module): number | undefined; + addChild(chunk: ChunkGroup): boolean; + removeChild(chunk: ChunkGroup): boolean; + setParents(newParents: Iterable): void; } class ChunkHash { } + interface SourcePosition { + line: number; + column?: number; + } + + interface RealDependencyLocation { + start: SourcePosition; + end?: SourcePosition; + index?: number; + } + + interface SynteticDependencyLocation { + name: string; + index?: number; + } + + type DependencyLocation = SynteticDependencyLocation | RealDependencyLocation; + class Dependency { constructor(); getResourceIdentifier(): any; @@ -1123,6 +1156,8 @@ declare namespace webpack { requireExtensions: SyncWaterfallHook; requireEnsure: SyncWaterfallHook; localVars: SyncWaterfallHook; + afterStartup: SyncWaterfallHook; + hashForChunk: SyncHook; }; outputOptions: Output; requireFn: string; @@ -1201,6 +1236,8 @@ declare namespace webpack { missingDependencies: SortableSet; hash?: string; getStats(): Stats; + addChunkInGroup(groupOptions: GroupOptions): ChunkGroup; + addChunkInGroup(groupOptions: GroupOptions, module: Module, loc: DependencyLocation, request: string): ChunkGroup; addModule(module: CompilationModule, cacheGroup: any): any; // tslint:disable-next-line:ban-types addEntry(context: any, entry: any, name: any, callback: Function): void; diff --git a/types/webpack/test/index.ts b/types/webpack/test/index.ts index 1ad44c9c7e..4e17f50c26 100644 --- a/types/webpack/test/index.ts +++ b/types/webpack/test/index.ts @@ -315,6 +315,18 @@ configuration = { mainTemplate.hooks.localVars.tap('SomePlugin', resource => { return resource.trimLeft(); }); + mainTemplate.hooks.afterStartup.tap('SomePlugin', (resource, chunk) => { + if (chunk.name) { + return `/* In named chunk: ${chunk.name} */ ${resource};`; + } else { + return resource; + } + }); + mainTemplate.hooks.hashForChunk.tap('SomePlugin', (hash, chunk) => { + if (chunk.name) { + hash.update(chunk.name); + } + }); if (mainTemplate.hooks.jsonpScript == null) { return; } @@ -929,6 +941,45 @@ class DefinePlugin extends webpack.Plugin { } } +class ChunkGroupTestPlugin extends webpack.Plugin { + apply(compiler: webpack.Compiler) { + compiler.hooks.compilation.tap("ChunkGroupTestPlugin", compilation => { + const namedChunkGroupA = compilation.addChunkInGroup('vendors-a'); + const namedChunkGroupB = compilation.addChunkInGroup({ name: 'vendors-b' }); + const unnamedChunkGroup = compilation.addChunkInGroup({}); + if (namedChunkGroupA.getNumberOfChildren() > 0) { + for (const chunk of namedChunkGroupA.chunks) {} + } + Array.from(namedChunkGroupA.childrenIterable).forEach(childGroup => { + namedChunkGroupA.removeChild(childGroup); + namedChunkGroupA.addChild(childGroup); + }); + Array.from(namedChunkGroupA.parentsIterable).forEach(parentGroup => {}); + namedChunkGroupA.setParents([namedChunkGroupB]); + namedChunkGroupA.setParents(new Set([unnamedChunkGroup])); + compilation.hooks.optimizeModules.tap("ChunkGroupTestPlugin", modules => { + for (const module of modules) { + const group = compilation.addChunkInGroup('module', module, { start: { line: 0 } }, 'module.js'); + if (module.index) { + group.setModuleIndex(module, module.index); + } + if (module.index2) { + group.setModuleIndex2(module, module.index2); + } + console.log(group.getModuleIndex(module), group.getModuleIndex2(module)); + break; + } + }); + compilation.hooks.optimizeChunks.tap("ChunkGroupTestPlugin", chunks => { + const firstChunk = chunks[0]; + for (const groupChunk of namedChunkGroupA.chunks) { + namedChunkGroupA.insertChunk(firstChunk, groupChunk); + } + }); + }); + } +} + configuration = { module: { rules: [