Skip to content
Open
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
80 changes: 80 additions & 0 deletions packages/cli/src/__tests__/pack-bin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { UserConfig } from 'tsdown';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';

const originalArgv = process.argv;

const packMocks = vi.hoisted(() => ({
buildWithConfigs: vi.fn<() => Promise<void>>(async () => undefined),
enableDebug: vi.fn<(debug: boolean | string | Array<string>) => void>(),
globalLogger: { error: vi.fn<(...args: Array<unknown>) => void>() },
resolveUserConfig: vi.fn<(config: UserConfig) => Promise<Array<never>>>(async () => []),
resolveViteConfig: vi.fn<() => Promise<{ pack?: UserConfig | Array<UserConfig> } | undefined>>(
async () => undefined,
),
}));

vi.mock('vite/pack', () => ({
buildWithConfigs: packMocks.buildWithConfigs,
enableDebug: packMocks.enableDebug,
globalLogger: packMocks.globalLogger,
resolveUserConfig: packMocks.resolveUserConfig,
}));

vi.mock('../resolve-vite-config.ts', () => ({
resolveViteConfig: packMocks.resolveViteConfig,
}));

beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
process.argv = ['node', 'pack-bin'];
});

afterEach(() => {
process.argv = originalArgv;
});

it('preserves a non-array plugin object when dts is enabled', async () => {
const userPlugin = { name: 'custom-user-plugin' };
packMocks.resolveViteConfig.mockResolvedValueOnce({
pack: { dts: true, plugins: userPlugin },
});

await import('../pack-bin.ts');

expect(packMocks.resolveUserConfig).toHaveBeenCalledOnce();
expect(packMocks.resolveUserConfig.mock.calls[0]?.[0].plugins).toEqual([
userPlugin,
expect.objectContaining({ name: 'vite-plus:external-dts-type-only' }),
]);
});

it('preserves a Promise plugin option when dts is enabled', async () => {
const pluginPromise = Promise.resolve({ name: 'async-user-plugin' });
packMocks.resolveViteConfig.mockResolvedValueOnce({
pack: { dts: true, plugins: pluginPromise },
});

await import('../pack-bin.ts');

expect(packMocks.resolveUserConfig).toHaveBeenCalledOnce();
expect(packMocks.resolveUserConfig.mock.calls[0]?.[0].plugins).toEqual([
pluginPromise,
expect.objectContaining({ name: 'vite-plus:external-dts-type-only' }),
]);
});

it('appends the external dts plugin to an existing plugins array', async () => {
const userPlugin = { name: 'array-user-plugin' };
packMocks.resolveViteConfig.mockResolvedValueOnce({
pack: { dts: true, plugins: [userPlugin] },
});

await import('../pack-bin.ts');

expect(packMocks.resolveUserConfig).toHaveBeenCalledOnce();
expect(packMocks.resolveUserConfig.mock.calls[0]?.[0].plugins).toEqual([
userPlugin,
expect.objectContaining({ name: 'vite-plus:external-dts-type-only' }),
]);
});
9 changes: 7 additions & 2 deletions packages/cli/src/pack-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,13 @@ cli
merged.envPrefix ??= DEFAULT_ENV_PREFIXES;
// Keep postcss/lightningcss external to the dts bundle (see plugin doc)
if (merged.dts) {
const existingPlugins = Array.isArray(merged.plugins) ? merged.plugins : [];
merged.plugins = [...existingPlugins, externalDtsTypeOnlyPlugin()];
if (Array.isArray(merged.plugins)) {
merged.plugins = [...merged.plugins, externalDtsTypeOnlyPlugin()];
} else if (merged.plugins) {
merged.plugins = [merged.plugins, externalDtsTypeOnlyPlugin()];
} else {
merged.plugins = [externalDtsTypeOnlyPlugin()];
}
}
const resolvedConfig = await resolveUserConfig(merged, flags, configDeps);
resolvedConfig.forEach((config) => config.configDeps.forEach((dep) => configDeps.add(dep)));
Expand Down