Skip to content

Commit

Permalink
Builder-Vite: Add null character prefix to virtual file IDs in code g…
Browse files Browse the repository at this point in the history
…enerator plugin
  • Loading branch information
valentinpalkovic committed Aug 13, 2024
1 parent db593fa commit 8066f72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 10 additions & 6 deletions code/builders/builder-vite/src/plugins/code-generator-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
virtualStoriesFile,
} from '../virtual-file-names';

function resolvedVirtualPath(path: string): string {
return `\0${path}`;
}

export function codeGeneratorPlugin(options: Options): Plugin {
const iframePath = require.resolve('@storybook/builder-vite/input/iframe.html');
let iframeId: string;
Expand Down Expand Up @@ -70,33 +74,33 @@ export function codeGeneratorPlugin(options: Options): Plugin {
},
resolveId(source) {
if (source === virtualFileId) {
return virtualFileId;
return resolvedVirtualPath(virtualFileId);
}
if (source === iframePath) {
return iframeId;
}
if (source === virtualStoriesFile) {
return virtualStoriesFile;
return resolvedVirtualPath(virtualStoriesFile);
}
if (source === virtualPreviewFile) {
return virtualPreviewFile;
}
if (source === virtualAddonSetupFile) {
return virtualAddonSetupFile;
return resolvedVirtualPath(virtualAddonSetupFile);
}

return undefined;
},
async load(id, config) {
if (id === virtualStoriesFile) {
if (id === resolvedVirtualPath(virtualStoriesFile)) {
return generateImportFnScriptCode(options);
}

if (id === virtualAddonSetupFile) {
if (id === resolvedVirtualPath(virtualAddonSetupFile)) {
return generateAddonSetupCode();
}

if (id === virtualFileId) {
if (id === resolvedVirtualPath(virtualFileId)) {
return generateModernIframeScriptCode(options, projectRoot);
}

Expand Down
8 changes: 4 additions & 4 deletions code/builders/builder-vite/src/virtual-file-names.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const virtualFileId = '\0virtual:/@storybook/builder-vite/vite-app.js';
export const virtualStoriesFile = '\0virtual:/@storybook/builder-vite/storybook-stories.js';
export const virtualPreviewFile = '\0virtual:/@storybook/builder-vite/preview-entry.js';
export const virtualAddonSetupFile = '\0virtual:/@storybook/builder-vite/setup-addons.js';
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';

0 comments on commit 8066f72

Please sign in to comment.