From 864be6050121d0efb2c3cefdb7614fa92a1954d5 Mon Sep 17 00:00:00 2001 From: "Alexis H. Munsayac" Date: Sat, 27 Jan 2024 18:11:37 +0800 Subject: [PATCH] Remove `@babel/helper-module-imports` --- packages/forgetti/package.json | 2 - .../src/core/get-import-identifier.ts | 41 ++++++++++++------- pnpm-lock.yaml | 13 ------ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/packages/forgetti/package.json b/packages/forgetti/package.json index 72d2bbf..da1d5a8 100644 --- a/packages/forgetti/package.json +++ b/packages/forgetti/package.json @@ -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", @@ -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" }, diff --git a/packages/forgetti/src/core/get-import-identifier.ts b/packages/forgetti/src/core/get-import-identifier.ts index 8635473..3ea035e 100644 --- a/packages/forgetti/src/core/get-import-identifier.ts +++ b/packages/forgetti/src/core/get-import-identifier.ts @@ -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 + ).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; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66c1ee7..e9b0f3a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -73,9 +73,6 @@ importers: packages/forgetti: dependencies: - '@babel/helper-module-imports': - specifier: ^7.22.15 - version: 7.22.15 '@babel/traverse': specifier: ^7.23.2 version: 7.23.2 @@ -89,9 +86,6 @@ importers: '@types/babel__core': specifier: ^7.20.5 version: 7.20.5 - '@types/babel__helper-module-imports': - specifier: ^7.18.3 - version: 7.18.3 '@types/babel__traverse': specifier: ^7.20.3 version: 7.20.3 @@ -1576,13 +1570,6 @@ packages: '@babel/types': 7.23.0 dev: true - /@types/babel__helper-module-imports@7.18.3: - resolution: {integrity: sha512-2pyr9Vlriessj2KI85SEF7qma8vA3vzquQMw3wn6kL5lsfjH/YxJ1Noytk4/FJElpYybUbyaC37CVfEgfyme9A==} - dependencies: - '@types/babel__core': 7.20.5 - '@types/babel__traverse': 7.20.3 - dev: true - /@types/babel__template@7.4.1: resolution: {integrity: sha512-azBFKemX6kMg5Io+/rdGT0dkGreboUVR0Cdm3fz9QJWpaQGJRQXl7C+6hOTCZcMll7KFyEQpgbYI2lHdsS4U7g==} dependencies: