index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const {transform} = require("../dist");
  2. // Enum constants taken from the TypeScript codebase.
  3. const ModuleKindCommonJS = 1;
  4. const JsxEmitReactJSX = 4;
  5. const JsxEmitReactJSXDev = 5;
  6. /**
  7. * ts-node transpiler plugin
  8. *
  9. * This plugin hooks into ts-node so that Sucrase can handle all TS-to-JS
  10. * conversion while ts-node handles the ESM loader, require hook, REPL
  11. * integration, etc. ts-node automatically discovers the relevant tsconfig file,
  12. * so the main logic in this integration is translating tsconfig options to the
  13. * corresponding Sucrase options.
  14. *
  15. * Any tsconfig options relevant to Sucrase are translated, but some config
  16. * options outside the scope of Sucrase are ignored. For example, we assume the
  17. * isolatedModules option, and we ignore target because Sucrase doesn't provide
  18. * JS syntax downleveling (at least not in a way that is useful for Node).
  19. *
  20. * One notable caveat is that importsNotUsedAsValues and preserveValueImports
  21. * are ignored right now, since they are deprecated and don't have exact Sucrase
  22. * equivalents. To preserve imports and exports, use verbatimModuleSyntax.
  23. */
  24. function create(createOptions) {
  25. const {nodeModuleEmitKind} = createOptions;
  26. const {
  27. module,
  28. jsx,
  29. jsxFactory,
  30. jsxFragmentFactory,
  31. jsxImportSource,
  32. esModuleInterop,
  33. verbatimModuleSyntax,
  34. } = createOptions.service.config.options;
  35. return {
  36. transpile(input, transpileOptions) {
  37. const {fileName} = transpileOptions;
  38. const transforms = [];
  39. // Detect JS rather than TS so we bias toward including the typescript
  40. // transform, since almost always it doesn't hurt to include.
  41. const isJS =
  42. fileName.endsWith(".js") ||
  43. fileName.endsWith(".jsx") ||
  44. fileName.endsWith(".mjs") ||
  45. fileName.endsWith(".cjs");
  46. if (!isJS) {
  47. transforms.push("typescript");
  48. }
  49. if (module === ModuleKindCommonJS || nodeModuleEmitKind === "nodecjs") {
  50. transforms.push("imports");
  51. }
  52. if (fileName.endsWith(".tsx") || fileName.endsWith(".jsx")) {
  53. transforms.push("jsx");
  54. }
  55. const {code, sourceMap} = transform(input, {
  56. transforms,
  57. disableESTransforms: true,
  58. jsxRuntime: jsx === JsxEmitReactJSX || jsx === JsxEmitReactJSXDev ? "automatic" : "classic",
  59. production: jsx === JsxEmitReactJSX,
  60. jsxImportSource,
  61. jsxPragma: jsxFactory,
  62. jsxFragmentPragma: jsxFragmentFactory,
  63. keepUnusedImports: verbatimModuleSyntax,
  64. preserveDynamicImport: nodeModuleEmitKind === "nodecjs",
  65. injectCreateRequireForImportRequire: nodeModuleEmitKind === "nodeesm",
  66. enableLegacyTypeScriptModuleInterop: !esModuleInterop,
  67. sourceMapOptions: {compiledFilename: fileName},
  68. filePath: fileName,
  69. });
  70. return {
  71. outputText: code,
  72. sourceMapText: JSON.stringify(sourceMap),
  73. };
  74. },
  75. };
  76. }
  77. exports.create = create;