1 | const https = require('https')
|
2 | const fs = require('fs')
|
3 | const { URLSearchParams } = require('url')
|
4 |
|
5 |
|
6 | const code = fs.readFileSync('./FormData.js', 'utf8').toString()
|
7 |
|
8 |
|
9 | const 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 |
|
18 | const 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 |
|
29 | const 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 |
|
41 | post_req.write(post_data)
|
42 | post_req.end()
|