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 react-vite and projects with absolute path preview entries on Windows #21545

Merged
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
5 changes: 3 additions & 2 deletions code/lib/builder-vite/src/utils/process-preview-annotation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PreviewAnnotation } from '@storybook/types';
import { resolve, isAbsolute, relative } from 'path';
import slash from 'slash';
import { transformAbsPath } from './transform-abs-path';
import { stripAbsNodeModulesPath } from '@storybook/core-common';

/**
* Preview annotations can take several forms, and vite needs them to be
Expand Down Expand Up @@ -29,8 +29,9 @@ export function processPreviewAnnotation(path: PreviewAnnotation | undefined, pr

// For addon dependencies that use require.resolve(), we need to convert to a bare path
// so that vite will process it as a dependency (cjs -> esm, etc).
// TODO: Evaluate if searching for node_modules in a yarn pnp environment is correct
if (path.includes('node_modules')) {
return transformAbsPath(path);
return stripAbsNodeModulesPath(path);
}

// resolve absolute paths relative to project root
Expand Down
1 change: 1 addition & 0 deletions code/lib/core-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export * from './utils/template';
export * from './utils/validate-config';
export * from './utils/validate-configuration-files';
export * from './utils/satisfies';
export * from './utils/strip-abs-node-modules-path';

export { createFileSystemCache } from './utils/file-cache';
9 changes: 8 additions & 1 deletion code/lib/core-common/src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { join, parse } from 'path';
import { loadCustomPresets } from './utils/load-custom-presets';
import { safeResolve, safeResolveFrom } from './utils/safeResolve';
import { interopRequireDefault } from './utils/interpret-require';
import { stripAbsNodeModulesPath } from './utils/strip-abs-node-modules-path';

const isObject = (val: unknown): val is Record<string, any> =>
val != null && typeof val === 'object' && Array.isArray(val) === false;
Expand Down Expand Up @@ -149,7 +150,13 @@ export const resolveAddonName = (
? {
previewAnnotations: [
previewFileAbsolute
? { bare: previewFile, absolute: previewFileAbsolute }
? {
// TODO: Evaluate if searching for node_modules in a yarn pnp environment is correct
bare: previewFile.includes('node_modules')
? stripAbsNodeModulesPath(previewFile)
Copy link
Member

Choose a reason for hiding this comment

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

It's unfortunate that this is necessary, but it is caused by #21197 (comment) in all projects but also could happen if the user specifies addons in their main.js using absolute paths (for yarn pnp, for instance).

I would love to find a way to avoid so many absolute paths, but it seems like for now at least it's what we are stuck with.

Note that windows will likely still break if the user specifies absolute paths to non-node_modules files, but I don't think that should not be supported anyway (I can't think of a reason it would be needed, at least).

Copy link
Member

Choose a reason for hiding this comment

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

In a pnp situation, the path won't include node_modules.

: previewFile,
absolute: previewFileAbsolute,
}
: previewFile,
],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import path from 'path';
import { normalizePath } from 'vite';
import slash from 'slash';

function normalizePath(id: string) {
Copy link
Member

Choose a reason for hiding this comment

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

This is copied largely from the normalizePath from vite, which was used previously in builder-vite.

return path.posix.normalize(slash(id));
}

// We need to convert from an absolute path, to a traditional node module import path,
// so that vite can correctly pre-bundle/optimize
export function transformAbsPath(absPath: string) {
export function stripAbsNodeModulesPath(absPath: string) {
// TODO: Evaluate if searching for node_modules in a yarn pnp environment is correct
const splits = absPath.split(`node_modules${path.sep}`);
// Return everything after the final "node_modules/"
const module = normalizePath(splits[splits.length - 1]);
Expand Down