Skip to content

Commit

Permalink
Merge pull request #20550 from storybookjs/norbert/fix-20427-20513
Browse files Browse the repository at this point in the history
Fix: pathing of managerEntries containing long paths with hidden folders
  • Loading branch information
ndelangen authored Jan 10, 2023
2 parents 27ab591 + 7e13db5 commit a43a3e7
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions code/lib/builder-manager/src/utils/managerEntries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import findCacheDirectory from 'find-cache-dir';
import fs from 'fs-extra';
import { join, parse, relative } from 'node:path';
import { join, parse, relative, sep } from 'node:path';
import slash from 'slash';

const sanitizeBase = (path: string) => {
return path.replaceAll('.', '').replaceAll('@', '').replaceAll(sep, '-').replaceAll('/', '-');
};

const sanitizeFinal = (path: string) => {
const sections = path.split(/-?node_modules-?/);

return sections[sections.length - 1].replaceAll('storybook-addon-', '').replaceAll('dist-', '');
};

/**
* Manager entries should be **self-invoking** bits of code.
* They can of-course import from modules, and ESbuild will bundle all of that into a single file.
Expand All @@ -20,15 +31,20 @@ import slash from 'slash';
*/
export async function wrapManagerEntries(entrypoints: string[]) {
return Promise.all(
entrypoints.map(async (entry) => {
entrypoints.map(async (entry, i) => {
const { name, dir } = parse(entry);
const cacheLocation = findCacheDirectory({ name: 'sb-manager' });

if (!cacheLocation) {
throw new Error('Could not create/find cache directory');
}

const location = join(cacheLocation, relative(process.cwd(), dir), `${name}-bundle.mjs`);
const base = relative(process.cwd(), dir);
const location = join(
cacheLocation,
sanitizeFinal(join(`${sanitizeBase(base)}-${i}`, `${sanitizeBase(name)}-bundle.mjs`))
);

await fs.ensureFile(location);
await fs.writeFile(location, `import '${slash(entry)}';`);

Expand Down

0 comments on commit a43a3e7

Please sign in to comment.