有没有办法自动生成bundledDependencies列表?

Tre*_*ham 13 node.js npm nodejitsu

我有一个应用程序,我正在部署到Nodejitsu.最近,他们遇到了npm问题导致我的应用程序在我尝试(并且失败)重启后几个小时后离线,因为无法安装其依赖项.有人告诉我,将来可以通过列出bundledDependencies我的package.json中的所有依赖项来避免这种情况,从而导致依赖项与应用程序的其余部分一起上传.这意味着我需要我的package.json看起来像这样:

"dependencies": {
  "express": "2.5.8",
  "mongoose": "2.5.9",
  "stylus": "0.24.0"
},
"bundledDependencies": [
  "express",
  "mongoose",
  "stylus"
]
Run Code Online (Sandbox Code Playgroud)

现在,在DRY的基础上,这是没有吸引力的.但更糟糕的是维护:每次添加或删除依赖项时,我都必须在两个地方进行更改.是否有一个命令,我可以使用同步bundledDependenciesdependencies

wpr*_*prl 10

如何实现grunt.js任务呢?这有效:

module.exports = function(grunt) {

  grunt.registerTask('bundle', 'A task that bundles all dependencies.', function () {
    // "package" is a reserved word so it's abbreviated to "pkg"
    var pkg = grunt.file.readJSON('./package.json');
    // set the bundled dependencies to the keys of the dependencies property
    pkg.bundledDependencies = Object.keys(pkg.dependencies);
    // write back to package.json and indent with two spaces
    grunt.file.write('./package.json', JSON.stringify(pkg, undefined, '  '));
  });

};
Run Code Online (Sandbox Code Playgroud)

将它放在项目的根目录中,该文件名为grunt.js.要安装grunt,请使用npm : npm install -g grunt. 然后通过执行捆绑包grunt bundle.

评论员提到了一个非常有用的npm模块:https://www.npmjs.com/package/grunt-bundled-dependencies(我还没试过.)