Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getConfigExport, isConfigFunction } from './helpers.js';
import { loadWithJiti } from './jiti.js';
import { loadWithJiti, normalizeContext } from './jiti.js';
import { JS_CONFIG_REGEXP, loadWithNative } from './native.js';
import { resolveConfigPath } from './resolve.js';
import type {
Expand Down Expand Up @@ -75,7 +75,10 @@ export async function loadConfig<
}
}

let usedJiti = false;

if (!loadedConfig) {
usedJiti = true;
loadedConfig = await loadWithJiti<Config, Params>(
configPath,
exportName,
Expand All @@ -93,14 +96,14 @@ export async function loadConfig<
}

return {
content: result,
content: usedJiti ? normalizeContext(result) : result,
filePath: configPath,
dependencies,
};
}

return {
content: configExport,
content: usedJiti ? normalizeContext(configExport) : configExport,
filePath: configPath,
dependencies,
};
Expand Down
23 changes: 23 additions & 0 deletions src/jiti.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import { normalize } from 'node:path';
import { getConfigExport } from './helpers.js';
import type { ConfigDefinition, LoadedConfig } from './types.js';

type ConfigWithContext = { context: string };

const hasContext = (config: unknown): config is ConfigWithContext =>
typeof config === 'object' &&
config !== null &&
typeof (config as ConfigWithContext).context === 'string';

/**
* jiti exposes a POSIX-style `__dirname` on Windows, so a `context` built from
* it keeps forward slashes and no longer matches the native paths the bundler
* compares it against. Drop this once jiti stops rewriting `__dirname`.
*/
export const normalizeContext = <Config>(content: Config): Config => {
for (const config of Array.isArray(content) ? content : [content]) {
if (hasContext(config)) {
config.context = normalize(config.context);
}
}

return content;
};

export const loadWithJiti = async <Config, Params extends unknown[]>(
configPath: string,
exportName: string | false,
Expand Down
1 change: 1 addition & 0 deletions tests/normalize-context/demo.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { context: 'D:/a/project', name: 'test' };
1 change: 1 addition & 0 deletions tests/normalize-context/function.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => ({ context: 'D:/a/project' });
40 changes: 40 additions & 0 deletions tests/normalize-context/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect, test } from 'rstack/test';
import { loadConfig } from '../../src/index';

const __dirname = import.meta.dirname;

const nativeContext =
process.platform === 'win32' ? 'D:\\a\\project' : 'D:/a/project';

test('normalizes the context field of a jiti loaded config', async () => {
const result = await loadConfig<{ context: string; name: string }>({
cwd: __dirname,
path: 'demo.config.ts',
loader: 'jiti',
});

expect(result.content).toEqual({ context: nativeContext, name: 'test' });
});

test('normalizes the context field of a function config', async () => {
const result = await loadConfig<{ context: string }>({
cwd: __dirname,
path: 'function.config.ts',
loader: 'jiti',
});

expect(result.content.context).toBe(nativeContext);
});

test('normalizes the context field of every config in an array', async () => {
const result = await loadConfig<[{ context: string }, { name: string }]>({
cwd: __dirname,
path: 'multi.config.ts',
loader: 'jiti',
});

expect(result.content).toEqual([
{ context: nativeContext },
{ name: 'no-context' },
]);
});
1 change: 1 addition & 0 deletions tests/normalize-context/multi.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [{ context: 'D:/a/project/src/..' }, { name: 'no-context' }];