Skip to content

Commit

Permalink
Fix import declarations w/o specifiers in evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 3, 2021
1 parent 000ba7d commit 63d15f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
37 changes: 20 additions & 17 deletions lib/plugin/recma-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export function recmaDocument(options = {}) {
function handleEsm(node) {
var value
var replace
var id
var init
var declarations

// Rewrite the source of the `import` / `export … from`.
Expand Down Expand Up @@ -323,31 +323,34 @@ export function recmaDocument(options = {}) {
}

// ```
// import 'a'
// //=> await import('a')
// import a from 'b'
// //=> const {default: a} = await import('b')
// export {a, b as c} from 'd'
// //=> const {a, c: b} = await import('d')
// export * from 'a'
// //=> const _exportAll0 = await import('a')
// ```
id = node.specifiers
? specifiersToObjectPattern(node.specifiers)
: u('Identifier', {name: '_exportAll' + ++exportAllCount})
init = u('AwaitExpression', {
argument: create(node, u('ImportExpression', {source: node.source}))
})

replace = u('VariableDeclaration', {
kind: 'const',
declarations: [
u('VariableDeclarator', {
id,
init: u('AwaitExpression', {
argument: create(
node,
u('ImportExpression', {source: node.source})
)
if (node.specifiers && node.specifiers.length === 0) {
replace = init
} else {
replace = u('VariableDeclaration', {
kind: 'const',
declarations: [
u('VariableDeclarator', {
id: node.specifiers
? specifiersToObjectPattern(node.specifiers)
: u('Identifier', {name: '_exportAll' + ++exportAllCount}),
init
})
})
]
})
]
})
}
} else if (node.declaration) {
replace = node.declaration
} else {
Expand Down
14 changes: 13 additions & 1 deletion test/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
import * as runtime from 'react/jsx-runtime.js'
import {renderToStaticMarkup} from 'react-dom/server.js'
import test from 'tape'
import {evaluate, evaluateSync} from '../index.js'
import {evaluate, evaluateSync, compile} from '../index.js'

test('xdm (evaluate)', async function (t) {
t.throws(
Expand Down Expand Up @@ -85,6 +85,18 @@ test('xdm (evaluate)', async function (t) {
'should support an `import` of a full url w/ `useDynamicImport`'
)

t.match(
String(
await compile(
'import "a"',
// @ts-ignore runtime.js does not have a typing
{outputFormat: 'function-body', useDynamicImport: true, ...runtime}
)
),
/\nawait import\("a"\);?\n/,
'should support an `import` w/o specifiers w/ `useDynamicImport`'
)

t.match(
renderToStaticMarkup(
React.createElement(
Expand Down

0 comments on commit 63d15f4

Please sign in to comment.