From 04312de0643d8d7f955097b3e4513ee840aa5b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kub=C3=AD=C4=8Dek?= Date: Wed, 12 Apr 2023 11:47:27 +0200 Subject: [PATCH] Unify cache location configurability #22044 --- code/lib/builder-vite/package.json | 2 ++ .../src/plugins/external-globals-plugin.ts | 10 ++++++---- code/lib/builder-vite/src/vite-config.ts | 3 ++- code/lib/core-common/package.json | 2 ++ .../src/utils/resolve-path-in-sb-cache.ts | 14 +++----------- code/yarn.lock | 4 ++++ 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/code/lib/builder-vite/package.json b/code/lib/builder-vite/package.json index d53e504c1328..a32ed1ac096f 100644 --- a/code/lib/builder-vite/package.json +++ b/code/lib/builder-vite/package.json @@ -52,9 +52,11 @@ "@storybook/preview": "7.1.0-alpha.3", "@storybook/preview-api": "7.1.0-alpha.3", "@storybook/types": "7.1.0-alpha.3", + "@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", "glob": "^8.1.0", "glob-promise": "^6.0.2", diff --git a/code/lib/builder-vite/src/plugins/external-globals-plugin.ts b/code/lib/builder-vite/src/plugins/external-globals-plugin.ts index 60841e22bc97..3e6b939aa4be 100644 --- a/code/lib/builder-vite/src/plugins/external-globals-plugin.ts +++ b/code/lib/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/lib/builder-vite/src/vite-config.ts b/code/lib/builder-vite/src/vite-config.ts index e9ce254809b3..c7b363622bcc 100644 --- a/code/lib/builder-vite/src/vite-config.ts +++ b/code/lib/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 9844cadee3fe..16ca38d9ab1d 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.3", "@storybook/types": "7.1.0-alpha.3", + "@types/find-cache-dir": "^3.2.1", "@types/node": "^16.0.0", "@types/pretty-hrtime": "^1.0.0", "chalk": "^4.1.0", "esbuild": "^0.17.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": "^8.1.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 11b10a55bfe6..06346a3f8815 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5706,10 +5706,12 @@ __metadata: "@storybook/preview-api": 7.1.0-alpha.3 "@storybook/types": 7.1.0-alpha.3 "@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: ^8.1.0 glob-promise: ^6.0.2 @@ -6017,6 +6019,7 @@ __metadata: dependencies: "@storybook/node-logger": 7.1.0-alpha.3 "@storybook/types": 7.1.0-alpha.3 + "@types/find-cache-dir": ^3.2.1 "@types/mock-fs": ^4.13.1 "@types/node": ^16.0.0 "@types/picomatch": ^2.3.0 @@ -6025,6 +6028,7 @@ __metadata: esbuild: ^0.17.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: ^8.1.0