Skip to content

Commit ed7174b

Browse files
committed
Cleanup
1 parent 0f58806 commit ed7174b

File tree

2 files changed

+47
-35
lines changed
  • packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule

2 files changed

+47
-35
lines changed

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/index.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
generateExportStatement,
1212
generateEsModuleSpecifier,
1313
generateInteropRequire,
14+
generateInteropRequireExpression,
1415
} from './utils';
1516
import { customGenerator } from './generator';
1617

@@ -193,45 +194,16 @@ export function convertEsModule(code: string) {
193194
if (specifier.type === n.ImportDefaultSpecifier) {
194195
// import Test from 'test';
195196
// const _test = require('test');
196-
// var Test = interopRequireDefault(_test);
197+
// var Test = interopRequireDefault(_test).default;
197198
localName = specifier.local.name;
198199
importName = 'default';
199200
addDefaultInterop();
200201

201-
program.body.splice(i, 0, {
202-
type: 'VariableDeclaration',
203-
kind: 'var',
204-
declarations: [
205-
{
206-
type: 'VariableDeclarator',
207-
init: {
208-
type: 'MemberExpression',
209-
object: {
210-
type: 'CallExpression',
211-
callee: {
212-
type: 'Identifier',
213-
name: '$_csb__interopRequireDefault',
214-
},
215-
arguments: [
216-
{
217-
type: 'Identifier',
218-
name: varName,
219-
},
220-
],
221-
},
222-
computed: false,
223-
property: {
224-
type: 'Identifier',
225-
name: 'default',
226-
},
227-
},
228-
id: {
229-
type: 'Identifier',
230-
name: localName,
231-
},
232-
},
233-
],
234-
});
202+
program.body.splice(
203+
i,
204+
0,
205+
generateInteropRequireExpression(varName, localName)
206+
);
235207
return;
236208
}
237209
if (specifier.type === n.ImportSpecifier) {

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/utils.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,43 @@ export function generateInteropRequire() {
363363
},
364364
};
365365
}
366+
367+
export function generateInteropRequireExpression(
368+
varName: string,
369+
localName: string
370+
) {
371+
return {
372+
type: n.VariableDeclaration,
373+
kind: 'var' as 'var',
374+
declarations: [
375+
{
376+
type: n.VariableDeclarator,
377+
init: {
378+
type: n.MemberExpression,
379+
object: {
380+
type: n.CallExpression,
381+
callee: {
382+
type: n.Identifier,
383+
name: '$_csb__interopRequireDefault',
384+
},
385+
arguments: [
386+
{
387+
type: n.Identifier,
388+
name: varName,
389+
},
390+
],
391+
},
392+
computed: false,
393+
property: {
394+
type: n.Identifier,
395+
name: 'default',
396+
},
397+
},
398+
id: {
399+
type: n.Identifier,
400+
name: localName,
401+
},
402+
},
403+
],
404+
};
405+
}

0 commit comments

Comments
 (0)