mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
webpack: Add typings for ChunkGroup, and mainTemplate hooks (#43818)
* [webpack] add compilation.addChunkInGroup method Links: - https://github.com/webpack/webpack/blob/v4.42.1/lib/Compilation.js#L1485 * [webpack] add mainTemplate.hooks.afterStartup and hashForChunk typings Links: - https://github.com/webpack/webpack/blob/v4.42.1/lib/MainTemplate.js#L102 - https://github.com/webpack/webpack/blob/v4.42.1/lib/MainTemplate.js#L127 * [webpack] add compilation.ChunkGroup typings Links: - [chunks](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L70) - [childrenIterable](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L240) - [parentsIterable](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L281) - [insertChunk](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L158) - [getNumberOfChildren](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L236) - [module index methods](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L456-L492) - [addChild](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L224) - [removeChild](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L244) - [setParents](https://github.com/webpack/webpack/blob/v4.42.1/lib/ChunkGroup.js#L266)
This commit is contained in:
parent
72c1e33e13
commit
34698fefff
37
types/webpack/index.d.ts
vendored
37
types/webpack/index.d.ts
vendored
@ -27,6 +27,7 @@
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
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<ChunkGroup>;
|
||||
parentsIterable: SortableSet<ChunkGroup>;
|
||||
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<ChunkGroup>): 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<string, Chunk, string>;
|
||||
requireEnsure: SyncWaterfallHook<string, Chunk, string>;
|
||||
localVars: SyncWaterfallHook<string, Chunk, string>;
|
||||
afterStartup: SyncWaterfallHook<string, Chunk, string>;
|
||||
hashForChunk: SyncHook<CryptoHash, Chunk>;
|
||||
};
|
||||
outputOptions: Output;
|
||||
requireFn: string;
|
||||
@ -1201,6 +1236,8 @@ declare namespace webpack {
|
||||
missingDependencies: SortableSet<string>;
|
||||
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;
|
||||
|
||||
@ -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: [
|
||||
|
||||
Loading…
Reference in New Issue
Block a user