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

Release: Prerelease 8.0.0-rc.1 #26268

Merged
merged 22 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1d5e9f8
filter docgen import resolution files to only JS
ndelangen Feb 29, 2024
9940766
Merge branch 'next' into norbert/fix-react-docgen-vite-plugin
ndelangen Feb 29, 2024
7246a17
making a little simplification, see if it causes a regression too
ndelangen Feb 29, 2024
dd3ba90
fix
ndelangen Feb 29, 2024
686867f
fix issue with files without extension
ndelangen Feb 29, 2024
5fa9f5b
add regression test for import of css file
ndelangen Feb 29, 2024
0d52b80
remove it
ndelangen Feb 29, 2024
6a6e4cf
fix
ndelangen Feb 29, 2024
470ff23
Docs: Snippets fix testing package imports
jonniebigodes Feb 29, 2024
cea2827
fix for webpack as well!
ndelangen Feb 29, 2024
b7cdb94
Fix addon compatibility check error reporting in storybook dev
yannbf Feb 29, 2024
c594ba9
Merge pull request #26256 from storybookjs/docs_fix_login_form_snippets
jonniebigodes Feb 29, 2024
899ba75
make use of RESOLVE_EXTENSIONS as requested
ndelangen Feb 29, 2024
ef36c36
add dependency
ndelangen Feb 29, 2024
463d488
duplicated to code to prevent ugly import
ndelangen Feb 29, 2024
da5ee52
added extra extensions
ndelangen Feb 29, 2024
70ee50f
fix filter logic
ndelangen Feb 29, 2024
c9d7eb1
Merge pull request #26258 from storybookjs/fix/incompatible-addons-error
yannbf Feb 29, 2024
075f911
Merge pull request #26254 from storybookjs/norbert/fix-react-docgen-v…
ndelangen Mar 1, 2024
c3335f7
Onboarding: Fix manager dist reference
shilman Mar 2, 2024
64be727
Merge pull request #26282 from storybookjs/shilman/26280-fix-onboardi…
shilman Mar 2, 2024
2d75e1f
Write changelog for 8.0.0-rc.1 [skip ci]
storybook-bot Mar 2, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.prerelease.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 8.0.0-rc.1

