UNPKG

1.07 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 post_data = new URLSearchParams({
10 compilation_level: 'ADVANCED_OPTIMIZATIONS',
11 output_format: 'text',
12 output_info: 'compiled_code',
13 warning_level: 'QUIET',
14 js_code: code
15}).toString()
16
17// An object of options to indicate where to post to
18const post_options = {
19 host: 'closure-compiler.appspot.com',
20 path: '/compile',
21 method: 'POST',
22 headers: {
23 'Content-Type': 'application/x-www-form-urlencoded',
24 'Content-Length': Buffer.byteLength(post_data)
25 }
26}
27
28// Set up the request
29const post_req = https.request(post_options, res => {
30 res.setEncoding('utf8')
31
32 if (res.statusCode !== 200) {
33 console.log('FATAL An error occurred trying to use closure compiler')
34 process.exit(-2)
35 }
36
37 res.pipe(fs.createWriteStream('formdata.min.js'))
38})
39
40// post the data
41post_req.write(post_data)
42post_req.end()