processViewerDeclaration.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. module.exports = function (data) {
  2. var str = "" + data;
  3. // this regex is not working on node 6 for some reason:
  4. // str = str.replace(/declare module 'babylonjs-viewer\/' {((?!(declare))(.|\n))*\n}/g, '');
  5. let lines = str.split('\n');
  6. var firstIndex = lines.findIndex((line => { return line.indexOf("'babylonjs-viewer/'") !== -1 }));
  7. var lastIndex = lines.findIndex(((line, idx) => { return line.trim() === '}' && idx > firstIndex }));
  8. lines.splice(firstIndex, lastIndex - firstIndex + 1);
  9. str = lines.join('\n');
  10. str = str.replace(/declare module (.*) {/g, 'declare module BabylonViewer {').replace("import * as BABYLON from 'babylonjs';", "");
  11. str = str.replace(/import {(.*)} from ['"]babylonjs-viewer(.*)['"];/g, '').replace(/import 'babylonjs-loaders';/, '').replace(/import 'pep';/, '');
  12. //find all used BABYLON and BABYLON-Loaders classes:
  13. var babylonRegex = /import {(.*)} from ['"](babylonjs|babylonjs-loaders)['"];/g
  14. var match = babylonRegex.exec(str);
  15. let classes = new Set();
  16. while (match != null) {
  17. if (match[1]) {
  18. match[1].split(",").forEach(element => {
  19. classes.add(element.trim());
  20. });
  21. }
  22. match = babylonRegex.exec(str);
  23. }
  24. str = str.replace(babylonRegex, '');
  25. classes.forEach(cls => {
  26. let rg = new RegExp(`([ <])(${cls})([^\\w])`, "g")
  27. str = str.replace(rg, "$1BABYLON.$2$3");
  28. });
  29. str = str.replace(/export {(.*)};/g, '')
  30. return str;
  31. }