gulp-processAmdDeclarationToModule.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 regex = /(.*)type ([A-Za-z0-9]*) = import\("(.*)"\)\.(.*);/g;
  15. var match = regex.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. // Append Module Name
  23. // Declaration
  24. line = line.replace(/declare module "/g, `declare module "${moduleName}/`);
  25. // From
  26. line = line.replace(/ from "/g, ` from "${moduleName}/`);
  27. // Module augmentation
  28. line = line.replace(/ module "/g, ` module "${moduleName}/`);
  29. // Inlined Import
  30. line = line.replace(/import\("/g, `import("${moduleName}/`);
  31. lines[index] = line;
  32. }
  33. // Recreate the file.
  34. str = lines.join('\n');
  35. // Add Entry point.
  36. str += `declare module "${moduleName}" {
  37. export * from "${moduleName}/${entryPoint.replace("./","").replace(".ts", "")}";
  38. }`;
  39. return str;
  40. }
  41. module.exports = function(fileLocation, options, cb) {
  42. options = options || { };
  43. var data = fs.readFileSync(fileLocation);
  44. newData = processData(data, options);
  45. fs.writeFileSync(options.output || fileLocation, newData);
  46. }