Skip to content

Commit

Permalink
[broccoli-eyeglass] Use the build cache for assets caching so it will…
Browse files Browse the repository at this point in the history
… be shared across the session.
  • Loading branch information
chriseppstein committed Mar 23, 2019
1 parent 9bf60f7 commit 91f8541
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
13 changes: 8 additions & 5 deletions packages/broccoli-eyeglass/src/index.ts
Expand Up @@ -62,7 +62,6 @@ class EyeglassCompiler extends BroccoliSassCompiler {
private relativeAssets: boolean | undefined;
private assetDirectories: Array<string> | undefined;
private assetsHttpPrefix: string | undefined;
private _assetImportCache: Record<string, string>;
private _assetImportCacheStats: { hits: number; misses: number };
private _dependenciesHash: string | undefined;
constructor(inputTrees: BroccoliPlugin.BroccoliNode | Array<BroccoliPlugin.BroccoliNode>, options: BroccoliEyeglassOptions) {
Expand Down Expand Up @@ -100,7 +99,6 @@ class EyeglassCompiler extends BroccoliSassCompiler {
this.assetsHttpPrefix = assetsHttpPrefix;
this.events.on("compiling", this.handleNewFile.bind(this));

this._assetImportCache = Object.create(null);
this._assetImportCacheStats = {
hits: 0,
misses: 0,
Expand Down Expand Up @@ -136,6 +134,7 @@ class EyeglassCompiler extends BroccoliSassCompiler {

let eyeglass = new Eyeglass(options);


if (this.assetDirectories) {
for (var i = 0; i < this.assetDirectories.length; i++) {
eyeglass.assets.addSource(
Expand Down Expand Up @@ -229,14 +228,18 @@ class EyeglassCompiler extends BroccoliSassCompiler {
// Cache the asset import code that is generated in eyeglass
cacheAssetImports(key: string, getValue: () => string): string {
// if this has already been generated, return it from cache
if (this._assetImportCache[key] !== undefined) {
let assetImportKey = `assetImport(${key})`;
let assetImport = this.buildCache.get(assetImportKey) as string | undefined;
if (assetImport !== undefined) {
assetImportCacheDebug("cache hit for key '%s'", key);
this._assetImportCacheStats.hits += 1;
return this._assetImportCache[key];
return assetImport;
}
assetImportCacheDebug("cache miss for key '%s'", key);
this._assetImportCacheStats.misses += 1;
return (this._assetImportCache[key] = getValue());
assetImport = getValue();
this.buildCache.set(assetImportKey, assetImport);
return assetImport;
}
}

Expand Down
23 changes: 17 additions & 6 deletions packages/broccoli-eyeglass/test/test_eyeglass_plugin.js
Expand Up @@ -223,7 +223,7 @@ describe("EyeglassCompiler", function() {
let path = modules.path();
modules.copy(FIXTURES.path("manualModule"));
input.copy(FIXTURES.path("usesManualModule/input"));

let optimizer = new EyeglassCompiler(input.path(), {
cssDir: ".",
fullException: true,
Expand Down Expand Up @@ -1361,14 +1361,19 @@ describe("EyeglassCompiler", function() {
output = createBuilder(compiler);

// cache should start empty
assert.strictEqual(Object.keys(compiler._assetImportCache).length, 0);
assert.strictEqual(compiler.buildCache.size, 0);

yield output.build();

assertEqualDirs(output.path(), expectedOutput);

// cache should have one entry
assert.strictEqual(Object.keys(compiler._assetImportCache).length, 1);
let assetsCached = 0;
for (let k of compiler.buildCache.keys()) {
if (k.startsWith("assetImport(")) {
assetsCached += 1;
}
}
assert.strictEqual(assetsCached, 1);
// first file should be a miss, 2nd should return from cache
assert.strictEqual(compiler._assetImportCacheStats.misses, 1);
assert.strictEqual(compiler._assetImportCacheStats.hits, 1);
Expand Down Expand Up @@ -1422,13 +1427,19 @@ describe("EyeglassCompiler", function() {
output = createBuilder(compiler);

// cache should start empty
assert.strictEqual(Object.keys(compiler._assetImportCache).length, 0);
assert.strictEqual(compiler.buildCache.size, 0);

yield output.build();

assertEqualDirs(output.path(), expectedOutput);
// cache should have one entry
assert.strictEqual(Object.keys(compiler._assetImportCache).length, 1);
let assetsCached = 0;
for (let k of compiler.buildCache.keys()) {
if (k.startsWith("assetImport(")) {
assetsCached += 1;
}
}
assert.strictEqual(assetsCached, 1);
// first file should be a miss, 2nd should return from cache
assert.strictEqual(compiler._assetImportCacheStats.misses, 1);
assert.strictEqual(compiler._assetImportCacheStats.hits, 1);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-cli-eyeglass/src/index.ts
Expand Up @@ -195,7 +195,7 @@ const EMBER_CLI_EYEGLASS = {
let {assets} = EYEGLASS_INFO_PER_APP.get(app);
return assets.ln_s(srcFile, destUri);
},

setupConfig(config: ConstructorParameters<typeof EyeglassCompiler>[1], options) {
let {isApp, app, parentPath} = EYEGLASS_INFO_PER_ADDON.get(this);
let {sessionCache} = EYEGLASS_INFO_PER_APP.get(app);
Expand Down

0 comments on commit 91f8541

Please sign in to comment.