gulpfile.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. var gulp = require("gulp");
  2. var uglify = require("gulp-uglify");
  3. var typescript = require("gulp-typescript");
  4. var sourcemaps = require("gulp-sourcemaps");
  5. var srcToVariable = require("gulp-content-to-variable");
  6. var appendSrcToVariable = require("./gulp-appendSrcToVariable");
  7. var addDtsExport = require("./gulp-addDtsExport");
  8. var addModuleExports = require("./gulp-addModuleExports");
  9. var merge2 = require("merge2");
  10. var concat = require("gulp-concat");
  11. var rename = require("gulp-rename");
  12. var cleants = require('gulp-clean-ts-extends');
  13. var changedInPlace = require('gulp-changed-in-place');
  14. var runSequence = require('run-sequence');
  15. var replace = require("gulp-replace");
  16. var uncommentShader = require("./gulp-removeShaderComments");
  17. var expect = require('gulp-expect-file');
  18. var optimisejs = require('gulp-optimize-js');
  19. var webserver = require('gulp-webserver');
  20. var path = require('path');
  21. var config = require("./config.json");
  22. var debug = require('gulp-debug');
  23. var includeShadersStream;
  24. var shadersStream;
  25. var workersStream;
  26. var extendsSearchRegex = /var\s__extends[\s\S]+?\};/g;
  27. var decorateSearchRegex = /var\s__decorate[\s\S]+?\};/g;
  28. /**
  29. * TS configurations shared in the gulp file.
  30. */
  31. var tsConfig = {
  32. noExternalResolve: true,
  33. target: 'ES5',
  34. declarationFiles: true,
  35. typescript: require('typescript'),
  36. experimentalDecorators: true,
  37. isolatedModules: false
  38. };
  39. var tsProject = typescript.createProject(tsConfig);
  40. var externalTsConfig = {
  41. noExternalResolve: false,
  42. target: 'ES5',
  43. declarationFiles: true,
  44. typescript: require('typescript'),
  45. experimentalDecorators: true,
  46. isolatedModules: false
  47. };
  48. /*
  49. * Shader Management.
  50. */
  51. function shadersName(filename) {
  52. return filename.replace('.fragment', 'Pixel')
  53. .replace('.vertex', 'Vertex')
  54. .replace('.fx', 'Shader');
  55. }
  56. function includeShadersName(filename) {
  57. return filename.replace('.fx', '');
  58. }
  59. /*
  60. * Main necessary files stream Management.
  61. */
  62. gulp.task("includeShaders", function (cb) {
  63. includeShadersStream = config.includeShadersDirectories.map(function (shadersDef) {
  64. return gulp.src(shadersDef.files).
  65. pipe(expect.real({ errorOnFailure: true }, shadersDef.files)).
  66. pipe(uncommentShader()).
  67. pipe(srcToVariable({
  68. variableName: shadersDef.variable, asMap: true, namingCallback: includeShadersName
  69. }));
  70. });
  71. cb();
  72. });
  73. gulp.task("shaders", ["includeShaders"], function (cb) {
  74. shadersStream = config.shadersDirectories.map(function (shadersDef) {
  75. return gulp.src(shadersDef.files).
  76. pipe(expect.real({ errorOnFailure: true }, shadersDef.files)).
  77. pipe(uncommentShader()).
  78. pipe(srcToVariable({
  79. variableName: shadersDef.variable, asMap: true, namingCallback: shadersName
  80. }));
  81. });
  82. cb();
  83. });
  84. gulp.task("workers", function (cb) {
  85. workersStream = config.workers.map(function (workerDef) {
  86. return gulp.src(workerDef.files).
  87. pipe(expect.real({ errorOnFailure: true }, workerDef.files)).
  88. pipe(uglify()).
  89. pipe(srcToVariable({
  90. variableName: workerDef.variable
  91. }));
  92. });
  93. cb();
  94. });
  95. /*
  96. * Compiles all typescript files and creating a js and a declaration file.
  97. */
  98. gulp.task('typescript-compile', function () {
  99. var tsResult = gulp.src(config.core.typescript)
  100. .pipe(sourcemaps.init())
  101. .pipe(typescript(tsProject));
  102. //If this gulp task is running on travis, file the build!
  103. if (process.env.TRAVIS) {
  104. var error = false;
  105. tsResult.on('error', function () {
  106. error = true;
  107. }).on('end', function () {
  108. if (error) {
  109. console.log('Typescript compile failed');
  110. process.exit(1);
  111. }
  112. });
  113. }
  114. return merge2([
  115. tsResult.dts
  116. .pipe(concat(config.build.declarationFilename))
  117. //.pipe(addDtsExport("BABYLON"))
  118. .pipe(gulp.dest(config.build.outputDirectory)),
  119. tsResult.js
  120. .pipe(sourcemaps.write("./",
  121. {
  122. includeContent:false,
  123. sourceRoot: (filePath) => {
  124. var repeatCount = filePath.relative.split(path.sep).length - 1;
  125. return '../'.repeat(repeatCount);
  126. }
  127. }))
  128. .pipe(gulp.dest(config.build.srcOutputDirectory))
  129. ])
  130. });
  131. /**
  132. * External library Build (mat, post processes, ...).
  133. */
  134. gulp.task('materialsLibrary', function () {
  135. return buildExternalLibraries(config.materialsLibrary);
  136. });
  137. gulp.task('postProcessesLibrary', function () {
  138. return buildExternalLibraries(config.postProcessesLibrary);
  139. });
  140. gulp.task('proceduralTexturesLibrary', function () {
  141. return buildExternalLibraries(config.proceduralTexturesLibrary);
  142. });
  143. /**
  144. * Helper methods to build external library (mat, post processes, ...).
  145. */
  146. var buildExternalLibraries = function(settings) {
  147. var tasks = settings.libraries.map(function (library) {
  148. return buildExternalLibrary(library, settings);
  149. });
  150. return merge2(tasks);
  151. }
  152. var buildExternalLibrary= function(library, settings) {
  153. var compilOutput = gulp.src(library.file, { base: '../../' })
  154. .pipe(sourcemaps.init())
  155. .pipe(typescript(externalTsConfig));
  156. var js = compilOutput.js;
  157. var shader = gulp.src(library.shaderFiles)
  158. .pipe(uncommentShader())
  159. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", true, shadersName));
  160. var fulljs = merge2(js, shader)
  161. .pipe(concat(library.output));
  162. var unminifiedAndMaps = fulljs.pipe(sourcemaps.write('.temp', {
  163. includeContent:false,
  164. sourceRoot: function (file) {
  165. return '../';
  166. }
  167. }))
  168. .pipe(gulp.dest(settings.build.distOutputDirectory));
  169. var minified = fulljs
  170. .pipe(cleants())
  171. .pipe(replace(extendsSearchRegex, ""))
  172. .pipe(replace(decorateSearchRegex, ""))
  173. .pipe(rename({extname: ".min.js"}))
  174. .pipe(uglify())
  175. .pipe(optimisejs())
  176. .pipe(gulp.dest(settings.build.distOutputDirectory));
  177. return merge2(unminifiedAndMaps, minified);
  178. }
  179. /**
  180. * Build tasks to concat minify uflify optimise the BJS js in different flavor (workers...).
  181. */
  182. gulp.task("buildCore", ["shaders"], function () {
  183. return merge2(
  184. gulp.src(config.core.files).
  185. pipe(expect.real({ errorOnFailure: true }, config.core.files)),
  186. shadersStream,
  187. includeShadersStream
  188. )
  189. .pipe(concat(config.build.minCoreFilename))
  190. .pipe(cleants())
  191. .pipe(replace(extendsSearchRegex, ""))
  192. .pipe(replace(decorateSearchRegex, ""))
  193. .pipe(addModuleExports("BABYLON"))
  194. .pipe(uglify())
  195. .pipe(optimisejs())
  196. .pipe(gulp.dest(config.build.outputDirectory));
  197. });
  198. gulp.task("buildNoWorker", ["shaders"], function () {
  199. return merge2(
  200. gulp.src(config.core.files).
  201. pipe(expect.real({ errorOnFailure: true }, config.core.files)),
  202. gulp.src(config.extras.files).
  203. pipe(expect.real({ errorOnFailure: true }, config.extras.files)),
  204. shadersStream,
  205. includeShadersStream
  206. )
  207. .pipe(concat(config.build.minNoWorkerFilename))
  208. .pipe(cleants())
  209. .pipe(replace(extendsSearchRegex, ""))
  210. .pipe(replace(decorateSearchRegex, ""))
  211. .pipe(addModuleExports("BABYLON"))
  212. .pipe(uglify())
  213. .pipe(optimisejs())
  214. .pipe(gulp.dest(config.build.outputDirectory));
  215. });
  216. gulp.task("build", ["workers", "shaders"], function () {
  217. return merge2(
  218. gulp.src(config.core.files).
  219. pipe(expect.real({ errorOnFailure: true }, config.core.files)),
  220. gulp.src(config.extras.files).
  221. pipe(expect.real({ errorOnFailure: true }, config.extras.files)),
  222. shadersStream,
  223. includeShadersStream,
  224. workersStream
  225. )
  226. .pipe(concat(config.build.filename))
  227. .pipe(cleants())
  228. .pipe(replace(extendsSearchRegex, ""))
  229. .pipe(replace(decorateSearchRegex, ""))
  230. .pipe(addModuleExports("BABYLON"))
  231. .pipe(gulp.dest(config.build.outputDirectory))
  232. .pipe(rename(config.build.minFilename))
  233. .pipe(uglify())
  234. .pipe(optimisejs())
  235. .pipe(gulp.dest(config.build.outputDirectory));
  236. });
  237. /**
  238. * The default task, concat and min the main BJS files.
  239. */
  240. gulp.task('default', function (cb) {
  241. runSequence("buildNoWorker", "build", "buildCore", cb);
  242. });
  243. /**
  244. * Build the releasable files.
  245. */
  246. gulp.task("typescript", function (cb) {
  247. runSequence("typescript-compile", "default", cb);
  248. });
  249. gulp.task("typescript-libraries", ["materialsLibrary", "postProcessesLibrary", "proceduralTexturesLibrary"], function () {
  250. });
  251. gulp.task("typescript-all", function (cb) {
  252. runSequence("typescript", "typescript-libraries", cb);
  253. });
  254. /**
  255. * Watch ts files and fire repective tasks.
  256. */
  257. gulp.task('watch', ['typescript-compile'], function () {
  258. var tasks = [gulp.watch(config.core.typescript, ['typescript-compile'])];
  259. config.materialsLibrary.libraries.map(function (material) {
  260. tasks.push(gulp.watch(material.file, () => buildExternalLibrary(material, config.materialsLibrary)));
  261. tasks.push(gulp.watch(material.shaderFiles, () => buildExternalLibrary(material, config.materialsLibrary)));
  262. });
  263. config.postProcessesLibrary.libraries.map(function (postProcess) {
  264. tasks.push(gulp.watch(postProcess.file, buildExternalLibrary(postProcess, config.postProcessesLibrary)));
  265. tasks.push(gulp.watch(postProcess.shaderFiles, buildExternalLibrary(postProcess, config.postProcessesLibrary)));
  266. });
  267. config.proceduralTexturesLibrary.libraries.map(function (proceduralTexture) {
  268. tasks.push(gulp.watch(proceduralTexture.file, buildExternalLibrary(proceduralTexture, config.proceduralTexturesLibrary)));
  269. tasks.push(gulp.watch(proceduralTexture.shaderFiles, buildExternalLibrary(proceduralTexture, config.proceduralTexturesLibrary)));
  270. });
  271. return tasks;
  272. });
  273. /**
  274. * Embedded webserver for test convenience.
  275. */
  276. gulp.task('webserver', function () {
  277. gulp.src('../../.').pipe(webserver({
  278. port: 1338,
  279. livereload: false
  280.     }));
  281. });
  282. /**
  283. * Combine Webserver and Watch as long as vscode does not handle multi tasks.
  284. */
  285. gulp.task('run', ['watch', 'webserver'], function () {
  286. });