gulp将多个JS合并为一个

时间:2019-01-09 16:32:32

标签: javascript merge gulp uglifyjs

我有一个任务gulp dev,应该将JS文件数组合并到dist / js中,但是我似乎无法得到gulp命令来创建文件dist / js,有人可以看到我去了哪里现在9个小时错了。 谢谢

原始文件

gulp.task("dev", function () {

    // set the dev config (cache in utils.js)
    utils.setConfig({
        env: "dev",
        watch: true,
        notify: true,
        tasks: ["js", "css", "copy", "bower", "svg-sprite"]
    });

    // build with this config
    utils.build();

});

然后...

任务/ js

    var gulp           = require("gulp"),
    utils          = require("./utils"),
    config         = utils.loadConfig(),
    gulpif         = require("gulp-if"),
    fs             = require("fs"),
    uglify         = require("gulp-uglify"),
    sourcemaps     = require("gulp-sourcemaps"),
    browserify     = require("browserify"),
    shim           = require("browserify-shim"),
    through2       = require("through2"),
    babelify       = require("babelify"),
    minify         = require('gulp-minify'),
    replaceName    = require('gulp-replace-name');


// dev/default settings
utils.setTaskConfig("js", {

    default: {

        // Pass array instead of single file!
        src: [
            config.root + "/js/index.js",
            config.root + "/js/owlCarousel.js",
            config.root + "/js/search/search.js",

            // Angular 1.x doesn't play well with CommonJS modules :(
            config.root + "/js/search/angular-1.5.1.min.js",
            config.root + "/js/search/angular-animate-1.5.1.min.js",
            config.root + "/js/search/angular-sanitize-1.5.1.min.js"
        ],

        dest: config.dest + "/js",

        // js uglify options, to skip, set value to false or omit entirely
        // otherwise, pass options object (can be empty {})
        uglify: false,

        // browserify options
        browserify: {
            debug: true // enable sourcemaps
        }
    },

    prod: {

        browserify: {},

        // uglify javascript for production
        uglify: {}
    }
});


// register the watch
utils.registerWatcher("js", [
    config.root + "/js/**/*.js",
    config.root + "/js/**/*.jsx"
]);



/* compile application javascript */
gulp.task("js", function(){

    var js = utils.loadTaskConfig("js");

    // for browserify usage, see https://medium.com/@sogko/gulp-browserify-the-gulp-y-way-bb359b3f9623
    // ^^ we can't use vinyl-transform anymore because it breaks when trying to use b.transform()
    // https://github.com/sogko/gulp-recipes/tree/master/browserify-vanilla
    var browserifyIt = through2.obj(function (file, enc, callback){

        // https://github.com/substack/node-browserify/issues/1044#issuecomment-72384131
        var b = browserify(js.browserify || {}) // pass options
            .add(file.path) // this file
            .transform(babelify)
            .transform(shim);

        b.bundle(function(err, res){
            if (err){
                callback(err, null); // emit error so drano can do it's thang
            }
            else {
                file.contents = res; // assumes file.contents is a Buffer
                callback(null, file); // pass file along
            }
        });

    });

    return gulp.src(js.src)
        .pipe(utils.drano())
        .pipe(browserifyIt)
        .pipe(sourcemaps.init({ loadMaps: true })) // loads map from browserify file
        .pipe(gulpif((js.uglify), uglify(js.uglify)))
        .pipe(minify(({
            ignoreFiles: ['*.min.js', 'search.js']
        })))
        .pipe(sourcemaps.write("./"))
        .pipe(gulp.dest(js.dest));
});
gulp.src([
    './dist/js/*.min.js',
    './dist/js/*-min.js',
    './dist/js/amcharts.js',
    './dist/js/amstock.js',
    './dist/js/table-childssorter.js',
    './dist/js/serial.js',
    './dist/js/vendor.js',
    './dist/js/jquery-3.1.1.js',
    './dist/js/jquery.tablesorter.js',
    './dist/js/search.js'
])
.pipe(replaceName(/\-min.js/g, '.js'))
.pipe(gulp.dest('./dist/minified/js'));

0 个答案:

没有答案