Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite: Fix wrong import paths when configDir is not in project root #30206

Merged
merged 1 commit into from
Jan 7, 2025
Merged
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
13 changes: 7 additions & 6 deletions code/builders/builder-vite/src/codegen-importfn-script.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

import { toImportFn } from './codegen-importfn-script';

describe('toImportFn', () => {
it('should correctly map story paths to import functions for absolute paths on Linux', async () => {
const root = '/absolute/path';
vi.spyOn(process, 'cwd').mockReturnValue('/absolute/path');

const stories = ['/absolute/path/to/story1.js', '/absolute/path/to/story2.js'];

const result = await toImportFn(root, stories);
const result = await toImportFn(stories);

expect(result).toMatchInlineSnapshot(`
"const importers = {
Expand All @@ -22,10 +23,10 @@ describe('toImportFn', () => {
});

it('should correctly map story paths to import functions for absolute paths on Windows', async () => {
const root = 'C:\\absolute\\path';
vi.spyOn(process, 'cwd').mockReturnValue('C:\\absolute\\path');
const stories = ['C:\\absolute\\path\\to\\story1.js', 'C:\\absolute\\path\\to\\story2.js'];

const result = await toImportFn(root, stories);
const result = await toImportFn(stories);

expect(result).toMatchInlineSnapshot(`
"const importers = {
Expand All @@ -43,7 +44,7 @@ describe('toImportFn', () => {
const root = '/absolute/path';
const stories: string[] = [];

const result = await toImportFn(root, stories);
const result = await toImportFn(stories);

expect(result).toMatchInlineSnapshot(`
"const importers = {};
Expand Down
6 changes: 3 additions & 3 deletions code/builders/builder-vite/src/codegen-importfn-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function toImportPath(relativePath: string) {
*
* @param stories An array of absolute story paths.
*/
export async function toImportFn(root: string, stories: string[]) {
export async function toImportFn(stories: string[]) {
const objectEntries = stories.map((file) => {
const relativePath = relative(root, file);
const relativePath = relative(process.cwd(), file);
Comment on lines +32 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Removing the root parameter and hardcoding process.cwd() could make this function less flexible for future use cases where a different root directory is needed. Consider keeping the parameter but defaulting to process.cwd().


return [toImportPath(relativePath), genDynamicImport(normalize(file))] as [string, string];
});
Expand All @@ -51,5 +51,5 @@ export async function generateImportFnScriptCode(options: Options): Promise<stri

// We can then call toImportFn to create a function that can be used to load each story dynamically.
// eslint-disable-next-line @typescript-eslint/return-await
return await toImportFn(options.projectRoot || process.cwd(), stories);
return await toImportFn(stories);
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 3 additions & 3 deletions code/builders/builder-vite/src/vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ export async function commonConfig(

const { viteConfigPath } = await getBuilderOptions<BuilderOptions>(options);

options.projectRoot = options.projectRoot || resolve(options.configDir, '..');
const projectRoot = resolve(options.configDir, '..');

// I destructure away the `build` property from the user's config object
// I do this because I can contain config that breaks storybook, such as we had in a lit project.
// If the user needs to configure the `build` they need to do so in the viteFinal function in main.js.
const { config: { build: buildProperty = undefined, ...userConfig } = {} } =
(await loadConfigFromFile(configEnv, viteConfigPath, options.projectRoot)) ?? {};
(await loadConfigFromFile(configEnv, viteConfigPath, projectRoot)) ?? {};

const sbConfig: InlineConfig = {
configFile: false,
cacheDir: resolvePathInStorybookCache('sb-vite', options.cacheKey),
root: options.projectRoot,
root: projectRoot,
// Allow storybook deployed as subfolder. See https://github.com/storybookjs/builder-vite/issues/238
base: './',
plugins: await pluginConfig(options),
Expand Down
1 change: 0 additions & 1 deletion code/core/src/types/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export interface BuilderOptions {
ignorePreview?: boolean;
cache?: FileSystemCache;
configDir: string;
projectRoot?: string;
docsMode?: boolean;
features?: StorybookConfigRaw['features'];
versionCheck?: VersionCheck;
Expand Down
Loading