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

angular-webpack-transformer

v1.0.2

Published

This plugin allows you to asynchronously change the configuration of the angular webpack

Downloads

19

Readme

angular-webpack-transformer

The angular-webpack-transformer package is a plugin that allows for asynchronous changes to the webpack configuration of an Angular project. It is dependent on the ngx-build-plus package and its documentation should be consulted for further information.

This plugin offers the following features:

  • Asynchronous modification of the webpack config file
  • A simple API for quickly editing webpack loaders

Get started

To get started, ngx-build-plus must be installed and configured, followed by the installation and configuration of angular-webpack-transformer. After that, create a file named webpack.transformer.js in the root of your project.

The webpack.transformer.js file should export a function that utilizes the use function provided by angular-webpack-transformer to make changes to the webpack config.

The example file provided below adds a postcss-loader and configures the sass-loader by adding the additionalData option and also add two new preloaders svg-sprite-loader and svgo-loader for generating svg sprites

const { types, rules } = require('angular-webpack-transformer');
const path = require('path');


module.exports = function (config, use) {
    // Sync
    rules.modifyOrAddLoader([types.RuleTestTypes.Sass, types.RuleTestTypes.Scss, types.RuleTestTypes.Css], 'postcss-loader', (loader, rule) => {
        if (loader) {
            return "postcss-loader";
        } else {
            rule.unshift({
                loader: require.resolve('postcss-loader')
            });
        }
    }, config);
 
    // Async
    use(async config => {
        return rules.modifyOrAddLoader([types.RuleTestTypes.Sass, types.RuleTestTypes.Scss], 'sass-loader', (sassLoader, rule) => {
            if (sassLoader) {
                sassLoader.options.additionalData = (content, loader) => {
                    // 
                }
                return sassLoader;
            }
        }, config);
    });
    
    use(async config => {
        return rules.modifyOrAddLoader([types.RuleTestTypes.Svg], 'svg-sprite-loader', (loader, rule) => {
            if (!loader) {
                rule.push({
                    loader: 'svg-sprite-loader',
                    options: {
                        symbolId: filePath => "app-" + path.basename(filePath, path.extname(filePath))
                    }
                });
                rule.push('svgo-loader');
            }
        }, config);
    });
}

and finally use --plugin option ng serve --plugin angular-webpack-transformer or ng build --plugin angular-webpack-transformer, example of packages.json:

"scripts": {
    "start": "ng serve --plugin angular-webpack-transformer --extra-webpack-config webpack.partial.js -o"
}

This package can be useful for developers who need to make custom changes to their webpack config and want an easy way to do so, without having to manually modify the config file. Additionally, the ability to asynchronously make changes to the config allows for a more efficient development process.

Please note that simply adding the postcss and postcss-loader, svg-sprite-loader, svgo-loader to your configuration file is not enough. You must also ensure that they are installed in your project by running the command "npm i postcss postcss-loader". Once that is done, you can use the file postcss.config.js to customize your postcss plugins. For example, you can use this file to add plugins such as autoprefixer or cssnano to your project

// postcss.config.js
const autoprefixer = require('autoprefixer');
const postcssRtlLogicalProperties = require('postcss-rtl-logical-properties');
const postcssRTL = require('postcss-rtl');


module.exports = () => {
    return {
        plugins: [
            postcssRtlLogicalProperties(),
            autoprefixer(),
            postcssRTL({
                blacklist: postcssRtlLogicalProperties.ignoreDeclarationList,
                addPrefixToSelector: (selector, prefix) => {
                    return `${prefix} ${selector}`;
                }
            })
        ]
    }
}

and for the svgo plugin, you can create the svgo.config.js file in the root of the project:

module.exports = {
   datauri: 'enc', // 'base64' (default), 'enc' or 'unenc'.
   js2svg: {
       indent: 1, // string with spaces or number of spaces. 4 by default
       pretty: false, // boolean, false by default
   },
   plugins: [
       // set of built-in plugins enabled by default
       'preset-default',

       // or by expanded notation which allows to configure plugin
       "removeTitle",
       "removeDesc",
       "removeEmptyAttrs",
       "removeEmptyText",
       "removeEmptyContainers",
       "removeStyleElement",
       "removeRasterImages",
       "removeScriptElement", 
       {
           "name": "removeAttrs",
           "params": {
               attrs: '(fill|stroke|class|style|filter)' 
           }
       }
   ]
}

API

The function from webpack.transformer.js accepts two parameters:


module.exports = async function (config, use) {
    return config;
}

The first parameter is the webpack config that can be modified, and the second parameter, "use," is a method that passes the config to a callback for processing. This callback is also asynchronous.

The following example shows how to use the use method:

const { types, rules } = require('angular-webpack-transformer');

module.exports = async function (config, use) {
    use(async config => {
        return rules.modifyOrAddLoader([types.RuleTestType.Sass, types.RuleTestType.Scss, types.RuleTestType.Css], 'postcss-loader', (loader, rule) => {
            if (loader) {
                return "postcss-loader";
            } else {
                rule.unshift({
                    loader: require.resolve('postcss-loader')
                });
            }
        }, config);
    });
}

rules.modifyOrAddLoader

The method Rules.modifyOrAddLoader searches for webpack loaders and provides a callback for their modification. The first callback is the loader, and the second is an array of rules. If the first element is found, return it back, otherwise, you can manually modify the array of rules.

rules.find

The method Rules.find searches for rules in the config using a regexp.

types

The types can be found in the file src/utils/types.ts