Skip to content

Commit

Permalink
fix: Pass chunkGraph through in generate function (#300)
Browse files Browse the repository at this point in the history
* pass through chunkGraph in generate fn

* add test

* fix test

---------

Co-authored-by: shellscape <andrew@shellscape.org>
  • Loading branch information
pgoldberg and shellscape committed Jun 11, 2024
1 parent 64faade commit 59feddb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const generateManifest = (
(e, [name, entrypoint]) => Object.assign(e, { [name]: entrypoint.getFiles() }),
{} as Record<string, any>
);
result = generate(seed, files, entrypoints);
result = generate(seed, files, entrypoints, compilation.chunkGraph);
} else {
result = files.reduce(
(manifest, file) => Object.assign(manifest, { [file.name]: file.path }),
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { relative, resolve } from 'path';

import { SyncHook } from 'tapable';
import type { Compiler, WebpackPluginInstance, Compilation } from 'webpack';
import type { Compiler, WebpackPluginInstance, Compilation, ChunkGraph } from 'webpack';
import { FileDescriptor } from './helpers';
import { beforeRunHook, emitHook, getCompilerHooks, normalModuleLoaderHook } from './hooks';

Expand All @@ -18,7 +18,8 @@ export interface InternalOptions {
generate: (
seed: Record<any, any>,
files: FileDescriptor[],
entries: Record<string, string[]>
entries: Record<string, string[]>,
chunkGraph: ChunkGraph
) => Manifest;
map: (file: FileDescriptor) => FileDescriptor;
publicPath: string;
Expand Down
28 changes: 28 additions & 0 deletions test/unit/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,31 @@ test('should generate manifest with "entrypoints" key', async (t) => {
}
});
});

test('should generate manifest with chunk modules', async (t) => {
const config = {
context: __dirname,
entry: '../fixtures/nameless.js',
output: { path: join(outputPath, 'chunk-to-is-entry-chunk') }
};

const { manifest } = await compile(config, t, {
generate: (_seed, files, _entrypoints, chunkGraph) => {
const chunkToIsEntryChunk = Object.fromEntries(
files
.filter((file) => file.isChunk)
.map((file) => [file.name, chunkGraph.getNumberOfEntryModules(file.chunk) > 0])
);
return {
chunkToIsEntryChunk
};
}
});

t.deepEqual(manifest, {
chunkToIsEntryChunk: {
'fixtures_file_js.js': false,
'main.js': true
}
});
});

0 comments on commit 59feddb

Please sign in to comment.