diff --git a/code/builders/builder-vite/package.json b/code/builders/builder-vite/package.json index 3734a141fd02..8f8cde6d5dd8 100644 --- a/code/builders/builder-vite/package.json +++ b/code/builders/builder-vite/package.json @@ -51,9 +51,11 @@ "@storybook/preview": "7.1.0-alpha.32", "@storybook/preview-api": "7.1.0-alpha.32", "@storybook/types": "7.1.0-alpha.32", + "@types/find-cache-dir": "^3.2.1", "browser-assert": "^1.2.1", "es-module-lexer": "^0.9.3", "express": "^4.17.3", + "find-cache-dir": "^3.0.0", "fs-extra": "^11.1.0", "magic-string": "^0.30.0", "remark-external-links": "^8.0.0", diff --git a/code/builders/builder-vite/src/plugins/external-globals-plugin.ts b/code/builders/builder-vite/src/plugins/external-globals-plugin.ts index fe5efe1bce4a..dd53ab4a4e56 100644 --- a/code/builders/builder-vite/src/plugins/external-globals-plugin.ts +++ b/code/builders/builder-vite/src/plugins/external-globals-plugin.ts @@ -1,7 +1,8 @@ import { join } from 'node:path'; +import findCacheDirectory from 'find-cache-dir'; import { init, parse } from 'es-module-lexer'; import MagicString from 'magic-string'; -import { emptyDir, ensureDir, ensureFile, writeFile } from 'fs-extra'; +import { ensureFile, writeFile } from 'fs-extra'; import { mergeAlias } from 'vite'; import type { Alias, Plugin } from 'vite'; @@ -47,9 +48,10 @@ export async function externalGlobalsPlugin(externals: Record) { } const newAlias = mergeAlias([], config.resolve?.alias) as Alias[]; - const cachePath = join(process.cwd(), 'node_modules', '.cache', 'vite-plugin-externals'); - await ensureDir(cachePath); - await emptyDir(cachePath); + const cachePath = findCacheDirectory({ + name: 'sb-vite-plugin-externals', + create: true, + }) as string; await Promise.all( (Object.keys(externals) as Array).map(async (externalKey) => { const externalCachePath = join(cachePath, `${externalKey}.js`); diff --git a/code/builders/builder-vite/src/vite-config.ts b/code/builders/builder-vite/src/vite-config.ts index e9ce254809b3..c7b363622bcc 100644 --- a/code/builders/builder-vite/src/vite-config.ts +++ b/code/builders/builder-vite/src/vite-config.ts @@ -1,5 +1,6 @@ import * as path from 'path'; import { loadConfigFromFile, mergeConfig } from 'vite'; +import findCacheDirectory from 'find-cache-dir'; import type { ConfigEnv, InlineConfig as ViteInlineConfig, @@ -51,7 +52,7 @@ export async function commonConfig( const sbConfig: InlineConfig = { configFile: false, - cacheDir: 'node_modules/.cache/.vite-storybook', + cacheDir: findCacheDirectory({ name: 'sb-vite' }), root: path.resolve(options.configDir, '..'), // Allow storybook deployed as subfolder. See https://github.com/storybookjs/builder-vite/issues/238 base: './', diff --git a/code/lib/core-common/package.json b/code/lib/core-common/package.json index 1eb3923b3320..2f30c24dfd27 100644 --- a/code/lib/core-common/package.json +++ b/code/lib/core-common/package.json @@ -45,12 +45,14 @@ "dependencies": { "@storybook/node-logger": "7.1.0-alpha.32", "@storybook/types": "7.1.0-alpha.32", + "@types/find-cache-dir": "^3.2.1", "@types/node": "^16.0.0", "@types/pretty-hrtime": "^1.0.0", "chalk": "^4.1.0", "esbuild": "^0.18.0", "esbuild-register": "^3.4.0", "file-system-cache": "^2.0.0", + "find-cache-dir": "^3.0.0", "find-up": "^5.0.0", "fs-extra": "^11.1.0", "glob": "^10.0.0", diff --git a/code/lib/core-common/src/utils/resolve-path-in-sb-cache.ts b/code/lib/core-common/src/utils/resolve-path-in-sb-cache.ts index 7157abdc0257..de88c2e2c00b 100644 --- a/code/lib/core-common/src/utils/resolve-path-in-sb-cache.ts +++ b/code/lib/core-common/src/utils/resolve-path-in-sb-cache.ts @@ -1,5 +1,5 @@ import path from 'path'; -import pkgDir from 'pkg-dir'; +import findCacheDirectory from 'find-cache-dir'; /** * Get the path of the file or directory with input name inside the Storybook cache directory: @@ -10,16 +10,8 @@ import pkgDir from 'pkg-dir'; * @return {string} Absolute path to the file or directory */ export function resolvePathInStorybookCache(fileOrDirectoryName: string): string { - const cwd = process.cwd(); - const projectDir = pkgDir.sync(cwd); - - let cacheDirectory; - - if (!projectDir) { - cacheDirectory = path.resolve(cwd, '.cache/storybook'); - } else { - cacheDirectory = path.resolve(projectDir, 'node_modules/.cache/storybook'); - } + let cacheDirectory = findCacheDirectory({ name: 'storybook' }); + cacheDirectory ||= path.join(process.cwd(), '.cache/storybook'); return path.join(cacheDirectory, fileOrDirectoryName); } diff --git a/code/yarn.lock b/code/yarn.lock index cf0bdcbce2db..ee3b25e9bc84 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5849,10 +5849,12 @@ __metadata: "@storybook/preview-api": 7.1.0-alpha.32 "@storybook/types": 7.1.0-alpha.32 "@types/express": ^4.17.13 + "@types/find-cache-dir": ^3.2.1 "@types/node": ^16.0.0 browser-assert: ^1.2.1 es-module-lexer: ^0.9.3 express: ^4.17.3 + find-cache-dir: ^3.0.0 fs-extra: ^11.1.0 glob: ^10.0.0 magic-string: ^0.30.0 @@ -6164,6 +6166,7 @@ __metadata: dependencies: "@storybook/node-logger": 7.1.0-alpha.32 "@storybook/types": 7.1.0-alpha.32 + "@types/find-cache-dir": ^3.2.1 "@types/mock-fs": ^4.13.1 "@types/node": ^16.0.0 "@types/picomatch": ^2.3.0 @@ -6172,6 +6175,7 @@ __metadata: esbuild: ^0.18.0 esbuild-register: ^3.4.0 file-system-cache: ^2.0.0 + find-cache-dir: ^3.0.0 find-up: ^5.0.0 fs-extra: ^11.1.0 glob: ^10.0.0