Skip to content

Commit

Permalink
Enable cache directory path to be set explicitly (#3064)
Browse files Browse the repository at this point in the history
* Enable cache directory path to be set explicitly

* Add documentation :)
  • Loading branch information
fwouts committed May 20, 2021
1 parent 45e2a22 commit 84e39f8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/reference/configuration.md
Expand Up @@ -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`.
Expand Down
12 changes: 12 additions & 0 deletions 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';
Expand All @@ -21,6 +23,7 @@ import {
import {
addLeadingSlash,
addTrailingSlash,
GLOBAL_CACHE_DIR,
NATIVE_REQUIRE,
REQUIRE_OR_IMPORT,
removeTrailingSlash,
Expand All @@ -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 = {
Expand All @@ -53,6 +63,7 @@ const DEFAULT_CONFIG: SnowpackUserConfig = {
out: 'build',
baseUrl: '/',
metaUrlPath: '_snowpack',
cacheDirPath: DEFAULT_PROJECT_CACHE_DIR,
clean: true,
sourcemap: false,
watch: false,
Expand Down Expand Up @@ -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']}],
Expand Down
1 change: 1 addition & 0 deletions snowpack/src/index.ts
Expand Up @@ -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(),
Expand Down
11 changes: 3 additions & 8 deletions 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';
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions snowpack/src/types.ts
Expand Up @@ -279,6 +279,7 @@ export interface SnowpackConfig {
out: string;
baseUrl: string;
metaUrlPath: string;
cacheDirPath: string;
clean: boolean;
sourcemap: 'inline' | false | undefined;
watch: boolean;
Expand Down

1 comment on commit 84e39f8

@vercel
Copy link

@vercel vercel bot commented on 84e39f8 May 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.