gulpTasks-viewerLibraries.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.pipe(gulp.dest(outputDirectory));
  58. if (dest.addBabylonDeclaration) {
  59. // include the babylon declaration
  60. if (dest.addBabylonDeclaration === true) {
  61. dest.addBabylonDeclaration = [config.build.declarationFilename];
  62. }
  63. var decsToAdd = dest.addBabylonDeclaration.map(function(dec) {
  64. return config.build.outputDirectory + '/' + dec;
  65. });
  66. sequence.unshift(gulp.src(decsToAdd)
  67. .pipe(rename(function(path) {
  68. path.dirname = '';
  69. }))
  70. .pipe(gulp.dest(outputDirectory)))
  71. }
  72. }
  73. out.destinations.forEach(dest => {
  74. processDestination(dest);
  75. });
  76. sequence.push(build);
  77. });
  78. return merge2(sequence);
  79. }
  80. /**
  81. * Dynamic viewer module creation In Serie for WebPack leaks.
  82. */
  83. function buildViewerLibraries(settings) {
  84. var tasks = settings.libraries.map(function(library) {
  85. var build = function(cb) {
  86. return buildViewerLibrary(library, settings);
  87. }
  88. return build;
  89. });
  90. return gulp.series.apply(this, tasks);
  91. }
  92. /**
  93. * Dynamic viewer module creation.
  94. */
  95. config.viewerModules.map(function(module) {
  96. var settings = config[module];
  97. // Build the libraries.
  98. gulp.task(module, buildViewerLibraries(settings));
  99. });