Skip to content

Commit

Permalink
Merge pull request #662 from ember-cli/use-shared-template-compiler-c…
Browse files Browse the repository at this point in the history
…ache

Avoid building the template compiler cache key repeatedly
  • Loading branch information
rwjblue authored Feb 26, 2021
2 parents 72e6a6e + 47041c9 commit 7335a9f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
15 changes: 5 additions & 10 deletions lib/template-compiler-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const Filter = require('broccoli-persistent-filter');
Expand Down Expand Up @@ -105,20 +104,16 @@ class TemplateCompiler extends Filter {
return strippedOptions;
}

_templateCompilerContents() {
if (this.options.templateCompilerPath) {
return fs.readFileSync(this.options.templateCompilerPath, { encoding: 'utf8' });
} else {
return '';
}
}

optionsHash() {
if (!this._optionsHash) {
let templateCompilerCacheKey = utils.getTemplateCompilerCacheKey(
this.options.templateCompilerPath
);

this._optionsHash = crypto
.createHash('md5')
.update(stringify(this._buildOptionsForHash()), 'utf8')
.update(stringify(this._templateCompilerContents()), 'utf8')
.update(templateCompilerCacheKey, 'utf8')
.digest('hex');
}

Expand Down
15 changes: 13 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,20 @@ function setup(pluginInfo, options) {
return plugin;
}

function makeCacheKey(templateCompilerPath, pluginInfo, extra) {
function getTemplateCompilerCacheKey(templateCompilerPath) {
let templateCompilerFullPath = require.resolve(templateCompilerPath);
let { templateCompilerCacheKey } = TemplateCompilerCache.get(templateCompilerFullPath);
let cacheData = TemplateCompilerCache.get(templateCompilerFullPath);

if (cacheData === undefined) {
getTemplateCompiler(templateCompilerFullPath);
cacheData = TemplateCompilerCache.get(templateCompilerFullPath);
}

return cacheData.templateCompilerCacheKey;
}

function makeCacheKey(templateCompilerPath, pluginInfo, extra) {
let templateCompilerCacheKey = getTemplateCompilerCacheKey(templateCompilerPath);
let cacheItems = [templateCompilerCacheKey, extra].concat(pluginInfo.cacheKeys.sort());

// extra may be undefined
Expand Down Expand Up @@ -347,4 +357,5 @@ module.exports = {
isInlinePrecompileBabelPluginRegistered,
buildParalleizedBabelPlugin,
getTemplateCompiler,
getTemplateCompilerCacheKey,
};
1 change: 1 addition & 0 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('ember-cli-htmlbars addon', function () {
let htmlbarsOptions = {
isHTMLBars: true,
templateCompiler: require('ember-source/dist/ember-template-compiler.js'),
templateCompilerPath: require.resolve('ember-source/dist/ember-template-compiler.js'),
};

subject = this.addon.transpileTree(input.path(), htmlbarsOptions);
Expand Down
1 change: 1 addition & 0 deletions node-tests/ast_plugins_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('AST plugins', function () {
htmlbarsOptions = {
isHTMLBars: true,
templateCompiler: templateCompiler,
templateCompilerPath: require.resolve('ember-source/dist/ember-template-compiler.js'),
};
})
);
Expand Down
1 change: 1 addition & 0 deletions node-tests/template_compiler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('TemplateCompiler', function () {
ast: [],
},
templateCompiler: require('ember-source/dist/ember-template-compiler.js'),
templateCompilerPath: require.resolve('ember-source/dist/ember-template-compiler.js'),
};

htmlbarsPrecompile = htmlbarsOptions.templateCompiler.precompile;
Expand Down

0 comments on commit 7335a9f

Please sign in to comment.