update-lazy-module-paths.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const ts = require("../../third_party/github.com/Microsoft/TypeScript/lib/typescript");
  4. function* visit(directory) {
  5. for (const path of directory.subfiles) {
  6. if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
  7. const entry = directory.file(path);
  8. if (entry) {
  9. const content = entry.content;
  10. if (content.includes('loadChildren')) {
  11. const source = ts.createSourceFile(entry.path, content.toString().replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true);
  12. yield source;
  13. }
  14. }
  15. }
  16. }
  17. for (const path of directory.subdirs) {
  18. if (path === 'node_modules') {
  19. continue;
  20. }
  21. yield* visit(directory.dir(path));
  22. }
  23. }
  24. function updateLazyModulePaths() {
  25. return tree => {
  26. for (const sourceFile of visit(tree.root)) {
  27. let recorder;
  28. ts.forEachChild(sourceFile, function analyze(node) {
  29. if (ts.isPropertyAssignment(node) &&
  30. (ts.isIdentifier(node.name) || ts.isStringLiteral(node.name)) &&
  31. node.name.text === 'loadChildren' &&
  32. ts.isStringLiteral(node.initializer)) {
  33. const valueNode = node.initializer;
  34. const parts = valueNode.text.split('#');
  35. const path = parts[0];
  36. const moduleName = parts[1] || 'default';
  37. const fix = `() => import('${path}').then(m => m.${moduleName})`;
  38. if (!recorder) {
  39. recorder = tree.beginUpdate(sourceFile.fileName);
  40. }
  41. const index = valueNode.getStart();
  42. const length = valueNode.getWidth();
  43. recorder
  44. .remove(index, length)
  45. .insertLeft(index, fix);
  46. }
  47. ts.forEachChild(node, analyze);
  48. });
  49. if (recorder) {
  50. tree.commitUpdate(recorder);
  51. }
  52. }
  53. };
  54. }
  55. exports.updateLazyModulePaths = updateLazyModulePaths;