Skip to content

Commit

Permalink
fix: infinite loop when traversing the chunk graph in OutputPlugin (c…
Browse files Browse the repository at this point in the history
…allstack#679)

* fix: ifinite recursion in output plugin

* chore: add changeset

* refactor: typings
  • Loading branch information
jbroma authored Aug 2, 2024
1 parent e51b7a6 commit d8924c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-cats-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

fix infinite loop when traversing the chunk graph in OutputPlugin
16 changes: 10 additions & 6 deletions packages/repack/src/webpack/plugins/OutputPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Rule, WebpackPlugin } from '../../types';
import { AssetsCopyProcessor } from './utils/AssetsCopyProcessor';
import { AuxiliaryAssetsCopyProcessor } from './utils/AuxiliaryAssetsCopyProcessor';

type ChunkId = Exclude<webpack.StatsChunk['id'], undefined>;
/**
* Matching options to check if given {@link DestinationConfig} should be used.
*/
Expand Down Expand Up @@ -227,19 +228,22 @@ export class OutputPlugin implements WebpackPlugin {

const getAllInitialChunks = (
chunk: webpack.StatsChunk,
chunks: Map<string | number, webpack.StatsChunk>
chunks: Map<ChunkId, webpack.StatsChunk>,
visited = new Set<ChunkId>()
): Array<webpack.StatsChunk> => {
if (!chunk.parents?.length) {
return [chunk];
// Prevent cycles when traversing chunks graph
if (visited.has(chunk.id!)) {
return [];
}
visited.add(chunk.id!);

// Chunk might reference itself as a parent (and/or child)
if (chunk.parents.length === 1 && chunk.parents[0] === chunk.id) {
// If chunk has no parents, it's an initial chunk
if (!chunk.parents?.length) {
return [chunk];
}

return chunk.parents.flatMap((parent) => {
return getAllInitialChunks(chunks.get(parent)!, chunks);
return getAllInitialChunks(chunks.get(parent)!, chunks, visited);
});
};

Expand Down

0 comments on commit d8924c6

Please sign in to comment.