13

Nodejitsu にデプロイするアプリがあります。最近、依存関係をインストールできなかったため、アプリを再起動しようとした (そして失敗した) 後、アプリが数時間オフラインになる npm の問題が発生しました。これは、package.jsonのようにすべての依存関係をリストすることで、将来的に回避できると言われました。これにより、依存bundledDependencies関係がアプリケーションの残りの部分と一緒にアップロードされます。つまり、package.json を次のようにする必要があります。

"dependencies": {
  "express": "2.5.8",
  "mongoose": "2.5.9",
  "stylus": "0.24.0"
},
"bundledDependencies": [
  "express",
  "mongoose",
  "stylus"
]

さて、DRY の理由から、これは魅力的ではありません。しかし、さらに悪いのはメンテナンスです。依存関係を追加または削除するたびに、2 つの場所で変更を加える必要があります。bundledDependencies同期に使用できるコマンドはありdependenciesますか?

4

2 に答える 2

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, '  '));
  });

};

これをプロジェクトのルートディレクトリの。というファイルに入れますgrunt.js。gruntをインストールするには、npmを使用しますnpm install -g grunt。次に、を実行してパッケージをバンドルしますgrunt bundle

コメント投稿者は、役立つ可能性のあるnpmモジュールについて言及しました:https ://www.npmjs.com/package/grunt-bundled-dependencies (私は試していません)。

于 2012-12-14T21:57:48.560 に答える