gulpTasks-viewerLibraries.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. const ViewerResolve = require('../../WebpackPlugins/viewerResolve');
  13. // Import Build Config
  14. var config = require("../../Config/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. wpConfig.resolve.plugins = [new ViewerResolve(["babylonjs", "babylonjs-loaders"])];
  27. let wpBuild = webpackStream(wpConfig, require("webpack"));
  28. //shoud dtsBundle create the declaration?
  29. if (settings.build.dtsBundle) {
  30. let event = wpBuild
  31. .pipe(through.obj(function(file, enc, cb) {
  32. // only declaration files
  33. const isdts = /\.d\.ts$/.test(file.path);
  34. if (isdts) this.push(file);
  35. cb();
  36. }))
  37. .pipe(gulp.dest(outputDirectory));
  38. // dts-bundle does NOT support (gulp) streams, so files have to be saved and reloaded,
  39. // until I fix it
  40. event.on("end", function() {
  41. // create the file
  42. dtsBundle.bundle(settings.build.dtsBundle);
  43. // process the declaration
  44. let fileLocation = path.join(path.dirname(settings.build.dtsBundle.main), settings.build.dtsBundle.out);
  45. processDeclaration(fileLocation, settings.build.umd.packageName, settings.build.umd.processDeclaration);
  46. });
  47. }
  48. let build = wpBuild
  49. .pipe(through.obj(function(file, enc, cb) {
  50. // only pipe js files
  51. const isJs = /\.js$/.test(file.path);
  52. if (isJs) this.push(file);
  53. cb();
  54. }))
  55. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: false, externalUsingBabylon: true, noBabylonInit: true }));
  56. function processDestination(dest) {
  57. var outputDirectory = config.build.outputDirectory + dest.outputDirectory;
  58. build = build
  59. .pipe(rename(dest.filename))
  60. .pipe(gulp.dest(outputDirectory));
  61. if (dest.addBabylonDeclaration) {
  62. // include the babylon declaration
  63. if (dest.addBabylonDeclaration === true) {
  64. dest.addBabylonDeclaration = ["babylon.module.d.ts"];
  65. }
  66. var decsToAdd = dest.addBabylonDeclaration.map(function(dec) {
  67. return config.build.outputDirectory + '/' + dec;
  68. });
  69. sequence.unshift(gulp.src(decsToAdd)
  70. .pipe(rename(function(path) {
  71. path.dirname = '';
  72. }))
  73. .pipe(gulp.dest(outputDirectory)))
  74. }
  75. }
  76. out.destinations.forEach(dest => {
  77. processDestination(dest);
  78. });
  79. sequence.push(build);
  80. });
  81. return merge2(sequence);
  82. }
  83. /**
  84. * Dynamic viewer module creation In Serie for WebPack leaks.
  85. */
  86. function buildViewerLibraries(settings) {
  87. var tasks = settings.libraries.map(function(library) {
  88. var build = function(cb) {
  89. return buildViewerLibrary(library, settings);
  90. }
  91. return build;
  92. });
  93. return gulp.series.apply(this, tasks);
  94. }
  95. /**
  96. * Dynamic viewer module creation.
  97. */
  98. config.viewerModules.map(function(module) {
  99. var settings = config[module];
  100. // Build the libraries.
  101. gulp.task(module, buildViewerLibraries(settings));
  102. });