-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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: Add a rollup-plugin-webpack-stats
to allow stats from preview builds
#25923
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
code/builders/builder-vite/src/plugins/webpack-stats-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// This plugin is a direct port of https://github.com/IanVS/vite-plugin-turbosnap | ||
|
||
import type { BuilderStats } from '@storybook/types'; | ||
import path from 'path'; | ||
import type { Plugin } from 'vite'; | ||
|
||
/* | ||
* Reason, Module are copied from chromatic types | ||
* https://github.com/chromaui/chromatic-cli/blob/145a5e295dde21042e96396c7e004f250d842182/bin-src/types.ts#L265-L276 | ||
*/ | ||
interface Reason { | ||
moduleName: string; | ||
} | ||
interface Module { | ||
id: string | number; | ||
name: string; | ||
modules?: Array<Pick<Module, 'name'>>; | ||
reasons?: Reason[]; | ||
} | ||
|
||
type WebpackStatsPluginOptions = { | ||
workingDir: string; | ||
}; | ||
|
||
/** | ||
* Strips off query params added by rollup/vite to ids, to make paths compatible for comparison with git. | ||
*/ | ||
function stripQueryParams(filePath: string): string { | ||
return filePath.split('?')[0]; | ||
} | ||
|
||
/** | ||
* We only care about user code, not node_modules, vite files, or (most) virtual files. | ||
*/ | ||
function isUserCode(moduleName: string) { | ||
return Boolean( | ||
moduleName && | ||
!moduleName.startsWith('vite/') && | ||
!moduleName.startsWith('\x00') && | ||
!moduleName.startsWith('\u0000') && | ||
moduleName !== 'react/jsx-runtime' && | ||
!moduleName.match(/node_modules\//) | ||
); | ||
} | ||
|
||
export type WebpackStatsPlugin = Plugin & { storybookGetStats: () => BuilderStats }; | ||
|
||
export function pluginWebpackStats({ workingDir }: WebpackStatsPluginOptions): WebpackStatsPlugin { | ||
/** | ||
* Convert an absolute path name to a path relative to the vite root, with a starting `./` | ||
*/ | ||
function normalize(filename: string) { | ||
// Do not try to resolve virtual files | ||
if (filename.startsWith('/virtual:')) { | ||
return filename; | ||
} | ||
// Otherwise, we need them in the format `./path/to/file.js`. | ||
else { | ||
const relativePath = path.relative(workingDir, stripQueryParams(filename)); | ||
// This seems hacky, got to be a better way to add a `./` to the start of a path. | ||
return `./${relativePath}`; | ||
} | ||
} | ||
|
||
/** | ||
* Helper to create Reason objects out of a list of string paths | ||
*/ | ||
function createReasons(importers?: readonly string[]): Reason[] { | ||
return (importers || []).map((i) => ({ moduleName: normalize(i) })); | ||
} | ||
|
||
/** | ||
* Helper function to build a `Module` given a filename and list of files that import it | ||
*/ | ||
function createStatsMapModule(filename: string, importers?: readonly string[]): Module { | ||
return { | ||
id: filename, | ||
name: filename, | ||
reasons: createReasons(importers), | ||
}; | ||
} | ||
|
||
const statsMap = new Map<string, Module>(); | ||
|
||
return { | ||
name: 'storybook:rollup-plugin-webpack-stats', | ||
// We want this to run after the vite build plugins (https://vitejs.dev/guide/api-plugin.html#plugin-ordering) | ||
enforce: 'post', | ||
moduleParsed: function (mod) { | ||
if (isUserCode(mod.id)) { | ||
mod.importedIds | ||
.concat(mod.dynamicallyImportedIds) | ||
.filter((name) => isUserCode(name)) | ||
.forEach((depIdUnsafe) => { | ||
const depId = normalize(depIdUnsafe); | ||
if (statsMap.has(depId)) { | ||
const m = statsMap.get(depId); | ||
if (m) { | ||
m.reasons = (m.reasons ?? []) | ||
.concat(createReasons([mod.id])) | ||
.filter((r) => r.moduleName !== depId); | ||
statsMap.set(depId, m); | ||
} | ||
} else { | ||
statsMap.set(depId, createStatsMapModule(depId, [mod.id])); | ||
} | ||
}); | ||
} | ||
}, | ||
|
||
storybookGetStats() { | ||
const stats = { modules: Array.from(statsMap.values()) }; | ||
return { ...stats, toJson: () => stats }; | ||
}, | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this code is causing a behavior that always displays a warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @acid-chicken -- that makes sense! But how to reproduce? I'm not seeing the problem on a basic
react-vite
8.0.0-beta.2
sandbox project. Any tips?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tmeasday Thank you for the reply!
Digging a little deeper into this issue, I found that this log only appears at build time.
Therefore, you can probably reproduce by executing
yarn create vite
(w/ react & typescript option),yarn dlx storybook@next init
, thenyarn build-storybook
(notyarn storybook
).I hope this will be of some help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, OK of course. Thank you @acid-chicken