compile.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // v1.2.7
  2. //是否使用IDE自带的node环境和插件,设置false后,则使用自己环境(使用命令行方式执行)
  3. const useIDENode = process.argv[0].indexOf("LayaAir") > -1 ? true : false;
  4. const useCMDNode = process.argv[1].indexOf("layaair2-cmd") > -1 ? true : false;
  5. function useOtherNode(){
  6. return useIDENode||useCMDNode;
  7. }
  8. //获取Node插件和工作路径
  9. let ideModuleDir = useOtherNode() ? process.argv[1].replace("gulp\\bin\\gulp.js", "").replace("gulp/bin/gulp.js", "") : "";
  10. let workSpaceDir = useOtherNode() ? process.argv[2].replace("--gulpfile=", "").replace("\\.laya\\compile.js", "").replace("/.laya/compile.js", "") : "./../";
  11. const gulp = require(ideModuleDir + "gulp");
  12. const rollup = require(ideModuleDir + "rollup");
  13. const typescript = require(ideModuleDir + 'rollup-plugin-typescript2');//typescript2 plugin
  14. const glsl = require(ideModuleDir + 'rollup-plugin-glsl');
  15. const path = require('path');
  16. const fs = require('fs');
  17. // 如果是发布时调用编译功能,增加prevTasks
  18. let prevTasks = "";
  19. if (global.publish) {
  20. prevTasks = ["loadConfig"];
  21. }
  22. gulp.task("compile", prevTasks, function () {
  23. // 发布时调用编译功能,判断是否点击了编译选项
  24. if (global.publish) {
  25. workSpaceDir = global.workSpaceDir; // 发布时调用编译,workSpaceDir使用publish.js里的变量
  26. let forceCompile = !fs.existsSync(path.join(workSpaceDir, "bin", "js", "bundle.js")); // 发布时,并且没有编译过,则强制编译
  27. if (!global.config.compile && !forceCompile) {
  28. return;
  29. }
  30. }
  31. return rollup.rollup({
  32. input: workSpaceDir + '/src/Main.ts',
  33. onwarn:(waring,warn)=>{
  34. if(waring.code == "CIRCULAR_DEPENDENCY"){
  35. console.log("warnning Circular dependency:");
  36. console.log(waring);
  37. }
  38. },
  39. treeshake: false, //建议忽略
  40. plugins: [
  41. typescript({
  42. tsconfig:workSpaceDir + "/tsconfig.json",
  43. check: true, //Set to false to avoid doing any diagnostic checks on the code
  44. tsconfigOverride:{compilerOptions:{removeComments: true}},
  45. include:/.*.ts/,
  46. }),
  47. glsl({
  48. // By default, everything gets included
  49. include: /.*(.glsl|.vs|.fs)$/,
  50. sourceMap: false,
  51. compress:false
  52. }),
  53. /*terser({
  54. output: {
  55. },
  56. numWorkers:1,//Amount of workers to spawn. Defaults to the number of CPUs minus 1
  57. sourcemap: false
  58. })*/
  59. ]
  60. }).then(bundle => {
  61. return bundle.write({
  62. file: workSpaceDir + '/bin/js/bundle.js',
  63. format: 'iife',
  64. name: 'laya',
  65. sourcemap: false
  66. });
  67. }).catch(err=>{
  68. console.log(err);
  69. });
  70. });