diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index 8e2035604f..ab25e811e0 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -444,6 +444,13 @@ Inspired by the same [Create React App](https://create-react-app.dev/docs/using- Set to `false` to prevent Snowpack from deleting the build output folder (`buildOptions.out`) between builds. +### buildOptions.cacheDirPath + +**Type**: `string` +**Default**: `./node_modules/.cache/snowpack` + +Specify the cache directory in which bundled Node modules will be cached. + ### buildOptions.webModulesUrl _NOTE:_ Deprecated, see `buildOptions.metaUrlPath`. diff --git a/snowpack/src/config.ts b/snowpack/src/config.ts index 6772e3314e..ac7338c30a 100644 --- a/snowpack/src/config.ts +++ b/snowpack/src/config.ts @@ -1,4 +1,6 @@ +import crypto from 'crypto'; import {all as merge} from 'deepmerge'; +import projectCacheDir from 'find-cache-dir'; import {existsSync} from 'fs'; import {isPlainObject} from 'is-plain-object'; import {validate} from 'jsonschema'; @@ -21,6 +23,7 @@ import { import { addLeadingSlash, addTrailingSlash, + GLOBAL_CACHE_DIR, NATIVE_REQUIRE, REQUIRE_OR_IMPORT, removeTrailingSlash, @@ -31,6 +34,13 @@ import type {Awaited} from './util'; const CONFIG_NAME = 'snowpack'; const ALWAYS_EXCLUDE = ['**/_*.{sass,scss}', '**.d.ts']; +const DEFAULT_PROJECT_CACHE_DIR = + projectCacheDir({name: 'snowpack'}) || + // If `projectCacheDir()` is null, no node_modules directory exists. + // Use the current path (hashed) to create a cache entry in the global cache instead. + // Because this is specifically for dependencies, this fallback should rarely be used. + path.join(GLOBAL_CACHE_DIR, crypto.createHash('md5').update(process.cwd()).digest('hex')); + // default settings const DEFAULT_ROOT = process.cwd(); const DEFAULT_CONFIG: SnowpackUserConfig = { @@ -53,6 +63,7 @@ const DEFAULT_CONFIG: SnowpackUserConfig = { out: 'build', baseUrl: '/', metaUrlPath: '_snowpack', + cacheDirPath: DEFAULT_PROJECT_CACHE_DIR, clean: true, sourcemap: false, watch: false, @@ -173,6 +184,7 @@ const configSchema = { properties: { out: {type: 'string'}, baseUrl: {type: 'string'}, + cacheDirPath: {type: 'string'}, clean: {type: 'boolean'}, sourcemap: { oneOf: [{type: 'boolean'}, {type: 'string', enum: ['inline']}], diff --git a/snowpack/src/index.ts b/snowpack/src/index.ts index 06f2099d53..ef691c429f 100644 --- a/snowpack/src/index.ts +++ b/snowpack/src/index.ts @@ -67,6 +67,7 @@ ${colors.bold('Flags:')} --help Show this help message. --version Show the current version. --reload Clear the local cache (useful for troubleshooting). + --cache-dir-path Specify a custom cache directory. --verbose Enable verbose log messages. --quiet Enable minimal log messages. `.trim(), diff --git a/snowpack/src/sources/local.ts b/snowpack/src/sources/local.ts index 031e5e6918..6af0115df7 100644 --- a/snowpack/src/sources/local.ts +++ b/snowpack/src/sources/local.ts @@ -1,12 +1,10 @@ import Arborist from '@npmcli/arborist'; -import crypto from 'crypto'; import { InstallOptions, InstallTarget, resolveDependencyManifest as _resolveDependencyManifest, resolveEntrypoint, } from 'esinstall'; -import projectCacheDir from 'find-cache-dir'; import findUp from 'find-up'; import {existsSync, promises as fs} from 'fs'; import * as colors from 'kleur/colors'; @@ -123,12 +121,9 @@ export class PackageSourceLocal implements PackageSource { this.config = config; this.npmConnectionOptions = getNpmConnectionOptions(config.packageOptions.source); if (config.packageOptions.source === 'local') { - this.cacheDirectory = - projectCacheDir({name: 'snowpack'}) || - // If `projectCacheDir()` is null, no node_modules directory exists. - // Use the current path (hashed) to create a cache entry in the global cache instead. - // Because this is specifically for dependencies, this fallback should rarely be used. - path.join(GLOBAL_CACHE_DIR, crypto.createHash('md5').update(process.cwd()).digest('hex')); + this.cacheDirectory = config.buildOptions.cacheDirPath + ? path.resolve(config.buildOptions.cacheDirPath) + : GLOBAL_CACHE_DIR; this.packageSourceDirectory = config.root; this.arb = null; } else { diff --git a/snowpack/src/types.ts b/snowpack/src/types.ts index bc18aa6e14..ae6428b1b1 100644 --- a/snowpack/src/types.ts +++ b/snowpack/src/types.ts @@ -279,6 +279,7 @@ export interface SnowpackConfig { out: string; baseUrl: string; metaUrlPath: string; + cacheDirPath: string; clean: boolean; sourcemap: 'inline' | false | undefined; watch: boolean;