'How can I get all module dependencies of each entry in webpack5, including the ones that are external

My appeal is, some dependencies in my project have been external, and these dependencies are shared by multiple entries. I want to count the external dependencies used by each entry

With webpack4:

function _getAllDeps(module) {
    const moduleRecord = {};
    const result = {};
    const _find = (module) => {
        if (!module || moduleRecord[module.request]) {
            return false;
        }
        moduleRecord[module.request] = true;
        if (module.dependencies && Array.isArray(module.dependencies)) {
            module.dependencies.forEach((dep) => {
                _find(dep.module);
                
                // all dependencies are there, and I can pick the 'externals' by `module.external`
                if (!(dep.module && dep.module.external)) {
                    return false;
                }
                const { request } = dep.module;
                result[request] = true;
            });
        }
    };
    _find(module);
    return Object.keys(result);
}

compiler.hooks.emit.tapAsync('xxxxx', (compilation, done) => {
    compilation.chunks.forEach((chunk) => {
        const actualDeps = _getAllDeps(chunk.entryModule)

        // Now, I have get all the dependences used by the chunk
        // And I can compare with dependencies in package.json
        // Finally, I will get external dependencies used by each entry
 
    })
})

But in webpack5:

compiler.hooks.emit.tapAsync('DepsJsonWebpackPlugin', (compilation, done) => {
    compilation.chunks.forEach((chunk) => {
        console.log(chunk.name);
        for (const module of compilation.chunkGraph.getChunkModulesIterable(chunk)) {
          console.log(module.request);
        }
    });
    done();
});

All the dependencies specified in externals lost.

So, how can I get all module dependencies of each entry in webpack5, including the ones that are external



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source