processViewerDeclaration.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. module.exports = function(data, options) {
  2. var str = "" + data;
  3. // Start process
  4. let lines = str.split('\n');
  5. var firstIndex = lines.findIndex((line => { return line.indexOf(`'${options.packageName}/'`) !== -1 }));
  6. var lastIndex = lines.findIndex(((line, idx) => { return line.trim() === '}' && idx > firstIndex }));
  7. lines.splice(firstIndex, lastIndex - firstIndex + 1);
  8. // Let's go line by line and check if we have special folder replacements
  9. for (var index = 0; index < lines.length; index++) {
  10. var namespace = options.moduleName;
  11. var regex = /declare module '(.*)' {/g;
  12. if (options.moduleSpecifics) {
  13. var match = regex.exec(lines[index]);
  14. if (!match) {
  15. continue;
  16. }
  17. var module = match[1];
  18. options.moduleSpecifics.forEach(function(specific) {
  19. if (module.indexOf(specific.path) > -1) {
  20. namespace = specific.namespace;
  21. }
  22. });
  23. }
  24. lines[index] = lines[index].replace(regex, `declare module ${namespace} {`);
  25. }
  26. str = lines.join('\n');
  27. str = str.replace("import * as BABYLON from 'babylonjs';", "");
  28. let regexp = new RegExp(`import {(.*)} from ['"]${options.packageName}(.*)['"];`, 'g');
  29. str = str.replace(regexp, '');
  30. if (options.importsToRemove) {
  31. while (options.importsToRemove.length) {
  32. let remove = options.importsToRemove.pop();
  33. str = str.replace(new RegExp(`import '${remove}';`), '');
  34. }
  35. }
  36. // Find all used BABYLON and BABYLON-Loaders classes:
  37. if ((options.classMap)) {
  38. Object.keys(options.classMap).forEach(package => {
  39. var babylonRegex = new RegExp(`import {(.*)} from ['"](${package})['"];`, "g");
  40. var match = babylonRegex.exec(str);
  41. let classes = new Set();
  42. while (match != null) {
  43. if (match[1]) {
  44. match[1].split(",").forEach(element => {
  45. classes.add(element.trim());
  46. });
  47. }
  48. match = babylonRegex.exec(str);
  49. }
  50. str = str.replace(babylonRegex, '');
  51. classes.forEach(cls => {
  52. let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
  53. str = str.replace(rg, `$1${options.classMap[package]}.$2$3`);
  54. });
  55. })
  56. }
  57. str = str.replace(/export {(.*)};/g, '');
  58. str = str.replace(/import (.*);/g, "");
  59. str = str.split("\n").filter(line => line.trim()).filter(line => line.indexOf("export * from") === -1).join("\n");
  60. // Remove empty module declaration
  61. var cleanEmptyNamespace = function(str, moduleName) {
  62. let emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\n}\n", "g");
  63. str = str.replace(emptyDeclareRegexp, "");
  64. emptyDeclareRegexp = new RegExp("declare module " + moduleName + " {\r\n}\r\n", "g");
  65. str = str.replace(emptyDeclareRegexp, "");
  66. return str;
  67. }
  68. str = cleanEmptyNamespace(str, options.moduleName);
  69. if (options.moduleSpecifics) {
  70. options.moduleSpecifics.forEach(function(specific) {
  71. str = cleanEmptyNamespace(str, specific.namespace);
  72. });
  73. }
  74. return str;
  75. }