From 9c27b2f52fb5c0134ba547483eb39777394ac922 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Mon, 12 Aug 2024 19:38:48 +0800 Subject: [PATCH] fix: preserve import.meta.url in ESM --- e2e/cases/shims/esm/rslib.config.ts | 14 ++++++++++++++ e2e/cases/shims/esm/src/index.ts | 7 +++++++ e2e/cases/shims/index.test.ts | 19 +++++++++++++++++++ packages/core/src/config.ts | 10 ++++++++++ packages/core/src/index.ts | 1 + packages/core/src/types/index.ts | 4 ++-- .../tests/__snapshots__/config.test.ts.snap | 7 +++++++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 e2e/cases/shims/esm/rslib.config.ts create mode 100644 e2e/cases/shims/esm/src/index.ts create mode 100644 e2e/cases/shims/index.test.ts diff --git a/e2e/cases/shims/esm/rslib.config.ts b/e2e/cases/shims/esm/rslib.config.ts new file mode 100644 index 00000000..64748a91 --- /dev/null +++ b/e2e/cases/shims/esm/rslib.config.ts @@ -0,0 +1,14 @@ +import { generateBundleEsmConfig } from '@e2e/helper'; +import { defineConfig } from '@rslib/core'; + +export default defineConfig({ + lib: [generateBundleEsmConfig(__dirname)], + output: { + target: 'node', + }, + source: { + entry: { + main: './src/index.ts', + }, + }, +}); diff --git a/e2e/cases/shims/esm/src/index.ts b/e2e/cases/shims/esm/src/index.ts new file mode 100644 index 00000000..ff0c165b --- /dev/null +++ b/e2e/cases/shims/esm/src/index.ts @@ -0,0 +1,7 @@ +export const foo = () => { + const d1 = __dirname; + const d2 = __dirname; + const f1 = __filename; + const f2 = __dirname; + const importMetaUrl = import.meta.url; +}; diff --git a/e2e/cases/shims/index.test.ts b/e2e/cases/shims/index.test.ts new file mode 100644 index 00000000..ef0b9de6 --- /dev/null +++ b/e2e/cases/shims/index.test.ts @@ -0,0 +1,19 @@ +import { join } from 'node:path'; +import { buildAndGetResults } from '@e2e/helper'; +import { expect, test } from 'vitest'; + +test('shims for __dirname and __filename in ESM', async () => { + const fixturePath = join(__dirname, 'esm'); + const { entries } = await buildAndGetResults(fixturePath); + for (const shim of [ + 'import {fileURLToPath as __webpack_fileURLToPath__} from "url";', + "var src_dirname = __webpack_fileURLToPath__(import.meta.url + '/..').slice(0, -1);", + 'var src_filename = __webpack_fileURLToPath__(import.meta.url);', + // import.meta.url should not be substituted + 'const importMetaUrl = import.meta.url;', + ]) { + expect(entries.esm).toContain(shim); + } +}); + +test.todo('shims for import.meta.url in CJS', async () => {}); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 050a206e..1d065fc1 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -186,6 +186,13 @@ const composeFormatConfig = (format: Format): RsbuildConfig => { type: 'modern-module', }, }, + module: { + parser: { + javascript: { + importMeta: false, + }, + }, + }, optimization: { concatenateModules: true, }, @@ -430,6 +437,9 @@ const composeTargetConfig = (target = 'web'): RsbuildConfig => { tools: { rspack: { target: ['node'], + // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`, + // and leave them as-is in the rest of the cases. + // { node: { __dirname: ..., __filename: ... } } }, }, output: { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 32147d2d..09ed4b59 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -3,5 +3,6 @@ export { runCli } from './cli/commands'; export { defineConfig, loadConfig } from './config'; export { build } from './build'; export { logger } from './utils/logger'; +export type * from './types'; export const version: string = RSLIB_VERSION; diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index 1d6a9e83..235e5fdb 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -1,2 +1,2 @@ -export * from './config'; -export * from './utils'; +export type * from './config'; +export type * from './utils'; diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index 8f81e961..42a4a5bc 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -43,6 +43,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 "outputModule": true, }, "externalsType": "module-import", + "module": { + "parser": { + "javascript": { + "importMeta": false, + }, + }, + }, "optimization": { "concatenateModules": true, },