Skip to content

Fix double-default problem with psuedo CJS, psuedo ESM aka 'babel' modules #43

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

Closed
Closed
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
24 changes: 23 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@ import { ImportUtil, type Importer } from 'babel-import-util';
import { globalId } from './global-id.ts';

// TS can't find declarations for this package (there aren't any)
// this package is actually CJS, so there are no named exports, only default exports.
// TypeScript sometimes trolls us and converts module.exports = { ... } to named exports.
//
// Because we don't know the environment of *our* consumers, we need to support both
// ways of accessing `decoratorSyntax` within @babel/plugin-syntax-decorators
// @ts-ignore
import { default as decoratorSyntax } from '@babel/plugin-syntax-decorators';
import * as maybeSyntaxDecorators from '@babel/plugin-syntax-decorators';

// We *may* have a double default now
function getSyntaxDecorators(maybeModule: any): (...args: any[]) => any {
if (!maybeModule) {
throw new Error(
`Could not find 'decoratorSyntax' in '@babel/plugin-syntax-decorators'`,
);
}

if ('default' in maybeModule) {
return getSyntaxDecorators(maybeModule.default);
}

return maybeModule;
}

const decoratorSyntax = getSyntaxDecorators(maybeSyntaxDecorators);

interface State extends Babel.PluginPass {
currentClassBodies: t.ClassBody[];
Expand Down