Skip to content

Web Dev Server with importMapsPlugin will not resolve import maps for dynamic imports. #2910

Open

Description

@arodic

If you attempt to import a file using import('./module.js') and module.js includes an import path that requires importmap resolution, the import will fail with error 500

Activity

  1. bashmish commented on Mar 24, 2025

    @bashmish
    Member

    This breaks currently due to the rollup adapter logic, which by default fails on the unresolved imports.
    We wrap rollup nodeResolve plugin, and when you have nodeResolve activated, it will break.

    Locally you can write a simple plugin to workaround this, but it's far from ideal. E.g. smth like that:

    // put the modules which will be resolved by Import Maps in runtime
    const importMapsModules = ['./module.js'];
    
    /** @returns {import('rollup').Plugin} */
    export function rollupPluginFixImportMapsModules() {
      return {
        name: 'rollup-plugin-fix-import-maps-modules',
    
        async resolveId(id) {
          if (importMapsModules.includes(id)) {
            return null;
          }
          return undefined;
        },
      };
    }
    
    /** @returns {import('@web/dev-server-core').Plugin} */
    export function wdsPluginFixImportMapsModules() {
      return {
        name: 'wds-plugin-fix-import-maps-modules',
    
        async resolveImport({ source }) {
          if (importMapsModules.includes(source)) {
            return source;
          }
          return undefined;
        },
      };
    }

    For rollup there are better ways to solve it, but I'm using this workaround currently to reuse the list of modules between the 2.

  2. bashmish commented on Mar 24, 2025

    @bashmish
    Member

    I do have some ideas on how to solve it in a generic way, but it doesn't seem trivial.
    Either way you either need to know about import maps in the WDS or you need to disable the failure on unresolved imports (I don't think it's a good idea).
    Import maps can be checked by WDS using static analyses similar to how we do it in current Import Maps plugin, but with the recent support for dynamic Import Maps such logic will also not be a universal solution to this problem.

    I'm currently actively working with Import Maps and exploring different ways of working with them, so keep posting here new findings and ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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