Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid using /@fs/ for node_modules
Browse files Browse the repository at this point in the history
IanVS committed Apr 8, 2022
1 parent 3d35f95 commit 15b1dad
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/builder-vite/codegen-iframe-script.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { normalizePath } from 'vite';
import { virtualPreviewFile, virtualStoriesFile } from './virtual-file-names';

import type { ExtendedOptions } from './types';

// We need to convert from an absolute path, to a traditional node module import path,
// so that vite can correctly pre-bundle/optimize
function transformPath(absPath: string) {
const splits = absPath.split('node_modules/');
// Return everything after the final "node_modules/"
return splits[splits.length - 1];
}

export async function generateIframeScriptCode(options: ExtendedOptions) {
const { presets, frameworkPath, framework } = options;
const frameworkImportPath = frameworkPath || `@storybook/${framework}`;
@@ -11,7 +18,7 @@ export async function generateIframeScriptCode(options: ExtendedOptions) {
const configEntries = [...presetEntries].filter(Boolean);

const absoluteFilesToImport = (files: string[], name: string) =>
files.map((el, i) => `import ${name ? `* as ${name}_${i} from ` : ''}'/@fs/${normalizePath(el)}'`).join('\n');
files.map((el, i) => `import ${name ? `* as ${name}_${i} from ` : ''}'${transformPath(el)}'`).join('\n');

const importArray = (name: string, length: number) => new Array(length).fill(0).map((_, i) => `${name}_${i}`);

0 comments on commit 15b1dad

Please sign in to comment.