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

@mxmarquitetura/mxm-webpack5-import-map-plugin

v1.0.0

Published

A webpack plugin to help generate import maps for outputted entry files, based on webpack-manifest-plugin

Downloads

6

Readme

Contributors Contributions Welcome Forks Stargazers Issues HitCount Known Vulnerabilities Codecov CircleCI

https://nodei.co/npm/webpack-import-map-plugin.png?downloads=true&downloadRank=true&stars=true

Table of Contents

About The Project

This plugin allows you to use filename hashing, etc. and automatically generate an import-map to use standalone, or as a patch file for something like import-map-deployer.

Two main use cases are generating an import-map that allows for using hashes in filenames, to make cache-busting easy, or to generate a delta/patch file to use in a deployment pipeline.

Built With

  • JavaScript
  • Lots of coffee

Getting Started

To get a local copy up and running follow these simple steps.

Prerequisites

  • Node >= 10.x
  • Webpack >= 2.x
  • Some knowledge of import-maps

Installation

  1. Add the devDependency to your project
npm i -D webpack-import-map-plugin
  1. Add the plugin in your webpack config's plugins and configure it
// webpack.config.js
const ImportMapWebpackPlugin = require('webpack-import-map-plugin');

Demo

// with a Webpack 4 config like:
config.entry = { entryName: 'entry-file.js' };
config.output.filename = '[name].[contenthash:8].js';

// Add to plugins
new ImportMapWebpackPlugin({
    filter: x => {
        return ['entryName.js'].includes(x.name);
    },
    transformKeys: filename => {
        if (filename === 'entryName.js') {
            return '@my-super-scope/out-file';
        }
    },
    fileName: 'import-map.json',
    baseUrl: 'https://super-cdn.com/'
});
// output import-map.json
{
    "imports": {
        "@my-super-scope": "https://super-cdn.com/entryName.12345678.js"
    }
}

Usage

Configuration Options

The configuration object is very similar to webpack-manifest-plugin but not exactly the same. Some sensible defaults are set for most options, though YMMMV.

include

A filter, or array of filters to run on the entry filenames that should be included in the resulting import-map. Strings will match exactly, run in order provided. A falsy value will include all files. Note: Run before exclude.

  • type: RegExp | string
  • default: ''

exclude

A filter, or array of filters to run on the entry filenames that should be excluded in the resulting import-map. Strings will match exactly, run in order provided. A falsy value will not exclude any files. Note: Run after include.

  • type: RegExp | string
  • default: ''

filter

A more complex filter function that receives the whole file object from webpack and returns a falsy value to exclude the file from the resulting import map. Note: Run after include and exclude.

  • type: Function(FileDescriptor): boolean
  • default: null

transformKeys

A function run on the filename after the filters that allow you to generate a "key" for your import-map. You can strip key extensions, or rewrite with this function.You probably want to implement this option.

  • type: Function(string): string
  • default: null

transformValues

A function run on the emitted asset file path (i.e. /dist/main.hash.js). You could use this function to rewrite prefixed paths, etc.

  • type: Function(string): string
  • default: null

baseUrl

A string url to to prefix the values with. A good usage would be prepending a cdn address or placeholder to rewrite in a pipeline process. Note: Run after transformValues.

  • type: string
  • default: null

fileName

The output filename, emitted to the output directory.

  • type: string
  • default: 'import-map.json'

writeToFileEmit

If set to true will emit to build folder and memory in combination with webpack-dev-server.

  • type: boolean
  • default: false

generate

The function that generates the resulting import-map. This is where you could implement scopes, etc.

  • type: Function(Object, FileDescriptor, string[]): Object
  • default:
(seed, files, entrypoints) => files.reduce(function (manifest, file) {
    manifest[file.name] = file.path;
    return manifest;
}, seed);

seed

A cache of key/value pairs to used to seed the import-map. A good use for this would be including libraries/runtimes, or a base import-map for webpack-dev-server debugging.

  • type: Object
  • default: {}

map

Modify files details before the manifest is created.

  • type: Function(FileDescriptor): FileDescriptor
  • default: null

sort

Sort files before they are passed to generate.

  • type: Function(FileDescriptor): number
  • default: null

serialize

The serializing function for the result import-map. Unless you are doing something unique, it's best to leave this as is.

  • type: Function(Object): string
  • default:
function (manifest) {
    return JSON.stringify(manifest, null, 4);
}

Type: FileDescriptor

FileDescriptor

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please make sure any contributions have proper unit tests and update any relevant documentation.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Zachary Leighton - [email protected]

Project Link: https://github.com/zleight1/webpack-import-map-plugin

Acknowledgements