gulp-processAmdDeclarationToModule.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Gulp Tools
  2. var fs = require("fs");
  3. var processData = function(data, options) {
  4. var moduleName = options.moduleName;
  5. var entryPoint = options.entryPoint;
  6. var str = "" + data;
  7. // Start process by extracting all lines.
  8. let lines = str.split('\n');
  9. // Let's go line by line and check if we have special folder replacements
  10. // Replaces declare module '...'; by declare module 'babylonjs/...'; for instance
  11. for (let index = 0; index < lines.length; index++) {
  12. let line = lines[index];
  13. // Replace Type Imports
  14. var regexTypeImport = /(.*)type ([A-Za-z0-9]*) = import\("(.*)"\)\.(.*);/g;
  15. var match = regexTypeImport.exec(line);
  16. if (match) {
  17. var spaces = match[1]
  18. var module = match[3];
  19. var type = match[4];
  20. line = `${spaces}import { ${type} } from "${module}";`;
  21. }
  22. // Checks if line is about external module
  23. var externalModule = false;
  24. if (options.externals) {
  25. for (let ext in options.externals) {
  26. externalModule = line.indexOf(ext) > -1;
  27. if (externalModule) {
  28. break;
  29. }
  30. }
  31. }
  32. // If not Append Module Name
  33. if (!externalModule) {
  34. // Declaration
  35. line = line.replace(/declare module "/g, `declare module "${moduleName}/`);
  36. // From
  37. line = line.replace(/ from "/g, ` from "${moduleName}/`);
  38. // Module augmentation
  39. line = line.replace(/ module "/g, ` module "${moduleName}/`);
  40. // Inlined Import
  41. line = line.replace(/import\("/g, `import("${moduleName}/`);
  42. // Side Effect Import
  43. line = line.replace(/import "/g, `import "${moduleName}/`);
  44. }
  45. // Replace Static Readonly declaration for UMD TS Version compat
  46. var regexVar = /(.*)readonly (.*) = (.*);/g;
  47. match = regexVar.exec(line);
  48. if (match) {
  49. let spaces = match[1];
  50. let name = match[2];
  51. let value = match[3];
  52. if (value === "true" || value === "false") {
  53. line = `${spaces}readonly ${name}: boolean;`;
  54. }
  55. else if (value.startsWith('"')) {
  56. line = `${spaces}readonly ${name}: string;`;
  57. }
  58. else {
  59. line = `${spaces}readonly ${name}: number;`;
  60. }
  61. }
  62. lines[index] = line;
  63. }
  64. // Recreate the file.
  65. str = lines.join('\n');
  66. // !!! Be carefull
  67. // Could cause issues if this appears in several import scope
  68. // with different aliases.
  69. // !!! Be carefull multiline not managed.
  70. // Remove unmanaged externals Appears as classMap false in the config.
  71. if (options.externals) {
  72. for (let ext in options.externals) {
  73. // Need to remove the module and dependencies if false.
  74. if (options.externals[ext] === false) {
  75. // Replace import { foo, bar } from ...
  76. const package = ext;
  77. var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})[\/'"](.*);`, "g");
  78. var match = babylonRegex.exec(str);
  79. let classes = new Set();
  80. while (match != null) {
  81. if (match[1]) {
  82. match[1].split(",").forEach(element => {
  83. classes.add(element.trim());
  84. });
  85. }
  86. match = babylonRegex.exec(str);
  87. }
  88. str = str.replace(babylonRegex, '');
  89. classes.forEach(cls => {
  90. let className = cls;
  91. let alias = cls;
  92. // Deal with import { foo as A, bar as B } from ...
  93. if (cls.indexOf(" as ") > -1) {
  94. const tokens = cls.split(" as ");
  95. className = tokens[0];
  96. alias = tokens[1];
  97. }
  98. // !!! Be carefull multiline not managed.
  99. const rg = new RegExp(`.*[ <]${alias}[^\\w].*`, "g")
  100. str = str.replace(rg, "");
  101. });
  102. }
  103. }
  104. // Remove Empty Lines
  105. str = str.replace(/^\s*$/gm, "");
  106. }
  107. // Hide Exported Consts if necessary
  108. if (options.hiddenConsts) {
  109. for (let toHide of options.hiddenConsts) {
  110. var constStart = str.indexOf(`export const ${toHide}`);
  111. if (constStart > -1) {
  112. for (let i = constStart; i < str.length; i++) {
  113. if (str[i] === "}") {
  114. // +1 to enroll the last }
  115. // +2 to enroll the trailing ;
  116. str = str.substr(0, constStart) + str.substr(i + 2);
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. // Add Entry point.
  124. str += `
  125. declare module "${moduleName}" {
  126. export * from "${moduleName}/${entryPoint.replace(/\.\//g,"").replace(".ts", "")}";
  127. }`;
  128. return str;
  129. }
  130. module.exports = function(fileLocation, options, cb) {
  131. options = options || { };
  132. var data = fs.readFileSync(fileLocation);
  133. newData = processData(data, options);
  134. fs.writeFileSync(options.output || fileLocation, newData);
  135. }