Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta: fix support of export declaration in source files #3558

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bin/build-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ async function isTypeModule (file) {
return typeModule
}

// eslint-disable-next-line no-shadow
function transformExportDeclarations (path) {
const { value } = path.node.source
if (value.endsWith('.jsx') && value.startsWith('./')) {
// Rewrite .jsx imports to .js:
path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign
}

path.replaceWith(
t.assignmentExpression(
'=',
t.memberExpression(t.identifier('module'), t.identifier('exports')),
t.callExpression(t.identifier('require'), [path.node.source]),
),
)
}

async function buildLib () {
const metaMtimes = await Promise.all(META_FILES.map((filename) => lastModified(path.join(__dirname, '..', filename))))
const metaMtime = Math.max(...metaMtimes)
Expand Down Expand Up @@ -122,6 +139,33 @@ async function buildLib () {
)
}
},
ExportAllDeclaration: transformExportDeclarations,
// eslint-disable-next-line no-shadow,consistent-return
ExportNamedDeclaration (path) {
if (path.node.source != null) return transformExportDeclarations(path)
},
// eslint-disable-next-line no-shadow
ExportDefaultDeclaration (path) {
const moduleExports = t.memberExpression(t.identifier('module'), t.identifier('exports'))
if (!t.isDeclaration(path.node.declaration)) {
path.replaceWith(
t.assignmentExpression('=', moduleExports, path.node.declaration),
)
} else if (path.node.declaration.id != null) {
const { id } = path.node.declaration
path.insertBefore(path.node.declaration)
path.replaceWith(
t.assignmentExpression('=', moduleExports, id),
)
} else {
const id = t.identifier('_default')
path.node.declaration.id = id // eslint-disable-line no-param-reassign
path.insertBefore(path.node.declaration)
path.replaceWith(
t.assignmentExpression('=', moduleExports, id),
)
}
},
},
}] : undefined
const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
Expand Down