Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Add an option to warn when importing duplicate versions of the same module #149

@philipwalton

Description

@philipwalton

There are several webpack plugins that offer similar functionality (e.g. this one), but IMO warning about duplicates should be a built-in feature of any bundling tool.

In a rollup build today I noticed I was seeing multiple instances of the same classes in my bundle (e.g. WorkboxError, WorkboxError$1, WorkboxError$2, etc.), so I added the following code to my build script to detect duplicates:

const moduleIds = {}
for (const module of bundle.modules) {
  const NODE_MODULES = 'node_modules';

  if (module.id.includes(NODE_MODULES)) {
    const id = module.id.slice(
        module.id.lastIndexOf(NODE_MODULES) + NODE_MODULES.length + 1);

    if (!moduleIds[id]) moduleIds[id] = [];

    moduleIds[id].push(module.id);
  }
}

for (const [id, instances] of Object.entries(moduleIds)) {
  if (instances.length > 1) {
    console.log(`Duplicate module detected for: ${id}`)
    console.log(instances);
  }
}

After that, I get lots of entries like the following one reporting duplicate modules:

Duplicate module detected for: workbox-core/_private/WorkboxError.mjs
[ '/Users/philipwalton/Projects/philipwalton/blog/node_modules/workbox-background-sync/node_modules/workbox-core/_private/WorkboxError.mjs',
  '/Users/philipwalton/Projects/philipwalton/blog/node_modules/workbox-routing/node_modules/workbox-core/_private/WorkboxError.mjs',
  '/Users/philipwalton/Projects/philipwalton/blog/node_modules/workbox-strategies/node_modules/workbox-core/_private/WorkboxError.mjs',
  '/Users/philipwalton/Projects/philipwalton/blog/node_modules/workbox-broadcast-cache-update/node_modules/workbox-core/_private/WorkboxError.mjs',
  '/Users/philipwalton/Projects/philipwalton/blog/node_modules/workbox-precaching/node_modules/workbox-core/_private/WorkboxError.mjs',
  '/Users/philipwalton/Projects/philipwalton/blog/node_modules/workbox-cache-expiration/node_modules/workbox-core/_private/WorkboxError.mjs' ]

Note that these aren't different versions of the same module, they're actually the same version, they're just imported multiple times because they have different paths (i.e. they're in nested node_module directories rather than a common top-level directory shared by all consuming modules).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions