building.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var gulp = require('gulp');
  2. var header = require('gulp-header');
  3. var clean = require('gulp-clean');
  4. var gulpWebpack = require('webpack-stream');
  5. var webpack = require('webpack');
  6. var babel = require("gulp-babel");
  7. var runSequence = require('gulp4-run-sequence');
  8. var fs = require('fs');
  9. var settings = require('./settings.json');
  10. var shared = require('./shared.js');
  11. gulp.task("clean", gulp.series(function () {
  12. return gulp.src(["bin/", "dist/"], { read: false })
  13. .pipe(clean());
  14. }));
  15. gulp.task("babel", gulp.series(function () {
  16. return babelFunc();
  17. }));
  18. function babelFunc() {
  19. return gulp.src("src/**/*")
  20. .pipe(babel({
  21. presets: ['es2015', 'stage-3']
  22. }))
  23. .pipe(gulp.dest("bin/"));
  24. }
  25. gulp.task("webpack", gulp.series(["babel"], function () {
  26. return webpackFunc();
  27. }));
  28. function webpackFunc() {
  29. return gulp.src('bin/JsBarcode.js')
  30. .pipe(gulpWebpack(
  31. {
  32. mode: "none",
  33. output: {
  34. filename: 'JsBarcode.all.js'
  35. }
  36. }
  37. , webpack))
  38. .pipe(gulp.dest("dist/"));
  39. }
  40. gulp.task("webpack-min", gulp.series(["babel"], function () {
  41. return webpackMin('all');
  42. }));
  43. function webpackMin(name, dest) {
  44. dest = dest || './';
  45. return gulp.src('bin/JsBarcode.js')
  46. .pipe(gulpWebpack(
  47. {
  48. mode: "production",
  49. output: {
  50. filename: shared.minifiedFilename(name)
  51. },
  52. }
  53. , webpack))
  54. .pipe(header(settings.banner, require(settings.baseDir + 'package.json')))
  55. .pipe(gulp.dest("dist/" + dest));
  56. }
  57. gulp.task("webpack-all", gulp.series(function (cb) {
  58. var barcodes = require('./barcode-building.json');
  59. // Move the real barcodes/index.js out of the way while compiling the individual barcodes
  60. fs.renameSync("src/barcodes/index.js", "src/barcodes/index.tmp.js");
  61. // Take a barcode from the barcodes array, call the functions to compile that
  62. // format and have a callback when it has finished.
  63. function loopBarcode(i) {
  64. if (i < barcodes.length) {
  65. createBarcodeInclude(barcodes[i], function () {
  66. loopBarcode(i + 1);
  67. });
  68. }
  69. else {
  70. fs.renameSync("src/barcodes/index.tmp.js", "src/barcodes/index.js");
  71. cb(); // Done
  72. }
  73. }
  74. loopBarcode(0);
  75. }));
  76. // Takes information about a barcode formatSize
  77. // Modifies the barcodes/index.js file to only import the specified format
  78. // and then does a recompilation and minifies everything with webpack
  79. function createBarcodeInclude(barcode, callback) {
  80. var toFile = "";
  81. toFile += "import {" + barcode.names + "} from '" + barcode.barcodeFile + "'";
  82. toFile += "\n";
  83. toFile += "export default {" + barcode.names + "}";
  84. // Write a new barcodes/index file that only includes the specified barcode
  85. fs.writeFile("src/barcodes/index.js", toFile, function () {
  86. // Remove the compiled barcodes/index file (if it exist)
  87. if (fs.existsSync("bin/barcodes/index.js")) {
  88. fs.unlinkSync("bin/barcodes/index.js");
  89. }
  90. // Re-compile with babel and webpack everything
  91. babelFunc().on('end', function () {
  92. webpackMin(barcode.name, 'barcodes/').on('end', callback);
  93. });
  94. });
  95. }
  96. gulp.task('compress', gulp.series(function (cb) {
  97. runSequence(
  98. "clean",
  99. "webpack-all",
  100. "webpack",
  101. "webpack-min",
  102. cb
  103. );
  104. }));
  105. gulp.task('compile', gulp.series(['babel']));
  106. gulp.task('compile-web', gulp.series(['webpack']));