用gulp生成源地图

时间:2019-05-15 18:05:35

标签: gulp

我刚刚开始使用gulp捆绑并缩小我们的js和css文件。我在下面设置了gulpfile.js来使用我们的bundleconfig.json,它对于捆绑和缩小js文件非常有用,但我也想使其处理生成源地图。我浏览了sourcemap插件文档,但运气不佳。我将如何修改gulpfile.js以处理此问题?我还提供了bundleconfig.json作为参考。

gulpfile.js:

"use strict";

var gulp = require("gulp"),
    concat = require("gulp-concat"),
    cssmin = require("gulp-cssmin"),
    uglify = require("gulp-uglify"),
    merge = require("merge-stream"),
    del = require("del"),
    bundleconfig = require("./bundleconfig.json");

var regex = {
    css: /\.css$/,
    html: /\.(html|htm)$/,
    js: /\.js$/
};

gulp.task("min:js", function () {
    var tasks = getBundles(regex.js).map(function (bundle) {
        return gulp.src(bundle.inputFiles, { base: "." })
            .pipe(concat(bundle.outputFileName))
            .pipe(uglify())
            .pipe(gulp.dest("."));
    });
    return merge(tasks);
});

gulp.task("min:css", function () {
    var tasks = getBundles(regex.css).map(function (bundle) {
        return gulp.src(bundle.inputFiles, { base: "." })
            .pipe(concat(bundle.outputFileName))
            .pipe(cssmin())
            .pipe(gulp.dest("."));
    });
    return merge(tasks);
});


gulp.task("clean", function () {
    var files = bundleconfig.map(function (bundle) {
        return bundle.outputFileName;
    });

    return del(files);
});

gulp.task("watch", function () {
    getBundles(regex.js).forEach(function (bundle) {
        gulp.watch(bundle.inputFiles, ["min:js"]);
    });

    getBundles(regex.css).forEach(function (bundle) {
        gulp.watch(bundle.inputFiles, ["min:css"]);
    });
});

gulp.task("min", gulp.parallel("min:js", "min:css"));

function getBundles(regexPattern) {
    return bundleconfig.filter(function (bundle) {
        return regexPattern.test(bundle.outputFileName);
    });
}

bundleconfig.json:

[  
  {
    "outputFileName": "Scripts/vendor.min.js",
    "inputFiles": [
      "jsPackages/jquery/jquery.js",
      "jsPackages/jquery-validate/jquery.validate.js",
      "jsPackages/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js",
      "jsPackages/moment.js/moment.js",
      "jsPackages/chart.js/dist/Chart.js",
      "jsPackages/chartjs-plugin-annotation/chartjs-plugin-annotation.js",
      "jsPackages/chartjs-plugin-labels/src/chartjs-plugin-labels.js",
      "jsPackages/chart.js/dist/Chart.js",
      "jsPackages/leaflet/leaflet.js"
    ]
  }
]

1 个答案:

答案 0 :(得分:0)

经过很多辛苦工作后,我发现了这是我为遇到类似问题的人提供的可行解决方案:

"use strict";

var gulp = require("gulp"),
    concat = require("gulp-concat"),
    cssmin = require("gulp-cssmin"),
    uglify = require("gulp-uglify"),
    merge = require("merge-stream"),
    sourcemaps = require('gulp-sourcemaps'),
    del = require("del"),
    bundleconfig = require("./bundleconfig.json"),
    replace = require("gulp-replace"),
    babel = require("gulp-babel");

var regex = {
    css: /\.css$/,
    html: /\.(html|htm)$/,
    js: /\.js$/
};

gulp.task("min:js", function () {
    var tasks = getBundles(regex.js).map(function (bundle) {
        return gulp.src(bundle.inputFiles, { base: "." })
            .pipe(sourcemaps.init({loadMaps: true}))
            .pipe(babel())
            .pipe(concat(bundle.outputFileName))
            .pipe(uglify())
            .pipe(sourcemaps.write())
            .pipe(gulp.dest("."));
    });
    return merge(tasks);
});

gulp.task('publish:webfonts', function () {
    return gulp.src('Scripts/jsPackages/font-awesome/webfonts/*')
        .pipe(gulp.dest('Content/webfonts/'));
});

gulp.task('clean:obj', function () {
    return del("obj/**", {force: true});
});

gulp.task("min:css", function () {
    var tasks = getBundles(regex.css).map(function (bundle) {
        return gulp.src(bundle.inputFiles, { base: "." })
            .pipe(concat(bundle.outputFileName))
            .pipe(cssmin())
            .pipe(gulp.dest("."));
    });
    return merge(tasks);
});

gulp.task("clean:bundles", function () {
    var files = bundleconfig.map(function (bundle) {
        return bundle.outputFileName;
    });

    return del(files);
});

gulp.task("watch", function () {
    getBundles(regex.js).forEach(function (bundle) {
        gulp.watch(bundle.inputFiles, ["min:js"]);
    });

    getBundles(regex.css).forEach(function (bundle) {
        gulp.watch(bundle.inputFiles, ["min:css"]);
    });
});

gulp.task("min", gulp.parallel("min:js", "publish:webfonts", "min:css"));
gulp.task("clean", gulp.parallel("clean:bundles", "clean:obj"));

function getBundles(regexPattern) {
    return bundleconfig.filter(function (bundle) {
        return regexPattern.test(bundle.outputFileName);
    });
}
相关问题