-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
45 lines (42 loc) · 1.03 KB
/
plugin.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
const yaml = Npm.require('js-yaml');
Plugin.registerCompiler(
{
extensions: ['yml', 'yaml'],
},
() => new YamlCachingCompiler(),
);
class YamlCachingCompiler extends CachingCompiler {
constructor() {
super({
compilerName: 'yaml',
defaultCacheSize: 1024 * 1024 * 10,
});
}
getCacheKey(inputFile) {
return inputFile.getSourceHash();
}
compileResultSize(compileResult) {
return compileResult.length;
}
compileOneFile(inputFile) {
try {
const object = yaml.safeLoad(inputFile.getContentsAsString());
return `module.exports = ${JSON.stringify(object)}`;
} catch (e) {
inputFile.error({
message: e.reason,
sourcePath: inputFile.getPathInPackage(),
line: e.mark.line,
column: e.mark.column,
});
return null;
}
}
addCompileResult(inputFile, compileResult) {
inputFile.addJavaScript({
path: inputFile.getPathInPackage() + '.js',
sourcePath: inputFile.getPathInPackage(),
data: compileResult,
});
}
}