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

@joshwilsonvu/webpack-plugin-esm-bridge

v0.1.1

Published

Readable, granular, and performant JS loading for HTML.

Downloads

6

Readme

webpack-plugin-esm-bridge

Readable, granular, and performant JS loading for HTML.

NPM version

What is this?

This plugin "bridges" plain HTML or compile-to-HTML templates (even non-JS, like Rails) with your bundler, so that your markup can use native ESM imports to load your bundled JS instead of dealing with manifests, helper functions, reverse proxies, etc.

By loading only the JS you really need for each page, you'll avoid ending up with a single multi-MB JS bundle. And when loading JS is as simple as renaming a file and adding an import, you can use TypeScript, npm dependencies, etc. as easily as an inline script.

// my-module.entry.ts - bundled
import { whatever } from "a-dependency";

export function doSomething(value: string) { /* ... */ }
<script type="module">
  // runs in the browser - not bundled
  import { doSomething } from "my-module.entry.ts";

  doSomething({{ templatedValue }});
</script>

or,

<script type="module" src="my-module.entry.ts"></script>

How does it work?

It adds entry points based on a glob pattern like *.entry.*, and generates an import map that maps the source file to the generated JS bundle. Creating entry points as easily as renaming files allows you to break up your JS into smaller bundles and only load what's needed for a particular page or partial. The import map keeps your templates clean and maintainable.

[!WARNING] This plugin enables Webpack/Rspack's experimental support for generating ESM if not already enabled. Track Webpack's progress here.

When should I use this?

When you have a setup that serves plain HTML or HTML templates, and:

  • you have a substantial amount of JS, or
  • you want to use JS tooling—transpilation, minification, TypeScript, or asset modules

If you're using a JS framework, you don't need this.

importmap-rails also uses import maps to serve JS, but it doesn't perform any bundling. This is fine for small projects, but even with module preloading and HTTP/2, there is still some overhead. Minification and tree-shaking are also necessary for larger production applications. This plugin aims to make it as easy as possible to get the benefits of bundling and modern JS tools without complicating development.

Because each entry point is bundled, preloading modules is usually not necessary—bundles are loaded as the HTML is parsed, and bundles typically don't need to load more JS that would cause a waterfall.

If you want to prefetch an entry point anyway (i.e. for navigation that doesn't involve a full page load), you can add a modulepreload link as appropriate, or use a dynamic import. These work with import maps.

<link rel="modulepreload" href="some-module.entry.js">
import('some-module.entry.js')

Installation

You'll need to configure a Webpack or Rspack setup first. You don't need to connect the bundler output to your backend.

npm i @joshwilsonvu/webpack-plugin-esm-bridge
// webpack.config.js
const EsmBridge = require('webpack-plugin-esm-bridge');

module.exports = {
  entry: {}, // to disable default entry
  // ...
  plugins: [
    EsmBridge({
      patterns: '*.entry.*',
      // other options
    }),
  ],
}
// webpack.config.js ("type": "module" in package.json)
import EsmBridge from 'webpack-plugin-esm-bridge/rspack';

export default {
  entry: {}, // to disable default entry
  // ...
  plugins: [
    EsmBridge({
      patterns: '*.entry.*',
      // other options
    }),
  ],
}

Then, you'll need to ensure that the generated import map is included at the top of your HTML (before any ESM) once per page load. By default, it's "importmap.json" in the bundler's output directory, but this can be changed with the importMap.fileName option.

Finally, you'll need to configure the bundler's output.publicPath to something other than "auto" for import map generation to work.

Configuration

| Option | Type | Default | Description | | --- | --- | --- | --- | | patterns | string \| Array<string> | | Required. The glob pattern(s) to use to find entry points (within the configured context), ex. *.entry.*. A bundle will be created for each entry point, and these files can be referenced from native imports. | | importMap.include | 'globbed' \| 'all' | 'globbed' | 'globbed' only includes files matching 'patterns' in the import map; 'all' includes manually configured entry files as well. | | importMap.fileName | string | 'importmap.json' | The name of the generated import map that must be included at the top of your HTML. | | importMap.prefix | string | '' | A prefix to be prepended to each mapped module specifier. Ex. '~' produces imports like "~/my-module.entry.ts" | | importMap.trimExtension | boolean | false | Whether to remove the extension from each mapped module specifier. Ex. "my-module.entry.ts""my-module.entry" | | importMap.integrity | boolean | false | Whether to add subresource integrity to the import map. May increase compilation time and import map size. | | importMap.onCreate | (importMap: ImportMap) => void \| Promise<void> | | A function to arbitrarily modify the generated import map. Can be used to merge in other entries not controlled by this plugin. | | importMap.disabled | boolean | false | Disables generation of the import map altogether. The plugin will only add entry points matching 'patterns'. | | globbyOptions | object | | Additional options to pass to globby. Can be used to override the cwd to search in, among other things. | | noHtmlWebpackPlugin | boolean | false | Disables the built-in integration with html-webpack-plugin. By default, the import map will be automatically inlined into the HTML template for you. |