2016-12-22 13 views
1

妥当な数の依存関係を持つきれいな標準タスクをまとめましたが、何らかの理由で実際に依存関係をロードするのに1〜3分かかります。私は普通のことを少しでも新しくしていますが、極端な遅さについての他の質問のほとんどは、数秒の時間を引用しているので、間違ったことをやっていると思います。 package.jsonからGrunt:依存関係のロードには数分かかります

const path = require("path"); 

module.exports = function(grunt) { 
    require('jit-grunt')(grunt); 
    require('time-grunt')(grunt); 

    const ignoredSourceScriptPatterns = ['!**/*.debug.js', '!**/*.min.js', '!scripts/*', '!**/*.map'], 
     baseUIPath = 'presentation/ui', 
     scripts = grunt.file.expand({filter: 'isFile', 
      matchBase: true, 
      cwd: baseUIPath}, 
     ['*.js', ...ignoredSourceScriptPatterns]); 

    // Project configuration. 
    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 
     copy: { 
      build: { 
       cwd: 'presentation/', 
       src: 'ui/**', 
       dest: path.join('presentation', 'static'), 
       expand: true 
      } 
     }, 
     uglify: { 
      options: { 
       sourceMap: true, 
       compress: false 
      }, 
      build: { 
       cwd: baseUIPath, 
       files: function() { 
        var modules = grunt.file.expand({ 
          filter: 'isDirectory', 
          expand: true, 
          cwd: 'presentation/ui/modules' 
         }, ['**', '!**/css', '!**/scripts', ...ignoredSourceScriptPatterns]), 
         components = grunt.file.expand({ 
          filter: 'isFile', 
          expand: true, 
          cwd: 'presentation/ui' 
         }, ['components/!*.js', ...ignoredSourceScriptPatterns]), 
         files, componentFiles; 

        files = modules.map(function(path) { 
         var modulePath = `modules/${path}/scripts`, 
          moduleName = modulePath.split('/').reduce(function(result, next) { 
           var nameFragment; 

           switch(next) { 
           case "modules": 
            nameFragment = "Poptart."; 
            break; 
           case "scripts": 
            nameFragment = "min.js"; 
            break; 
           default: 
            nameFragment = `${next.charAt(0).toUpperCase() + next.slice(1)}.`; 
            break; 
           } 
           return result + nameFragment; 
          }, ""); 

         return { 
          src: grunt.file.expand({ 
           filter: 'isFile' 
          }, [`${baseUIPath}/${modulePath}/*.js`, ...ignoredSourceScriptPatterns]).sort(
           function(a, b) { 
            return a.length - b.length; 
           } 
          ), 
          dest: `${baseUIPath}/${modulePath}/${moduleName}` 
         }; 
        }); 

        componentFiles = components.map(function(componentPath) { 
         return { 
          src: `${baseUIPath}/${componentPath}`, 
          dest: `${baseUIPath}/${componentPath.replace('.js', '.min.js')}` 
         }; 
        }); 

        files = [...files, ...componentFiles]; 

        files.push({ 
         src: grunt.file.expand({ 
          filter: 'isFile' 
         }, [`${baseUIPath}/*.js`, ...ignoredSourceScriptPatterns]), 
         dest: `${baseUIPath}/poptart.min.js` 
        }); 

        return files; 
       }(), 
       extDot: 'last', 
       expand: true 
      } 
     }, 
     cssmin: { 
      options: { 
       sourceMap: true 
      }, 
      build: { 
       cwd: 'presentation/static/ui/', 
       src: ['**/*.css', '!css/jquery-ui/**', '!css/ionicons/**', '!**/*.min.js'], 
       dest: 'presentation/static/ui/', 
       ext: '.min.css', 
       expand: true 
      } 
     }, 
     eslint: { 
      options: { 
       configFile: 'presentation/build/eslint.json', 
       ignorePath: 'presentation/build/.eslintignore' 
      }, 
      target: ['presentation/**/*.js', '!presentation/ui/scripts/*', '!presentation/static/**'] 
     }, 
     shell: { 
      test: { 
       command: 'python manage.py test -p "*tests.py"', 
       options: { 
        stdout: true, 
        failOnError: true 
       } 
      } 
     }, 
     karma: { 
      unit: { 
       configFile: 'karma.conf.js', 
       singleRun: true 
      } 
     }, 
     mochaTest: { 
      unit: { 
       options: { 
        reporter: 'spec' 
       }, 
       src: ['presentation/test/server/*.js'] 
      } 
     } 
    }); 

    /*grunt.loadNpmTasks('grunt-shell'); 
    grunt.loadNpmTasks('grunt-eslint'); 
    grunt.loadNpmTasks('grunt-karma'); 
    grunt.loadNpmTasks('grunt-mocha-test'); 
    grunt.loadNpmTasks('grunt-contrib-uglify'); 
    grunt.loadNpmTasks('grunt-contrib-cssmin'); 
    grunt.loadNpmTasks('grunt-contrib-concat'); 
    grunt.loadNpmTasks('grunt-newer');*/ 

    grunt.registerTask('default', ['lint', 'test']); 
    grunt.registerTask('test', ['shell:test', 'mochaTest:unit', 'karma:unit']); 
    grunt.registerTask('lint', ['eslint']); 
    grunt.registerTask('build-static', ['uglify', 'copy', 'cssmin']); 
}; 

そして、私のdevの依存関係:ここで

