transformImport.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // adding js extension in the import statement.
  2. // Reference:
  3. // https://regexr.com/47jlq
  4. // https://gist.github.com/manekinekko/7e58a17bc62a9be47172
  5. const regexp = /((?:(?:import)|(?:export))\s+?(?:(?:(?:[\w*\s{},\/]*)\s+from\s+?)|))(?:(?:"(.*?)")|(?:'(.*?)'))([\s]*?(?:;|$|))/g;
  6. module.exports.transformImport = function (code, processModuleName) {
  7. return code.replace(regexp, (str, prefix, moduleNameA, moduleNameB, postfix) => {
  8. let moduleName = (moduleNameA === undefined ? moduleNameB : moduleNameA).trim();
  9. const quote = moduleNameA === undefined ? "'" : '"';
  10. return prefix + quote + processModuleName(moduleName) + quote + postfix;
  11. // Not support other extensions.
  12. });
  13. }
  14. const testCases = `import videos from './videos/index.js'
  15. export default (socket, context) => {
  16. // dynamically importing all the socket.io handler (it's dynamic import that happen at run time)
  17. import {
  18. something
  19. } from "./test/okbb"
  20. const f = 2;
  21. import test from 'obb'
  22. import {
  23. Component
  24. } from '@angular2/core';
  25. import defaultMember from "module-0";
  26. import * as name from "module-1 ";
  27. import { member } from " module-2";
  28. import { member as alias } from "module-3";
  29. import { member1 , member2 } from "module-4";
  30. import { member1 , member2 as alias2 , member3 as alias3 } from "module-5";
  31. import defaultMember, { member, member } from "module-6";
  32. import defaultMember, * as name from "module-7";
  33. import "module-8";
  34. import "module-9" // comment no problem
  35. import {
  36. AAAA,
  37. // BBB
  38. } from 'module-10';
  39. import "module-b' // doesn't match -> the opening and closing quation mark are different
  40. importing hya from 'ttt'
  41. import fbsfrom ''
  42. // Export expressions.
  43. export { aaa };
  44. export * from "module-11";
  45. export { aaa } from "module-12";
  46. // Should not be parsed
  47. export default aaa;
  48. export function bbb () {
  49. };
  50. `
  51. module.exports.runTest = function () {
  52. const expected = [
  53. './videos/index.js',
  54. './test/okbb',
  55. 'obb',
  56. '@angular2/core',
  57. 'module-0',
  58. 'module-1',
  59. 'module-2',
  60. 'module-3',
  61. 'module-4',
  62. 'module-5',
  63. 'module-6',
  64. 'module-7',
  65. 'module-8',
  66. 'module-9',
  67. 'module-10',
  68. 'module-11',
  69. 'module-12'
  70. ]
  71. let cursor = 0;
  72. module.exports.transformImport(testCases, (moduleName) => {
  73. if (expected[cursor] !== moduleName) {
  74. throw new Error(`Expected ${expected[cursor]}. Actual ${moduleName}`);
  75. }
  76. cursor++;
  77. return moduleName;
  78. })
  79. if (cursor !== expected.length) {
  80. throw new Error('Test failed');
  81. }
  82. console.log('All test passed!')
  83. }
  84. // module.exports.runTest();