From 53d3988f1acd70714cd53cb32c47f5e12b84f4c0 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Tue, 20 Apr 2021 08:36:45 -0400 Subject: [PATCH] refactor(core): extract util for packager cache location --- packages/core/src/index.ts | 1 + packages/core/src/packager.ts | 10 ++++++++++ packages/core/tests/packager.test.ts | 10 +++++++++- packages/webpack/src/ember-webpack.ts | 7 ++----- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 4c115ed1a1..2a4f327323 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -5,6 +5,7 @@ export { applyVariantToBabelConfig, applyVariantToTemplateCompiler, getAppMeta, + getPackagerCacheDir, } from './packager'; export { HTMLEntrypoint } from './html-entrypoint'; export { StatSummary } from './stat-summary'; diff --git a/packages/core/src/packager.ts b/packages/core/src/packager.ts index d92ea34e6b..3fe9d5cd2c 100644 --- a/packages/core/src/packager.ts +++ b/packages/core/src/packager.ts @@ -1,6 +1,7 @@ import { AppMeta } from '@embroider/shared-internals'; import { readFileSync } from 'fs-extra'; import { join } from 'path'; +import { tmpdir } from 'os'; // This is a collection of flags that convey what kind of build you want. They // are intended to be generic across Packagers, and it's up to Packager authors @@ -98,3 +99,12 @@ export function applyVariantToTemplateCompiler(_variant: Variant, templateCompil export function getAppMeta(pathToVanillaApp: string): AppMeta { return JSON.parse(readFileSync(join(pathToVanillaApp, 'package.json'), 'utf8'))['ember-addon'] as AppMeta; } + +/** + * Get the path to a cache directory in the recommended location + * + * This ensures they have exactly the same lifetime as some of embroider's own caches. + */ +export function getPackagerCacheDir(name: string): string { + return join(tmpdir(), 'embroider', name); +} diff --git a/packages/core/tests/packager.test.ts b/packages/core/tests/packager.test.ts index b21dcc5775..0331b6462e 100644 --- a/packages/core/tests/packager.test.ts +++ b/packages/core/tests/packager.test.ts @@ -1,6 +1,7 @@ -import { AppMeta, getAppMeta } from '../src'; +import { AppMeta, getAppMeta, getPackagerCacheDir } from '../src'; import { writeJSONSync } from 'fs-extra'; import { join } from 'path'; +import { tmpdir } from 'os'; import * as tmp from 'tmp'; tmp.setGracefulCleanup(); @@ -33,3 +34,10 @@ describe('getAppMeta', () => { }); }); }); + +describe('getPackagerCacheDir', () => { + test('getting the path to a cache directory', () => { + const cacheDir = getPackagerCacheDir('foo'); + expect(cacheDir).toBe(join(tmpdir(), 'embroider', 'foo')); + }); +}); diff --git a/packages/webpack/src/ember-webpack.ts b/packages/webpack/src/ember-webpack.ts index 65d34c80b9..2bf0cfbdd3 100644 --- a/packages/webpack/src/ember-webpack.ts +++ b/packages/webpack/src/ember-webpack.ts @@ -18,6 +18,7 @@ import { Variant, applyVariantToBabelConfig, getAppMeta, + getPackagerCacheDir, getOrCreate, } from '@embroider/core'; import webpack, { Configuration } from 'webpack'; @@ -550,11 +551,7 @@ function nonNullArray(array: T[]): NonNullable[] { function babelLoaderOptions(majorVersion: 6 | 7, variant: Variant, appBabelConfigPath: string) { // eslint-disable-next-line @typescript-eslint/no-require-imports let options = Object.assign({}, applyVariantToBabelConfig(variant, require(appBabelConfigPath)), { - // all stage3 packagers should keep persistent caches under - // `join(tmpdir(), 'embroider')`. An important reason is that - // they should have exactly the same lifetime as some of - // embroider's own caches. - cacheDirectory: join(tmpdir(), 'embroider', 'webpack-babel-loader'), + cacheDirectory: getPackagerCacheDir('webpack-babel-loader'), }); if (majorVersion === 7) { if (options.plugins) {