forked from Asana/bazeltsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
37 lines (36 loc) · 1.54 KB
/
webpack.config.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
const webpack = require('webpack');
module.exports = {
entry: './lib/bazeltsc.js',
output: {
filename: 'bazeltsc.js'
},
target: 'node',
externals: {
// This handles require("typescript") from a file that is inside Bazel's
// sandbox.
//
// This is complicated. We want typescript to be a "peer dependency" -- not
// bundled, but rather, located dynamically at runtime (in order to allow
// people who are using bazeltsc to use it with any version of TypeScript).
// But if we just use a regular import or require() from this file, then we
// will be potentially bypassing Bazel's sandboxing, by starting at this
// file's location and searching up from there for node_modules/typescript
// directory. Instead, we want to start at Bazel's execution_root (see
// `bazel info execution_root`).
//
// The safest way we have found to do that is to dynamically write a short
// JavaScript file to the process.cwd() directory (which is the
// execution_root), and let _that_ file call require().
'typescript': `(function() {
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const pseudorand = crypto.randomBytes(4).readUInt32LE(0);
const typescriptProxy = path.join(process.cwd(), "typescript_proxy_" + pseudorand + ".js");
fs.writeFileSync(typescriptProxy, 'module.exports = require("typescript");');
const typescript = require(typescriptProxy);
fs.unlinkSync(typescriptProxy);
return typescript;
})()`
}
}