Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webpack-version-plugin

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-version-plugin - npm Package Compare versions

Comparing version 1.0.7 to 1.0.8

2

package.json
{
"name": "webpack-version-plugin",
"version": "1.0.7",
"version": "1.0.8",
"description": "webpack-version-plugin",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -45,3 +45,4 @@ # webpack-version-plugin

files: {
app: ['js/app_fa74f310.js']
app: ['js/app_fa74f310.js', 'css/app_fa74f310.css'],
vendor: ['js/vendor_ff7f0450.js']
}

@@ -70,4 +71,66 @@ }*/

```
or get the hash from the packaged files:
```javascript
// webpack.config.js
module.exports = {
entry: {
app: 'src/app',
vendor: ['react', 'react-dom']
},
output: {
path: 'dist/',
filename: 'js/[name]_script_[hash:8].js',
publicPath: 'cdn_path'
},
plugins: [
new ExtractTextPlugin({
filename: 'css/[name]_style_[contenthash:8].css',
disable: false,
allChunks: true
}),
new WebpackVersionPlugin({
// You must set the cb option
cb: function(hashMap) {
console.log(hashMap);
/* do something, the hashMap like this:
{
...
files: {
app: ['js/app_fa74f310.js', 'css/app_fa74f310.css'],
vendor: ['js/vendor_ff7f0450.js']
}
}*/
const versionConfig = {
app: {},
vendor: {}
};
for (const entry in hashMap.files) {
const oneEntryFiles = hashMap.files[entry];
oneEntryFiles.forEach((item) => {
const type = item.split('_')[1];
const hash = item.replace(/.+_(\S+?)\..+/g, '$1');
versionConfig[entry][type] = hash;
});
}
}
}),
...
]
}
//version result
versionConfig: {
app: {
script: '0c9de9fe',
style: 'f8ed31b1'
},
vendor: {
script: '0c9de9fe'
}
}
```
## 起源
最近在给项目的生成文件需要添加形如 `[hash:8]` 版本号并把该版本号同步记录到某配置文件内,想到每次更改生成新的打包文件,都要去版本配置的文件里更改版本号,程序员最不喜欢做这种重复性的劳动了,就想着有没有 `webpack` 插件能做类似的功能,能把我的版本号写到配置文件里去,找了一圈只找到 [webpack-version-hash-plugin](https://www.npmjs.com/package/webpack-version-hash-plugin) ,而且只能输出在固定的位置和固定的格式 (╯‵□′)╯︵┻━┻ ,无奈,我当时直接用了时间戳作为构建的版本号,后来就做了现在的这个插件,我可以在插件的 `cb` 函数中把获取到的版本号写到我单独的 `json` 文件中,当然,这里获取到的是完整的`hash` 值,可以按需截取。

@@ -20,3 +20,3 @@ function WebpackVersionPlugin(opts) {

compilation.chunks.forEach(function (item) {
compilation.chunks.forEach(function (item, index) {
hashMap.chunkHash[item.name] = item.hash;

@@ -23,0 +23,0 @@ hashMap.files[item.name] = compilation.chunks[index].files;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc