Skip to content

Commit

Permalink
fix(storybook-builder): fix paths on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bashmish committed May 27, 2024
1 parent 7398620 commit 60434bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Plugin } from 'esbuild';
import { readFile } from 'fs-extra';
import { dirname } from 'path';
import { dirname, relative } from 'path';

export function esbuildPluginCommonjsNamedExports(modules: string[]): Plugin {
return {
name: 'commonjs-named-exports',
async setup(build) {
const slash = (await import('slash')).default;

const { init, parse } = await import('cjs-module-lexer');
await init();

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

return {
resolveDir,
contents: `export { ${finalExports.join(',')} } from '${resolvedPath}';`,
contents: `export { ${finalExports.join(',')} } from '${slash(
relative(resolveDir, resolvedPath),
)}';`,
};
});

Expand Down
3 changes: 2 additions & 1 deletion packages/storybook-builder/src/generate-stories-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { normalizePath } from '@rollup/pluginutils';
import { logger } from '@storybook/node-logger';
import type { Options } from '@storybook/types';
import * as path from 'path';
import { pathToFileURL } from 'url';
import { listStories } from './list-stories.js';

/**
Expand Down Expand Up @@ -34,7 +35,7 @@ async function toImportFn(stories: string[]) {
}

const importPath = toImportPath(relativePath);
let actualPath = `${process.cwd()}/${relativePath}`;
let actualPath = pathToFileURL(path.join(process.cwd(), relativePath)).href;
if (actualPath.endsWith('.mdx')) {
actualPath = `${actualPath}.js`;
}
Expand Down
9 changes: 7 additions & 2 deletions packages/storybook-builder/src/rollup-plugin-mdx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { compile } from '@storybook/mdx2-csf';
import type { Options } from '@storybook/types';
import { exists, readFile } from 'fs-extra';
import { isAbsolute } from 'path';
import { isAbsolute, sep } from 'path';
import remarkExternalLinks from 'remark-external-links';
import remarkSlug from 'remark-slug';
import type { Plugin } from 'rollup';
Expand All @@ -23,14 +23,19 @@ export function rollupPluginMdx(storybookOptions: Options): Plugin {
async resolveId(id) {
if (id.endsWith('.mdx.js')) {
return id;
console.log('resolveId > id', id);
}
},

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

const mdxPath = id.replace(/\.js$/, '');
const mdxCode = await readFile(mdxPath, 'utf8');

console.log('load > mdxPath', mdxPath);
console.log('load > mdxPath.split(/).join(sep)', mdxPath.split('/').join(sep));

const mdxCode = await readFile(mdxPath.split('/').join(sep), 'utf8');

const mdxLoaderOptions = await storybookOptions.presets.apply('mdxLoaderOptions', {
...mdxPluginOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { stringifyProcessEnvs } from '@storybook/core-common';
import { build } from 'esbuild';
import { remove } from 'fs-extra';
import { join } from 'path';
import { join, normalize } from 'path';
import type { Plugin } from 'rollup';
import { esbuildPluginCommonjsNamedExports } from './esbuild-plugin-commonjs-named-exports.js';
import { getNodeModuleDir } from './get-node-module-dir.js';

export const PREBUNDLED_MODULES_DIR = 'node_modules/.prebundled_modules';
export const PREBUNDLED_MODULES_DIR = normalize('node_modules/.prebundled_modules');
console.log('PREBUNDLED_MODULES_DIR', PREBUNDLED_MODULES_DIR);

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

0 comments on commit 60434bd

Please sign in to comment.