-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
45 lines (37 loc) · 1.23 KB
/
index.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
'use strict';
var KaitaiStructCompiler = require("kaitai-struct-compiler");
var loaderUtils = require("loader-utils");
var yaml = require('js-yaml');
var path = require('path');
var fs = require('fs');
// this function is called from webpack and 'this' refers
// to https://webpack.js.org/api/loaders/#the-loader-context
module.exports = function (source) {
const options = loaderUtils.getOptions(this) || {};
const debug = options.hasOwnProperty('debug') ? options.debug : this.debug;
if (this.cacheable) this.cacheable();
var callback = this.async();
var structure;
try {
structure = yaml.safeLoad(source);
} catch (error) {
callback(new Error(error));
return;
}
const moduleDir = this.context;
var yamlImporter = {
importYaml: (name, mode) => {
const filePath = path.join(moduleDir, name + ".ksy");
this.addDependency(filePath);
const ksyStr = fs.readFileSync(filePath, "utf8");
const ksyObj = yaml.safeLoad(ksyStr);
return Promise.resolve(ksyObj);
}
};
var compiler = new KaitaiStructCompiler();
compiler.compile("javascript", structure, yamlImporter, debug).then(function (code) {
callback(null, Object.values(code).join('\n'), null);
}).catch(function (error) {
callback(new Error(error), null);
});
};