This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ast-plugins.js
72 lines (57 loc) · 2.68 KB
/
ast-plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* jshint node: true */
'use strict';
const fs = require('fs');
const path = require('path');
const HTMLBarsInlinePrecompilePlugin = require('babel-plugin-htmlbars-inline-precompile');
const pluginCache = {};
module.exports = {
setup(pluginInfo, options) {
// borrowed from ember-cli-htmlbars http://git.io/vJDrW
let projectConfig = options.projectConfig || {};
let templateCompilerPath = options.templateCompilerPath;
let useCache = options.useCache;
let EmberENV = projectConfig.EmberENV || {};
// ensure we get a fresh templateCompilerModuleInstance per ember-addon
// instance NOTE: this is a quick hack, and will only work as long as
// templateCompilerPath is a single file bundle
//
// (╯°□°)╯︵ ɹǝqɯǝ
//
// we will also fix this in ember for future releases
delete require.cache[templateCompilerPath];
// do a full clone of the EmberENV (it is guaranteed to be structured
// cloneable) to prevent ember-template-compiler.js from mutating
// the shared global config
let clonedEmberENV = JSON.parse(JSON.stringify(EmberENV));
global.EmberENV = clonedEmberENV;
let Compiler = require(templateCompilerPath);
let cacheKey = this.makeCacheKey(templateCompilerPath, pluginInfo);
let precompileInlineHTMLBarsPlugin;
// babel memoizes every plugin it sees,
// and creating a new plugin for every transpilation will eventually overflow the heap
// so use a local cache to avoid that
if (useCache && pluginCache[cacheKey] !== undefined) {
precompileInlineHTMLBarsPlugin = pluginCache[cacheKey];
} else {
pluginInfo.plugins.forEach((plugin) => Compiler.registerPlugin('ast', plugin));
let precompile = Compiler.precompile;
precompile.baseDir = () => path.resolve(__dirname, '..');
precompile.cacheKey = () => cacheKey;
let modulePaths = ['ember-cli-htmlbars-inline-precompile', 'htmlbars-inline-precompile'];
precompileInlineHTMLBarsPlugin = [HTMLBarsInlinePrecompilePlugin, { precompile, modulePaths }];
pluginCache[cacheKey] = precompileInlineHTMLBarsPlugin;
}
delete require.cache[templateCompilerPath];
delete global.Ember;
delete global.EmberENV;
return precompileInlineHTMLBarsPlugin;
},
makeCacheKey(templateCompilerPath, pluginInfo, extra) {
require(templateCompilerPath);
let templateCompilerFullPath = require.resolve(templateCompilerPath);
let templateCompilerCacheKey = fs.readFileSync(templateCompilerFullPath, { encoding: 'utf-8' });
let cacheItems = [templateCompilerCacheKey, extra].concat(pluginInfo.cacheKeys.sort());
// extra may be undefined
return cacheItems.filter(Boolean).join('|');
}
};