我有一个使用Grunt和Bower的项目.Grunt-uglify将连接/缩小Bower目录中的deploy/scripts.js文件到文件夹.我正在使用Grunt-newer,因此只有deploy/scripts.js在添加或更改新文件时才会更新.一切都很好......除了......
当我使用Bower添加新库时,文件日期反映了文件上传到Bower库(或托管它的任何人)的时间,而不是它在我的计算机上创建的日期.因此,Grunt-newer认为新的Bower库比旧deploy/scripts.js文件更新并且不更新文件.
一个 - 麻烦 - 解决方案是打开新的库.js文件,并重新保存它.它会修改文件日期,因此,grunt-newer将创建deploy/script.js文件.然而,Bower的实用性似乎没有用这种尴尬的解决方案.
我正在使用Grunt来优化我的AngularJS应用程序以进行生产.它使用useminPrepare和usemin从我的index.html页面读取要连接/缩小的文件.我正在尝试使用源图,因此我可以查看错误发生的位置.这是package.json的相关版本:
"grunt": "0.4.5",
"grunt-autoprefixer": "2.2.0",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-copy": "~0.5.0",
"grunt-contrib-cssmin": "~0.11.0",
"grunt-contrib-uglify": "~0.9.1",
"grunt-rev": "~0.1.0",
"grunt-usemin": "~2.0.2",
Run Code Online (Sandbox Code Playgroud)
这是我Gruntfile.js对相关任务的精简版:
'use strict';
// usemin custom step
var useminAutoprefixer = {
name: 'autoprefixer',
createConfig: require('grunt-usemin/lib/config/cssmin').createConfig // Reuse cssmins createConfig
};
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: ["dist", '.tmp'],
copy: {
main: {
files: [
{
expand: true,
src: ['**', '!css/**', '!*.js', '!node_modules/**', '!*.log', '!tests/**'],
dest: 'dist/'
}
]
}
},
cssmin: {
options: {
root: …Run Code Online (Sandbox Code Playgroud) angularjs gruntjs source-maps grunt-usemin grunt-contrib-uglify
我正在使用Grunt来连接和缩小文件(使用grunt-contrib-uglify和grunt-contrib-concat),我想添加一个源映射.uglify文档说只是为sourceMap添加一个选项设置为布尔值true.但是当我将它添加到我的任务中时(我尝试了几个不同的任务),该过程运行正常,直到它到达源地图部分,然后我得到:
写真......错误
警告:无法写入"true"文件(错误代码:undefined).使用--force继续.
连接完成,缩小完成.但是......没有运气源图.
来自我的Grunt文件的示例:
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= pkg.version %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
},
publicjs: {
options: {
sourceMap: true
},
files: {
'js/<%= pkg.name %>_public.min.js': ['<%= concat.publicjs.dest %>']
}
}
}
Run Code Online (Sandbox Code Playgroud)
(我也尝试过在我的顶级选项对象中使用sourceMap.)
有任何想法吗?显然Grunt可以写入目录,因为它创建了连接和缩小的文件,我想不出还有什么可能是麻烦.
我的目标是连接所有的css,js文件并缩小所有这些文件.我可以缩小我的力度concat.js,但我正在努力缩小我的力度concat.css.
module.exports = function(grunt) {
"use strict";
grunt.initConfig({
concat: {
js: {
src: [
'js/bootstrap.min.js',
'js/jquery-1.10.2.min.js',
'js/jquery.easypiechart.min.js',
'js/jquery.isotope.min.js',
'js/jquery.magnific-popup.min.js',
'js/waypoints.min.js',
'js/respond.min.js',
'js/jquery.vegas.min.js',
'js/modernizr-2.6.2.min.js',
'js/jquery.nav.js',
'js/html5shiv.js',
'js/jquery.scrollTo.js',
'js/jquery.sticky.js',
'js/jquery.validate.js',
'js/main.js',
],
dest: 'dist/concat.js'
},
css: {
src: [
'css/magnific-popup.css',
'css/main.css',
'css/xl.css',
'css/lg.css',
'css/md.css',
'css/sm.css',
'css/xs.css',
'css/print.css',
'css/bootstrap.min.css',
'css/font-awesome.min.css',
],
dest: 'dist/concat.css'
}
},
watch: {
js: {
files: ['js/*.js'],
task: ['concat:js']
},
css: {
files: ['css/*.css'],
task: ['concat:css']
}
},
uglify: …Run Code Online (Sandbox Code Playgroud) 我希望我的Package.json是这样的
{
"name": "Billing",
"version": "0.0.0",
"dependencies": {
"grunt": "~0.4.1",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-compass": "~0.5.0",
"grunt-contrib-uglify": "~0.2.2",
"grunt-contrib-cssmin": "~0.6.2",
"matchdep": "~0.1.2"
},
"devDependencies": {
"grunt-contrib-handlebars": "~0.5.4",
"grunt-contrib-less": "~0.8.1"
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个npm install但是得到了这个错误
package.json必须是实际的JSON,而不仅仅是JavaScript.
所以我使用命令行来创建json,并添加了依赖项.我发现大多数软件包都是在没有出现这种错误的情况下安装的,而且我的package.json也通过使用npm install grunt-contrib-watch --save-dev等方式得到了正确的更新.
在Windows上我看到这个错误仅针对两个包:对于grunt-contrib-uglify"以及grunt-contrib-handlebars.所以我的JSON文件结束于
{
"name": "Billing",
"version": "0.0.0",
"dependencies": {
"grunt": "~0.4.1",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-compass": "~0.5.0",
"grunt-contrib-cssmin": "~0.6.2",
"matchdep": "~0.1.2"
},
"devDependencies": {
"grunt-contrib-less": "~0.8.1"
}
}
Run Code Online (Sandbox Code Playgroud) Gruntfile的输出是一个空文件public/assets/app.js.Sass-part工作正常,但JS-part没有.
//Gruntfile
module.exports = function(grunt) {
//Initializing the configuration object
grunt.initConfig({
// Task configuration
sass: {
dist: {
options: {
style: 'compressed'
},
files: {
'./public/assets/app.css': './app/assets/scss/style.scss'
},
noCache: true
}
},
concat: {
options: {
separator: ';',
},
dist: {
src: [
'./app/bower/jquery/jquery.js',
'./app/assets/js/script.js'
],
dest: './public/assets/app.js',
},
},
uglify: {
options: {
mangle: false
},
dist: {
files: {
'./public/assets/app.js': './public/assets/app.js',
}
},
},
jshint: {
all: ['Gruntfile.js', './public/assets/app.js']
},
watch: {
js: {
files: [ …Run Code Online (Sandbox Code Playgroud) 我有以下目录结构:
--development
-js
-pages
--js
-pages
Run Code Online (Sandbox Code Playgroud)
我正在尝试将所有文件放在development/js/pages中并输出它们js/pages并使用grunt uglify缩小它们但仍保留单独的文件 - 我不想将所有文件组合在一起.我尝试过以下方法:
build: {
src: 'development/js/pages/*.js',
dest: 'js/page',
options: {
sourceMap: true,
compress: true,
mangle: true
}
}
Run Code Online (Sandbox Code Playgroud)
但这给了我一个错误:无法写入js/page文件
然后我尝试了这个,这是在uglify文档上:
build : {
files: [{
cwd: 'development/js/pages',
src: '*.js',
dest: 'js/page'
}],
options : {
sourceMap: true,
compress: true,
mangle: true
}
}
Run Code Online (Sandbox Code Playgroud)
但这只是给我这个开发/ js/pages中的每个文件的错误:
源文件"filename.js"未找到
然后另一个错误说Destination(js/page)没有写,因为源文件是空的.
有什么想法吗?
我习惯于grunt build成功完成任务,但由于我将项目编辑与其他一些开发人员同事合并,它突然失败并出现了我以前从未见过的错误:
grunt build
Loading "imagemin.js" tasks...ERROR
>> Error: Cannot find module 'process-nextick-args'
Loading "cdnify.js" tasks...ERROR
>> Error: Cannot find module 'abbrev'
Loading "grunt-karma.js" tasks...ERROR
>> Error: Cannot find module 'uglify-js'
Running "clean:dist" (clean) task
>> 0 paths cleaned.
Running "wiredep:app" (wiredep) task
Warning: Cannot find module './lang/clone' Use --force to continue.
Aborted due to warnings.
Execution Time (2015-09-25 09:44:30 UTC)
wiredep:app 31ms
Total 31ms
Run Code Online (Sandbox Code Playgroud)
在Gruntfile.js中,我评论imagemin并cdnify输出concurrent:dist和build任务.
我做错了什么?
这里有一些关于我正在使用的东西版本的信息:
node -v …Run Code Online (Sandbox Code Playgroud) 我想在我的所有项目的角度文件上通过grunt运行ng-annotate.
找不到此选项.
我只通过指定文件名来选择注释.
ngAnnotate: {
dist: {
files: {
'output_file_name.js': ['input_file_name'],
},
}
}
Run Code Online (Sandbox Code Playgroud)
我需要一些更通用的东西,它会在特定目录上递归运行.
我正在运行 grunt-contrib-uglify v 2.15.1。当我使用 lambda 或箭头函数时失败:
Running "uglify:admin" (uglify) task
{ message: 'SyntaxError: Unexpected token: operator (>)',
filename: 'admin-app-ng.js',
line: 4927,
col: 50,
pos: 216049,
stack: 'Error\n at new JS_Parse_Error (eval at <anonymous>
Run Code Online (Sandbox Code Playgroud)
如果我删除 lambda,那么 uglify 函数。在使用 lambdas/箭头函数时有没有办法使用 uglify?
这是箭头函数:
myService.getActions($scope.myVar, (type) => (type === 'myType')?true:false);
Run Code Online (Sandbox Code Playgroud) gruntjs ×9
angularjs ×2
bower ×1
build ×1
css ×1
dependencies ×1
grunt-usemin ×1
javascript ×1
lambda ×1
ng-annotate ×1
node.js ×1
npm ×1
source-maps ×1
windows ×1