Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 26 additions & 6 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,14 @@ const composeFormatConfig = ({
}
};

type DeepRequired<T> = Required<{
[K in keyof T]: T[K] extends Required<T[K]> ? T[K] : DeepRequired<T[K]>;
}>;

const composeShimsConfig = (
format: Format,
shims?: Shims,
): { rsbuildConfig: RsbuildConfig; resolvedShims: Shims } => {
): { rsbuildConfig: RsbuildConfig; enabledShims: DeepRequired<Shims> } => {
const resolvedShims = {
cjs: {
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true,
Expand All @@ -615,6 +619,23 @@ const composeShimsConfig = (
},
};

const enabledShims = {
cjs:
format === 'cjs'
? resolvedShims.cjs
: {
'import.meta.url': false,
},
esm:
format === 'esm'
? resolvedShims.esm
: {
__filename: false,
__dirname: false,
require: false,
},
};

let rsbuildConfig: RsbuildConfig = {};
switch (format) {
case 'esm': {
Expand Down Expand Up @@ -648,7 +669,7 @@ const composeShimsConfig = (
throw new Error(`Unsupported format: ${format}`);
}

return { rsbuildConfig, resolvedShims };
return { rsbuildConfig, enabledShims };
};

export const composeModuleImportWarn = (request: string): string => {
Expand Down Expand Up @@ -1020,7 +1041,7 @@ const composeTargetConfig = (
const composeExternalHelpersConfig = (
externalHelpers: boolean,
pkgJson?: PkgJson,
): RsbuildConfig => {
) => {
let defaultConfig = {
tools: {
swc: {
Expand Down Expand Up @@ -1077,7 +1098,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
redirect = {},
umdName,
} = config;
const { rsbuildConfig: shimsConfig, resolvedShims } = composeShimsConfig(
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
format!,
shims,
);
Expand Down Expand Up @@ -1124,8 +1145,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
);
const cssConfig = composeCssConfig(lcp, config.bundle);
const entryChunkConfig = composeEntryChunkConfig({
enabledImportMetaUrlShim:
format === 'cjs' && !!resolvedShims?.cjs?.['import.meta.url'],
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
});
const dtsConfig = await composeDtsConfig(config, dtsExtension);
const externalsWarnConfig = composeExternalsWarnConfig(
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
: join(cwd, entryPath);
const relativePath = relative(rootDir, dirname(entrySourcePath));
entry = join(declarationDir!, relativePath, basename(entrySourcePath))
// Remove query in file path, such as RSLIB_ENTRY_QUERY.
.replace(/\?.*$/, '')
.replace(
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
Expand Down
6 changes: 3 additions & 3 deletions tests/scripts/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert';
import fs from 'node:fs';
import path, { dirname, join, normalize } from 'node:path';
import { basename, dirname, join, normalize } from 'node:path';
import { fileURLToPath } from 'node:url';
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import {
Expand Down Expand Up @@ -327,9 +327,9 @@ export function queryContent(
basename?: boolean;
} = {},
): string | null {
const basename = options?.basename ?? false;
const useBasename = options?.basename ?? false;
const matched = Object.entries(contents).find(([key]) => {
const toQueried = basename ? path.basename(key) : key;
const toQueried = useBasename ? basename(key) : key;
return typeof query === 'string'
? toQueried === query
: query.test(toQueried);
Expand Down
Loading