gulpTasks-viewerLibraries.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Gulp Tools
  2. var gulp = require("gulp");
  3. var webpackStream = require("webpack-stream");
  4. var fs = require("fs");
  5. var dtsBundle = require('dts-bundle');
  6. var merge2 = require("merge2");
  7. var through = require('through2');
  8. var path = require("path");
  9. var rename = require("gulp-rename");
  10. // Gulp Helpers
  11. var processDeclaration = require('../helpers/gulp-processTypescriptDeclaration');
  12. var addModuleExports = require("../helpers/gulp-addModuleExports");
  13. // Import Build Config
  14. var config = require("../config.json");
  15. /**
  16. * Build the viewer
  17. */
  18. var buildViewerLibrary = function(library, settings) {
  19. const sequence = [];
  20. var outputDirectory = config.build.outputDirectory + settings.build.distOutputDirectory;
  21. settings.build.outputs.forEach(out => {
  22. let wpConfig = require(settings.build.webpack);
  23. if (!out.minified) {
  24. wpConfig.mode = "development";
  25. }
  26. let wpBuild = webpackStream(wpConfig, require("webpack"));
  27. //shoud dtsBundle create the declaration?
  28. if (settings.build.dtsBundle) {
  29. let event = wpBuild
  30. .pipe(through.obj(function(file, enc, cb) {
  31. // only declaration files
  32. const isdts = /\.d\.ts$/.test(file.path);
  33. if (isdts) this.push(file);
  34. cb();
  35. }))
  36. .pipe(gulp.dest(outputDirectory));
  37. // dts-bundle does NOT support (gulp) streams, so files have to be saved and reloaded,
  38. // until I fix it
  39. event.on("end", function() {
  40. // create the file
  41. dtsBundle.bundle(settings.build.dtsBundle);
  42. // process the declaration
  43. let fileLocation = path.join(path.dirname(settings.build.dtsBundle.main), settings.build.dtsBundle.out);
  44. processDeclaration(fileLocation, settings.build.processDeclaration);
  45. });
  46. }
  47. let build = wpBuild
  48. .pipe(through.obj(function(file, enc, cb) {
  49. // only pipe js files
  50. const isJs = /\.js$/.test(file.path);
  51. if (isJs) this.push(file);
  52. cb();
  53. }))
  54. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: false, externalUsingBabylon: true, noBabylonInit: true }));
  55. function processDestination(dest) {
  56. var outputDirectory = config.build.outputDirectory + dest.outputDirectory;
  57. build = build
  58. .pipe(rename(dest.filename))
  59. .pipe(gulp.dest(outputDirectory));
  60. if (dest.addBabylonDeclaration) {
  61. // include the babylon declaration
  62. if (dest.addBabylonDeclaration === true) {
  63. dest.addBabylonDeclaration = [config.build.declarationFilename];
  64. }
  65. var decsToAdd = dest.addBabylonDeclaration.map(function(dec) {
  66. return config.build.outputDirectory + '/' + dec;
  67. });
  68. sequence.unshift(gulp.src(decsToAdd)
  69. .pipe(rename(function(path) {
  70. path.dirname = '';
  71. }))
  72. .pipe(gulp.dest(outputDirectory)))
  73. }
  74. }
  75. out.destinations.forEach(dest => {
  76. processDestination(dest);
  77. });
  78. sequence.push(build);
  79. });
  80. return merge2(sequence);
  81. }
  82. /**
  83. * Dynamic viewer module creation In Serie for WebPack leaks.
  84. */
  85. function buildViewerLibraries(settings) {
  86. var tasks = settings.libraries.map(function(library) {
  87. var build = function(cb) {
  88. return buildViewerLibrary(library, settings);
  89. }
  90. return build;
  91. });
  92. return gulp.series.apply(this, tasks);
  93. }
  94. /**
  95. * Dynamic viewer module creation.
  96. */
  97. config.viewerModules.map(function(module) {
  98. var settings = config[module];
  99. // Build the libraries.
  100. gulp.task(module, buildViewerLibraries(settings));
  101. });