简体   繁体   English

在其中一个脚本中使用package.json的version属性

[英]Use version attribute of package.json in one of it's script

I need to use the version attribute of my package.json in one of it's scripts to put the version in the name of the JS bundle (i prefer to use the version as a unique identifier instead of a hash). 我需要在其中一个脚本中使用我的package.json的version属性将版本放在JS包的名称中(我更喜欢使用该版本作为唯一标识符而不是哈希)。

what i have: 我有的:

"build-js": "browserify -t [ babelify --presets [ react es2015 ] ] js/components/App.js -o js/bundle.js"

what i need ( i know it doesn't parse, but you get the idea ): 我需要什么( 我知道它不解析,但你明白了 ):

"build-js": "browserify -t [ babelify --presets [ react es2015 ] ] js/components/App.js -o js/bundle."+this.version+".js"

When you run an npm script, npm will set all the package.json fields as environment variables that you can use: 当您运行npm脚本时,npm会将所有package.json字段设置为您可以使用的环境变量:

https://docs.npmjs.com/misc/scripts#packagejson-vars https://docs.npmjs.com/misc/scripts#packagejson-vars

you can use the npm_package_version environment variable 您可以使用npm_package_version环境变量

You can do this with: 你可以这样做:

"build-js": "browserify -t [ babelify --presets [ react es2015 ] ] js/components/App.js -o js/bundle.$npm_package_version.js"

See this video for more details: https://egghead.io/lessons/tools-use-custom-config-settings-in-your-npm-scripts 有关详细信息,请参阅此视频: https//egghead.io/lessons/tools-use-custom-config-settings-in-your-npm-scripts

PS It's worth not putting your bundle in the same directory as your source, usually it goes into dist/ , eg dist/bundle.js . PS不值得将你的软件包放在与你的源相同的目录中,通常它会进入dist/ ,例如dist/bundle.js This way if you are publishing to npm you can ignore the unbuilt source directory. 这样,如果要发布到npm,则可以忽略未构建的源目录。 Or you can just remove the dist/ dir and rebuild, plus in the future you'll want other assets in dist as part of your build process. 或者你可以删除dist/ dir并重建,以及将来你需要dist中的其他资产作为构建过程的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM