webpack-import-maps-loader

Webpack loader for import maps

Usage no npm install needed!

<script type="module">
  import webpackImportMapsLoader from 'https://cdn.skypack.dev/webpack-import-maps-loader';
</script>

README

webpack-import-maps-loader

Plugin to rewrite bare imports to URLs as defined in import map

Known limitations

Only dynamic imports supported at this time.

// Promise
import('user').then(({logout}) =>  logout());

// Using await
const { logout } = await import('user');

If you know how to get Webpack to ignore a regular import. Please get in touch!

Installation

$ npm install -D webpack-import-maps-loader

Usage

With module map inlined:

export default {
  // ...other webpack config
  module: {
    rules: [
      {
        test: /\.[tj]sx?$/,
        use: {
          loader: "webpack-import-maps-loader",
          options: {
            imports: {
              "bare-import": "https://assets.domain.tld/bare-import/index.js",
            },
          },
        },
      }
    ]
  }
};

Or loaded as a file:

// import-map.json
{
  "imports": {
    "bare-import": "https://assets.domain.tld/bare-import/index.js"
  }
}

// webpack.config.js
export default {
  module: {
    rules: [
      {
        test: /\.[tj]sx?$/,
        use: {
          loader: "webpack-import-maps-loader",
          options: require('./import-map.json'),
        },
      },
    ],
  },
};