Angularjs 如何使用grunt缩小文件?
我正在创建一个带有grunt/ye阿曼的角应用程序,并试图创建一个包含小型文件的公共版本。问题是没有创建js文件和css:Angularjs 如何使用grunt缩小文件?,angularjs,gruntjs,Angularjs,Gruntjs,我正在创建一个带有grunt/ye阿曼的角应用程序,并试图创建一个包含小型文件的公共版本。问题是没有创建js文件和css: // Generated on 2015-06-01 using generator-angular 0.10.0 'use strict'; // # Globbing // for performance reasons we're only matching one level down: // 'test/spec/{,*/}*.js' // use this i
// Generated on 2015-06-01 using generator-angular 0.10.0
'use strict';
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Configurable paths for the application
var appConfig = {
app: require('./bower.json').appPath || 'app',
dist: 'dist'
};
// Define the configuration for all the tasks
grunt.initConfig({
// Watches files for changes and runs tasks based on the changed files
watch: {
bower: {
files: ['bower.json'],
tasks: ['wiredep']
},
js: {
files: ['app/{,*/}*.js','app/**/*.json'],
tasks: ['newer:jshint:all'],
options: {
livereload: '<%= connect.options.livereload %>'
}
},
jsTest: {
files: ['test/spec/{,*/}*.js'],
tasks: ['newer:jshint:test', 'karma']
},
compass: {
files: ['app/styles/{,*/}*.{scss,sass}'],
tasks: ['compass:server', 'autoprefixer']
},
gruntfile: {
files: ['Gruntfile.js']
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'app/**/*.html',
'app/**/*.js',
'app/**/*.json',
'.tmp/styles/{,*/}*.css',
'app/styles/{,*/}*.css',
'app/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
]
}
},
// The actual grunt server settings
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: 'localhost',
livereload: 35729
},
livereload: {
options: {
open: true,
middleware: function (connect) {
return [
connect.static('.tmp'),
connect().use(
'/bower_components',
connect.static('./bower_components')
),
connect.static(appConfig.app)
];
}
}
},
test: {
options: {
port: 9001,
middleware: function (connect) {
return [
connect.static('.tmp'),
connect.static('test'),
connect().use(
'/bower_components',
connect.static('./bower_components')
),
connect.static(appConfig.app)
];
}
}
},
dist: {
options: {
open: true,
base: 'dist'
}
}
},
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: {
src: [
'Gruntfile.js',
'app/scripts_old/{,*/}*.js'
]
},
test: {
options: {
jshintrc: 'test/.jshintrc'
},
src: ['test/spec/{,*/}*.js']
}
},
// Empties folders to start fresh
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'dist/{,*/}*',
'!dist/.git{,*/}*'
]
}]
},
server: '.tmp'
},
// Add vendor prefixed styles
autoprefixer: {
options: {
browsers: ['last 1 version']
},
dist: {
files: [{
expand: true,
cwd: '.tmp/styles/',
src: '{,*/}*.css',
dest: '.tmp/styles/'
}]
}
},
// Automatically inject Bower components into the app
wiredep: {
app: {
src: ['app/index.html'],
ignorePath: /\.\.\//
},
sass: {
src: ['app/styles/{,*/}*.{scss,sass}'],
ignorePath: /(\.\.\/){1,2}bower_components\//
}
},
// Compiles Sass to CSS and generates necessary files if requested
compass: {
options: {
sassDir: 'app/styles',
cssDir: '.tmp/styles',
generatedImagesDir: '.tmp/images/generated',
imagesDir: 'app/images',
javascriptsDir: 'app/scripts_old',
fontsDir: 'app/styles/fonts',
importPath: './bower_components',
httpImagesPath: '/images',
httpGeneratedImagesPath: '/images/generated',
httpFontsPath: '/styles/fonts',
relativeAssets: false,
assetCacheBuster: false,
raw: 'Sass::Script::Number.precision = 10\n'
},
dist: {
options: {
generatedImagesDir: 'dist/images/generated'
}
},
server: {
options: {
debugInfo: true
}
}
},
// Renames files for browser caching purposes
filerev: {
dist: {
src: [
'dist/scripts_old/{,*/}*.js',
'dist/styles/{,*/}*.css',
'dist/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'dist/styles/fonts/*'
]
}
},
// Reads HTML for usemin blocks to enable smart builds that automatically
// concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them
useminPrepare: {
html: 'app/index.html',
options: {
dest: 'dist',
flow: {
html: {
steps: {
js: ['concat', 'uglifyjs'],
css: ['cssmin']
},
post: {}
}
}
}
},
// Performs rewrites based on filerev and the useminPrepare configuration
usemin: {
html: ['dist/{,*/}*.html'],
css: ['dist/styles/{,*/}*.css'],
js: ['dist/scripts/{,*/}*.js'],
options: {
assetsDirs: [
'dist',
'dist/images',
'dist/styles'
],
patterns: {
js: [[/(images\/[^''""]*\.(png|jpg|jpeg|gif|webp|svg))/g, 'Replacing references to images']]
}
}
},
// The following *-min tasks will produce minified files in the dist folder
// By default, your `index.html`'s <!-- Usemin block --> will take care of
// minification. These next options are pre-configured if you do not wish
// to use the Usemin blocks.
cssmin: {
dist: {
files: {
'dist/styles/main.css': [
'.tmp/styles/{,*/}*.css'
]
}
}
},
uglify: {
dist: {
files: {
'dist/scripts/scripts.js': [
'dist/scripts/scripts.js'
]
}
}
},
concat: {
dist: {}
},
imagemin: {
dist: {
files: [{
expand: true,
cwd: 'app/images',
src: '{,*/}*.{png,jpg,jpeg,gif}',
dest: 'dist/images'
}]
}
},
svgmin: {
dist: {
files: [{
expand: true,
cwd: 'app/images',
src: '{,*/}*.svg',
dest: 'dist/images'
}]
}
},
htmlmin: {
dist: {
options: {
collapseWhitespace: true,
conservativeCollapse: true,
collapseBooleanAttributes: true,
removeCommentsFromCDATA: true,
removeOptionalTags: true
},
files: [{
expand: true,
cwd: 'dist',
src: ['*.html', 'views/{,*/}*.html'],
dest: 'dist'
}]
}
},
// ng-annotate tries to make the code safe for minification automatically
// by using the Angular long form for dependency injection.
ngAnnotate: {
dist: {
files: [{
expand: true,
cwd: '.tmp/concat/scripts',
src: ['*.js', '!oldieshim.js'],
dest: '.tmp/concat/scripts'
}]
}
},
// Replace Google CDN references
cdnify: {
dist: {
html: ['dist/*.html']
}
},
// Copies remaining files to places other tasks can use
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: 'app',
dest: 'dist',
src: [
'*.{ico,png,txt}',
'.htaccess',
'modules/**.*.js',
'*.html',
'views/{,*/}*.html',
'images/{,*/}*.{webp}',
'fonts/{,*/}*.*'
]
}, {
expand: true,
cwd: '.tmp/images',
dest: 'dist/images',
src: ['generated/*']
}, {
expand: true,
cwd: '.',
src: 'bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*',
dest: 'dist'
}]
},
styles: {
expand: true,
cwd: 'app/styles',
dest: '.tmp/styles/',
src: '{,*/}*.css'
}
},
// Run some tasks in parallel to speed up the build process
concurrent: {
server: [
'compass:server'
],
test: [
'compass'
],
dist: [
'compass:dist',
'imagemin',
'svgmin'
]
},
// Test settings
karma: {
unit: {
configFile: 'test/karma.conf.js',
singleRun: true
}
},
protractor: {
options: {
keepAlive: true,
configFile: "test/protractor.conf.js"
},
run: {}
}
});
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
grunt.log.warn('running grunt serve , value of target',target);
if (target === 'dist') {
grunt.log.warn('target = dist');
return grunt.task.run(['build', 'connect:dist:keepalive']);
}
grunt.task.run([
'clean:server',
'wiredep',
'test',
//'concurrent:server',
'autoprefixer',
'connect:livereload',
'watch'
]);
});
grunt.registerTask('server', 'DEPRECATED TASK. Use the "serve" task instead', function (target) {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
grunt.task.run(['serve:' + target]);
});
grunt.loadNpmTasks("grunt-protractor-runner");
grunt.registerTask('test', [
'clean:server',
//'concurrent:test',
'autoprefixer',
'connect:test',
'karma'
//'protractor:run'
]);
grunt.registerTask('build', [
'clean:dist',
'wiredep',
//'useminPrepare',
//'concurrent:dist',
'autoprefixer',
'concat',
'ngAnnotate',
'copy:dist',
//'cdnify',
'cssmin',
'uglify',
'filerev',
'usemin',
'htmlmin'
]);
grunt.registerTask('default', [
'newer:jshint',
'test',
'build'
]);
};
如何生成css和js文件?请确保您在uglify配置中映射了正确的源文件。根据您当前的上述粘贴脚本,src和目标文件具有相同的名称
uglify: {
dist: {
files: {
'dist/scripts/scripts.js': [
'dist/scripts/scripts.js'
]
}
}
}
有关更多信息,请参阅。最初尝试使用以下简单配置使其工作:
uglify: {
dist: {
files: {'dest/b.js': ['src/bb.js', 'src/bbb.js']}
}
}
htmlmin任务是如何工作的。有关这些文件在dist文件夹中的位置,请参见下文
htmlmin: {
dist: {
options: {
collapseWhitespace: true,
conservativeCollapse: true,
collapseBooleanAttributes: true,
removeCommentsFromCDATA: true,
removeOptionalTags: true
},
files: [{
expand: true,
cwd: 'dist',
src: ['*.html', 'views/{,*/}*.html'],
dest: 'dist'
}]
}
}
试过运行
build
task吗?我试过上面列出的结果,您可以清楚地看到构建的原因。运行“cssmin:dist”(cssmin)任务>>未写入目标,因为缩小的CSS为空。运行“uglify:dist”(uglify)任务>>未写入目标dist/scripts/scripts.js,因为src文件为空。我现在设法创建了一些脚本现在正在研究如何缩小js htmlmin任务如何工作?这些文件在dist文件夹中的什么位置?我已经根据您的评论更新了我的原始答案。谢谢。看起来不错,还有一个问题。如果我想链接我的json mockdata文件,我应该在哪里指定?顺便说一句,htmlmin不会获取我的所有部分ie html文件,并将它们缩小为index.html in dist?
htmlmin: {
dist: {
options: {
collapseWhitespace: true,
conservativeCollapse: true,
collapseBooleanAttributes: true,
removeCommentsFromCDATA: true,
removeOptionalTags: true
},
files: [{
expand: true,
cwd: 'dist',
src: ['*.html', 'views/{,*/}*.html'],
dest: 'dist'
}]
}
}