我是gulp的新手,我遇到了一个奇怪的问题。有时,在通过gulp准备我的网站后,当我的javascript文件的最小化版本被网站使用时,无法找到javascript文件中定义的实体。或者我会因为无法读取未定义实体的属性而得到奇怪的错误。
其他时候网站运行正常。我还没有看到任何失败的时刻以及什么时候失败的模式。
我还注意到,从uglification过程中排除javascript文件会导致类似的错误出现在以前在uglification之后没有显示任何问题的文件中。
这是我的gulp档案:
/// <binding Clean='clean' />
"use strict";
var gulp = require( "gulp" );
var rimraf = require( "rimraf" );
var concat = require( "gulp-concat" );
var cssmin = require( "gulp-cssmin" );
var uglify = require( "gulp-uglify" );
var mainBowerFiles = require( 'main-bower-files' );
var filter = require( "gulp-filter" );
var less = require( 'gulp-less' );
var path = require( 'path' );
var coffee = require( 'gulp-coffee' );
var rename = require( 'gulp-rename' );
var sequence = require('gulp-sequence');
// paths should generally be simple folder links
// wildcard and glob specifications should be added to
// these as needed by a particular situation
var paths = {
webroot: "./wwwroot/"
};
paths.js = path.join(paths.webroot, 'js');
paths.css = path.join(paths.webroot, 'css');
paths.lib = path.join( paths.webroot, "lib" );
paths.repos = './repos/';
var chartPaths = [
path.join( paths.js, 'chart/**/*.js' ),
path.join( paths.js, 'chart-support/**/*.js' ),
path.join( paths.js, 'data/**/*.js' ),
path.join( paths.js, 'layout/**/*.js' ),
path.join( paths.lib, 'ngmap/build/scripts/ng-map.js' ),
path.join( paths.lib, 'oms/lib/*.js' ),
];
var repoPaths = [
path.join( paths.repos, 'oms', 'lib/*.js' ),
];
// primary tasks
//
// these are roughly in the sequence which should be followed when
// prepping the project. In particular, minimization should not
// be run until after all bower and subrepository management
// has been done
gulp.task( 'all', sequence( 'clean:lib', 'bower', 'repos', 'clean', 'min' ) );
gulp.task( "bower", sequence("bower:js-css", "bower:less") );
gulp.task( "repos", sequence("repos:coffee", "repos:js") );
gulp.task( "clean", sequence("clean:js", "clean:css") );
gulp.task( "min", sequence("min:chart-js", "min:css") );
// cleaning tasks
//
// delete all minimized javascript files in the js folder of wwwroot
gulp.task( "clean:js", function ( cb ) {
rimraf( path.join( paths.js, '**/*.min.js' ), cb );
});
// delete all minimized css files in the css folder of wwwroot
gulp.task( "clean:css", function ( cb ) {
rimraf( path.join( paths.css, '**/*.min.js' ), cb );
});
// delete lib folder of wwwroot
gulp.task( "clean:lib", function ( cb ) {
rimraf( paths.lib, cb );
} );
// bower tasks
//
// move bower-maintained javascript files into js folder of wwwroot
gulp.task( "bower:js-css", function () {
var f = filter( ['**/*.js', '**/*.css'] );
return gulp.src( mainBowerFiles(), { base: "./bower_components" } )
.pipe( f )
.pipe( gulp.dest( paths.lib ) );
} );
// move bower-maintained css files into css folder of wwwroot
gulp.task( 'bower:less', function () {
return gulp.src( './less/*.less' )
.pipe( less( {
paths: [
'./bower_components/bootstrap/less/'
]
}) )
.pipe( gulp.dest( paths.css ) );
} );
// subrepository compilation tasks
//
// compile coffee scripts into js files in the repos directory
gulp.task( "repos:coffee", function () {
return gulp.src( path.join( paths.repos, '**/*.coffee' ), { base: paths.repos } )
.pipe( coffee() )
.pipe( gulp.dest( function ( file ) {
return file.base;
} ) );
} );
// move repo javascript files into js folder of wwwroot
gulp.task( "repos:js", function () {
return gulp.src( repoPaths, { base: './repos' } )
.pipe( gulp.dest( paths.lib ) );
} );
// minimizing tasks
gulp.task( "min:chart-js", function () {
return gulp.src( chartPaths )
.pipe( concat( path.join(paths.js, 'chart.min.js' ) ) )
.pipe( uglify() )
.pipe( gulp.dest( "." ) );
} );
gulp.task( "min:css", function () {
return gulp.src( [path.join( paths.css, '**/*.css' ), "!" + path.join( paths.css, '**/*.min.css' )], { base: paths.css } )
//.pipe( concat( path.join(paths.css, 'site.min.css') ) )
.pipe( cssmin() )
.pipe( rename( function ( path ) {
path.basename += '.min';
return path;
}) )
.pipe( gulp.dest(paths.css) );
} );
由于没有任何文件都那么大,如果我不得不放弃丑化,它将成为一个巨大的问题。但在我参与更大的项目之前,我想弄明白我做错了什么。