2021-10-26 18:50:56 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-01-27 16:48:09 +01:00
|
|
|
// TODO: add ts support for next-swc api
|
|
|
|
// @ts-ignore
|
|
|
|
import { parse } from '../../swc'
|
|
|
|
// @ts-ignore
|
|
|
|
import { getBaseSWCOptions } from '../../swc/options'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
function addExportNames(names: string[], node: any) {
|
|
|
|
switch (node.type) {
|
|
|
|
case 'Identifier':
|
2022-01-27 16:48:09 +01:00
|
|
|
names.push(node.value)
|
2021-10-26 18:50:56 +02:00
|
|
|
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(
|
2022-01-27 16:48:09 +01:00
|
|
|
resourcePath: string,
|
2021-10-26 18:50:56 +02:00
|
|
|
transformedSource: string,
|
2022-01-27 16:48:09 +01:00
|
|
|
names: Array<string>
|
2021-10-26 18:50:56 +02:00
|
|
|
): Promise<void> {
|
2022-01-27 16:48:09 +01:00
|
|
|
const opts = getBaseSWCOptions({
|
|
|
|
filename: resourcePath,
|
|
|
|
globalWindow: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
const { body } = await parse(transformedSource, {
|
|
|
|
...opts.jsc.parser,
|
|
|
|
isModule: true,
|
|
|
|
})
|
2021-10-26 18:50:56 +02:00
|
|
|
for (let i = 0; i < body.length; i++) {
|
|
|
|
const node = body[i]
|
|
|
|
switch (node.type) {
|
2022-01-27 16:48:09 +01:00
|
|
|
// TODO: support export * from module path
|
|
|
|
// case 'ExportAllDeclaration':
|
2022-02-08 20:09:47 +01:00
|
|
|
case 'ExportDefaultExpression':
|
2021-10-26 18:50:56 +02:00
|
|
|
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
|
|
|
|
default:
|
2021-10-26 20:13:51 +02:00
|
|
|
break
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function transformSource(
|
|
|
|
this: any,
|
2022-01-27 16:48:09 +01:00
|
|
|
source: string
|
|
|
|
): Promise<string> {
|
2022-02-28 16:00:28 +01:00
|
|
|
const { resourcePath } = this
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
const transformedSource = source
|
|
|
|
if (typeof transformedSource !== 'string') {
|
|
|
|
throw new Error('Expected source to have been transformed to a string.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const names: string[] = []
|
2022-01-27 16:48:09 +01:00
|
|
|
await parseExportNamesInto(resourcePath, transformedSource, names)
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2021-10-28 19:02:55 +02:00
|
|
|
// next.js/packages/next/<component>.js
|
2022-02-28 16:00:28 +01:00
|
|
|
if (/[\\/]next[\\/](link|image)\.js$/.test(resourcePath)) {
|
2021-10-26 18:50:56 +02:00
|
|
|
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: '
|
2022-02-28 16:00:28 +01:00
|
|
|
newSrc += JSON.stringify(resourcePath)
|
2021-10-26 18:50:56 +02:00
|
|
|
newSrc += ', name: '
|
|
|
|
newSrc += JSON.stringify(name)
|
|
|
|
newSrc += '};\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
return newSrc
|
|
|
|
}
|