-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (119 loc) · 4.17 KB
/
Copy pathindex.js
File metadata and controls
137 lines (119 loc) · 4.17 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const commandExists = require('command-exists').sync;
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
/** @typedef {import("./index").Options} GrpcWebOptions */
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
class GrpcWebPlugin {
/**
* @param {GrpcWebOptions} options GrpcWebPlugin options
*/
constructor(options) {
/** @type {GrpcWebOptions} */
const userOptions = options || {};
/** @type {GrpcWebOptions} */
const defaultOptions = {
importStyle: 'closure',
binary: false,
mode: 'grpcwebtext',
outDir: '.',
extra: [],
synchronize: true,
watch: true,
};
/** @type {GrpcWebOptions} */
this.options = Object.assign(defaultOptions, userOptions);
}
/**
* Apply the plugin
* @param {WebpackCompiler} compiler Webpack Compiler
* @returns {void}
*/
apply(compiler) {
['protoc', 'protoc-gen-grpc-web'].map(prog => {
if (!commandExists(prog)) {
throw new Error(`${prog} is not installed`);
}
});
const { options } = this;
const logger = compiler.getInfrastructureLogger
? compiler.getInfrastructureLogger('GrpcWebPlugin')
: console;
let outputOption = '';
if (options.outputType === 'grpc-web') {
outputOption = `--grpc-web_out=import_style=${options.importStyle},mode=${options.mode}:${options.outDir}`;
} else if (options.outputType === 'js') {
outputOption = `--js_out=import_style=${options.importStyle}${options.binary ? ',binary' : ''}:${options.outDir}`;
}
if (options.synchronize) {
// Compile all .proto files during initialization
compiler.hooks.afterEnvironment.tap('GrpcWebPlugin', () => {
if (!fs.existsSync(options.outDir)) {
fs.mkdirSync(options.outDir, { recursive: true });
}
const debugInfo = `protoc -I=${options.protoPath} ${options.protoFiles.join(' ')} ${outputOption} ${options.extra.join(' ')}`;
logger.debug(debugInfo);
cp.spawn('protoc', [
`-I=${options.protoPath}`,
...options.protoFiles,
...options.extra,
outputOption,
], {
shell: true,
}).stderr.on('data', error => {
logger.error(debugInfo);
throw new Error(error.toString());
});
});
}
if (options.watch && options.synchronize) {
// Add protos to fileDependencies
compiler.hooks.afterCompile.tap('GrpcWebPlugin', compilation => {
options.protoFiles.forEach(protoFile => {
compilation.fileDependencies.add(
path.join(options.protoPath, protoFile)
);
});
});
// Recompile .proto files whenever they change
compiler.hooks.watchRun.tapAsync('GrpcWebPlugin', (compiler, callback) => {
let changedProtos = [];
if (compiler.modifiedFiles) { // Only in Webpack 5
changedProtos = Array.from(compiler.modifiedFiles).filter(isProtoFile);
} else if (compiler.watchFileSystem.watcher.mtimes) { // Older versions of watchpack use mtimes
changedProtos = Object.keys(
compiler.watchFileSystem.watcher.mtimes
).filter(isProtoFile);
}
if (changedProtos.length !== 0) {
if (!fs.existsSync(options.outDir)) {
fs.mkdirSync(options.outDir, { recursive: true });
}
logger.debug(
`protoc -I=${options.protoPath} ${changedProtos.join(' ')} ${outputOption} ${options.extra.join(' ')}`
);
cp.spawn('protoc', [
`-I=${options.protoPath}`,
...changedProtos,
...options.extra,
outputOption,
], {
shell: true,
}).on('exit', code => {
if (code !== 0) {
return callback(`Compilation failed in ${changedProtos}.`);
} else {
return callback();
}
}).stderr.on('data', error => {
return callback(`Error: ${error}`);
});
}
return callback();
});
}
}
}
const isProtoFile = filename => filename.endsWith('.proto');
module.exports = GrpcWebPlugin;