我正在尝试使用 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
任务仍然需要一分钟才能完成,但考虑到要编译的模块数量,这或多或少没问题。
您可以使用
time-grunt
输出有关哪个任务导致问题的数据。然后,您可以集中精力优化该特定任务。
https://github.com/sindresorhus/time-grunt
披露:这是一个与我无关的boss库。
我很想更新所有软件包。我在 grunt-ts 团队工作,我们通过 karma 使用的监视库和 grunt-watch 使用的监视库随机进行文件监视,为我们提供 100% 的 cpu
如果您有很多 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/
在
interval
中添加options
:
options: {
interval: 5007,
nospawn: true,
livereload: true
}
我也有同样的问题。将计算机连接到合适的电源是我的解决方案。 CPU 速度提高了 4 倍,Grunt 时间从 50 秒减少到 12 秒。