Skip to content

Commit

Permalink
Remove @babel/helper-module-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 27, 2024
1 parent 282ffea commit 864be60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
2 changes: 0 additions & 2 deletions packages/forgetti/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"devDependencies": {
"@babel/core": "^7.23.7",
"@types/babel__core": "^7.20.5",
"@types/babel__helper-module-imports": "^7.18.3",
"@types/babel__traverse": "^7.20.3",
"@types/node": "^20.11.1",
"pridepack": "2.6.0",
Expand All @@ -20,7 +19,6 @@
"vitest": "^0.34.6"
},
"dependencies": {
"@babel/helper-module-imports": "^7.22.15",
"@babel/traverse": "^7.23.2",
"@babel/types": "^7.23.0"
},
Expand Down
41 changes: 27 additions & 14 deletions packages/forgetti/src/core/get-import-identifier.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import type * as babel from '@babel/core';
import type * as t from '@babel/types';
import { addDefault, addNamed } from '@babel/helper-module-imports';
import * as t from '@babel/types';
import type { ImportDefinition } from './presets';
import type { StateContext } from './types';

export default function getImportIdentifier(
ctx: StateContext,
export function getImportIdentifier(
state: StateContext,
path: babel.NodePath,
definition: ImportDefinition,
registration: ImportDefinition,
): t.Identifier {
const name = definition.kind === 'default' ? 'default' : definition.name;
const target = `${definition.source}[${name}]`;
const current = ctx.imports.get(target);
const name = registration.kind === 'named' ? registration.name : 'default';
const target = `${registration.source}[${name}]`;
const current = state.imports.get(target);
if (current) {
return current;
}
const newID =
definition.kind === 'named'
? addNamed(path, definition.name, definition.source)
: addDefault(path, definition.source);
ctx.imports.set(target, newID);
return newID;
const programParent = path.scope.getProgramParent();
const uid = programParent.generateUidIdentifier(
registration.kind === 'named' ? registration.name : 'default',
);
const newPath = (
programParent.path as babel.NodePath<t.Program>
).unshiftContainer(
'body',
t.importDeclaration(
[
registration.kind === 'named'
? t.importSpecifier(uid, t.identifier(registration.name))
: t.importDefaultSpecifier(uid),
],
t.stringLiteral(registration.source),
),
)[0];
programParent.registerDeclaration(newPath);
state.imports.set(target, uid);
return uid;
}
13 changes: 0 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 864be60

Please sign in to comment.