gpt4 book ai didi

node.js - 如何让 npm 运行 <script> 委托(delegate)给子 package.json?

转载 作者:IT老高 更新时间:2023-10-28 23:16:13 28 4
gpt4 key购买 nike

我有 2 个级别的 package.json 文件。

例子在这里:

https://github.com/justin808/react-webpack-rails-tutorial

原因是顶层是一个 Rails 应用程序,我将所有 Node 工具放在一个名为 client 的目录下,并带有它自己的 package.json 文件。顶级 package.json 文件是 Node buildpack 运行 npm install 脚本的便利和 Hook 。

我有一个转发 gulp 命令的示例。有什么方法可以将顶级 package.json 中未找到的任何内容普遍转发给子项?

顶级package.json.

{
"name": "react-webpack-rails-tutorial",
"version": "1.1.1",
"description": "Code from the React Webpack tutorial.",
"main": "server.js",
"engines": {
"node": "0.10.32"
},
"scripts": {
"postinstall": "cd ./client && npm install",
"gulp": "cd ./client && npm run gulp"
},
"repository": {
"type": "git",
"url": "https://github.com/justin808/react-webpack-rails-tutorial.git"
},
"keywords": [
"react",
"tutorial",
"comment",
"example"
],
"author": "justin808",
"license": "MIT",
"bugs": {
"url": "https://github.com/justin808/react-webpack-rails-tutorial/issues"
},
"homepage": "https://github.com/justin808/react-webpack-rails-tutorial"
}

子目录package.json

{
"name": "react-webpack-rails-tutorial",
"version": "1.1.0",
"description": "Code from the React Webpack tutorial.",
"main": "server.js",
"engines": {
"node": "0.10.32"
},
"repository": {
"type": "git",
"url": "https://github.com/justin808/react-webpack-rails-tutorial.git"
},
"keywords": [
"react",
"tutorial",
"comment",
"example"
],
"author": "justin808",
"license": "MIT",
"bugs": {
"url": "https://github.com/justin808/react-webpack-rails-tutorial/issues"
},
"homepage": "https://github.com/justin808/react-webpack-rails-tutorial",
"dependencies": {
"babel-core": "^5.0.8",
"babel-loader": "^5.0.0",
"body-parser": "^1.12.2",
"es5-shim": "^4.1.0",
"imports-loader": "^0.6.3",
"jquery": "^2.1.3",
"loader-utils": "^0.2.6",
"marked": "^0.3.3",
"react": "^0.13.1",
"react-bootstrap": "^0.20.1",
"sleep": "^2.0.0",
"webpack": "^1.7.3"
},
"devDependencies": {
"babel-eslint": "^2.0.2",
"bootstrap-sass": "^3.3.4",
"bootstrap-sass-loader": "^1.0.3",
"css-loader": "^0.9.1",
"eslint": "^0.18.0",
"eslint-plugin-react": "^2.0.2",
"expose-loader": "^0.6.0",
"express": "^4.12.3",
"file-loader": "^0.8.1",
"gulp": "^3.8.11",
"gulp-eslint": "^0.8.0",
"node-sass": "^2.1.1",
"react-hot-loader": "^1.2.4",
"sass-loader": "^0.6.0",
"style-loader": "^0.9.0",
"url-loader": "^0.5.5",
"webpack-dev-server": "^1.8.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"gulp": "gulp"
}
}

最佳答案

您可以使用 npm run 脚本来简化事务(参见 npm-scripts)。在父 package.json:

  "scripts": {
...
"client-build": "cd client && npm run build"
}

客户端有一个 package.jsonnpm run build 命令用于构建客户端代码。

然后调用 npm run client-build 作为其他任务的 shell 命令的一部分。例如:

  "scripts": {
"start": "npm run client-build && gulp some-task",
...
}

这可能有助于将子项目分解为具有自己的 git 存储库的单独模块并通过安装后脚本构建它。在这种情况下,当在父项目上运行 npm install 时,子项目将有机会构建自己。

关于node.js - 如何让 npm 运行 &lt;script&gt; 委托(delegate)给子 package.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453050/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com