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-ruleset

v0.1.8

Published

A class for working with webpack config rules.

Downloads

46

Readme

WebpackRuleset

WebpackRuleset is a slimmed down version of webpack/lib/RuleSet for matching, modifying, and inserting rules in webpack config. Webpack's included RuleSet class normalizes it's rule set, which means the list of loaders returned is not the original rules from config. This class simulates what Webpack's RuleSet does, but keeps a reference to the original rules from config. This allows WebpackRuleset to be used to mutate a Webpack config in place.

Installation

npm install webpack-ruleset

A Webpack config rule can include a child lists of rules as either rules or oneOf attributes. WebpackRuleset will iterate over child rules simmilar to how Webpack iterates over rules when processing an imported resource.

Usage

WebpackRuleset.forEach(action)

Iterates over all rules in the rule set. For oneOf child rules, iteration continues until action returns a truthy value.

Update a rule based on a filename that the rule would match.

const WebpackRuleset = require('webpack-ruleset');

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

ruleSet.forEach(rule => {
    if (rule.loader.includes('babel-loader')) {
        rule.options.babelrc = true;
        return true;
    }
    return false;
});

WebpackRuleset.forAll(action)

Iterates over all rules in the rule set. For oneOf child rules, iteration continues regardless of what value action returns.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);
const HASHED_NAME_RE = /\.?\-?\[(chunk|content)?hash(:\d*)?\]/;

ruleSet.forAll(rule => {
    if (rule.options && rule.options.name && HASHED_NAME_RE.test(rule.options.name)) {
        rule.options.name = formatPath(rule.options.name);
    }
});

Matcher methods

WebpackRuleset also includes a number of matcher methods. Matcher methods take a matcher as their first argument.

The matcher argument can be any of the following:

  • A function, which will be passed both the rule and Webpack "normalized" rule and should return true for a matching rule and false for a non-matching rule.
function matcher(rule, normalizedRule) {
    return normalizedRule.resource && normalizedRule.resource('any.css');
}
  • A resource path string to test rules against. examples: "/full/path/to/something.js" "src/styles.css" - resolved relative to process.cwd() ".css" - treated as "fake_file.css"
const matcher = '.css';
  • A string suffexed with "-loader". If the matcher is a string ending in "-loader", it will be matched against
const matcher = 'babel-loader';
  • An object with action, loader, or resource and optional loaderType ('pre' or 'post') attributes. Specifying a matcher as an object allows you to specify wether you want to match pre or post loaders.
const action = '.js' // Matches loaders that would match files with the .js extension
const action = {     // Matches pre-loaders that would match files with the .js extension
    loaderType: 'pre',
    resource: '.js',
}

WebpackRuleset.forMatching(matcher, action)

Locate and execute the action on the first matching rule.

Iterates over all rules in the rule set. For oneOf child rules, iteration continues until the matcher matches a rule.

Update a rule based on a filename that the rule would match.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

ruleSet.forMatching('.js', jsRule => {
    jsRule.options.babelrc = true;
});

Update a rule based on the loader name.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

ruleSet.forMatching('css-loader', cssRule => {
    cssRule.options.modules = true;
});

WebpackRuleset.forAllMatching(matcher, action)

Locate and execute the action on all rules that match.

Iterates over all rules in the rule set. For oneOf child rules, iteration continues, even after the matcher matches a rule.

When a oneOf list of rules is encountered, forMatching will call the given action on all matching rules in the oneOf list.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);
const rulesWithHashedNames = r => r.options && r.options.name && isHashedName(r.options.name);

ruleSet.forAllMatching(rulesWithHashedNames, rule => {
    rule.options.name = formatPath(rule.options.name);
});

WebpackRuleset.filter(matcher)

Gathers matching rules into a flat array.

Iterates over all rules in the rule set. For oneOf child rules, iteration continues until the matcher matches a rule.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

const cssLoaders = ruleSet.filter('.css');

WebpackRuleset.filterAll(matcher)

Gathers matching rules into a flat array.

Iterates over all rules in the rule set. For oneOf child rules, iteration continues, even after the matcher matches a rule.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

const cssLoaders = ruleSet.filterAll('.css');

WebpackRuleset.getOneRule(matcher)

Locate a matching rule. Asserts that only one rule matches.

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

const jsRule = ruleSet.getOneRule('.js');
jsRule.options.babelrc = true;

WebpackRuleset.insertRuleBeforeMatch(matcher, insert)

Inserts a new rule before the matching rule. Asserts that only one matching rule is found.

Accepts either a rule object or a function as the second argument. If the insert argument is a function it is called with the matching rule and the parent of the matching rule as parents, otherwise it is inserted as the new rule.

function addCssModuleOption(loader) {
    if (loader.loader && /css\-loader/.test(loader.loader)) {
        return {...loader, options: {...loader.options, modules: true}};
    }
    return loader;
}

const ruleSet = new WebpackRuleset(webpackConfig.module.rules);

// Given a rule set containing a standard css loader, add a css modules rule, with
// css files suffixed with ".global.css" falling through to the existing, non-modules
// css loader.
ruleSet.insertRuleBeforeMatch('.css', globalCssRule => {
    return Object.assign({},
        globalCssRule,
        {
            loader: globalCssRule.loader && globalCssRule.loader.map(addCssModuleOption),
            use: globalCssRule.use && globalCssRule.use.map(addCssModuleOption),
            include: paths.appSrc,
            exclude: /\.global\.css$/,
        }
    );
});

WebpackRuleset.insertRuleAfterMatch(matcher, insert)

Same as insertRuleBeforeMatch, except that the new rule is inserted after the matching rule.