forked from tkesgar/replacer-brunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (94 loc) · 3.12 KB
/
index.js
File metadata and controls
114 lines (94 loc) · 3.12 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"use strict";
const fs = require("fs");
class BrunchReplacer {
constructor(config) {
// Grab the config options from brunch.config.plugins.replacer
this.config = config.plugins.replacer || {};
// Set default dictionary
if (!this.config.dict) this.config.dict = [];
// Set default replace method
if (!this.config.replace) {
this.config.replace = (str, key, value) => {
const reg = new RegExp(key, "g");
return str.replace(reg, value);
};
}
// Allow override of file pattern (defaults to js, svelte, jsx)
if (this.config.pattern) {
this.pattern = this.config.pattern;
delete this.config.pattern;
}
// Stringify non-string values.
for (const entry of this.config.dict) {
const value = entry.value;
if (typeof value === "undefined") {
entry.value = "";
} else {
entry.value = typeof value === "string" ? value : JSON.stringify(value);
}
}
}
// Handle rewritting keys in html assets (index.html)
onCompile(error, assets) {
// Grab the dict and replace method
const dict = this.config.dict;
const replace = this.config.replace;
for (let asset of assets) {
// Make sure its an html file
if (asset.destinationPath.includes(".html")) {
let promise = new Promise((resolve, reject) => {
// Get the html text
let htmlString = asset.compiled.toString();
// Replace all the keys
for (const entry of dict) {
const key = entry.key;
const value = entry.value;
htmlString = replace(htmlString, key, value, asset.destinationPath);
}
// Create a buffer from the final html string
const buffer = Buffer.from(htmlString);
// Write to file
try {
fs.writeFile(asset.destinationPath, buffer, (err) => {
if (err) reject(err);
else resolve();
});
} catch (error) {
console.error(error);
}
});
// Resolve promise
promise.catch((error) => {
console.error(
`Key value rewritting for ${asset.destinationPath} failed`
);
console.error(error.message);
});
}
}
}
compile(file) {
// Grab the ditc and replace method
const dict = this.config.dict;
const replace = this.config.replace;
// Ignore falsy and files without data.
if (!file || !file.data) return Promise.resolve(file);
// Perform replacement.
for (const entry of dict) {
const key = entry.key;
const value = entry.value;
file.data = replace(file.data, key, value, file.path);
}
// Resolve promise
return Promise.resolve(file);
}
}
// It is a brunch plugin
BrunchReplacer.prototype.brunchPlugin = true;
// Set the type
BrunchReplacer.prototype.type = "javascript";
// Set the default file pattern, can be overriden in the options by using pattern
BrunchReplacer.prototype.pattern = /\.(js|svelte|jsx)?$/;
// Set default env
BrunchReplacer.prototype.defaultEnv = "*";
module.exports = BrunchReplacer;