-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 missing source map warning #28984
Changes from 7 commits
e6d7ac5
667b9b6
e6fdccd
55d2ac1
0a1d9e8
bfb839b
9fa3956
2983993
06b3056
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,12 @@ import type { BuilderStats } from 'storybook/internal/types'; | |
import slash from 'slash'; | ||
import type { Plugin } from 'vite'; | ||
|
||
import { | ||
SB_VIRTUAL_FILES, | ||
getOriginalVirtualModuleId, | ||
getResolvedVirtualModuleId, | ||
} from '../virtual-file-names'; | ||
|
||
/* | ||
* Reason, Module are copied from chromatic types | ||
* https://github.com/chromaui/chromatic-cli/blob/145a5e295dde21042e96396c7e004f250d842182/bin-src/types.ts#L265-L276 | ||
|
@@ -35,12 +41,13 @@ function stripQueryParams(filePath: string): string { | |
/** 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\//) | ||
(moduleName && | ||
// keep Storybook's virtual files because they import the story files, so they are essential to the module graph | ||
Object.values(SB_VIRTUAL_FILES).includes(getOriginalVirtualModuleId(moduleName))) || | ||
(!moduleName.startsWith('vite/') && | ||
!moduleName.startsWith('\0') && | ||
moduleName !== 'react/jsx-runtime' && | ||
!moduleName.match(/node_modules\//)) | ||
); | ||
} | ||
|
||
|
@@ -53,6 +60,14 @@ export function pluginWebpackStats({ workingDir }: WebpackStatsPluginOptions): W | |
if (filename.startsWith('/virtual:')) { | ||
return filename; | ||
} | ||
// ! Maintain backwards compatibility with the old virtual file names | ||
// ! to ensure that the stats file doesn't change between the versions | ||
// ! Turbosnap is also only compatible with the old virtual file names | ||
// ! the old virtual file names did not start with the obligatory \0 character | ||
if (Object.values(SB_VIRTUAL_FILES).includes(getOriginalVirtualModuleId(filename))) { | ||
return getOriginalVirtualModuleId(filename); | ||
} | ||
|
||
// Otherwise, we need them in the format `./path/to/file.js`. | ||
else { | ||
const relativePath = relative(workingDir, stripQueryParams(filename)); | ||
|
@@ -82,25 +97,27 @@ export function pluginWebpackStats({ workingDir }: WebpackStatsPluginOptions): W | |
// 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])); | ||
} | ||
}); | ||
if (!isUserCode(mod.id)) { | ||
return; | ||
} | ||
mod.importedIds | ||
.concat(mod.dynamicallyImportedIds) | ||
.filter((name) => isUserCode(name)) | ||
.forEach((depIdUnsafe) => { | ||
const depId = normalize(depIdUnsafe); | ||
if (!statsMap.has(depId)) { | ||
statsMap.set(depId, createStatsMapModule(depId, [mod.id])); | ||
return; | ||
} | ||
Comment on lines
+114
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider extracting this logic into a separate function for better maintainability |
||
const m = statsMap.get(depId); | ||
if (!m) { | ||
return; | ||
} | ||
m.reasons = (m.reasons ?? []) | ||
.concat(createReasons([mod.id])) | ||
.filter((r) => r.moduleName !== depId); | ||
statsMap.set(depId, m); | ||
}); | ||
Comment on lines
+106
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might not be obvious from the diff, but this just changes the flow to use early-return, no actual logic changed. |
||
}, | ||
|
||
storybookGetStats() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
export const virtualFileId = '/virtual:/@storybook/builder-vite/vite-app.js'; | ||
export const virtualStoriesFile = '/virtual:/@storybook/builder-vite/storybook-stories.js'; | ||
export const virtualPreviewFile = '/virtual:/@storybook/builder-vite/preview-entry.js'; | ||
export const virtualAddonSetupFile = '/virtual:/@storybook/builder-vite/setup-addons.js'; | ||
export const SB_VIRTUAL_FILES = { | ||
VIRTUAL_APP_FILE: '/virtual:/@storybook/builder-vite/vite-app.js', | ||
VIRTUAL_STORIES_FILE: '/virtual:/@storybook/builder-vite/storybook-stories.js', | ||
VIRTUAL_PREVIEW_FILE: '/virtual:/@storybook/builder-vite/preview-entry.js', | ||
VIRTUAL_ADDON_SETUP_FILE: '/virtual:/@storybook/builder-vite/setup-addons.js', | ||
}; | ||
|
||
export function getResolvedVirtualModuleId(virtualModuleId: string) { | ||
return `\0${virtualModuleId}`; | ||
} | ||
|
||
export function getOriginalVirtualModuleId(resolvedVirtualModuleId: string) { | ||
return resolvedVirtualModuleId.slice(1); | ||
} |
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.
style: Consider using a more descriptive variable name instead of 'moduleName'