I have a react project with a package.json that has below structure:
"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.
Solutions tried: i tried doing as below but doesn't resolve the version number when deployed but sol 1 works fine locally:
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.
PS: Using webpack and babel
As you mentioned Webpack, I think you could use copy-webpack-plugin
assuming in your case the package.json would look following:
{
"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:
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
If all goes well in console webpack should throw smth like the following 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