mirror of
https://github.com/protobufjs/protobuf.js.git
synced 2025-12-08 20:58:55 +00:00
88 lines
2.7 KiB
JavaScript
88 lines
2.7 KiB
JavaScript
module.exports = bundle;
|
|
|
|
var browserify = require("browserify");
|
|
|
|
var header = require("gulp-header");
|
|
var gulpif = require("gulp-if");
|
|
var sourcemaps = require("gulp-sourcemaps");
|
|
var uglify = require("gulp-uglify");
|
|
var gutil = require("gulp-util");
|
|
|
|
var buffer = require("vinyl-buffer");
|
|
var vinylfs = require("vinyl-fs");
|
|
var source = require("vinyl-source-stream");
|
|
|
|
var pkg = require(__dirname + "/../package.json");
|
|
var license = [
|
|
"/*!",
|
|
" * protobuf.js v${version} (c) 2016, Daniel Wirtz",
|
|
" * Compiled ${date}",
|
|
" * Licensed under the BSD-3-Clause License",
|
|
" * see: https://github.com/dcodeIO/protobuf.js for details",
|
|
" */"
|
|
].join("\n") + "\n";
|
|
|
|
/**
|
|
* Bundles the library.
|
|
* @param {Object} options Bundler options
|
|
* @param {string} options.entry Entry file
|
|
* @param {string} options.target Target directory
|
|
* @param {boolean} [options.compress=false] Whether to minify or not
|
|
* @param {string[]} [options.exclude] Excluded source files
|
|
*/
|
|
function bundle(options) {
|
|
if (!options || !options.entry || !options.target)
|
|
throw TypeError("missing options");
|
|
var bundler = browserify({
|
|
entries: options.entry,
|
|
debug: true
|
|
})
|
|
bundler
|
|
.external("long")
|
|
.exclude("process")
|
|
.exclude("_process");
|
|
if (options.exclude)
|
|
options.exclude.forEach(bundler.exclude, bundler);
|
|
return bundler
|
|
.plugin(require("bundle-collapser/plugin"))
|
|
.bundle()
|
|
.pipe(source(options.compress ? "protobuf.min.js" : "protobuf.js"))
|
|
.pipe(buffer())
|
|
.pipe(sourcemaps.init({ loadMaps: true }))
|
|
.pipe(
|
|
gulpif(options.compress, uglify({
|
|
mangleProperties: {
|
|
regex: /^_/
|
|
}
|
|
}))
|
|
)
|
|
.pipe(header(license, {
|
|
date: (new Date()).toUTCString().replace("GMT", "UTC"),
|
|
version: pkg.version
|
|
}))
|
|
.pipe(sourcemaps.write(".", { sourceRoot: "" }))
|
|
.pipe(vinylfs.dest(options.target))
|
|
.on("log", gutil.log)
|
|
.on("error", gutil.log);
|
|
}
|
|
|
|
var fs = require("fs");
|
|
var zopfli = require("node-zopfli");
|
|
|
|
/**
|
|
* Compresses a file using zopfli gzip.
|
|
* @param {string} sourceFile Source file
|
|
* @param {string} destinationFile Destination file
|
|
* @param {function(?Error)} callback Node-style callback
|
|
*/
|
|
bundle.compress = function compress(sourceFile, destinationFile, callback) {
|
|
var src = fs.createReadStream(sourceFile);
|
|
var dst = fs.createWriteStream(destinationFile);
|
|
src.on("error", callback);
|
|
dst.on("error", callback);
|
|
dst.on("close", function() {
|
|
callback(null);
|
|
});
|
|
src.pipe(zopfli.createGzip()).pipe(dst);
|
|
};
|