![](/img/trans.png)
[英]How to cache node_modules for unchanged package.json during yarn / npm install during docker build?
[英]NPM build phase of my package not executed during install
我正在開發一個用於以通用方式使用Web Worker的程序包,但是在嘗試將其添加為另一個項目的依賴項時發現了一些問題。
通常,我希望在install
時在package.json
中有一個構建腳本部分,它將被自動調用以生成rollup.config.js
的輸出。 但是它似乎什么也沒執行。 我對npm build
應該如何工作有任何誤解?
如果不是,則在下一個文件示例中, package.json
中存在任何其他沖突腳本,導致該腳本不起作用:
{
"name": "web-threads",
"version": "1.0.5",
"description": "generic threads using web workers for the web",
"main": "dist/web-threads.js",
"scripts": {
"build": "rollup -c",
"test": "jest",
"test:dev": "jest --watchAll test/unit",
"test:int": "jest test/integration",
"test:cov": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"push": "yarn test && git push",
"deploy:major": "yarn version --major",
"deploy:minor": "yarn version --minor",
"deploy:patch": "yarn version --patch",
"deploy:push": "git push && git push --tags",
"preversion": "yarn test"
},
"keywords": [""],
"repository": "",
"author": "",
"license": "MIT",
"private": false,
"devDependencies": {
"babel-jest": "23.4.2",
"babel-preset-env": "1.7.0",
"babel-preset-stage-0": "6.24.1",
"coveralls": "3.0.2",
"faker": "4.1.0",
"jest": "23.5.0",
"jest-puppeteer": "3.3.1",
"puppeteer": "1.7.0",
"rollup": "0.65.0",
"rollup-plugin-babel": "3.0.7",
"rollup-plugin-uglify": "4.0.0",
"uglify-es": "3.3.9"
},
"babel": {
"presets": ["env","stage-0"]
},
"jest": {
"testMatch": [
"**/test/**/*-test.js"
],
"transform": {
"^.+\\.jsx|.js?$": "babel-jest"
}
}
}
我也將依賴關系改為不是devDependencies
但這並沒有幫助解決問題。
NPM構建文檔: https : //docs.npmjs.com/cli/build
您可以嘗試添加postinstall
腳本。 如npm docs中所述
安裝后:在安裝軟件包后運行。
因此,@ Olian04的答案將正確的信息發送給我,並在文檔上做了一些介紹。 確實,我對構建有一個誤解,因為它實際上不是script
而僅僅是過程階段的一個hook
。
因此,解決包中所需編譯問題的正確方法似乎是由prepare運行的不同過程。 這是一個腳本,文檔定義為:
對於不是特定於平台的構建步驟(例如,將CoffeeScript或其他語言編譯為JavaScript),請使用prepare腳本執行此操作,並將所需的程序包設為devDependency。
准備腳本將在發布之前運行,以便用戶可以使用該功能而無需他們自己對其進行編譯。 在開發模式下(即在本地運行npm install),它也會運行此腳本,以便您可以輕松對其進行測試。
例:
{ "name": "web-threads",
"description": "a delightfully fruity coffee varietal",
"version": "1.2.3",
"devDependencies": {
"coffee-script": "~1.6.3"
},
"scripts": {
"prepare": "coffee -o lib/ -c src/waza.coffee"
},
"main": "lib/waza.js"
}
作為摘要,對於需要在安裝計算機/平台本地進行的事情,請使用postinstall
(但需要滿足所有依賴性)。 使用不依賴平台的prepare
過程,這將不需要用戶擁有所有工具來打包程序包,並且您也不會污染存儲庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.