ember-legacy-class-transform
Version:
The default blueprint for ember-cli addons.
39 lines • 1.26 kB
JavaScript
import { preprocess } from "@glimmer/syntax";
import TemplateCompiler from "./template-compiler";
const defaultId = (() => {
if (typeof require === 'function') {
try {
/* tslint:disable:no-require-imports */
const crypto = require('crypto');
/* tslint:enable:no-require-imports */
let idFn = src => {
let hash = crypto.createHash('sha1');
hash.update(src, 'utf8');
// trim to 6 bytes of data (2^48 - 1)
return hash.digest('base64').substring(0, 8);
};
idFn("test");
return idFn;
} catch (e) {}
}
return function idFn() {
return null;
};
})();
const defaultOptions = {
id: defaultId,
meta: {}
};
export function precompile(string, options = defaultOptions) {
let ast = preprocess(string, options);
let { block, meta } = TemplateCompiler.compile(options, ast);
let idFn = options.id || defaultId;
let blockJSON = JSON.stringify(block.toJSON());
let templateJSONObject = {
id: idFn(JSON.stringify(meta) + blockJSON),
block: blockJSON,
meta
};
// JSON is javascript
return JSON.stringify(templateJSONObject);
}