-
-
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
Merged
JReinhold
merged 9 commits into
next
from
valentin/fix-missing-source-map-warning-second-attempt
Sep 9, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6d7ac5
Builder-Vite: Fix missing source map warning
valentinpalkovic 667b9b6
Merge branch 'next' of github.com:storybookjs/storybook into valentin…
JReinhold e6fdccd
skip source map generation for external globals
JReinhold 55d2ac1
add test case for multi-line imports in external globals replacements
JReinhold 0a1d9e8
exclude sb-preview/runtime.js from stats-json
JReinhold bfb839b
keep virtual files backwards compatible in stats-json
JReinhold 9fa3956
Merge branch 'valentin/fix-missing-source-map-warning-second-attempt'…
JReinhold 2983993
cleanup
JReinhold 06b3056
improve readability of case when matching internal virtual files.
JReinhold 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
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
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -34,11 +40,18 @@ function stripQueryParams(filePath: string): string { | |
|
||
/** We only care about user code, not node_modules, vite files, or (most) virtual files. */ | ||
function isUserCode(moduleName: string) { | ||
if (!moduleName) { | ||
return false; | ||
} | ||
|
||
// keep Storybook's virtual files because they import the story files, so they are essential to the module graph | ||
if (Object.values(SB_VIRTUAL_FILES).includes(getOriginalVirtualModuleId(moduleName))) { | ||
return true; | ||
} | ||
|
||
return Boolean( | ||
moduleName && | ||
!moduleName.startsWith('vite/') && | ||
!moduleName.startsWith('\x00') && | ||
!moduleName.startsWith('\u0000') && | ||
!moduleName.startsWith('vite/') && | ||
!moduleName.startsWith('\0') && | ||
moduleName !== 'react/jsx-runtime' && | ||
!moduleName.match(/node_modules\//) | ||
); | ||
|
@@ -53,6 +66,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 +103,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; | ||
} | ||
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() { | ||
|
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 |
---|---|---|
@@ -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); | ||
} |
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.
style: Consider extracting this logic into a separate function for better maintainability