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-chunk-renamer-plugin

v1.0.1

Published

Adds the template option [lc-name] and allows for chunk renaming

Downloads

1,254

Readme

webpack-chunk-renamer-plugin

Adds the template option [lc-name] that will substitute a lower-case name. Also adds automatic renaming of chunks.

Version 1.0.0 requires Webpack 5.

I prefer to have my exported JavaScript objects start with an upper-case letter, but I like the actual JavaScript files to be all lower case names. For example, suppose you have these entry points:

    entry: {
        Console: './src/Console/Console.js',
        Site: './src/Site/Site.js'
    },

Then configure the output this way to create a library with the name exposed as a global object.

    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
        library: '[name]',
        libraryTarget: 'umd',
        libraryExport: "default",
        publicPath: ''
    },

The library will export Console and Site, but will also name the .js files Console.js and Site.js. I prefer that the files be console.js and site.js, which is consistent with many libraries. Setting the entry points as console and site will do that, but then the exported global variables will be console and site as well.

This plugin adds a new template: [lc-name] that will convert the name to lower case. It can be used in place of [name] to specify the file name:

    output: {
        filename: '[lc-name].js',
        path: path.resolve(__dirname, 'dist'),
        library: '[name]',
        libraryTarget: 'umd',
        libraryExport: "default",
        publicPath: ''
    },

Now the exported global variables will be Console and Site and the files console.js and site.js.

Chunk Renaming

The other renaming functionality this plugin supports is renaming entry chunks. The main reason for these features is to overcome the overly aggressive renaming of chunks that Webpack does by default.

If the option initialChunksWithEntry is set true, the initial chucks will use output.filename as their name:

    plugins: [
        new WebpackChunkRenamerPlugin({
	        initialChunksWithEntry: true
        })
    ],

It can also be used to specify a specific name for a chunk:

    plugins: [
        new WebpackChunkRenamerPlugin({
            'vendor': 'vendor.js',
            'commons': 'commons.js'
        })
    ],

This is useful to ensure the vendor chunk has the name vendor.js rather than having a hash value in it.

Install

npm: npm install webpack-chunk-renamer-plugin --save-dev

Utilizing

Require the plugin in webpack.config.js:

const WebpackChunkRenamerPlugin = require('webpack-chunk-renamer-plugin');

And add to the list of plugins with an options:

    plugins: [
        new WebpackChunkRenamerPlugin({
            'vendor': 'vendor.js',
            'commons': 'commons.js',
	        initialChunksWithEntry: true
        })
    ],

An Issue with splitChunks

If utilized with splitChunks, the generated filename may not be as expected. For example, suppose we apply the above example and this splitChunks configuration to create a vendor chunk:

    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendor',
                    chunks: 'all'
                }
            }
        }
    },

The generated output file will be vendor.vendor.js. The reason is that splitChunks will look at the generated filename and ensure it will be unique if more than one chunk is generated. The code tests to ensure either [id] or [name] are present. If neither is present, it prefixes [id]. to the filename, creating [id].[lc-name].js, which creates the file vendor.vendor.js. The fix for this problem is to specify the name for chunked files separately using [name] instead of [lc-name]:

    output: {
        filename: '[lc-name].js',
        chunkFilename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
        library: '[name]',
        libraryTarget: 'umd',
        libraryExport: "default",
        publicPath: ''
    },

License

webpack-chunk-renamer-plugin is released under the MIT license.


Made by Charles B. Owen