1 | "use strict"; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }Object.defineProperty(exports, "__esModule", {value: true});var _CJSImportProcessor = require('./CJSImportProcessor'); var _CJSImportProcessor2 = _interopRequireDefault(_CJSImportProcessor);
|
2 | var _computeSourceMap = require('./computeSourceMap'); var _computeSourceMap2 = _interopRequireDefault(_computeSourceMap);
|
3 | var _identifyShadowedGlobals = require('./identifyShadowedGlobals'); var _identifyShadowedGlobals2 = _interopRequireDefault(_identifyShadowedGlobals);
|
4 | var _NameManager = require('./NameManager'); var _NameManager2 = _interopRequireDefault(_NameManager);
|
5 | var _parser = require('./parser');
|
6 |
|
7 | var _TokenProcessor = require('./TokenProcessor'); var _TokenProcessor2 = _interopRequireDefault(_TokenProcessor);
|
8 | var _RootTransformer = require('./transformers/RootTransformer'); var _RootTransformer2 = _interopRequireDefault(_RootTransformer);
|
9 | var _formatTokens = require('./util/formatTokens'); var _formatTokens2 = _interopRequireDefault(_formatTokens);
|
10 | var _getTSImportedNames = require('./util/getTSImportedNames'); var _getTSImportedNames2 = _interopRequireDefault(_getTSImportedNames);
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function getVersion() {
|
69 |
|
70 | return require("../package.json").version;
|
71 | } exports.getVersion = getVersion;
|
72 |
|
73 | function transform(code, options) {
|
74 | try {
|
75 | const sucraseContext = getSucraseContext(code, options);
|
76 | const transformer = new (0, _RootTransformer2.default)(
|
77 | sucraseContext,
|
78 | options.transforms,
|
79 | Boolean(options.enableLegacyBabel5ModuleInterop),
|
80 | options,
|
81 | );
|
82 | let result = {code: transformer.transform()};
|
83 | if (options.sourceMapOptions) {
|
84 | if (!options.filePath) {
|
85 | throw new Error("filePath must be specified when generating a source map.");
|
86 | }
|
87 | result = {
|
88 | ...result,
|
89 | sourceMap: _computeSourceMap2.default.call(void 0, result.code, options.filePath, options.sourceMapOptions),
|
90 | };
|
91 | }
|
92 | return result;
|
93 | } catch (e) {
|
94 | if (options.filePath) {
|
95 | e.message = `Error transforming ${options.filePath}: ${e.message}`;
|
96 | }
|
97 | throw e;
|
98 | }
|
99 | } exports.transform = transform;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function getFormattedTokens(code, options) {
|
106 | const tokens = getSucraseContext(code, options).tokenProcessor.tokens;
|
107 | return _formatTokens2.default.call(void 0, code, tokens);
|
108 | } exports.getFormattedTokens = getFormattedTokens;
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function getSucraseContext(code, options) {
|
120 | const isJSXEnabled = options.transforms.includes("jsx");
|
121 | const isTypeScriptEnabled = options.transforms.includes("typescript");
|
122 | const isFlowEnabled = options.transforms.includes("flow");
|
123 | const file = _parser.parse.call(void 0, code, isJSXEnabled, isTypeScriptEnabled, isFlowEnabled);
|
124 | const tokens = file.tokens;
|
125 | const scopes = file.scopes;
|
126 |
|
127 | const tokenProcessor = new (0, _TokenProcessor2.default)(code, tokens, isFlowEnabled);
|
128 | const nameManager = new (0, _NameManager2.default)(tokenProcessor);
|
129 | nameManager.preprocessNames();
|
130 | const enableLegacyTypeScriptModuleInterop = Boolean(options.enableLegacyTypeScriptModuleInterop);
|
131 |
|
132 | let importProcessor = null;
|
133 | if (options.transforms.includes("imports")) {
|
134 | importProcessor = new (0, _CJSImportProcessor2.default)(
|
135 | nameManager,
|
136 | tokenProcessor,
|
137 | enableLegacyTypeScriptModuleInterop,
|
138 | options,
|
139 | options.transforms.includes("typescript"),
|
140 | );
|
141 | importProcessor.preprocessTokens();
|
142 |
|
143 |
|
144 | _identifyShadowedGlobals2.default.call(void 0, tokenProcessor, scopes, importProcessor.getGlobalNames());
|
145 | if (options.transforms.includes("typescript")) {
|
146 | importProcessor.pruneTypeOnlyImports();
|
147 | }
|
148 | } else if (options.transforms.includes("typescript")) {
|
149 | _identifyShadowedGlobals2.default.call(void 0, tokenProcessor, scopes, _getTSImportedNames2.default.call(void 0, tokenProcessor));
|
150 | }
|
151 | return {tokenProcessor, scopes, nameManager, importProcessor};
|
152 | }
|