gulpTasks-viewerLibraries.js 4.0 KB

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