gulp-processAmdDeclarationToModule.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // 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. lines[index] = line;
  46. }
  47. // Recreate the file.
  48. str = lines.join('\n');
  49. // Add Entry point.
  50. str += `declare module "${moduleName}" {
  51. export * from "${moduleName}/${entryPoint.replace("./","").replace(".ts", "")}";
  52. }`;
  53. return str;
  54. }
  55. module.exports = function(fileLocation, options, cb) {
  56. options = options || { };
  57. var data = fs.readFileSync(fileLocation);
  58. newData = processData(data, options);
  59. fs.writeFileSync(options.output || fileLocation, newData);
  60. }