有没有办法引用gulpfile.js和package.json(来自远程位置)?

时间:2016-02-03 12:47:54

标签: javascript node.js maven npm gulp

我们有多个独立的ui模块(单独的git项目)。我们使用npm来下载gulp的dev依赖项,然后gulp precompiles sass,uglify js,build template cash,运行本地lint和测试。这意味着每个模块都具有几乎相同的package.json和gulpfile.js。有没有办法重复使用它们,引用它们而不是在每个项目中复制它们?或者也许从maven这样的父级继承设置和配置?

1 个答案:

答案 0 :(得分:3)

是的,绝对有。

在我现在正在工作的项目中,我们的情况大致相同:前端模块被分成不同的存储库,并且具有非常相似的gulp任务集。因此,我们已将这些任务移至gulp-common仓库,并通过npm将其安装为dev依赖项:

"devDependencies": {
    "gulp-common": "git+ssh://our-repo-url/gulp-common.git"
}

然后,为了加载这些gulp任务,我们修改了gulpfile.js所以现在它看起来像这样:

require('require-dir')('build/tasks'); // load specific tasks from the usual place
require('require-dir')('node_modules/gulp-common/build/tasks'); // load common tasks

与此同时,在gulp-common中,我们有一个./build目录,其中包含任务和index.js文件:

module.exports = require('require-dir')('./build');

干杯!