Skip to content

Commit e005e69

Browse files
authored
Merge pull request #27 from paradite/feat/webpack-5
feat(webpack): webpack 5 support
2 parents e8872e8 + c55d797 commit e005e69

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/detect.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,42 @@ function getUsedExportMap(includedFileMap, compilation) {
3838
const unusedExportMap = {};
3939

4040
compilation.chunks.forEach(function(chunk) {
41+
const isWebpack5 = compilation.chunkGraph ? true : false;
4142
for (const module of chunk.modulesIterable) {
4243
if (!module.resource) continue;
43-
44-
const providedExports = module.providedExports || module.buildMeta.providedExports;
44+
let providedExports;
45+
if(isWebpack5) {
46+
providedExports = compilation.chunkGraph.moduleGraph.getProvidedExports(module);
47+
} else {
48+
providedExports = module.providedExports || module.buildMeta.providedExports;
49+
}
50+
let usedExports;
51+
if(isWebpack5) {
52+
usedExports = compilation.chunkGraph.moduleGraph.getUsedExports(module, chunk.runtime);
53+
} else {
54+
usedExports = module.usedExports;
55+
}
4556
const path = convertToUnixPath(module.resource);
57+
let usedExportsArr = [];
58+
// in webpack 4 usedExports can be null | boolean | Array<string>
59+
// in webpack 5 it can be null | boolean | SortableSet<string>
60+
if(usedExports instanceof Set) {
61+
usedExportsArr = Array.from(usedExports);
62+
} else {
63+
usedExportsArr = usedExports;
64+
}
4665

4766
if (
48-
module.usedExports !== true &&
67+
usedExports !== true &&
4968
providedExports !== true &&
5069
/^((?!(node_modules)).)*$/.test(path) &&
5170
includedFileMap[path]
5271
) {
53-
if (module.usedExports === false) {
72+
if (usedExports === false) {
5473
unusedExportMap[path] = providedExports;
5574
} else if (providedExports instanceof Array) {
5675
const unusedExports = providedExports.filter(
57-
x => module.usedExports instanceof Array && !module.usedExports.includes(x)
76+
x => usedExportsArr instanceof Array && !usedExportsArr.includes(x)
5877
);
5978

6079
if (unusedExports.length > 0) {

0 commit comments

Comments
 (0)