[英]TS Read Dependency version from the script inside package.json
I have a react project with a package.json that has below structure:我有一个具有以下结构的 package.json 的反应项目:
"name": "@repo/packagex",
"description": "package X",
"version": "1.0.0",
"scripts": {
"build": "some script to move resources into a folder named resources/3.4.5/",
},
"dependencies": {
"@repo/package-y": "3.4.5",
}
...
If you notice, I am trying to copy some resources into a folder named as the version number of my @repo/package-y, and therefore whenever someone updates the version of @repo/package-y, the copy folder name should change to that version.如果你注意到了,我正在尝试将一些资源复制到一个以我的@repo/package-y 的版本号命名的文件夹中,因此每当有人更新@repo/package-y 的版本时,复制文件夹名称应该更改为那个版本。
Solutions tried: i tried doing as below but doesn't resolve the version number when deployed but sol 1 works fine locally:尝试过的解决方案:我尝试如下操作,但在部署时没有解析版本号,但 sol 1 在本地工作正常:
sol 1 tried
resources/${npm_package_dependencies__repo_package_y}
sol 2 tried
resources/%npm_package_dependencies__repo_package_y%
Is there a cross-platform way to access this version?.有没有跨平台的方式来访问这个版本? If I was working in a js/ts function i'd simply import the package.json file object and get whatever properties I want but now this is inside the package.json file.
如果我在 js/ts function 中工作,我只需导入 package.json 文件 object 并获取我想要的任何属性,但现在它位于 package.json 文件中。
PS: Using webpack and babel PS:使用 webpack 和 babel
As you mentioned Webpack, I think you could use copy-webpack-plugin正如您提到的 Webpack,我认为您可以使用copy-webpack-plugin
assuming in your case the package.json would look following:假设在您的情况下 package.json 看起来如下:
{
"name": "@repo/packagex",
"version": "1.0.0",
"scripts": {
"build": "webpack"
},
"dependencies": {
"@repo/package-y": "3.4.5"
},
"devDependencies": {
"copy-webpack-plugin": "^10.2.4",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
}
}
webpack.config.js content: webpack.config.js内容:
const {dependencies} = require('./package.json');
const CopyWebpackPlugin = require('copy-webpack-plugin');
/**
* @type {import('webpack').Configuration}
*/
const config = {
mode: 'development',
plugins: [
new CopyWebpackPlugin({
patterns: Object.keys(dependencies).map((dep) => ({
to: `resources/${dependencies[dep]}/`,
from: '*.*',
context: './some-resources'
}))
})
]
};
module.exports = config;
the output will be placed under the following path./dist/resources/3.4.5/some-resources/file.xyz output 将放在以下路径下。/dist/resources/3.4.5/some-resources/file.xyz
If all goes well in console webpack should throw smth like the following output如果在控制台中一切顺利 webpack 应该像下面这样抛出 smth output
@repo/packagex@1.0.0 build
webpack
asset main.js 1.18 KiB [compared for emit] (name: main)
asset resources/3.4.5/file.xyz 0 bytes [compared for emit] [from: some-resources/file.xyz] [copied]<br/>
./src/index.js 1 bytes [built] [code generated]
webpack 5.70.0 compiled successfully in 49 ms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.