UNPKG

3.92 kBTypeScriptView Raw
1import CJSImportProcessor from "../CJSImportProcessor";
2import NameManager from "../NameManager";
3import TokenProcessor from "../TokenProcessor";
4import ReactHotLoaderTransformer from "./ReactHotLoaderTransformer";
5import RootTransformer from "./RootTransformer";
6import Transformer from "./Transformer";
7/**
8 * Class for editing import statements when we are transforming to commonjs.
9 */
10export default class CJSImportTransformer extends Transformer {
11 readonly rootTransformer: RootTransformer;
12 readonly tokens: TokenProcessor;
13 readonly importProcessor: CJSImportProcessor;
14 readonly nameManager: NameManager;
15 readonly reactHotLoaderTransformer: ReactHotLoaderTransformer | null;
16 readonly enableLegacyBabel5ModuleInterop: boolean;
17 private hadExport;
18 private hadNamedExport;
19 private hadDefaultExport;
20 constructor(rootTransformer: RootTransformer, tokens: TokenProcessor, importProcessor: CJSImportProcessor, nameManager: NameManager, reactHotLoaderTransformer: ReactHotLoaderTransformer | null, enableLegacyBabel5ModuleInterop: boolean);
21 getPrefixCode(): string;
22 getSuffixCode(): string;
23 process(): boolean;
24 /**
25 * Transform this:
26 * import foo, {bar} from 'baz';
27 * into
28 * var _baz = require('baz'); var _baz2 = _interopRequireDefault(_baz);
29 *
30 * The import code was already generated in the import preprocessing step, so
31 * we just need to look it up.
32 */
33 private processImport;
34 /**
35 * Erase this import, and return true if it was either of the form "import type" or contained only
36 * "type" named imports. Such imports should not even do a side-effect import.
37 *
38 * The position should end at the import string.
39 */
40 private removeImportAndDetectIfType;
41 private removeRemainingImport;
42 private processIdentifier;
43 processObjectShorthand(): boolean;
44 processExport(): boolean;
45 private processAssignment;
46 private processExportDefault;
47 /**
48 * Transform a declaration like `export var`, `export let`, or `export const`.
49 */
50 private processExportVar;
51 /**
52 * Determine if the export is of the form:
53 * export var/let/const [varName] = [expr];
54 * In other words, determine if function name inference might apply.
55 */
56 private isSimpleExportVar;
57 /**
58 * Transform an `export var` declaration initializing a single variable.
59 *
60 * For example, this:
61 * export const f = () => {};
62 * becomes this:
63 * const f = () => {}; exports.f = f;
64 *
65 * The variable is unused (e.g. exports.f has the true value of the export).
66 * We need to produce an assignment of this form so that the function will
67 * have an inferred name of "f", which wouldn't happen in the more general
68 * case below.
69 */
70 private processSimpleExportVar;
71 /**
72 * Transform normal declaration exports, including handling destructuring.
73 * For example, this:
74 * export const {x: [a = 2, b], c} = d;
75 * becomes this:
76 * ({x: [exports.a = 2, exports.b], c: exports.c} = d;)
77 */
78 private processComplexExportVar;
79 /**
80 * Transform this:
81 * export function foo() {}
82 * into this:
83 * function foo() {} exports.foo = foo;
84 */
85 private processExportFunction;
86 /**
87 * Skip past a function with a name and return that name.
88 */
89 private processNamedFunction;
90 /**
91 * Transform this:
92 * export class A {}
93 * into this:
94 * class A {} exports.A = A;
95 */
96 private processExportClass;
97 /**
98 * Transform this:
99 * export {a, b as c};
100 * into this:
101 * exports.a = a; exports.c = b;
102 *
103 * OR
104 *
105 * Transform this:
106 * export {a, b as c} from './foo';
107 * into the pre-generated Object.defineProperty code from the ImportProcessor.
108 */
109 private processExportBindings;
110 private processExportStar;
111}