Skip to content

Commit

Permalink
fix(code splitting): potential over-extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed May 17, 2020
1 parent 47ef68d commit 0b94909
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,22 @@ export default (options: Options = {}): Plugin => {
};

const getEmitted = (): Map<string, string[]> => {
const idsMap = new Map<string, string[]>();
const emittedMap = new Map<string, string[]>();
const chunks = Object.values(bundle).filter((c): c is OutputChunk => c.type === "chunk");
const entries = chunks.filter(c => c.isEntry || c.isDynamicEntry);
const multiFile = typeof postcssLoaderOpts.extract !== "string" && entries.length > 1;

if (multiFile) {
for (const chunk of chunks) {
const emitted = preserveModules ? chunks : entries;
for (const chunk of emitted) {
const name = preserveModules
? path.basename(chunk.fileName, path.extname(chunk.fileName))
: chunk.name;
const ids = getImports(chunk);
if (ids.length !== 0) idsMap.set(name, ids);
if (ids.length !== 0) emittedMap.set(name, ids);
}

return idsMap;
return emittedMap;
}

const root = entries.find(e => e.isEntry) ?? entries[0];
Expand All @@ -237,9 +238,9 @@ export default (options: Options = {}): Plugin => {
? path.basename(root.fileName, path.extname(root.fileName))
: root.name;
const ids = entries.reduce<string[]>((acc, e) => [...acc, ...getImports(e)], []);
if (ids.length !== 0) idsMap.set(name, ids);
if (ids.length !== 0) emittedMap.set(name, ids);

return idsMap;
return emittedMap;
};

for await (const [name, ids] of getEmitted()) {
Expand Down

0 comments on commit 0b94909

Please sign in to comment.