@tntd/webpack-deps-version-plugin
v1.0.2
Published
Webpack plugin to inject dependency versions from lock files into the build
Readme
@tntd/webpack-deps-version-plugin
Webpack 插件,在打包时自动读取 package-lock.json 或 pnpm-lock.yaml,获取所有依赖的具体版本。
安装
npm install --save-dev @tntd/webpack-deps-version-plugin
# 或 yarn
yarn add -D @tntd/webpack-deps-version-plugin使用
// webpack.config.js
const { DepsVersionPlugin } = require('@tntd/webpack-deps-version-plugin');
module.exports = {
plugins: [
new DepsVersionPlugin({
cwd: __dirname, // 项目根目录,默认 process.cwd()
fieldName: 'depsVersions' // 注入到 compilation.options 的字段名
})
]
};获取版本信息
在 webpack 配置或插件中通过 compilation.options.depsVersions 访问:
compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
console.log(compilation.options.depsVersions);
// {
// 'lodash': '4.17.21',
// 'react': '18.2.0',
// ...
// }
});API
getDepsVersions(cwd?)
独立函数,直接读取 lock 文件返回版本映射:
const { getDepsVersions } = require('@tntd/webpack-deps-version-plugin');
const versions = getDepsVersions('/path/to/project');支持的 Lock 文件
| 锁文件 | 格式 |
|--------|------|
| package-lock.json | npm v5+ |
| pnpm-lock.yaml | pnpm v6+ / v9+ |
