-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cjsInterop support without splitting flag
- Loading branch information
Showing
4 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,125 @@ | ||
import type { | ||
ExportDefaultExpression, | ||
ModuleDeclaration, | ||
ParseOptions, | ||
} from '@swc/core' | ||
import type { Visitor } from '@swc/core/Visitor' | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
import { PrettyError } from '../errors' | ||
import { Plugin } from '../plugin' | ||
import { localRequire } from '../utils' | ||
|
||
export const cjsInterop = (): Plugin => { | ||
return { | ||
name: 'cjs-interop', | ||
|
||
async renderChunk(code, info) { | ||
const { entryPoint } = info | ||
if ( | ||
!this.options.cjsInterop || | ||
this.format !== 'cjs' || | ||
info.type !== 'chunk' || | ||
!/\.(js|cjs)$/.test(info.path) || | ||
!info.entryPoint || | ||
info.exports?.length !== 1 || | ||
info.exports[0] !== 'default' | ||
!entryPoint | ||
) { | ||
return | ||
} | ||
|
||
if (this.splitting) { | ||
// there is exports metadata when cjs+splitting is set | ||
if (info.exports?.length !== 1 || info.exports[0] !== 'default') return | ||
} else { | ||
const swc: typeof import('@swc/core') = localRequire('@swc/core') | ||
const { Visitor }: typeof import('@swc/core/Visitor') = | ||
localRequire('@swc/core/Visitor') | ||
if (!swc || !Visitor) { | ||
throw new PrettyError( | ||
`@swc/core is required for cjsInterop when splitting is not enabled. Please install it with \`npm install @swc/core -D\`` | ||
) | ||
} | ||
|
||
try { | ||
const entrySource = await fs.readFile(entryPoint, { | ||
encoding: 'utf8', | ||
}) | ||
const ast = await swc.parse(entrySource, getParseOptions(entryPoint)) | ||
const visitor = createExportVisitor(Visitor) | ||
visitor.visitProgram(ast) | ||
|
||
if ( | ||
!visitor.hasExportDefaultExpression || | ||
visitor.hasNonDefaultExportDeclaration | ||
) | ||
return | ||
} catch { | ||
return | ||
} | ||
} | ||
|
||
return { | ||
code: code + '\nmodule.exports = exports.default;\n', | ||
code: code + '\nmodule.exports=module.exports.default;\n', | ||
map: info.map, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
function getParseOptions(filename: string): ParseOptions { | ||
switch (path.extname(filename).toLowerCase()) { | ||
case '.js': | ||
return { | ||
syntax: 'ecmascript', | ||
decorators: true, | ||
} | ||
case '.jsx': | ||
return { | ||
syntax: 'ecmascript', | ||
decorators: true, | ||
jsx: true, | ||
} | ||
case '.ts': | ||
return { | ||
syntax: 'typescript', | ||
decorators: true, | ||
} | ||
case '.tsx': | ||
return { | ||
syntax: 'typescript', | ||
decorators: true, | ||
tsx: true, | ||
} | ||
default: | ||
throw new Error(`Unknown file type: ${filename}`) | ||
} | ||
} | ||
|
||
function createExportVisitor(VisitorCtor: typeof Visitor) { | ||
class ExportVisitor extends VisitorCtor { | ||
hasNonDefaultExportDeclaration = false | ||
hasExportDefaultExpression = false | ||
constructor() { | ||
super() | ||
type ExtractDeclName<T> = T extends `visit${infer N}` ? N : never | ||
const nonDefaultExportDecls: ExtractDeclName<keyof Visitor>[] = [ | ||
'ExportDeclaration', // export const a = {} | ||
'ExportNamedDeclaration', // export {}, export * as a from './a' | ||
'ExportAllDeclaration', // export * from './a' | ||
] | ||
|
||
nonDefaultExportDecls.forEach((decl) => { | ||
this[`visit${decl}`] = (n: any) => { | ||
this.hasNonDefaultExportDeclaration = true | ||
return n | ||
} | ||
}) | ||
} | ||
visitExportDefaultExpression( | ||
n: ExportDefaultExpression | ||
): ModuleDeclaration { | ||
this.hasExportDefaultExpression = true | ||
return n | ||
} | ||
} | ||
return new ExportVisitor() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters