Aziz Yuldoshev 2020-04-12 22:53:43 +02:00 committed by GitHub
parent 72c1e33e13
commit 34698fefff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 0 deletions

View File

@ -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;

View File

@ -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: [