From efa65b5aa011053f57279eb9233c83331549e3f8 Mon Sep 17 00:00:00 2001 From: Daniel Grefberg Date: Mon, 7 Aug 2023 10:30:00 +0200 Subject: [PATCH] fix: inject esm/cjs shims and fix import.meta compilation --- .changeset/famous-mangos-thank.md | 5 +++++ src/cli/cli.ts | 2 +- src/shared.ts | 2 +- src/utils/filesystem.ts | 6 +++--- tsup.config.ts | 1 + 5 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 .changeset/famous-mangos-thank.md diff --git a/.changeset/famous-mangos-thank.md b/.changeset/famous-mangos-thank.md new file mode 100644 index 0000000..327fd20 --- /dev/null +++ b/.changeset/famous-mangos-thank.md @@ -0,0 +1,5 @@ +--- +'clean-modules': patch +--- + +Fix `import.meta.url` not being transpiled to an equivalent value for CJS builds. diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 0da4a54..3500885 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -7,7 +7,7 @@ import { CleanCommand } from './commands/clean.command.js'; const [_node, _app, ...args] = process.argv; const esmRequire = createRequire(import.meta.url); -const cliDir = fileDir(import.meta); +const cliDir = fileDir(import.meta.url); const { name, version } = esmRequire(path.resolve(cliDir, '..', '..', 'package.json')); const cli = new Cli({ diff --git a/src/shared.ts b/src/shared.ts index 30e4893..3f111a8 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -2,7 +2,7 @@ import path from 'path'; import { fileDir } from './utils/filesystem.js'; export const DEFAULT_GLOBS_FILE_PATH = path.resolve( - fileDir(import.meta), + fileDir(import.meta.url), '..', '.cleanmodules-default' ); diff --git a/src/utils/filesystem.ts b/src/utils/filesystem.ts index a516502..2f76f24 100644 --- a/src/utils/filesystem.ts +++ b/src/utils/filesystem.ts @@ -187,8 +187,8 @@ export async function removeFiles( /** * Get directory of the file directory, like CommonJS `__dirname`. - * @example const thisFilesDir = fileDir(import.meta); + * @example const thisFilesDir = fileDir(import.meta.url); */ -export function fileDir(importMeta: ImportMeta) { - return path.dirname(fileURLToPath(importMeta.url)); +export function fileDir(importMetaUrl: string) { + return path.dirname(fileURLToPath(importMetaUrl)); } diff --git a/tsup.config.ts b/tsup.config.ts index 4a0c10d..c16cba8 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -8,5 +8,6 @@ export default defineConfig({ splitting: true, sourcemap: false, clean: true, + shims: true, dts: true, });