What is systemjs-webpack-interop?
The systemjs-webpack-interop package is designed to facilitate the integration of Webpack bundles with SystemJS, a dynamic module loader. This package helps in configuring Webpack to produce bundles that can be easily consumed by SystemJS, making it easier to manage dependencies and load modules dynamically in a micro-frontend architecture.
What are systemjs-webpack-interop's main functionalities?
Setting Public Path
This feature allows you to set the public path dynamically based on the module name. This is particularly useful in micro-frontend architectures where different applications might be served from different locations.
const { setPublicPath } = require('systemjs-webpack-interop');
setPublicPath('my-app');
Dynamic Import Map
This feature allows you to set an import map dynamically, which SystemJS uses to resolve module paths. This is useful for managing dependencies that are hosted on different CDNs or servers.
const { setImportMap } = require('systemjs-webpack-interop');
setImportMap({
imports: {
'my-dependency': 'https://cdn.example.com/my-dependency.js'
}
});
Webpack Plugin
This feature provides a Webpack plugin that automatically sets the public path for your Webpack bundles based on the SystemJS module name. This ensures that your bundles are correctly loaded by SystemJS.
const SystemJSPublicPathWebpackPlugin = require('systemjs-webpack-interop/SystemJSPublicPathWebpackPlugin');
module.exports = {
plugins: [
new SystemJSPublicPathWebpackPlugin({
systemjsModuleName: 'my-app'
})
]
};
Other packages similar to systemjs-webpack-interop
single-spa
Single-spa is a framework for bringing together multiple JavaScript microfrontends in a frontend application. It provides a more comprehensive solution for micro-frontend architecture, including routing and lifecycle management, whereas systemjs-webpack-interop focuses specifically on the integration between Webpack and SystemJS.
import-map-overrides
Import-map-overrides is a tool that allows you to override import maps in the browser. It provides a user interface for dynamically changing import maps, which can be useful for development and debugging. While systemjs-webpack-interop helps set import maps programmatically, import-map-overrides offers a more interactive approach.
systemjs-webpack-interop
An npm package for webpack bundles that are used as systemjs modules.
What is this?
systemjs-webpack-interop is an npm package that exports functions that help you create a webpack bundle that
is consumable by SystemJS as an in-browser module.
Specifically, the library does two things:
Background / other work
Webpack has several features that are geared towards better interop with SystemJS. Here are relevant links:
Installation
Note that systemjs-webpack-interop requires systemjs@>=6.
npm install --save systemjs-webpack-interop
yarn add systemjs-webpack-interop
Setting Public Path
systemjs-webpack-interop will dynamically set the webpack public path based on the URL that a SystemJS module was downloaded from.
As a Webpack Plugin
You can set the public path by adding the SystemJSPublicPathWebpackPlugin.
const SystemJSPublicPathWebpackPlugin = require("systemjs-webpack-interop/SystemJSPublicPathWebpackPlugin");
module.exports = {
plugins: [
new SystemJSPublicPathWebpackPlugin({
rootDirectoryLevel: 1,
systemjsModuleName: "@org-name/project-name"
})
]
};
With Code
You can also set the public path with code inside of your webpack project.
Newer versions of webpack
If you're using at least webpack 5.0.0-beta.15, simply add the following to the very top of your webpack entry file:
import "systemjs-webpack-interop/auto-public-path";
If you need the webpack public path to "chop off" some of the directories in the current module's url, you can specify a "root directory level". Note that the root directory level is read from right-to-left, with 1
indicating "current directory" and 2
indicating "up one directory":
import "systemjs-webpack-interop/auto-public-path/2";
import "systemjs-webpack-interop/auto-public-path/3";
Older versions of webpack
Resource Query approach
To set the webpack public path in older versions of webpack, add the following to the very top of your webpack entry file:
import "systemjs-webpack-interop/resource-query-public-path?systemjsModuleName=@org-name/project-name";
To set the root directory level:
import "systemjs-webpack-interop/resource-query-public-path?systemjsModuleName=@org-name/project-name&rootDirectoryLevel=2";
Old approach
To set the webpack public path in older versions of webpack, you'll need to do two things:
- Create a file called
set-public-path.js
- Import that file at the very top of your webpack entry file
import "./set-public-path.js";
import { setPublicPath } from "systemjs-webpack-interop";
setPublicPath("foo");
If you need the webpack public path to "chop off" some of the directories in the current module's url, you can specify a "root directory level". Note that the root directory level is read from right-to-left, with 1
indicating "current directory" and 2
indicating "up one directory":
setPublicPath("foo", 2);
API
setPublicPath
Arguments
setPublicPath(systemjsModuleName, rootDirectoryLevel = 1)
- systemjsModuleName (required): The string name of your systemjs module. This name should exist in your import map.
- rootDirectoryLevel (optional). An integer that defaults to 1, indicating which directory to use as the public path. The public path is
calculated using System.resolve,
which returns a full url for the module. The
rootDirectoryLevel
indicates which /
character in the full url string to use as the directory,
scanning the url from right-to-left.
Return value
undefined
Webpack config helpers
systemjs-webpack-interop exports NodeJS functions for helping you set up and verify a webpack config so that it works well with SystemJS.
Note that these functions only work if you're using webpack@>=4.30.0. Before that version of webpack, output.libraryTarget
of "system"
did not exist.
modifyWebpackConfig
const systemjsInterop = require("systemjs-webpack-interop/webpack-config");
module.exports = systemjsInterop.modifyWebpackConfig({
output: {
filename: "bundle.js"
},
module: {
rules: []
},
devtool: "sourcemap"
});
Arguments
modifyWebpackConfig(config)
Return value
A new, modified webpack config object.
checkWebpackConfig
const systemjsInterop = require("systemjs-webpack-interop/webpack-config");
module.exports = {
output: {
libraryTarget: "system"
},
module: {
rules: [{ parser: { system: false } }]
}
};
systemjsInterop.checkWebpackConfig(module.exports);
Arguments
checkWebpackConfig(config)
config
(required): A webpack config object to be verified. If the config object isn't following best practices for interop with systemjs, and error will be thrown.
Return value
undefined
if the webpack config is valid, and an Error will be thrown otherwise.