-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwebpack.helpers.js
More file actions
35 lines (29 loc) · 1.05 KB
/
Copy pathwebpack.helpers.js
File metadata and controls
35 lines (29 loc) · 1.05 KB
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
const handlebarsLayouts = require('handlebars-layouts');
const handlebarsContext = {};
function _handlebarsEqualHelper(name, value, options) {
return handlebarsContext[name] === value ? options.fn(this) : options.inverse(this);
}
function _handlebarsVariablesHelper(name, options) {
const content = options.fn(this);
handlebarsContext[name] = content;
}
function registerHandlersHelpers(Handlebars) {
Handlebars.registerHelper('equal', _handlebarsEqualHelper);
Handlebars.registerHelper('set', _handlebarsVariablesHelper);
handlebarsLayouts.register(Handlebars);
}
function _escapeForRegex(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
function makeDataReplacements(originalData) {
const { replacements, ...data } = originalData;
let dataAsString = JSON.stringify(data);
Object.keys(replacements).map(key => {
dataAsString = dataAsString.replace(new RegExp(_escapeForRegex(key), 'g'), replacements[key]);
});
return JSON.parse(dataAsString);
}
module.exports = {
makeDataReplacements,
registerHandlersHelpers,
};