Skip to content

Commit 51d9704

Browse files
committed
fix(storybook-builder): fix paths on Windows
1 parent 7398620 commit 51d9704

4 files changed

+23
-8
lines changed

packages/storybook-builder/src/esbuild-plugin-commonjs-named-exports.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { Plugin } from 'esbuild';
22
import { readFile } from 'fs-extra';
3-
import { dirname } from 'path';
3+
import { dirname, relative } from 'path';
44

55
export function esbuildPluginCommonjsNamedExports(modules: string[]): Plugin {
66
return {
77
name: 'commonjs-named-exports',
88
async setup(build) {
9+
const slash = (await import('slash')).default;
10+
911
const { init, parse } = await import('cjs-module-lexer');
1012
await init();
1113

@@ -56,7 +58,9 @@ export function esbuildPluginCommonjsNamedExports(modules: string[]): Plugin {
5658

5759
return {
5860
resolveDir,
59-
contents: `export { ${finalExports.join(',')} } from '${resolvedPath}';`,
61+
contents: `export { ${finalExports.join(',')} } from '${slash(
62+
relative(resolveDir, resolvedPath),
63+
)}';`,
6064
};
6165
});
6266

packages/storybook-builder/src/generate-stories-script.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { normalizePath } from '@rollup/pluginutils';
44
import { logger } from '@storybook/node-logger';
55
import type { Options } from '@storybook/types';
66
import * as path from 'path';
7+
import { pathToFileURL } from 'url';
78
import { listStories } from './list-stories.js';
89

910
/**
@@ -15,7 +16,11 @@ export async function generateStoriesScript(options: Options) {
1516
const stories = await listStories(options);
1617

1718
// We can then call toImportFn to create a function that can be used to load each story dynamically.
18-
return (await toImportFn(stories)).trim();
19+
const code = (await toImportFn(stories)).trim();
20+
21+
console.log('generateStoriesScript', code);
22+
23+
return code;
1924
}
2025

2126
/**
@@ -34,7 +39,7 @@ async function toImportFn(stories: string[]) {
3439
}
3540

3641
const importPath = toImportPath(relativePath);
37-
let actualPath = `${process.cwd()}/${relativePath}`;
42+
let actualPath = path.join(process.cwd(), relativePath);
3843
if (actualPath.endsWith('.mdx')) {
3944
actualPath = `${actualPath}.js`;
4045
}

packages/storybook-builder/src/rollup-plugin-mdx.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { compile } from '@storybook/mdx2-csf';
22
import type { Options } from '@storybook/types';
33
import { exists, readFile } from 'fs-extra';
4-
import { isAbsolute } from 'path';
4+
import { isAbsolute, sep } from 'path';
55
import remarkExternalLinks from 'remark-external-links';
66
import remarkSlug from 'remark-slug';
77
import type { Plugin } from 'rollup';
@@ -23,14 +23,19 @@ export function rollupPluginMdx(storybookOptions: Options): Plugin {
2323
async resolveId(id) {
2424
if (id.endsWith('.mdx.js')) {
2525
return id;
26+
console.log('resolveId > id', id);
2627
}
2728
},
2829

2930
async load(id) {
3031
if (!id.endsWith('.mdx.js')) return;
3132

3233
const mdxPath = id.replace(/\.js$/, '');
33-
const mdxCode = await readFile(mdxPath, 'utf8');
34+
35+
console.log('load > mdxPath', mdxPath);
36+
console.log('load > mdxPath.split(/).join(sep)', mdxPath.split('/').join(sep));
37+
38+
const mdxCode = await readFile(mdxPath.split('/').join(sep), 'utf8');
3439

3540
const mdxLoaderOptions = await storybookOptions.presets.apply('mdxLoaderOptions', {
3641
...mdxPluginOptions,

packages/storybook-builder/src/rollup-plugin-prebundle-modules.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { stringifyProcessEnvs } from '@storybook/core-common';
22
import { build } from 'esbuild';
33
import { remove } from 'fs-extra';
4-
import { join } from 'path';
4+
import { join, normalize } from 'path';
55
import type { Plugin } from 'rollup';
66
import { esbuildPluginCommonjsNamedExports } from './esbuild-plugin-commonjs-named-exports.js';
77
import { getNodeModuleDir } from './get-node-module-dir.js';
88

9-
export const PREBUNDLED_MODULES_DIR = 'node_modules/.prebundled_modules';
9+
export const PREBUNDLED_MODULES_DIR = normalize('node_modules/.prebundled_modules');
10+
console.log('PREBUNDLED_MODULES_DIR', PREBUNDLED_MODULES_DIR);
1011

1112
export function rollupPluginPrebundleModules(env: Record<string, string>): Plugin {
1213
const modulePaths: Record<string, string> = {};

0 commit comments

Comments
 (0)