Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: pathing of managerEntries containing long paths with hidden folders #20550

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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