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 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 |
|
19 | const 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 |
|
30 | const 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 |
|
42 | req.write(postData)
|
43 | req.end()
|