Grunt非常慢 - 100%cpu

时间:2015-02-13 16:09:16

标签: angularjs gruntjs typescript

我正在尝试用grunt(Ubuntu 14.04)构建AngularJS项目,但是在运行它时,100%的CPU吃得非常慢。

这是我的grunfile

module.exports = function (grunt) {

    grunt.loadNpmTasks('grunt-typescript');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
    grunt.loadNpmTasks('grunt-karma');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-compress');

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        typescript: {           
            home: {
                src: ['src/routes/home.ts'],
                dest: 'build/compiled_app/routes/home.js',
                options: {
                    ignoreTypeCheck: false
                }
            },
            controller_tests: {
                src: ['tests/unit/controller/*Test.ts'],
                dest: 'tests/build',
                options: {
                    ignoreTypeCheck: false
                }
            },
            services_tests: {
                src: ['tests/unit/services/*.ts'],
                dest: 'tests/build/ServicesTests.js',
                options: {
                    ignoreTypeCheck: false
                }
            },
            filters_tests: {
                src: ['tests/unit/filters/*.ts'],
                dest: 'tests/build/FiltersTests.js',
                options: {
                    ignoreTypeCheck: false
                }
            }
        },

        less: {
            options: {
                paths: ['src/less'],
                yuicompress: true,
                nospawn: true,
                livereload: true
            },
            compile: {
                expand: true,
                cwd: 'src/less',
                src: '**/*.less',
                dest: 'build/assets/css/',
                ext: '.css'
            }
        },

        copy: {
            assets: {
                expand: true,
                cwd: 'assets/',
                src: ['**', '!root_content/**'],
                dest: 'build/assets'
            },
            root_content: {
                expand: true,
                cwd: 'assets/root_content/',
                src: '**',
                dest: 'build/'
            },
            min_js: {
                expand: true,
                cwd: 'build/compiled_app/',
                src: ['**/*.js', '*.js'],
                dest: 'build/compiled_app/',
                ext: '.min.js'
            }
        },

        htmlmin: {
            options: {
                removeComments: true,
                collapseWhitespace: true
            },
            app_pages: {
                expand: true,
                cwd: 'pages/',
                src: ['**/*.html', '*.html'],
                dest: 'build/'
            },
            app_views: {
                expand: true,
                cwd: 'src/views',
                src: ['**/*.html', '*.html'],
                dest: 'build/compiled_app/views',
                filter: 'isFile'
            }
        },

        uglify: {
            options: {
                mangle: false
            },
            app: {
                expand: true,
                cwd: 'build/compiled_app/',
                src: '**/*.js',
                dest: 'build/compiled_app/',
                ext: '.min.js'
            }
        },

        karma: {
            unit: {
                basePath: '',
                frameworks: ['jasmine'],
                files: [
                    {pattern: 'tests/assets/javascripts/jquery-1.7.2.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-route.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-resource.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-cookies.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-ipcookie.min.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-mocks.js', included: true},
                    {pattern: 'tests/assets/javascripts/async.min.js', included: true},
                    {pattern: 'tests/assets/javascripts/underscore-min.js', included: true},
                    {pattern: 'assets/javascripts/ng-infinite-scroll.js', included: true},
                    {pattern: 'assets/javascripts/ui-bootstrap-0.6.0.min.js', included: true},
                    {pattern: 'assets/javascripts/angular-strap.min.js', included: true},
                    {pattern: 'build/compiled_app/*.js', included: true},
                    {pattern: 'build/compiled_app/routes/*.js', included: true},
                    {pattern: 'tests/build/**/*.js', included: true}
                ],
                preprocessors: { '**/tests/build/src/**/*.js': 'coverage' },
                reporters: ['progress', 'coverage', 'junit'],
                singleRun: true,
                browsers: ['PhantomJS'],
                coverageReporter: {
                    type: 'cobertura',
                    dir: 'tests/coverage/'
                },
                junitReporter: {
                    outputFile: 'tests/coverage/test-results.xml',
                    suite: ''
                }
            },
            e2e: {
                basePath: '',
                files: [
                    {pattern: 'tests/assets/javascripts/jquery-1.7.2.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-route.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-resource.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-cookies.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-ipcookie.min.js', included: true},
                    {pattern: 'tests/assets/javascripts/angular-mocks.js', included: true},
                    {pattern: 'tests/assets/javascripts/async.min.js', included: true},
                    {pattern: 'tests/assets/javascripts/underscore-min.js', included: true},
                    {pattern: 'assets/javascripts/ng-infinite-scroll.js', included: true},
                    {pattern: 'build/compiled_app/*.js', included: true},
                    {pattern: 'build/compiled_app/routes/*.js', included: true},
                    {pattern: 'tests/e2e/*.js', included: true}
                ],
                autoWatch: false,
                browsers: ['Chrome'],
                frameworks: ['ng-scenario'],
                singleRun: true,                
                plugins: [
                    'karma-junit-reporter',
                    'karma-chrome-launcher',
                    'karma-firefox-launcher',
                    'karma-jasmine',
                    'karma-ng-scenario'
                ],
                junitReporter: {
                    outputFile: 'test_out/e2e.xml',
                    suite: 'e2e'
                }
            }
        },

        clean: {
            output: ['build/', 'tests/build', 'tests/coverage']
        },


        watch: {

            typescript: {
                files: ['src/**/*.ts'],
                tasks: ['package-js'],
                options: {
                    debounceDelay: 500
                }
            },
            // Watch task for html partials and main pages
            // It calles task to package html in correct folder
            html: {
                files: ['src/**/*.html', 'pages/**/*.html'],
                tasks: ['package-html'],
                options: {
                    debounceDelay: 500
                }
            },
            // Watch task for less files. Will execute task to compile
            // less files and package them in correct folder
            less: {
                files: ['src/**/*.less'],
                tasks: ['package-less'],
                options: {
                    debounceDelay: 500
                }
            }

        },

        compress: {
            main: {
                options: {
                    mode: 'tgz',
                    archive: 'build/landing.tar.gz'
                },
                expand: true,
                cwd: 'build',
                src: ['**/*'],
                pretty: true
            }
        }

    });


    grunt.registerTask('package-js', ['typescript', 'copy:min_js']);
    grunt.registerTask('package-html', ['htmlmin']);
    grunt.registerTask('package-less', ['less']);

    grunt.registerTask('compile', ['less', 'typescript']);
    grunt.registerTask('app', ['compile', 'htmlmin:app_pages', 'htmlmin:app_views']);
    grunt.registerTask('assets', ['copy:assets', 'copy:root_content']);
    grunt.registerTask('package', ['app', 'assets', 'uglify:app']);
    grunt.registerTask('default', ['clean', 'package']);
    grunt.registerTask('build', ['default', 'compress']);
    grunt.registerTask('test', ['clean', 'typescript', 'karma:unit']);
    grunt.registerTask('tests', ['test']);
    grunt.registerTask('dev', ['app', 'copy:min_js']);

};