は私gruntfileは次のようになります。

  1. は使用:

    "devDependencies": { 
        "chai": "^3.5.0", 
        "chai-jquery": "^2.0.0", 
        "eslint": "^3.7.1", 
        "grunt": "^1.0.1", 
        "grunt-contrib-copy": "^1.0.0", 
        "grunt-contrib-cssmin": "^1.0.2", 
        "grunt-contrib-uglify": "^2.0.0", 
        "grunt-eslint": "^19.0.0", 
        "grunt-karma": "^2.0.0", 
        "grunt-mocha-test": "^0.13.2", 
        "grunt-newer": "^1.2.0", 
        "grunt-shell": "^1.3.1", 
        "jit-grunt": "^0.10.0", 
        "jquery": "^3.1.1", 
        "karma": "^1.3.0", 
        "karma-chai": "^0.1.0", 
        "karma-chrome-launcher": "^2.0.0", 
        "karma-cli": "^1.0.1", 
        "karma-jquery-chai": "^0.1.3", 
        "karma-mocha": "^1.3.0", 
        "karma-phantomjs-launcher": "^1.0.2", 
        "karma-sinon": "^1.0.5", 
        "mocha": "^3.2.0", 
        "sinon": "^1.17.6", 
        "sinon-chai": "^2.8.0", 
        "time-grunt": "^1.4.0" 
        } 
    

    は、ここで私はこれまで試したものです作業を読み込むことが実際に問題であったことを確認するために時間を浪費します。そうだった。実際に実行されているタスクは非常に合理的な時間を要しました。

  2. jit-gruntを使用しています。これは顕著な違いはありませんでした。
  3. npmプルーン。これは実際にはかなり大きな(〜30秒)差をつけましたが、依然としてタスクローディングのための完全に不合理な時間に私を残します。
  4. いくつかの依存関係の削除とアンロード(uglify、copy、cssmin)私はこの問題(最初はテスト/リンキング作業をしたばかりで、すべてが素早くうまくいった)を見始めた頃に追加しました。これも顕著な違いはありませんでした。

So.この尺度で他に何が遅くなるのでしょうか?

ありがとうございました!

+0

にあなたが使用している持っていない場合は、ファイル

  • の検索はたぶんちょうどファイルを検索していないときCWDのを追加そこに多くのテストフレームワークがあります。それらのすべてが必要であると確信していますか? – timothyclifford

  • +0

    私は間違いなく、これらのすべてが目的を果たしています。これは私の実際の製品で使用したいかもしれないものと遊ぶための私のサンドボックスアプリです。ここにはランダムなものがいくつかあります。いずれにしても、私はこの遅さを見始める前に、すべてがそこに戻っていたので、彼らは問題の核心ではないようです。 –

    +0

    私はそれぞれのセクションをひとつずつコメントアウトして、どこで問題が起こっているのかを見てみることにしました。それ以外の場合は 'node_modules'フォルダを削除してから' npm install'を再度実行してください。 – timothyclifford

    答えて

    1

    私はGruntfileを実行しようとしましたが、手動で依存関係をインストールしましたが、Gruntfileの読み込み時間は約4秒でした。速くはありませんが、あなたが話す時間はありません。

    私の推測はgrunt.file.expandが頑張っています。たぶんpresentation/uiフォルダが大きすぎますか? tree presentation/ui | wc -lを実行するとどうなりますか?それはあなたがそこに持っているファイルの数を示しています。

    それ以外の場合は、package.jsonとnpm lsの出力も参照すると便利です。

    +0

    package.jsonの依存関係の詳細で質問を更新しました。完全なnpm依存ツリーはばかげていましたが、NodeJSのために何をすべきか分かりません。 –

    +0

    file.expandは確実にリソースを殺しているようですが、私はそれを最適化するために何かをする必要があることを知っています(srcとdestファイルを動的にマップする方法についての提案があります)。私は、73のファイルと、プレゼンテーション/ユーの23のフォルダを持っていますが、間違いなく遅いところがあります。そうでないかもしれない - 私が言うように、私はあまりにも多くの経験をしています。いずれにしても、私のプレイサンドボックスエリアではなく実際のアプリでこれを実装すると、それよりずっと悪くなります。 –

    1

    uglifyタスクでは、これが原因でファイルが非効率的に検索されているようです。私は完全に夢中になっていない時でさえ私が遅さを見ていた理由を説明する狂ったとIEFのその巨大な塊を作ったことを完全に忘れてしまった。私は、ファイル検索の時間が読み込みタスクで集中している理由についても説明していると思います。いずれにし

    私はここで、動的に見出さスクリプトモジュールのソース/ DESTパスを返した場合、主な原因は、(予想通り)であった:

        return { 
             src: grunt.file.expand({ 
              filter: 'isFile' 
             }, [`${baseUIPath}/${modulePath}/*.js`, ...ignoredSourceScriptPatterns]).sort(
              function(a, b) { 
               return a.length - b.length; 
              } 
             ), 
             dest: `${baseUIPath}/${modulePath}/${moduleName}` 
            }; 
    

    Unspurprisingそれがループ内にあるからです。いずれにせよ、私はそれがcwdを欠いていたことに気づいた。それを追加すると、7秒に短縮されます。まだ改善が必要ですが、少なくとも今何を改善すべきかを知っています。この物語の

    モラル:

    1. あなたは
    関連する問題