webpack-version-plugin
Advanced tools
Comparing version 1.0.10 to 2.0.0
{ | ||
"name": "webpack-version-plugin", | ||
"version": "1.0.10", | ||
"version": "2.0.0", | ||
"description": "webpack-version-plugin", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
101
README.md
@@ -7,8 +7,9 @@ # webpack-version-plugin | ||
```bash | ||
$ npm install webpack-version-plugin@1.0.10 --save-dev | ||
$ npm install webpack-version-plugin --save-dev | ||
``` | ||
yarn | ||
```bash | ||
$ yarn add webpack-version-plugin@1.0.10 --dev | ||
$ yarn add webpack-version-plugin --dev | ||
``` | ||
## Usage | ||
@@ -24,12 +25,16 @@ ```javascript | ||
module.exports = { | ||
mode: 'production', | ||
entry: { | ||
app: 'src/app', | ||
vendor: ['react', 'react-dom'] | ||
app: path.join(__dirname, '../src/app'), | ||
vendor: ['react'] | ||
}, | ||
output: { | ||
path: 'dist/', | ||
filename: 'js/[name]_[hash:8].js', | ||
path: path.join(__dirname, '../dist/'), | ||
filename: 'js/[name]_[chunkhash:8].js', | ||
publicPath: 'cdn_path' | ||
}, | ||
plugins: [ | ||
new MiniCssExtractPlugin({ | ||
filename: 'css/[name]_[chunkhash:8].css' | ||
}), | ||
new WebpackVersionPlugin({ | ||
@@ -41,13 +46,13 @@ // You must set the cb option | ||
{ | ||
hash: 'fa74f31052feddb3032256f018063b88', | ||
chunkHash: { | ||
app: '4089fbc1699ec5b6009b0f9bfcdc8327', | ||
vendor: 'ff7f0450afc7ff3030cba2428e593dcf' | ||
hash: '6f0486fb6449204e4b3db696f95df4bc', | ||
app: { | ||
chunkHash: 'e03808e758b346644766a53e7996ef40', | ||
files: [ 'css/app_e03808e7.css', 'js/app_e03808e7.js' ] | ||
}, | ||
files: { | ||
app: ['js/app_fa74f310.js', 'css/app_fa74f310.css'], | ||
vendor: ['js/vendor_ff7f0450.js'] | ||
vendor: { | ||
chunkHash: 'fdf3345a25608a6df7614afaf3896002', | ||
vendor: [ 'js/vendor_fdf3345a.js' ] | ||
} | ||
}*/ | ||
versionConfig.vendorJsVersion = hashMap.hash; | ||
versionConfig.appVersion = hashMap.app.chunkHash; | ||
fs.writeFileSync(path.join(__dirname, './version.json'), JSON.stringify(versionConfig, null, 2)); | ||
@@ -60,3 +65,2 @@ } | ||
// version.json | ||
@@ -70,70 +74,13 @@ // before | ||
{ | ||
"vendorJsVersion": "fa74f31052feddb3032256f018063b88", | ||
"appVersion": "0cb630602d69887ef37c143c14bbeab7" | ||
"vendorJsVersion": "767e2c64d1208e06c8810bea26c29ab6", | ||
"appVersion": "e03808e758b346644766a53e7996ef40" | ||
} | ||
``` | ||
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; | ||
}); | ||
} | ||
} | ||
}), | ||
... | ||
] | ||
} | ||
## Changelog | ||
- 2018-04-16 | ||
Use webpack@v4 event hooks. | ||
//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` 值,可以按需截取。 |
@@ -10,18 +10,17 @@ function WebpackVersionPlugin(opts) { | ||
const self = this; | ||
compiler.plugin('emit', function(compilation, callback) { | ||
compiler.hooks.emit.tap({ name: 'WebpackVersionPlugin' }, function(compilation) { | ||
const hashMap = { | ||
hash: compilation.fullHash, | ||
chunkHash: {}, | ||
files: {} | ||
hash: compilation.fullHash | ||
}; | ||
compilation.chunks.forEach(function (item, index) { | ||
hashMap.chunkHash[item.name] = item.hash; | ||
hashMap.files[item.name] = compilation.chunks[index].files; | ||
compilation.chunks.forEach(function (item) { | ||
hashMap[item.name] = { | ||
chunkHash: item.hash, | ||
files: item.files, | ||
// contentHash: item.contentHash | ||
}; | ||
}); | ||
self.opts.cb(hashMap); | ||
callback(); | ||
}); | ||
@@ -28,0 +27,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5374
82