多个项目的集中共享 gulp 依赖项?
Centralized shared gulp dependencies for multiple projects?
是否可以为多个项目共享 gulp 依赖项,而不是将它们放在每个项目文件夹中(默认情况下)?
这就是我的目标(每个客户项目都将具有相同的结构):
/shared
node_modules
shared_scss
shared_js
gulpfile.js
/client-project-1
client_scss
client_js
gulpfile.js
/client-project-2
/client-project-3
/client-project-n
在 Node.js 中共享依赖项很容易:给定一个需要查找的模块 ID,require.resolve
algorithm searches in node_modules
directories 并沿着目录树向上移动,直到模块被解析:
If the module identifier passed to require()
is not a native module, and does not begin with '/'
, '../'
, or './'
, then Node.js starts at the parent directory of the current module, and adds /node_modules
, and attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
回到你的问题,你基本上可以将你的项目放入一个共享 node_modules
子目录的目录中,并且来自任何客户端项目目录的每个 require
调用都将从该共享的依赖项加载node_modules
(如果它在客户端目录中存在且未被覆盖)。
shared
├── node_modules
│ └── ...
├── shared_js
├── shared_scss
├── gulpfile.js
|
├── client-project-1
│ ├── client_js
│ ├── client_scss
│ └── gulpfile.js
|
├── client-project-2
├── client-project-3
└── client-project-n
如果您还将 shared_js
符号链接到 shared/node_modules/shared_js
,客户项目将能够通过 require('shared_js/...')
.
要求它们
或者,您可以将共享依赖项 globally 安装到 $HOME/.node_modules
或 $NODE_PATH
中,但不鼓励这样做。
是否可以为多个项目共享 gulp 依赖项,而不是将它们放在每个项目文件夹中(默认情况下)?
这就是我的目标(每个客户项目都将具有相同的结构):
/shared
node_modules
shared_scss
shared_js
gulpfile.js
/client-project-1
client_scss
client_js
gulpfile.js
/client-project-2
/client-project-3
/client-project-n
在 Node.js 中共享依赖项很容易:给定一个需要查找的模块 ID,require.resolve
algorithm searches in node_modules
directories 并沿着目录树向上移动,直到模块被解析:
If the module identifier passed to
require()
is not a native module, and does not begin with'/'
,'../'
, or'./'
, then Node.js starts at the parent directory of the current module, and adds/node_modules
, and attempts to load the module from that location.If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
回到你的问题,你基本上可以将你的项目放入一个共享 node_modules
子目录的目录中,并且来自任何客户端项目目录的每个 require
调用都将从该共享的依赖项加载node_modules
(如果它在客户端目录中存在且未被覆盖)。
shared
├── node_modules
│ └── ...
├── shared_js
├── shared_scss
├── gulpfile.js
|
├── client-project-1
│ ├── client_js
│ ├── client_scss
│ └── gulpfile.js
|
├── client-project-2
├── client-project-3
└── client-project-n
如果您还将 shared_js
符号链接到 shared/node_modules/shared_js
,客户项目将能够通过 require('shared_js/...')
.
或者,您可以将共享依赖项 globally 安装到 $HOME/.node_modules
或 $NODE_PATH
中,但不鼓励这样做。