From 18b58a9052217292ec713096fd1bf4876b9a273a Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 10 Aug 2016 00:48:32 -0400 Subject: [PATCH] Support cache busting by AST plugins. --- ember-addon-main.js | 36 ++++++++++++++++++++++++++++++------ package.json | 1 + 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ember-addon-main.js b/ember-addon-main.js index f69ba3f1..8d37c88c 100644 --- a/ember-addon-main.js +++ b/ember-addon-main.js @@ -3,6 +3,7 @@ var path = require('path'); var checker = require('ember-cli-version-checker'); var utils = require('./utils'); +var hashForDep = require('hash-for-dep'); module.exports = { name: 'ember-cli-htmlbars', @@ -90,6 +91,8 @@ module.exports = { delete require.cache[templateCompilerPath]; global.EmberENV = EmberENV; // Needed for eval time feature flag checks + var pluginInfo = this.astPlugins(); + var htmlbarsOptions = { isHTMLBars: true, EmberENV: EmberENV, @@ -97,8 +100,10 @@ module.exports = { templateCompilerPath: templateCompilerPath, plugins: { - ast: this.astPlugins() - } + ast: pluginInfo.plugins + }, + + pluginCacheKey: pluginInfo.cacheKeys }; delete require.cache[templateCompilerPath]; @@ -110,10 +115,29 @@ module.exports = { astPlugins: function() { var pluginWrappers = this.parentRegistry.load('htmlbars-ast-plugin'); - var plugins = pluginWrappers.map(function(wrapper) { - return wrapper.plugin; - }); + var plugins = []; + var cacheKeys = []; + + for (var i = 0; i < pluginWrappers.length; i++) { + var wrapper = pluginWrappers[i]; - return plugins; + plugins.push(wrapper.plugin); + + if (typeof wrapper.baseDir === 'function') { + var pluginHashForDep = hashForDep(wrapper.baseDir()); + cacheKeys.push(pluginHashForDep); + } else { + // support for ember-cli < 2.2.0 + var log = this.ui.writeDeprecateLine || this.ui.writeLine; + + log.call(this.ui, 'ember-cli-htmlbars is opting out of caching due to an AST plugin that does not provide a caching strategy: `' + wrapper.name + '`.'); + cacheKeys.push((new Date()).getTime() + '|' + Math.random()); + } + } + + return { + plugins: plugins, + cacheKeys: cacheKeys + }; } }; diff --git a/package.json b/package.json index 600df182..d15d57ce 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "dependencies": { "broccoli-persistent-filter": "^1.0.3", "ember-cli-version-checker": "^1.0.2", + "hash-for-dep": "^1.0.2", "json-stable-stringify": "^1.0.0", "strip-bom": "^2.0.0" }