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 file = parse(code, isJSXEnabled, isTypeScriptEnabled, isFlowEnabled);
|
89 | const tokens = file.tokens;
|
90 | const scopes = file.scopes;
|
91 |
|
92 | const nameManager = new NameManager(code, tokens);
|
93 | const helperManager = new HelperManager(nameManager);
|
94 | const tokenProcessor = new TokenProcessor(code, tokens, isFlowEnabled, helperManager);
|
95 | const enableLegacyTypeScriptModuleInterop = Boolean(options.enableLegacyTypeScriptModuleInterop);
|
96 |
|
97 | let importProcessor = null;
|
98 | if (options.transforms.includes("imports")) {
|
99 | importProcessor = new CJSImportProcessor(
|
100 | nameManager,
|
101 | tokenProcessor,
|
102 | enableLegacyTypeScriptModuleInterop,
|
103 | options,
|
104 | options.transforms.includes("typescript"),
|
105 | helperManager,
|
106 | );
|
107 | importProcessor.preprocessTokens();
|
108 |
|
109 |
|
110 | identifyShadowedGlobals(tokenProcessor, scopes, importProcessor.getGlobalNames());
|
111 | if (options.transforms.includes("typescript")) {
|
112 | importProcessor.pruneTypeOnlyImports();
|
113 | }
|
114 | } else if (options.transforms.includes("typescript")) {
|
115 | identifyShadowedGlobals(tokenProcessor, scopes, getTSImportedNames(tokenProcessor));
|
116 | }
|
117 | return {tokenProcessor, scopes, nameManager, importProcessor, helperManager};
|
118 | }
|