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

chunk-filename-replace-webpack-plugin

v1.0.7

Published

ChunkFilename for Webpack4, and dynamically imported chunk names (paths)

Downloads

2

Readme

chunk-filename-replace-webpack-plugin

针对webpack4修改js和css的ChunkFilename名称,也可以修改动态导入的chunk名称(路径),自定义你想修改的名字并输出到dist目录中。

⚠️ 这个插件是基于webpack4的 MainTemplate/ChunkTemplate实现,由于webpack5打包模板已经重构。MainTemplate/ChunkTemplate/ModuleTemplate 被废弃,所以本插件并不支持webpack5,而webpack5本身也不支持chunkFilename的修改,chunkFilename的修改都由插件实现(mini-css-extract-plugin已经支持webpack5中定制修改chunkFilename)

优势

  • 即使是webpack4,你也可以自定义的修改任意的chunkFilename(MiniCssExtractPlugin: 你们看我干啥?).
  • 对目标文件的修改,你可以用字符串来精准匹配,也可以用正则表达式来模糊匹配你想要修改的文件
  • 这个插件也可以对动态引入的chunk名称进行修改(修改runtime文件中异步引入src的路径)
  • 你仍然可以使用chunk文件的命名模版,例如:[contenthash], [chunkhash] 和 [name]等

开始使用

你需要安装 chunkFilename-replace-webpack-plugin并执行如下命令:

npm install chunk-filename-replace-webpack-plugin --save-dev

使用实例

webpack.config.js

const ChunkFilenameReplaceWebpackPlugin = require('chunk-filename-replace-webpack-plugin');
module.exports = {
	// ...
	plugins: [
        new ChunkFilenameReplaceWebpackPlugin([{
            from: {
                css: 'marvo'
            }, // Match chunkName to marvo CSS file
            to: {
                css: 'css/[name].ironman.[contenthash:4].css'
            }, // modified into css/[name].ironman.[contenthash:4].css
        },
        {
            from: {
                css: 'spiderman',
                js: 'spiderman'
            }
            to: {
            	css: 'css/marvo.chunk.spiderman.css',
            	js: 'js/marvo.chunk.[chunkhash:6].spiderman.js'
        	}
        }])
	]
}
⚠️ ‘from’字段里填写的css或js必须是chunkId!(一般是代码分割时产生的[name])

插件配置项

选项可以是对象也可以是对象数组: Object, Array<Object>

如果选项是对象数组,那么可以修改多个chunk名称

| 名称 | 类型 | 默认 | 描述 | | ---- | -------- | -------------------------------------- | ------------------------------------------------------------ | | from | {Object} | {css: String|RegExp,js: string} | 需要修改的目标chunkFilename,可以是字符串,也可以是正则表达式。字符串是精准匹配,正则表达式是模糊匹配. 匹配的目标是由Webpack分割代码生成的chunk文件,因此其实from选项匹配Webpack中的chunkId | | to | {Object} | {css: string,js: string} | 你想要修改的文件名称(路径) |

使用场景

有时你的工程里使用了代码分割,你可能会抽出一些库文件或公共文件,比如从业务中抽离elementUI组件库的js文件和css文件,一般在后端/前端的模版里引入elementUI的js和css,所以你可能需要将elementUI的名称定制成'ElementUI.js' 和 'ElementUI.css ',当然,你可以在splitChunks.cacheGroups里修改分包的代码名称,但是这只能对js的chunk文件名称进行定制化的修改,css的chunk文件名称需要用到mini-extract-css-plugin来修改(例如:chunkFilename: css/[name].css),但是一般在业务中我们经常要更改业务代码的css,这样就需要在css的chunk中添加contenthash等字符,这样就不能实现ElementUI.css了,这是你不希望的。在webpack4中mini-extract-css-plugin不能同时满足两种chunkFilename,有了css/[name].css就不能用css/[name].[contenthash].css,所以你可以用本插件来实现两种甚至多种chunkFilename命名。