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

webpack-import-plugins

v1.0.0

Published

Webpack plugin loader compatible with version 4 and version 5.

Downloads

11

Readme

webpack-import-plugins

Build Status npm-publish Maintainability Test Coverage

Ensures load plugin from local webpack compatible with versions 4 and 5.

Motivation

Many plugins are composed with canonical plugins from the webpack module itself, the problem comes when require("webpack/lib/<NameOfPlugin>") is used inside a custom plugin which is expected to use the webpack version 4 and nodejs resolution algorithm tries to find the module in a hoisted file structure. Due the webpack-cli is execute at the root context, the module provided could be wrong if is expected to load the local webpack module:

Webpack cli uses import-local which ensures always use a the local instance of webpack, but in casuistics where the modules are hoisted, (such yarn workspaces) is where this kind of problems comes, https://github.com/sindresorhus/import-local/issues/7.

[email protected]
├─┬ [email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ └── [email protected]
└── [email protected]

If there is a case where no-hoisted module has a webpack compiler, it will resolve webpack from the dep-tree directory.

Webpack 5 solve this using a instance of itself at the compiler instance, but many libraries are stil using webpack 4. ie: @storybook/react.

To solve this and keep the retrocompatibility, this module comes to solve this problem.

  • Check this issue to dive in deep: https://github.com/jantimon/html-webpack-plugin/issues/1546

How to use it

Just import the library using require('webpack-import-plugins'), and call it with the name of plugin expected in webpack 5, this will check if there is a plugin factory available, or delegates to the custom module resolution.

The plugin name must be defined with the category prefix, if is required, you can check the categories in the webpack@5 internal plugins list or for lazyness use the local auto generated catalog file v4, v5.

Using importWebpackPlugins in webpack.config.js


const importWebpackPlugins = require('webpack-import-plugins');
const { node: { NodeTemplatePlugin } } = importWebpackPlugins();
// Or
const NodeTemplatePlugin = importWebpackPlugins().node.NodeTemplatePlugin;

module.exports = {
  plugins: [new NodeTemplatePlugin(...)];
}

Using importWebpackPlugins in a CustomPlugin

const importWebpackPlugins = require('webpack-import-plugins');

class CustomPlugin {
  constructor () {
    
  }

  apply(compiler) {
    const wpPlugins = importWebpackPlugins(compiler.webpack);
    const NodeTemplatePlugin = wpPlugins.node.NodeTemplatePlugin;
    const {
        LoaderTargetPlugin, 
        EntryPlugin, 
     } = wpPlugins;
    // and use it!
    new NodeTemplatePlugin(...);
  } 
}

Typing

Typing is allowed and extracted from the original webpack plugins.

Bonus track

Gets the version of the local webpack

const { resolveWepack } = require('webpack-import-plugins');
const webpackVersion = () => {
  const { version } = require(resolveWebpack('package.json'));
  return version;
};

References

  • https://github.com/jantimon/html-webpack-plugin/issues/1546
  • https://github.com/jantimon/html-webpack-plugin/pull/1547