From 47041c9003e6e7347822251f11d62014e69b0d81 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 25 Feb 2021 21:43:15 -0500 Subject: [PATCH] Avoid building the template compiler cache key repeatedly --- lib/template-compiler-plugin.js | 15 +++++---------- lib/utils.js | 15 +++++++++++++-- node-tests/addon-test.js | 1 + node-tests/ast_plugins_test.js | 1 + node-tests/template_compiler_test.js | 1 + 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/template-compiler-plugin.js b/lib/template-compiler-plugin.js index 4beb2d80..0074792e 100644 --- a/lib/template-compiler-plugin.js +++ b/lib/template-compiler-plugin.js @@ -1,6 +1,5 @@ 'use strict'; -const fs = require('fs'); const path = require('path'); const utils = require('./utils'); const Filter = require('broccoli-persistent-filter'); @@ -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'); } diff --git a/lib/utils.js b/lib/utils.js index dd051799..b570e2fe 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 @@ -347,4 +357,5 @@ module.exports = { isInlinePrecompileBabelPluginRegistered, buildParalleizedBabelPlugin, getTemplateCompiler, + getTemplateCompilerCacheKey, }; diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index 4a008ee9..3b81566a 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -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); diff --git a/node-tests/ast_plugins_test.js b/node-tests/ast_plugins_test.js index 5396b006..633a4d27 100644 --- a/node-tests/ast_plugins_test.js +++ b/node-tests/ast_plugins_test.js @@ -26,6 +26,7 @@ describe('AST plugins', function () { htmlbarsOptions = { isHTMLBars: true, templateCompiler: templateCompiler, + templateCompilerPath: require.resolve('ember-source/dist/ember-template-compiler.js'), }; }) ); diff --git a/node-tests/template_compiler_test.js b/node-tests/template_compiler_test.js index 3f47d583..d4401c9e 100644 --- a/node-tests/template_compiler_test.js +++ b/node-tests/template_compiler_test.js @@ -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;