我已经使用了一段时间没有任何问题,一切都很好!它已经突然开始了,我尝试了几件事来提升它而没有任何结果。我认为它与grunt本身没有关联,但更多的是我正在使用的系统(在其他操作系统上它很好)。有什么问题?

更新

重新安装grunt-contrib-uglify后,一切似乎都没问题。考虑到要编译的模块的数量,仍然uglify任务需要一分钟才能完成,但这或多或少都很好。

4 个答案:

答案 0 :(得分:4)

您可以使用time-grunt输出有关导致问题的任务的数据。然后,您可以集中精力优化该特定任务。

https://github.com/sindresorhus/time-grunt

披露:这是一个与我无关的老板库。

答案 1 :(得分:0)

我很想更新所有软件包。我在grunt-ts团队工作,我们有文件随机看着我们100%的cpu都有karma使用的手表库和grunt-watch使用的那个

答案 2 :(得分:0)

如果您有许多grunt模块,那么jit-grunt可能有所帮助。它只加载您正在运行的任务所需的grunt模块,而不是每次都尝试加载每个模块。

您可以在此处查看load-grunt-config的实现:

http://ia.njamieson.co.uk/2015/03/27/speeding-up-grunt-initial-load-jit-grunt-and-load-grunt-config/

答案 3 :(得分:0)

interval中添加options

options: {
  interval: 5007,
  nospawn: true,
  livereload: true
}