UNPKG

1.09 kBJavaScriptView Raw
1const https = require('https')
2const fs = require('fs')
3const { URLSearchParams } = require('url')
4
5// This is an async file read
6const code = fs.readFileSync('./FormData.js', 'utf8').toString()
7
8// Build the post string from an object
9const postData = new URLSearchParams({
10 compilation_level: 'ADVANCED_OPTIMIZATIONS',
11 output_format: 'text',
12 output_info: 'compiled_code',
13 warning_level: 'QUIET',
14 output_wrapper: ';(function(){%output%})();',
15 js_code: code
16}).toString()
17
18// An object of options to indicate where to post to
19const options = {
20 host: 'closure-compiler.appspot.com',
21 path: '/compile',
22 method: 'POST',
23 headers: {
24 'Content-Type': 'application/x-www-form-urlencoded',
25 'Content-Length': Buffer.byteLength(postData)
26 }
27}
28
29// Set up the request
30const req = https.request(options, res => {
31 res.setEncoding('utf8')
32
33 if (res.statusCode !== 200) {
34 console.log('FATAL An error occurred trying to use closure compiler')
35 process.exit(-2)
36 }
37
38 res.pipe(fs.createWriteStream('formdata.min.js'))
39})
40
41// post the data
42req.write(postData)
43req.end()