【问题标题】:Gulp build failsGulp 构建失败
【发布时间】:2017-11-24 15:17:36
【问题描述】:

我正在尝试为我的项目添加 gulp 构建工具,该工具使用 angularjs。当我运行 gulp 构建工具时抛出错误

错误---插件“gulp-uglify”中的错误

F:\testcode - Copy\test\test\test.template.html: SyntaxError: Unexpected token: operator (

详情: 文件名:F:\testcode - 复制\test\test\test.template.html 行号:1

events.js:160 投掷者; // 未处理的“错误”事件 ^

这是我的 gulpfile.js 文件

 'use strict'
var gulp = require('gulp'),
    sass =  require('gulp-sass'),
    browserSync = require('browser-sync'),
    fs = require('fs'),
    del = require('del'),
    uglify = require('gulp-uglify'),
    usemin = require('gulp-usemin'),
    rev = require('gulp-rev'),
    htmlmin = require('gulp-htmlmin'),
    cleanCss = require('gulp-clean-css');

    gulp.task('clean',function () {
        return del(['dist'])
    });


    gulp.task('sass',function() {
        return gulp.src('./css/*.scss')
               .pipe(sass().on('error',sass.logError))
               .pipe(gulp.dest('./css'));
    });

    gulp.task('sass:watch',function(){
        gulp.watch('./css/*.scss',['sass'])
    });


    gulp.task('browser-sync',function(){
        var files = [
            './*.html',
            './static/css/**/*.css',
            './static/images/**/*.{png,jpg,gif}',
            './static/js/**/*.js'
        ];

        browserSync.init(files,{
            server:{
                baseDir:"./"
            }
        });
    });

    gulp.task('usemin', function() {
        return gulp.src('./static/js/**/*.html')
            .pipe(usemin({
                css: [ rev() ],
                html: [ htmlmin({ collapseWhitespace: true }) ],
                js: [ uglify(), rev() ],
                inlinejs: [ uglify() ],
                inlinecss: [ cleanCss(), 'concat' ]
            }))
            .pipe(uglify().on('error', function(e){
                console.log("Error---"+e);
            }))
            .pipe(gulp.dest('build/'));
    });

    gulp.task('default',['browser-sync'],function(){
        gulp.start('sass:watch');
    })

    gulp.task('build',['clean'],function () {
        gulp.start('usemin')
    })

【问题讨论】:

    标签: gulp gulp-uglify


    【解决方案1】:
    Follow the below steps :
    1) Install Nodejs 6.9.3
    2) Install gulp using "npm install -g gulp"
    3) create gulp file "gulpfile.js" in your project directory.
    4) Add the below code snippet : 
    a) Below task will concat(merge) all the included js file in ".SRC"  into one file
    
    
      
    
           var concat = require("gulp-concat"),
             var uglify = require('gulp-uglify'),
    
    gulp.task("js", function() {
         return gulp
           .src([
                  "./dev/libs/angular/angular.1.4.5.min.js",
                  "./dev/libs/angular/angular-animate.1.4.5.min.js"
             ])
             .pipe(concat("scripts.js"))
             .pipe(hash({
                     "format": "{name}_{size}.js"
               }))
             .pipe(gulp.dest("PATH_FOR_BUILD"));         
        });
    b) Below task will minify the concated file by removing spaces and comments to make the file in the compressed form :
       
    
         gulp.task('uglify', function() {
                return gulp.src("PATH_FOR_BUILD")
                    .pipe(uglify({ mangle: false }))
                    .pipe(gulp.dest("PATH_FOR_BUILD"));
            });
    Please check the below link for more details on deployment and build system: https://www.tutespace.com/2018/11/angular-js-build-deploy-using-gulp.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2021-11-11
      相关资源
      最近更新 更多