1
1
#!/usr/bin/env node
2
2
3
- var fs = require ( 'fs' ) ;
4
- var path = require ( 'path' ) ;
5
- var querystring = require ( 'querystring' ) ;
6
- var child_process = require ( 'child_process' ) ;
3
+ const fs = require ( 'node:fs' ) ;
4
+ const path = require ( 'node:path' ) ;
5
+ const child_process = require ( 'node:child_process' ) ;
6
+ const https = require ( 'node:https' ) ;
7
+ const { text } = require ( 'node:stream/consumers' ) ;
7
8
8
- var browserify = path . resolve ( path . join ( 'node_modules' , '.bin' , 'browserify' ) ) ;
9
- var webpack = path . resolve ( path . join ( 'node_modules' , '.bin' , 'webpack' ) ) ;
10
- var coffee = path . resolve ( path . join ( 'node_modules' , '.bin' , 'coffee' ) ) ;
9
+ const browserify = path . resolve ( path . join ( 'node_modules' , '.bin' , 'browserify' ) ) ;
10
+ const webpack = path . resolve ( path . join ( 'node_modules' , '.bin' , 'webpack' ) ) ;
11
+ const coffee = path . resolve ( path . join ( 'node_modules' , '.bin' , 'coffee' ) ) ;
11
12
12
13
function run ( command , callback ) {
13
14
console . log ( command ) ;
@@ -16,90 +17,90 @@ function run(command, callback) {
16
17
17
18
// Use browserify to package up source-map-support.js
18
19
fs . writeFileSync ( '.temp.js' , 'sourceMapSupport = require("./source-map-support");' ) ;
19
- run ( browserify + ' .temp.js' , function ( error , stdout ) {
20
+
21
+ run ( browserify + ' .temp.js' , ( error , stdout ) => {
20
22
if ( error ) throw error ;
21
23
22
24
// Wrap the code so it works both as a normal <script> module and as an AMD module
23
- var header = [
25
+ const header = [
24
26
'/*' ,
25
27
' * Support for source maps in V8 stack traces' ,
26
28
' * https://github.com/evanw/node-source-map-support' ,
27
29
' */' ,
28
30
] . join ( '\n' ) ;
29
- var code = [
31
+
32
+ const code = [
30
33
'(this["define"] || function(name, callback) { this["sourceMapSupport"] = callback(); })("browser-source-map-support", function(sourceMapSupport) {' ,
31
34
stdout . replace ( / \b b y t e \b / g, 'bite' ) . replace ( new RegExp ( __dirname + '/' , 'g' ) , '' ) . replace ( / @ l i c e n s e / g, 'license' ) ,
32
35
'return sourceMapSupport});' ,
33
36
] . join ( '\n' ) ;
34
37
35
38
// Use the online Google Closure Compiler service for minification
36
- var body = Buffer . from ( querystring . stringify ( {
39
+ const body = new URLSearchParams ( {
37
40
compilation_level : 'SIMPLE_OPTIMIZATIONS' ,
38
41
output_info : 'compiled_code' ,
39
42
output_format : 'text' ,
40
43
js_code : code
41
- } ) ) ;
44
+ } ) ;
45
+
46
+ const buffer = new TextEncoder ( ) . encode ( body . toString ( ) )
47
+
42
48
console . log ( 'making request to google closure compiler' ) ;
43
- var https = require ( 'https' ) ;
44
- var request = https . request ( {
49
+
50
+ const request = https . request ( {
45
51
method : 'POST' ,
46
52
host : 'closure-compiler.appspot.com' ,
47
53
path : '/compile' ,
48
54
headers : {
49
- 'content-length' : body . length ,
55
+ 'content-length' : buffer . byteLength ,
50
56
'content-type' : 'application/x-www-form-urlencoded'
51
57
} ,
52
58
} ) ;
53
- request . end ( body ) ;
54
- request . on ( 'response' , function ( response ) {
55
- if ( response . statusCode !== 200 ) {
56
- response . pipe ( process . stderr ) ;
57
- response . on ( 'end' , function ( ) {
58
- throw new Error ( 'failed to post to closure compiler' ) ;
59
- } ) ;
60
- return ;
61
- }
62
59
63
- var data = [ ] ;
64
- response . on ( 'data' , function ( chunk ) {
65
- data . push ( chunk ) ;
66
- } ) ;
67
- response . on ( 'end' , function ( ) {
68
- var stdout = Buffer . concat ( data ) ;
69
- var code = header + '\n' + stdout ;
60
+ request . once ( 'response' , response => {
61
+ text ( response ) . then ( stdout => {
70
62
fs . unlinkSync ( '.temp.js' ) ;
63
+
64
+ if ( response . statusCode !== 200 ) {
65
+ console . error ( stdout ) ;
66
+ throw new Error ( 'failed to post to closure compiler' ) ;
67
+ }
68
+
69
+ const code = header + '\n' + stdout ;
71
70
fs . writeFileSync ( 'browser-source-map-support.js' , code ) ;
72
71
fs . writeFileSync ( 'amd-test/browser-source-map-support.js' , code ) ;
73
72
} ) ;
74
73
} ) ;
74
+
75
+ request . end ( buffer ) ;
75
76
} ) ;
76
77
77
78
// Build the AMD test
78
- run ( coffee + ' --map --compile amd-test/script.coffee' , function ( error ) {
79
+ run ( coffee + ' --map --compile amd-test/script.coffee' , error => {
79
80
if ( error ) throw error ;
80
81
} ) ;
81
82
82
83
// Build the browserify test
83
- run ( coffee + ' --map --compile browserify-test/script.coffee' , function ( error ) {
84
+ run ( coffee + ' --map --compile browserify-test/script.coffee' , error => {
84
85
if ( error ) throw error ;
85
- run ( browserify + ' --debug browserify-test/script.js > browserify-test/compiled.js' , function ( error ) {
86
+ run ( browserify + ' --debug browserify-test/script.js > browserify-test/compiled.js' , error => {
86
87
if ( error ) throw error ;
87
88
} )
88
89
} ) ;
89
90
90
91
// Build the browser test
91
- run ( coffee + ' --map --compile browser-test/script.coffee' , function ( error ) {
92
+ run ( coffee + ' --map --compile browser-test/script.coffee' , error => {
92
93
if ( error ) throw error ;
93
94
} ) ;
94
95
95
96
// Build the header test
96
- run ( coffee + ' --map --compile header-test/script.coffee' , function ( error ) {
97
+ run ( coffee + ' --map --compile header-test/script.coffee' , error => {
97
98
if ( error ) throw error ;
98
- var contents = fs . readFileSync ( 'header-test/script.js' , 'utf8' ) ;
99
+ const contents = fs . readFileSync ( 'header-test/script.js' , 'utf8' ) ;
99
100
fs . writeFileSync ( 'header-test/script.js' , contents . replace ( / \/ \/ # s o u r c e M a p p i n g U R L = .* / g, '' ) )
100
101
} ) ;
101
102
102
103
// Build the webpack test
103
- child_process . exec ( webpack , { cwd : 'webpack-test' } , function ( error ) {
104
+ child_process . exec ( webpack , { cwd : 'webpack-test' } , error => {
104
105
if ( error ) throw error ;
105
106
} ) ;
0 commit comments