1f1632979c
Closes #33538. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
125 lines
3.4 KiB
TypeScript
125 lines
3.4 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import { parse } from '../../swc'
|
|
|
|
function addExportNames(names: string[], node: any) {
|
|
switch (node.type) {
|
|
case 'Identifier':
|
|
names.push(node.value)
|
|
return
|
|
case 'ObjectPattern':
|
|
for (let i = 0; i < node.properties.length; i++)
|
|
addExportNames(names, node.properties[i])
|
|
return
|
|
case 'ArrayPattern':
|
|
for (let i = 0; i < node.elements.length; i++) {
|
|
const element = node.elements[i]
|
|
if (element) addExportNames(names, element)
|
|
}
|
|
return
|
|
case 'Property':
|
|
addExportNames(names, node.value)
|
|
return
|
|
case 'AssignmentPattern':
|
|
addExportNames(names, node.left)
|
|
return
|
|
case 'RestElement':
|
|
addExportNames(names, node.argument)
|
|
return
|
|
case 'ParenthesizedExpression':
|
|
addExportNames(names, node.expression)
|
|
return
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
|
|
async function parseExportNamesInto(
|
|
resourcePath: string,
|
|
transformedSource: string,
|
|
names: Array<string>
|
|
): Promise<void> {
|
|
const { body } = await parse(transformedSource, {
|
|
filename: resourcePath,
|
|
isModule: true,
|
|
})
|
|
for (let i = 0; i < body.length; i++) {
|
|
const node = body[i]
|
|
switch (node.type) {
|
|
// TODO: support export * from module path
|
|
// case 'ExportAllDeclaration':
|
|
case 'ExportDefaultExpression':
|
|
case 'ExportDefaultDeclaration':
|
|
names.push('default')
|
|
continue
|
|
case 'ExportNamedDeclaration':
|
|
if (node.declaration) {
|
|
if (node.declaration.type === 'VariableDeclaration') {
|
|
const declarations = node.declaration.declarations
|
|
for (let j = 0; j < declarations.length; j++) {
|
|
addExportNames(names, declarations[j].id)
|
|
}
|
|
} else {
|
|
addExportNames(names, node.declaration.id)
|
|
}
|
|
}
|
|
if (node.specificers) {
|
|
const specificers = node.specificers
|
|
for (let j = 0; j < specificers.length; j++) {
|
|
addExportNames(names, specificers[j].exported)
|
|
}
|
|
}
|
|
continue
|
|
case 'ExportDeclaration':
|
|
if (node.declaration?.identifier) {
|
|
addExportNames(names, node.declaration.identifier)
|
|
}
|
|
continue
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
export default async function transformSource(
|
|
this: any,
|
|
source: string
|
|
): Promise<string> {
|
|
const { resourcePath } = this
|
|
|
|
const transformedSource = source
|
|
if (typeof transformedSource !== 'string') {
|
|
throw new Error('Expected source to have been transformed to a string.')
|
|
}
|
|
|
|
const names: string[] = []
|
|
await parseExportNamesInto(resourcePath, transformedSource, names)
|
|
|
|
// next.js/packages/next/<component>.js
|
|
if (/[\\/]next[\\/](link|image)\.js$/.test(resourcePath)) {
|
|
names.push('default')
|
|
}
|
|
|
|
let newSrc =
|
|
"const MODULE_REFERENCE = Symbol.for('react.module.reference');\n"
|
|
for (let i = 0; i < names.length; i++) {
|
|
const name = names[i]
|
|
if (name === 'default') {
|
|
newSrc += 'export default '
|
|
} else {
|
|
newSrc += 'export const ' + name + ' = '
|
|
}
|
|
newSrc += '{ $$typeof: MODULE_REFERENCE, filepath: '
|
|
newSrc += JSON.stringify(resourcePath)
|
|
newSrc += ', name: '
|
|
newSrc += JSON.stringify(name)
|
|
newSrc += '};\n'
|
|
}
|
|
|
|
return newSrc
|
|
}
|