Sin descripción

index.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, and Sucrase uses TypeScript's default behavior of
  22. * eliding imports only used as types. This usually makes no difference when
  23. * running the code, so for now we ignore these options without a warning.
  24. */
  25. function create(createOptions) {
  26. const {nodeModuleEmitKind} = createOptions;
  27. const {module, jsx, jsxFactory, jsxFragmentFactory, jsxImportSource, esModuleInterop} =
  28. createOptions.service.config.options;
  29. return {
  30. transpile(input, transpileOptions) {
  31. const {fileName} = transpileOptions;
  32. const transforms = [];
  33. // Detect JS rather than TS so we bias toward including the typescript
  34. // transform, since almost always it doesn't hurt to include.
  35. const isJS =
  36. fileName.endsWith(".js") ||
  37. fileName.endsWith(".jsx") ||
  38. fileName.endsWith(".mjs") ||
  39. fileName.endsWith(".cjs");
  40. if (!isJS) {
  41. transforms.push("typescript");
  42. }
  43. if (module === ModuleKindCommonJS || nodeModuleEmitKind === "nodecjs") {
  44. transforms.push("imports");
  45. }
  46. if (fileName.endsWith(".tsx") || fileName.endsWith(".jsx")) {
  47. transforms.push("jsx");
  48. }
  49. const {code, sourceMap} = transform(input, {
  50. transforms,
  51. disableESTransforms: true,
  52. jsxRuntime: jsx === JsxEmitReactJSX || jsx === JsxEmitReactJSXDev ? "automatic" : "classic",
  53. production: jsx === JsxEmitReactJSX,
  54. jsxImportSource,
  55. jsxPragma: jsxFactory,
  56. jsxFragmentPragma: jsxFragmentFactory,
  57. preserveDynamicImport: nodeModuleEmitKind === "nodecjs",
  58. injectCreateRequireForImportRequire: nodeModuleEmitKind === "nodeesm",
  59. enableLegacyTypeScriptModuleInterop: !esModuleInterop,
  60. sourceMapOptions: {compiledFilename: fileName},
  61. filePath: fileName,
  62. });
  63. return {
  64. outputText: code,
  65. sourceMapText: JSON.stringify(sourceMap),
  66. };
  67. },
  68. };
  69. }
  70. exports.create = create;