我正在使用grunt-contrib concat和uglify模块来处理一些javascript.目前,如果src/js/为空,它们仍将创建一个(空)concat'd文件,以及缩小版本和源映射.
我想src/js/在继续之前检查文件夹是否为空,并且如果是,则任务应该跳过(不会失败).任何想法如何做到这一点?
运行uglify时,我需要忽略某些文件和文件夹,但仍然会递归扫描整个文件夹结构.
我有一个concat任务,将我的所有插件连接在一起,并使它们成为一个文件.我需要确保uglify忽略这些文件并且不对它们进行操作,因为我不希望它们在目标目录中,因为concat已经为我处理了这些文件.
我尝试将文件和文件夹添加到我的src数组中,使用前面的!,但它仍然可以对它们进行操作.
以下是我正在尝试使用的内容,但它不起作用:
uglify: {
options: {
banner: '/*! <%= grunt.template.today("mm-dd-yyyy h:MM:ss TT") %> */\n'
},
files: {
src: [
'!ie'
,'!polyfills'
,'!vendor'
,'!iecompat.js'
,'**/*.js'
],
dest: 'app/scripts',
cwd: 'sources/scripts',
ext: ".js",
flatten: false,
expand: true
}
},
Run Code Online (Sandbox Code Playgroud) 有没有办法将所有凉亭安装的库自动组合并缩小为1个文件?
首先,我尝试了最基本的方法:组合.js所有子目录中的所有文件:
uglify: {
options: {compress: true},
my_target: { files: {
'vendor.js': ['bower_components/**/*.js'],
} } }
Run Code Online (Sandbox Code Playgroud)
但这显然是一种糟糕的做法.由于错误太多,它也不起作用.
我手动删除了所有文件,并且只保留了每个库所拥有的1个(主)文件,并且它有效.
有没有办法自动完成这个?
另外,建议这样做吗?(即将所有供应商库合并为1个文件)
我有以下内容Gruntfile.js:
module.exports = function(grunt) {
var config = {
pkg: grunt.file.readJSON('package.json'),
/* Some other tasks... */
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
def: {
files: {
'out/src.js': 'out/src.min.js'
}
}
}
};
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('default', [/* <other-tasks>, */ 'uglify:def']);
};
Run Code Online (Sandbox Code Playgroud)
文件夹结构如下:
project
|
+-out (folder)
+-Gruntfile.js
Run Code Online (Sandbox Code Playgroud)
重要提示:我grunt从project文件夹中运行.
在运行时grunt,有前一个任务uglify:def是负责生成src.js成project/out.
当我运行时,grunt我可以看到src.js生成project/out,但当Grunt运行时,uglisy:def …
我一直在尝试安装Grunt.当我运行grunt时,我收到以下消息和警告列表:
Local Npm module "grunt-contrib-copy" not found. Is it installed?
Local Npm module "grunt-contrib-uglify" not found. Is it installed?
Local Npm module "grunt-contrib-jshint" not found. Is it installed?
Local Npm module "grunt-contrib-less" not found. Is it installed?
Local Npm module "grunt-contrib-clean" not found. Is it installed?
Local Npm module "grunt-contrib-watch" not found. Is it installed?
Local Npm module "grunt-contrib-concurrent" not found. Is it installed?
Local Npm module "grunt-contrib-nodemon" not found. Is it installed?
Local Npm module "grunt-contrib-newer" not found. Is …Run Code Online (Sandbox Code Playgroud) node.js npm grunt-contrib-watch grunt-contrib-copy grunt-contrib-uglify
我正在运行grunt uglify任务来缩小我的js文件但是当字符串以`字符开头时它给我错误.
我的字符串看起来像这样
var html = `<div class='list-item'>
<span>ABC</span>
</div>`;
Run Code Online (Sandbox Code Playgroud)
我的Gruntfile.js uglifyjs块是这样的
uglify: {
'dev': {
options: {
mangle: false
},
files: {
'common/utility_functions.js': ['common/utility_functions.js']
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行grunt uglify时,它给了我以下错误
>> Uglifying source common/utility_functions.js failed.
Warning: Uglification failed.
SyntaxError: Unexpected character '`'.
Line 162 in common/utility_functions.js
Use --force to continue.
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮忙解决这个问题吗?我认为使用`字符是有效的.它在jshint过程中没有给我错误,因为我在jshint选项中添加了enext:true.
迁移到El Capitan之后,用户似乎遇到了grunt安装的问题,可能与El Capitan的无根变化有关.特别是,运行grunt --force命令会导致EPERM错误.工作流程如下:
假设已经安装了npm,使用package.json和gruntfile.js导航到grunt目录并调用grunt:
grunt --force
Run Code Online (Sandbox Code Playgroud)
示例Gruntfile.js文件内容:
module.exports = function(grunt) {
// All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
// Configuration for concatenating files goes here.
dist: {
src: [
'../js/libs/owl.carousel.js',
'../js/libs/jquery.actual.js',
'../js/libs/chosen.jquery.js',
'../js/libs/jquery.parallax.js',
'../js/src/common.js'
],
dest: '../js/pro/global.js',
},
},
uglify: {
build: {
src: '../js/pro/global.js',
dest: '../js/pro/global.min.js',
},
},
imagemin: {
dynamic: {
files: [{
expand: true,
cwd: '../img/src/',
src: ['**/*.{png,jpg,gif}'],
dest: '../img/pro/'
}]
}
},
compass: {
dev: {
options: {
sassDir: '../sass',
cssDir: '../css', … node.js gruntjs grunt-contrib-concat grunt-contrib-watch grunt-contrib-uglify
我最近开始通过Yeoman使用GruntJS,我喜欢Javascript缩小的想法,但它在开发过程中遇到了困难.我试图在Gruntfile中以不同的组合禁用uglify,usemin等,但是一切似乎都依赖于另一件事并打破了这个过程.有没有简单的方法来禁用缩小?我使用Yeoman提供的最新版本的Grunt,我发现旧的解决方案与使用Yeoman的用户有不同的Gruntfile设置.
这是我的Gruntfile:
// 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: {
options: {
dest: '<%= config.dist %>'
},
html: '<%= config.app %>/index.html'
},
Run Code Online (Sandbox Code Playgroud)
所以我的项目安装了很多插件 bower
所有这些都在他们自己的自定义文件夹中,包含在bower_components文件夹中
现在我想运行一个grunt基本上需要所有这些和uglify它们的脚本.
所以我创建了以下脚本:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
copy: {
dist: {
files: [ {src: 'index.html', dest: 'dist/index.html'} ]
}
},
'useminPrepare': {
options: {
dest: 'dist'
},
html: 'index.html'
},
usemin: {
html: ['dist/index.html']
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-usemin');
grunt.registerTask('default', ['useminPrepare', 'copy', 'concat', 'uglify', 'usemin']);
};
Run Code Online (Sandbox Code Playgroud)
取自另一个SO问题
但是每当我运行这个时,我都会收到以下警告/错误:
没有找到"concat"目标.警告:任务"concat"失败.使用--force继续.因警告而中止.
谁能告诉我我做错了什么?
我的index.html样子如何:
<!DOCTYPE html>
<html lang="en" data-ng-app="app" ng-controller="AppCtrl">
<head>
<script src="js/newrelic.js"></script>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, …Run Code Online (Sandbox Code Playgroud) 我正在使用 Angular JS (1.6),我在 grunt 中有以下任务用于缩小角度模块
ngAnnotate: {
dist: {
files: [{
expand: true,
cwd: '.tmp/concat/scripts',
src: '*.js',
dest: '.tmp/concat/scripts'
}]
}
}
Run Code Online (Sandbox Code Playgroud)
但上述任务不支持 ES2015 和 ES2016 功能如何 在 iegrunt 文件中的grunt任务中添加babel-plugin-angularjs-annotate
angularjs bundling-and-minification gruntjs grunt-contrib-uglify gruntfile