1 | import CJSImportProcessor from "./CJSImportProcessor";
|
2 | import computeSourceMap, {} from "./computeSourceMap";
|
3 | import {HelperManager} from "./HelperManager";
|
4 | import identifyShadowedGlobals from "./identifyShadowedGlobals";
|
5 | import NameManager from "./NameManager";
|
6 | import {validateOptions} from "./Options";
|
7 | import {parse} from "./parser";
|
8 |
|
9 | import TokenProcessor from "./TokenProcessor";
|
10 | import RootTransformer from "./transformers/RootTransformer";
|
11 | import formatTokens from "./util/formatTokens";
|
12 | import getTSImportedNames from "./util/getTSImportedNames";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export function getVersion() {
|
33 |
|
34 | return require("../package.json").version;
|
35 | }
|
36 |
|
37 | export function transform(code, options) {
|
38 | validateOptions(options);
|
39 | try {
|
40 | const sucraseContext = getSucraseContext(code, options);
|
41 | const transformer = new RootTransformer(
|
42 | sucraseContext,
|
43 | options.transforms,
|
44 | Boolean(options.enableLegacyBabel5ModuleInterop),
|
45 | options,
|
46 | );
|
47 | let result = {code: transformer.transform()};
|
48 | if (options.sourceMapOptions) {
|
49 | if (!options.filePath) {
|
50 | throw new Error("filePath must be specified when generating a source map.");
|
51 | }
|
52 | result = {
|
53 | ...result,
|
54 | sourceMap: computeSourceMap(result.code, options.filePath, options.sourceMapOptions),
|
55 | };
|
56 | }
|
57 | return result;
|
58 | } catch (e) {
|
59 | if (options.filePath) {
|
60 | e.message = `Error transforming ${options.filePath}: ${e.message}`;
|
61 | }
|
62 | throw e;
|
63 | }
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export function getFormattedTokens(code, options) {
|
71 | const tokens = getSucraseContext(code, options).tokenProcessor.tokens;
|
72 | return formatTokens(code, tokens);
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function getSucraseContext(code, options) {
|
85 | const isJSXEnabled = options.transforms.includes("jsx");
|
86 | const isTypeScriptEnabled = options.transforms.includes("typescript");
|
87 | const isFlowEnabled = options.transforms.includes("flow");
|
88 | const disableESTransforms = options.disableESTransforms === true;
|
89 | const file = parse(code, isJSXEnabled, isTypeScriptEnabled, isFlowEnabled);
|
90 | const tokens = file.tokens;
|
91 | const scopes = file.scopes;
|
92 |
|
93 | const nameManager = new NameManager(code, tokens);
|
94 | const helperManager = new HelperManager(nameManager);
|
95 | const tokenProcessor = new TokenProcessor(
|
96 | code,
|
97 | tokens,
|
98 | isFlowEnabled,
|
99 | disableESTransforms,
|
100 | helperManager,
|
101 | );
|
102 | const enableLegacyTypeScriptModuleInterop = Boolean(options.enableLegacyTypeScriptModuleInterop);
|
103 |
|
104 | let importProcessor = null;
|
105 | if (options.transforms.includes("imports")) {
|
106 | importProcessor = new CJSImportProcessor(
|
107 | nameManager,
|
108 | tokenProcessor,
|
109 | enableLegacyTypeScriptModuleInterop,
|
110 | options,
|
111 | options.transforms.includes("typescript"),
|
112 | helperManager,
|
113 | );
|
114 | importProcessor.preprocessTokens();
|
115 |
|
116 |
|
117 | identifyShadowedGlobals(tokenProcessor, scopes, importProcessor.getGlobalNames());
|
118 | if (options.transforms.includes("typescript")) {
|
119 | importProcessor.pruneTypeOnlyImports();
|
120 | }
|
121 | } else if (options.transforms.includes("typescript")) {
|
122 | identifyShadowedGlobals(tokenProcessor, scopes, getTSImportedNames(tokenProcessor));
|
123 | }
|
124 | return {tokenProcessor, scopes, nameManager, importProcessor, helperManager};
|
125 | }
|