From cdf01cf33d2db0f25558413ce3ba98b472c4dd07 Mon Sep 17 00:00:00 2001 From: Felix Haus Date: Sat, 16 Nov 2019 20:31:29 +0100 Subject: [PATCH] [webpack] Updates definitions for Chunk and Module (#40106) * [webpack] Updates definitions for Chunk and Module* Updates definitions for webpack.compilation.Chunk* Adds definitions for webpack.compilation.Module * [webpack] Bumps version number --- types/webpack/index.d.ts | 167 +++++++++++++++++++++++++-------- types/webpack/webpack-tests.ts | 24 +++++ 2 files changed, 154 insertions(+), 37 deletions(-) diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index 8752c7fb30..91b5297b50 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for webpack 4.39 +// Type definitions for webpack 4.41 // Project: https://github.com/webpack/webpack // Definitions by: Qubo // Benjamin Lim @@ -18,6 +18,7 @@ // Grgur Grisogono // Rubens Pinheiro Gonçalves Cavalcante // Anders Kaseorg +// Felix Haus // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -767,66 +768,158 @@ declare namespace webpack { class Asset { } - class Module { + class DependenciesBlock { + } + + class Module extends DependenciesBlock { + constructor(type: string, context?: string); + type: string; + context: string | null; + debugId: number; + hash: string | undefined; + renderedHash: string | undefined; + resolveOptions: any; + factoryMeta: any; + warnings: any[]; + errors: any[]; + buildMeta: any; + buildInfo: any; + reasons: any[]; + _chunks: SortableSet; + id: number | string | null; + index: number | null; + index2: number | null; + depth: number | null; + issuer: Module | null; + profile: any; + prefetched: boolean; + built: boolean; + + used: null | boolean; + usedExports: false | true | string[] | null; + optimizationBailout: string | any[]; + _rewriteChunkInReasons: { + oldChunk: Chunk, newChunks: Chunk[] + } | undefined; + useSourceMap: boolean; + _source: any; + + exportsArgument: string | 'exports'; + moduleArgument: string | 'module'; + + disconnect(): void; + unseal(): void; + setChunks(chunks: Chunk[]): void; + addChunk(chunk: Chunk): boolean; + removeChunk(chunk: Chunk): boolean; + isInChunk(chunk: Chunk): boolean; + isEntryModule(): boolean; + optional: boolean; + getChunks(): Chunk[]; + getNumberOfChunks: number; + chunksIterable: SortableSet; + hasEqualsChunks(module: Module): boolean; + addReason(module: Module, dependency: any, explanation: any): void; + removeReason(module: Module, dependency: any): boolean; + hasReasonForChunk(chunk: Chunk): boolean; + hasReasons(): boolean; + rewriteChunkInReasons(oldChunk: Chunk, newChunks: Chunk[]): void; + _doRewriteChunkInReasons(oldChunk: Chunk, newChunks: Chunk[]): void; + isUsed(exportName?: string): boolean | string; + isProvided(exportName: string): boolean | null; + toString(): string; + needRebuild(fileTimestamps: any, contextTimestamps: any): boolean; + updateHash(hash: any): void; + sortItems(sortChunks?: boolean): void; + unbuild(): void; } class Record { } class Chunk { - constructor(name: string); - id: any; - ids: any; + constructor(name?: string); + id: number | null; + ids: number[] | null; debugId: number; - name: any; - entryModule: any; - files: any[]; + name: string; + preventIntegration: boolean; + entryModule: Module | undefined; + _modules: SortableSet; + filenameTemplate: string | undefined; + _groups: SortableSet; + files: string[]; rendered: boolean; - hash: any; - renderedHash: any; - chunkReason: any; + hash: string | undefined; + contentHash: object; + renderedHash: string | undefined; + chunkReason: string | undefined; extraAsync: boolean; + removedModules: any; hasRuntime(): boolean; canBeInitial(): boolean; isOnlyInitial(): boolean; hasEntryModule(): boolean; - addModule(module: any): boolean; - removeModule(module: any): boolean; - setModules(modules: any): void; + addModule(module: Module): boolean; + removeModule(module: Module): boolean; + setModules(modules: Module[]): void; getNumberOfModules(): number; - modulesIterable: any[]; + // Internally returns this._modules + // So it should have the same type as this._modules + modulesIterable: SortableSet; - addGroup(chunkGroup: any): boolean; - removeGroup(chunkGroup: any): boolean; - isInGroup(chunkGroup: any): boolean; + addGroup(chunkGroup: ChunkGroup): boolean; + removeGroup(chunkGroup: ChunkGroup): boolean; + isInGroup(chunkGroup: ChunkGroup): boolean; getNumberOfGroups(): number; - groupsIterable: any[]; + // Internally returns this._groups + // So it should have the same type as this._groups + groupsIterable: SortableSet; - compareTo(otherChunk: any): -1 | 0 | 1; - containsModule(module: any): boolean; - getModules(): any[]; + compareTo(otherChunk: Chunk): -1 | 0 | 1; + containsModule(module: Module): boolean; + getModules(): Module[]; getModulesIdent(): any[]; - remove(reason: any): void; - moveModule(module: any, otherChunk: any): void; - integrate(otherChunk: any, reason: any): boolean; - split(newChunk: any): void; + remove(reason?: string): void; + moveModule(module: Module, otherChunk: Chunk): void; + integrate(otherChunk: Chunk, reason: string): boolean; + split(newChunk: Chunk): void; isEmpty(): boolean; updateHash(hash: any): void; - canBeIntegrated(otherChunk: any): boolean; - addMultiplierAndOverhead(size: number, options: any): number; + canBeIntegrated(otherChunk: Chunk): boolean; + addMultiplierAndOverhead( + size: number, + options: { + chunkOverhead?: number; + entryChunkMultiplicator?: number + }, + ): number; modulesSize(): number; - size(options: any): number; - integratedSize(otherChunk: any, options: any): number; + size(options?: { + chunkOverhead?: number; + entryChunkMultiplicator?: number + }): number; + integratedSize(otherChunk: Chunk, options: any): number | false; + sortModules( + sortByFn: (module1: Module, module2: Module) => -1 | 0 | 1 + ): void; + sortItems(): void; + getAllAsyncChunks(): Set; + getChunkMaps(realHash: boolean): { + hash: any; + contentHash: any; + name: any; + }; + getChildIdsByOrders(): any; + getChildIdsByOrdersMap(includeDirectChildren?: boolean): any; // tslint:disable-next-line:ban-types - sortModules(sortByFn: Function): void; - getAllAsyncChunks(): Set; - getChunkMaps(realHash: any): { hash: any, name: any }; - // tslint:disable-next-line:ban-types - getChunkModuleMaps(filterFn: Function): { id: any, hash: any }; - // tslint:disable-next-line:ban-types - hasModuleInGraph(filterFn: Function, filterChunkFn: Function): boolean; + getChunkModuleMaps(filterFn: Function): { id: any; hash: any }; + hasModuleInGraph( + filterFn: (module: Module) => boolean, + filterChunkFn: (chunk: Chunk) => boolean + ): boolean; toString(): string; } diff --git a/types/webpack/webpack-tests.ts b/types/webpack/webpack-tests.ts index 2060785e1e..d128bac5db 100644 --- a/types/webpack/webpack-tests.ts +++ b/types/webpack/webpack-tests.ts @@ -971,6 +971,30 @@ function testTemplateFn() { } const chunk = new webpack.compilation.Chunk('name'); + +chunk.sortModules((module1, module2) => { + if (module1) + return 1; + else if (module2) + return -1; + return 0; +}); +chunk.addMultiplierAndOverhead(12, {}); +chunk.addMultiplierAndOverhead(12, { + chunkOverhead: 1, + entryChunkMultiplicator: 2 +}); +chunk.size(); +chunk.size({}); +chunk.size({ + chunkOverhead: 1, + entryChunkMultiplicator: 2 +}); +chunk.hasModuleInGraph( + m => m.type === "webassembly/async", + chunk => chunk.name === "vendor" +); + const moduleTemplate = ({} as any) as webpack.compilation.ModuleTemplate; webpack.Template.renderChunkModules( chunk,