npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

systemjs-webpack-interop

v2.3.7

Published

helpers for getting webpack bundles to be loaded by systemjs

Downloads

618,687

Readme

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:

  1. Help you set the webpack public path "on the fly" to work with the dynamic url for the bundle in SystemJS' import map.
  2. Help you create or verify a webpack config to ensure proper interop with SystemJS.

Background / other work

Webpack has several features that are geared towards better interop with SystemJS. Here are relevant links:

  • https://github.com/systemjs/systemjs#compatibility-with-webpack
  • https://webpack.js.org/configuration/output/#outputlibrarytarget (search for libraryTarget: 'system' on that page)
  • https://webpack.js.org/configuration/module/#ruleparser (search for SystemJS on that page)

Installation

Note that systemjs-webpack-interop requires systemjs@>=6.

npm install --save systemjs-webpack-interop

# Or
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.

// webpack.config.js
const SystemJSPublicPathWebpackPlugin = require("systemjs-webpack-interop/SystemJSPublicPathWebpackPlugin");

module.exports = {
  plugins: [
    new SystemJSPublicPathWebpackPlugin({
      // optional: defaults to 1
      // 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":
      rootDirectoryLevel: 1,

      // ONLY NEEDED FOR WEBPACK 1-4. Not necessary for webpack@5
      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:

/* For a module at http://localhost:8080/dist/js/main.js,
 * this will set the webpack public path to be
 * http://localhost:8080/dist/js/
 */
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":

/* For a module at http://localhost:8080/dist/js/main.js,
 * this will set the webpack public path to be
 * http://localhost:8080/js/
 */
import "systemjs-webpack-interop/auto-public-path/2";
/* For a module at http://localhost:8080/dist/js/main.js,
 * this will set the webpack public path to be
 * http://localhost:8080/
 */
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:

  1. Create a file called set-public-path.js
  2. Import that file at the very top of your webpack entry file
/* In your webpack entry file, add the following import as the very very first import. It is important that it is first.
 * Here's a link to learn about entry files: https://webpack.js.org/configuration/entry-context/#entry
 */
import "./set-public-path.js";
/* set-public-path.js */
import { setPublicPath } from "systemjs-webpack-interop";

/* Make sure your import map has the name of your module in it. Example:
{
  "imports": {
    "@org-name/my-module": "https://example.com/dist/js/main.js"
  }
}
 */

// __webpack_public_path__ will be set to https://example.com/dist/js/
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":

/* For a module at http://localhost:8080/dist/js/main.js,
 * this will set the webpack public path to be
 * http://localhost:8080/dist/
 */
setPublicPath("foo", 2);

API

setPublicPath

Arguments

setPublicPath(systemjsModuleName, rootDirectoryLevel = 1)

  1. systemjsModuleName (required): The string name of your systemjs module. This name should exist in your import map.
  2. 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

// webpack.config.js
const systemjsInterop = require("systemjs-webpack-interop/webpack-config");

// Pass in your webpack config, and systemjs-webpack-interop will make it
// work better with SystemJS
module.exports = systemjsInterop.modifyWebpackConfig({
  output: {
    filename: "bundle.js"
  },
  module: {
    rules: []
  },
  devtool: "sourcemap"
});

Arguments

modifyWebpackConfig(config)

  1. config (optional): A webpack config object. If not provided, a default one will be created for you.

Return value

A new, modified webpack config object.

checkWebpackConfig

// webpack.config.js
const systemjsInterop = require("systemjs-webpack-interop/webpack-config");

// Pass in your webpack config, and systemjs-webpack-interop will make it
// work better with SystemJS
module.exports = {
  output: {
    libraryTarget: "system"
  },
  module: {
    rules: [{ parser: { system: false } }]
  }
};

// Throws errors if your webpack config won't interop well with SystemJS
systemjsInterop.checkWebpackConfig(module.exports);

Arguments

checkWebpackConfig(config)

  1. 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.