- CLI: Fix addon compatibility check error reporting in storybook dev - [#26258](https://github.com/storybookjs/storybook/pull/26258), thanks [@yannbf](https://github.com/yannbf)!
- Onboarding: Fix manager dist reference - [#26282](https://github.com/storybookjs/storybook/pull/26282), thanks [@shilman](https://github.com/shilman)!
- ReactVite: Docgen ignore un-parsable files - [#26254](https://github.com/storybookjs/storybook/pull/26254), thanks [@ndelangen](https://github.com/ndelangen)!

## 8.0.0-rc.0

Bumping 8.0.0-beta.6 to 8.0.0-rc.0. Please refer to the changelogs of previous beta releases.
Expand Down
2 changes: 1 addition & 1 deletion code/addons/onboarding/preset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function managerEntries(entry = []) {
return [...entry, require.resolve('./dist/manager.mjs')];
return [...entry, require.resolve('./dist/manager.js')];
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion code/frameworks/react-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"@storybook/builder-vite": "workspace:*",
"@storybook/react": "workspace:*",
"magic-string": "^0.30.0",
"react-docgen": "^7.0.0"
"react-docgen": "^7.0.0",
"resolve": "^1.22.8"
},
"devDependencies": {
"@types/node": "^18.0.0",
Expand Down
74 changes: 74 additions & 0 deletions code/frameworks/react-vite/src/plugins/docgen-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { extname } from 'path';
import resolve from 'resolve';

export class ReactDocgenResolveError extends Error {
// the magic string that react-docgen uses to check if a module is ignored
readonly code = 'MODULE_NOT_FOUND';

constructor(filename: string) {
super(`'${filename}' was ignored by react-docgen.`);
}
}

/* The below code was copied from:
* https://github.com/reactjs/react-docgen/blob/df2daa8b6f0af693ecc3c4dc49f2246f60552bcb/packages/react-docgen/src/importer/makeFsImporter.ts#L14-L63
* because it wasn't exported from the react-docgen package.
* watch out: when updating this code, also update the code in code/presets/react-webpack/src/loaders/docgen-resolver.ts
*/

// These extensions are sorted by priority
// resolve() will check for files in the order these extensions are sorted
export const RESOLVE_EXTENSIONS = [
'.js',
'.cts', // These were originally not in the code, I added them
'.mts', // These were originally not in the code, I added them
'.ctsx', // These were originally not in the code, I added them
'.mtsx', // These were originally not in the code, I added them
'.ts',
'.tsx',
'.mjs',
'.cjs',
'.mts',
'.cts',
'.jsx',
];

export function defaultLookupModule(filename: string, basedir: string): string {
const resolveOptions = {
basedir,
extensions: RESOLVE_EXTENSIONS,
// we do not need to check core modules as we cannot import them anyway
includeCoreModules: false,
};

try {
return resolve.sync(filename, resolveOptions);
} catch (error) {
const ext = extname(filename);
let newFilename: string;

// if we try to import a JavaScript file it might be that we are actually pointing to
// a TypeScript file. This can happen in ES modules as TypeScript requires to import other
// TypeScript files with .js extensions
// https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions
switch (ext) {
case '.js':
case '.mjs':
case '.cjs':
newFilename = `${filename.slice(0, -2)}ts`;
break;

case '.jsx':
newFilename = `${filename.slice(0, -3)}tsx`;
break;
default:
throw error;
}

return resolve.sync(newFilename, {
...resolveOptions,
// we already know that there is an extension at this point, so no need to check other extensions
extensions: [],
});
}
}
23 changes: 18 additions & 5 deletions code/frameworks/react-vite/src/plugins/react-docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import {
parse,
builtinHandlers as docgenHandlers,
builtinResolvers as docgenResolver,
builtinImporters as docgenImporters,
makeFsImporter,
} from 'react-docgen';
import MagicString from 'magic-string';
import type { PluginOption } from 'vite';
import actualNameHandler from './docgen-handlers/actualNameHandler';
import {
RESOLVE_EXTENSIONS,
ReactDocgenResolveError,
defaultLookupModule,
} from './docgen-resolver';

type DocObj = Documentation & { actualName: string };

// TODO: None of these are able to be overridden, so `default` is aspirational here.
const defaultHandlers = Object.values(docgenHandlers).map((handler) => handler);
const defaultResolver = new docgenResolver.FindExportedDefinitionsResolver();
const defaultImporter = docgenImporters.fsImporter;
const handlers = [...defaultHandlers, actualNameHandler];

type Options = {
Expand All @@ -36,14 +40,23 @@ export function reactDocgen({
name: 'storybook:react-docgen-plugin',
enforce: 'pre',
async transform(src: string, id: string) {
const relPath = path.relative(cwd, id);
if (!filter(relPath)) return;
if (!filter(path.relative(cwd, id))) {
return;
}

try {
const docgenResults = parse(src, {
resolver: defaultResolver,
handlers,
importer: defaultImporter,
importer: makeFsImporter((filename, basedir) => {
const result = defaultLookupModule(filename, basedir);

if (RESOLVE_EXTENSIONS.find((ext) => result.endsWith(ext))) {
return result;
}

throw new ReactDocgenResolveError(filename);
}),
filename: id,
}) as DocObj[];
const s = new MagicString(src);
Expand Down
82 changes: 0 additions & 82 deletions code/lib/cli/src/doctor/getIncompatibleAddons.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const getIncompatibleStorybookPackages = async (
const allDeps = await packageManager.getAllDependencies();
const storybookLikeDeps = Object.keys(allDeps).filter((dep) => dep.includes('storybook'));

if (storybookLikeDeps.length === 0) {
if (storybookLikeDeps.length === 0 && !context.skipErrors) {
throw new Error('No Storybook dependencies found in the package.json');
}

Expand Down
3 changes: 2 additions & 1 deletion code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,6 @@
"Dependency Upgrades"
]
]
}
},
"deferredNextVersion": "8.0.0-rc.1"
}
1 change: 1 addition & 0 deletions code/presets/react-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"fs-extra": "^11.1.0",
"magic-string": "^0.30.5",
"react-docgen": "^7.0.0",
"resolve": "^1.22.8",
"semver": "^7.3.7",
"webpack": "5"
},
Expand Down
74 changes: 74 additions & 0 deletions code/presets/react-webpack/src/loaders/docgen-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { extname } from 'path';
import resolve from 'resolve';

export class ReactDocgenResolveError extends Error {
// the magic string that react-docgen uses to check if a module is ignored
readonly code = 'MODULE_NOT_FOUND';

constructor(filename: string) {
super(`'${filename}' was ignored by react-docgen.`);
}
}

/* The below code was copied from:
* https://github.com/reactjs/react-docgen/blob/df2daa8b6f0af693ecc3c4dc49f2246f60552bcb/packages/react-docgen/src/importer/makeFsImporter.ts#L14-L63
* because it wasn't exported from the react-docgen package.
* watch out: when updating this code, also update the code in code/frameworks/react-vite/src/plugins/docgen-resolver.ts
*/

// These extensions are sorted by priority
// resolve() will check for files in the order these extensions are sorted
export const RESOLVE_EXTENSIONS = [
'.js',
'.cts', // These were originally not in the code, I added them
'.mts', // These were originally not in the code, I added them
'.ctsx', // These were originally not in the code, I added them
'.mtsx', // These were originally not in the code, I added them
'.ts',
'.tsx',
'.mjs',
'.cjs',
'.mts',
'.cts',
'.jsx',
];

export function defaultLookupModule(filename: string, basedir: string): string {
const resolveOptions = {
basedir,
extensions: RESOLVE_EXTENSIONS,
// we do not need to check core modules as we cannot import them anyway
includeCoreModules: false,
};

try {
return resolve.sync(filename, resolveOptions);
} catch (error) {
const ext = extname(filename);
let newFilename: string;

// if we try to import a JavaScript file it might be that we are actually pointing to
// a TypeScript file. This can happen in ES modules as TypeScript requires to import other
// TypeScript files with .js extensions
// https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions
switch (ext) {
case '.js':
case '.mjs':
case '.cjs':
newFilename = `${filename.slice(0, -2)}ts`;
break;

case '.jsx':
newFilename = `${filename.slice(0, -3)}tsx`;
break;
default:
throw error;
}

return resolve.sync(newFilename, {
...resolveOptions,
// we already know that there is an extension at this point, so no need to check other extensions
extensions: [],
});
}
}
19 changes: 16 additions & 3 deletions code/presets/react-webpack/src/loaders/react-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
parse,
builtinResolvers as docgenResolver,
builtinHandlers as docgenHandlers,
builtinImporters as docgenImporters,
makeFsImporter,
ERROR_CODES,
utils,
} from 'react-docgen';
Expand All @@ -11,6 +11,12 @@ import type { LoaderContext } from 'webpack';
import type { Handler, NodePath, babelTypes as t, Documentation } from 'react-docgen';
import { logger } from '@storybook/node-logger';

import {
RESOLVE_EXTENSIONS,
ReactDocgenResolveError,
defaultLookupModule,
} from './docgen-resolver';

const { getNameOrValue, isReactForwardRefCall } = utils;

const actualNameHandler: Handler = function actualNameHandler(documentation, componentDefinition) {
Expand Down Expand Up @@ -54,7 +60,6 @@ type DocObj = Documentation & { actualName: string };

const defaultHandlers = Object.values(docgenHandlers).map((handler) => handler);
const defaultResolver = new docgenResolver.FindExportedDefinitionsResolver();
const defaultImporter = docgenImporters.fsImporter;
const handlers = [...defaultHandlers, actualNameHandler];

export default async function reactDocgenLoader(
Expand All @@ -71,7 +76,15 @@ export default async function reactDocgenLoader(
filename: this.resourcePath,
resolver: defaultResolver,
handlers,
importer: defaultImporter,
importer: makeFsImporter((filename, basedir) => {
const result = defaultLookupModule(filename, basedir);

if (RESOLVE_EXTENSIONS.find((ext) => result.endsWith(ext))) {
return result;
}

throw new ReactDocgenResolveError(filename);
}),
babelOptions: {
babelrc: false,
configFile: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: red;
}
Loading