gulpfile.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 sass = require('gulp-sass');
  22. var webpack = require('webpack-stream');
  23. var zip = require('gulp-zip');
  24. var config = require("./config.json");
  25. var del = require('del');
  26. var debug = require('gulp-debug');
  27. var includeShadersStream;
  28. var shadersStream;
  29. var workersStream;
  30. var extendsSearchRegex = /var\s__extends[\s\S]+?\}\)\(\);/g;
  31. var decorateSearchRegex = /var\s__decorate[\s\S]+?\};/g;
  32. var referenceSearchRegex = /\/\/\/ <reference.*/g;
  33. /**
  34. * TS configurations shared in the gulp file.
  35. */
  36. var tsConfig = {
  37. noResolve: true,
  38. target: 'ES5',
  39. declarationFiles: true,
  40. typescript: require('typescript'),
  41. experimentalDecorators: true,
  42. isolatedModules: false
  43. };
  44. var tsProject = typescript.createProject(tsConfig);
  45. var externalTsConfig = {
  46. noResolve: false,
  47. target: 'ES5',
  48. declarationFiles: true,
  49. typescript: require('typescript'),
  50. experimentalDecorators: true,
  51. isolatedModules: false
  52. };
  53. function processDependency(kind, dependency, filesToLoad) {
  54. if (dependency.dependUpon) {
  55. for (var i = 0; i < dependency.dependUpon.length; i++) {
  56. var dependencyName = dependency.dependUpon[i];
  57. var parent = config.workloads[dependencyName];
  58. processDependency(kind, parent, filesToLoad);
  59. }
  60. }
  61. var content = dependency[kind];
  62. if (!content) {
  63. return;
  64. }
  65. for (var i = 0; i < content.length; i++) {
  66. var file = content[i];
  67. if (filesToLoad.indexOf(file) === -1) {
  68. filesToLoad.push(file);
  69. }
  70. }
  71. }
  72. function determineFilesToProcess(kind) {
  73. var currentConfig = config.build.currentConfig;
  74. var buildConfiguration = config.buildConfigurations[currentConfig];
  75. var filesToLoad = [];
  76. for (var index = 0; index < buildConfiguration.length; index++) {
  77. var dependencyName = buildConfiguration[index];
  78. var dependency = config.workloads[dependencyName];
  79. processDependency(kind, dependency, filesToLoad);
  80. }
  81. if (kind === "shaderIncludes") {
  82. for (var index = 0; index < filesToLoad.length; index++) {
  83. filesToLoad[index] = "../../src/Shaders/ShadersInclude/" + filesToLoad[index] + ".fx";
  84. }
  85. } else if (kind === "shaders") {
  86. for (var index = 0; index < filesToLoad.length; index++) {
  87. var name = filesToLoad[index];
  88. filesToLoad[index] = "../../src/Shaders/" + filesToLoad[index] + ".fx";
  89. }
  90. }
  91. return filesToLoad;
  92. }
  93. /*
  94. * Shader Management.
  95. */
  96. function shadersName(filename) {
  97. return path.basename(filename)
  98. .replace('.fragment', 'Pixel')
  99. .replace('.vertex', 'Vertex')
  100. .replace('.fx', 'Shader');
  101. }
  102. function includeShadersName(filename) {
  103. return filename.replace('.fx', '');
  104. }
  105. /*
  106. * Main necessary files stream Management.
  107. */
  108. gulp.task("includeShaders", function (cb) {
  109. var filesToProcess = determineFilesToProcess("shaderIncludes");
  110. includeShadersStream = gulp.src(filesToProcess).
  111. pipe(expect.real({ errorOnFailure: true }, filesToProcess)).
  112. pipe(uncommentShader()).
  113. pipe(srcToVariable({
  114. variableName: "BABYLON.Effect.IncludesShadersStore", asMap: true, namingCallback: includeShadersName
  115. }));
  116. cb();
  117. });
  118. gulp.task("shaders", ["includeShaders"], function (cb) {
  119. var filesToProcess = determineFilesToProcess("shaders");
  120. shadersStream = gulp.src(filesToProcess).
  121. pipe(expect.real({ errorOnFailure: true }, filesToProcess)).
  122. pipe(uncommentShader()).
  123. pipe(srcToVariable({
  124. variableName: "BABYLON.Effect.ShadersStore", asMap: true, namingCallback: shadersName
  125. }));
  126. cb();
  127. });
  128. gulp.task("workers", function (cb) {
  129. workersStream = config.workers.map(function (workerDef) {
  130. return gulp.src(workerDef.files).
  131. pipe(expect.real({ errorOnFailure: true }, workerDef.files)).
  132. pipe(uglify()).
  133. pipe(srcToVariable({
  134. variableName: workerDef.variable
  135. }));
  136. });
  137. cb();
  138. });
  139. /**
  140. * Build tasks to concat minify uflify optimise the BJS js in different flavor (workers...).
  141. */
  142. gulp.task("buildWorker", ["workers", "shaders"], function () {
  143. var filesToProcess = determineFilesToProcess("files");
  144. return merge2(
  145. gulp.src(filesToProcess).
  146. pipe(expect.real({ errorOnFailure: true }, filesToProcess)),
  147. shadersStream,
  148. includeShadersStream,
  149. workersStream
  150. )
  151. .pipe(concat(config.build.minWorkerFilename))
  152. .pipe(cleants())
  153. .pipe(replace(extendsSearchRegex, ""))
  154. .pipe(replace(decorateSearchRegex, ""))
  155. .pipe(addModuleExports("BABYLON"))
  156. .pipe(uglify())
  157. .pipe(optimisejs())
  158. .pipe(gulp.dest(config.build.outputDirectory));
  159. });
  160. gulp.task("build", ["shaders"], function () {
  161. var filesToProcess = determineFilesToProcess("files");
  162. return merge2(
  163. gulp.src(filesToProcess).
  164. pipe(expect.real({ errorOnFailure: true }, filesToProcess)),
  165. shadersStream,
  166. includeShadersStream
  167. )
  168. .pipe(concat(config.build.filename))
  169. .pipe(cleants())
  170. .pipe(replace(extendsSearchRegex, ""))
  171. .pipe(replace(decorateSearchRegex, ""))
  172. .pipe(addModuleExports("BABYLON"))
  173. .pipe(gulp.dest(config.build.outputDirectory))
  174. .pipe(rename(config.build.minFilename))
  175. .pipe(uglify())
  176. .pipe(optimisejs())
  177. .pipe(gulp.dest(config.build.outputDirectory));
  178. });
  179. /*
  180. * Compiles all typescript files and creating a js and a declaration file.
  181. */
  182. gulp.task('typescript-compile', function () {
  183. var tsResult = gulp.src(config.typescript)
  184. .pipe(sourcemaps.init())
  185. .pipe(tsProject());
  186. //If this gulp task is running on travis, file the build!
  187. if (process.env.TRAVIS) {
  188. var error = false;
  189. tsResult.on('error', function () {
  190. error = true;
  191. }).on('end', function () {
  192. if (error) {
  193. console.log('Typescript compile failed');
  194. process.exit(1);
  195. }
  196. });
  197. }
  198. return merge2([
  199. tsResult.dts
  200. .pipe(concat(config.build.declarationFilename))
  201. .pipe(gulp.dest(config.build.outputDirectory)),
  202. tsResult.dts
  203. .pipe(concat(config.build.declarationModuleFilename))
  204. .pipe(addDtsExport("BABYLON"))
  205. .pipe(gulp.dest(config.build.outputDirectory)),
  206. tsResult.js
  207. .pipe(sourcemaps.write("./",
  208. {
  209. includeContent: false,
  210. sourceRoot: (filePath) => {
  211. return '';
  212. }
  213. }))
  214. .pipe(gulp.dest(config.build.srcOutputDirectory))
  215. ])
  216. });
  217. /**
  218. * Helper methods to build external library (mat, post processes, ...).
  219. */
  220. var buildExternalLibraries = function (settings) {
  221. var tasks = settings.libraries.map(function (library) {
  222. return buildExternalLibrary(library, settings, false);
  223. });
  224. return merge2(tasks);
  225. }
  226. var buildExternalLibrary = function (library, settings, watch) {
  227. var tsProcess = gulp.src(library.files, { base: settings.build.srcOutputDirectory })
  228. .pipe(sourcemaps.init())
  229. .pipe(typescript(externalTsConfig));
  230. var shader = gulp.src(library.shaderFiles || [], { base: settings.build.srcOutputDirectory })
  231. .pipe(uncommentShader())
  232. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, library.output + '.fx'))
  233. .pipe(gulp.dest(settings.build.srcOutputDirectory));
  234. var dev = tsProcess.js.pipe(sourcemaps.write("./", {
  235. includeContent: false,
  236. sourceRoot: (filePath) => {
  237. return '';
  238. }
  239. }))
  240. .pipe(gulp.dest(settings.build.srcOutputDirectory));
  241. var outputDirectory = config.build.outputDirectory + settings.build.distOutputDirectory;
  242. var css = gulp.src(library.sassFiles || [])
  243. .pipe(sass().on('error', sass.logError))
  244. .pipe(concat(library.output.replace(".js", ".css")))
  245. .pipe(gulp.dest(outputDirectory));
  246. if (watch) {
  247. return merge2([shader, dev, css]);
  248. }
  249. else {
  250. var code = merge2([tsProcess.js, shader])
  251. .pipe(concat(library.output))
  252. .pipe(gulp.dest(outputDirectory))
  253. .pipe(cleants())
  254. .pipe(replace(extendsSearchRegex, ""))
  255. .pipe(replace(decorateSearchRegex, ""))
  256. .pipe(rename({ extname: ".min.js" }))
  257. .pipe(uglify())
  258. .pipe(optimisejs())
  259. .pipe(gulp.dest(outputDirectory));
  260. var dts = tsProcess.dts
  261. .pipe(concat(library.output))
  262. .pipe(replace(referenceSearchRegex, ""))
  263. .pipe(rename({ extname: ".d.ts" }))
  264. .pipe(gulp.dest(outputDirectory));
  265. var waitAll = merge2([dev, code, css, dts]);
  266. if (library.webpack) {
  267. return waitAll.on('end', function () {
  268. webpack(require(library.webpack))
  269. .pipe(rename(library.output.replace(".js", ".bundle.js")))
  270. .pipe(gulp.dest(outputDirectory))
  271. });
  272. }
  273. else {
  274. return waitAll;
  275. }
  276. }
  277. }
  278. /**
  279. * The default task, concat and min the main BJS files.
  280. */
  281. gulp.task("default", function (cb) {
  282. runSequence("buildWorker", "build", cb);
  283. });
  284. /**
  285. * Build the releasable files.
  286. */
  287. gulp.task("typescript", function (cb) {
  288. runSequence("typescript-compile", "default", cb);
  289. });
  290. /**
  291. * Dynamic module creation.
  292. */
  293. config.modules.map(function (module) {
  294. gulp.task(module, function () {
  295. return buildExternalLibraries(config[module]);
  296. });
  297. });
  298. gulp.task("typescript-libraries", config.modules, function () {
  299. });
  300. /**
  301. * Dynamic custom configurations.
  302. */
  303. config.buildConfigurations.distributed.map(function (customConfiguration) {
  304. gulp.task(customConfiguration, function (cb) {
  305. config.build.currentConfig = customConfiguration;
  306. config.build.outputDirectory = config.build.outputCustomConfigurationsDirectory + "/" + customConfiguration;
  307. runSequence("typescript-compile", "build", cb);
  308. });
  309. });
  310. gulp.task("typescript-customConfigurations", function (cb) {
  311. runSequence(config.buildConfigurations.distributed, cb);
  312. });
  313. /**
  314. * Do it all.
  315. */
  316. gulp.task("typescript-all", function (cb) {
  317. runSequence("typescript", "typescript-libraries", cb);
  318. });
  319. /**
  320. * Watch ts files and fire repective tasks.
  321. */
  322. gulp.task('watch', [], function () {
  323. var tasks = [gulp.watch(config.typescript, ['typescript-compile'])];
  324. config.modules.map(function (module) {
  325. config[module].libraries.map(function (library) {
  326. tasks.push(gulp.watch(library.files, function () {
  327. console.log(library.output);
  328. return buildExternalLibrary(library, config[module], true)
  329. .pipe(debug());
  330. }));
  331. tasks.push(gulp.watch(library.shaderFiles, function () {
  332. console.log(library.output);
  333. return buildExternalLibrary(library, config[module], true)
  334. .pipe(debug())
  335. }));
  336. tasks.push(gulp.watch(library.sassFiles, function () {
  337. console.log(library.output);
  338. return buildExternalLibrary(library, config[module], true)
  339. .pipe(debug())
  340. }));
  341. });
  342. });
  343. return tasks;
  344. });
  345. /**
  346. * Embedded local dev env management.
  347. */
  348. gulp.task('deployLocalDev', function () {
  349. gulp.src('../../localDev/template/**.*')
  350. .pipe(gulp.dest('../../localDev/src/'));
  351. });
  352. /**
  353. * Embedded webserver for test convenience.
  354. */
  355. gulp.task('webserver', function () {
  356. gulp.src('../../.').pipe(webserver({
  357. port: 1338,
  358. livereload: false
  359.     }));
  360. });
  361. /**
  362. * Combine Webserver and Watch as long as vscode does not handle multi tasks.
  363. */
  364. gulp.task('run', ['watch', 'webserver'], function () {
  365. });
  366. gulp.task("zip-blender", function () {
  367. return gulp.src('../../Exporters/Blender/src/**')
  368. .pipe(zip('Blender2Babylon-5.3.zip'))
  369. .pipe(gulp.dest('../../Exporters/Blender'));
  370. });
  371. gulp.task('clean-JS-MAP', function () {
  372. return del([
  373. '../../src/**/*.js.map', '../../src/**/*.js'
  374. ], { force: true });
  375. });