Skip to content

Commit

Permalink
chore(core): extract util for packager cache location
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Apr 20, 2021
1 parent e051440 commit e4627b4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
applyVariantToBabelConfig,
applyVariantToTemplateCompiler,
getAppMeta,
getPackagerCacheDir,
} from './packager';
export { HTMLEntrypoint } from './html-entrypoint';
export { StatSummary } from './stat-summary';
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/packager.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
10 changes: 9 additions & 1 deletion packages/core/tests/packager.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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'));
});
});
7 changes: 2 additions & 5 deletions packages/webpack/src/ember-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Variant,
applyVariantToBabelConfig,
getAppMeta,
getPackagerCacheDir,
getOrCreate,
} from '@embroider/core';
import webpack, { Configuration } from 'webpack';
Expand Down Expand Up @@ -550,11 +551,7 @@ function nonNullArray<T>(array: T[]): NonNullable<T>[] {
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) {
Expand Down

0 comments on commit e4627b4

Please sign in to comment.