Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid building the template compiler cache key repeatedly #662

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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