forked from kmontag/protobufjs-loader
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
122 lines (109 loc) · 3.32 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
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
115
116
117
118
119
120
121
122
"use strict";
const fs = require('fs');
const path = require('path');
const loaderUtils = require("loader-utils");
const pbjs = require('protobufjs/cli').pbjs;
const protobuf = require('protobufjs');
const tmp = require('tmp-promise');
const getOptions = require('loader-utils').getOptions;
const validateOptions = require('schema-utils');
// const jsonModule = require('protobufjs/cli/targets/json-module');
// const staticModule = require('protobufjs/cli/targets/static-module');
const schema = {
type: 'object',
properties: {
json: {
type: 'boolean',
},
paths: {
type: 'array',
},
pbjsArgs: {
type: 'array',
}
},
additionalProperties: false,
}
module.exports = function(source) {
let callback = this.async();
let self = this;
let loaderOptions = loaderUtils.getOptions(this)
const options = Object.assign({
json: false,
// Default to the paths given to the compiler (this.options is the
// webpack options object)
paths: loaderOptions.paths || [],
pbjsArgs: [],
}, getOptions(this));
validateOptions(schema, options, 'protobufjs-loader');
let filename;
tmp.file().then(function(o) {
filename = o.path;
return new Promise(function(resolve, reject) {
fs.write(o.fd, source, function(err, bytesWritten, buffer) {
if (err) {
reject(err);
} else {
resolve(bytesWritten, buffer);
}
});
});
}).then(function() {
let paths = options.paths;
let loadDependencies = new Promise(function(resolve, reject) {
let root = new protobuf.Root();
root.resolvePath = function(origin, target) {
// Adapted from
// https://github.com/dcodeIO/protobuf.js/blob/master/cli/pbjs.js
var normOrigin = protobuf.util.path.normalize(origin),
normTarget = protobuf.util.path.normalize(target);
var resolved = protobuf.util.path.resolve(normOrigin, normTarget, true);
var idx = resolved.lastIndexOf("google/protobuf/");
if (idx > -1) {
var altname = resolved.substring(idx);
if (altname in protobuf.common) {
resolved = altname;
}
}
if (fs.existsSync(resolved)) {
// Don't add a dependency on the temp file
if (resolved !== filename) {
self.addDependency(resolved);
}
return resolved;
}
for (var i = 0; i < paths.length; ++i) {
var iresolved = protobuf.util.path.resolve(paths[i] + "/", target);
if (fs.existsSync(iresolved)) {
self.addDependency(iresolved);
return iresolved;
}
}
};
protobuf.load(filename, root, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
let args = options.pbjsArgs;
paths.forEach(function(path) {
args = args.concat(['-p', path]);
});
args = args.concat([
'-t',
options.json ? 'json-module' : 'static-module',
]).concat([filename]);
pbjs.main(args, function(err, result) {
// Make sure we've added all dependencies before completing.
loadDependencies.catch(function(depErr) {
callback(depErr);
}).then(function() {
console.dir(result)
callback(err, result);
});
});
});
};