Skip to content

Commit 70139a3

Browse files
committed
Finished fixing #48; had to rewrite compilation
Switched to Java version of Closure Compiler to fix errors in the generated JavaScript Also no longer using noutil versions of files because it complicates the compilation process
1 parent 286d88a commit 70139a3

File tree

13 files changed

+501
-1014
lines changed

13 files changed

+501
-1014
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules
2-
**/*-noutil.js
3-
client-side/upload-download.js
2+
client-side/upload-download.js
3+
compiled/*-unminified.js

client-side/common.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*eslint-env browser*/
2-
const assert = require('/lib/assert.js')
2+
const assert = require('../lib/assert')
33
assert.instanceOf(window.Map, Function)
44
assert.instanceOf(window.Set, Function)
55
assert.instanceOf(window.ArrayBuffer, Function)
66
assert.instanceOf(window.Uint8Array, Function)
77
assert.instanceOf(window.Symbol, Function)
8-
require('/client-side/binary-ajax.js')
8+
require('./binary-ajax')
99
if (!window.sb) {
10-
window.sb = require('/structure-types.js')
11-
const recursiveRegistry = require('/recursive-registry.js')
10+
window.sb = require('../structure-types')
11+
const recursiveRegistry = require('../recursive-registry')
1212
for (const key in recursiveRegistry) {
1313
if ({}.hasOwnProperty.call(recursiveRegistry, key)) window.sb[key] = recursiveRegistry[key]
1414
}

client-side/download.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*eslint-env browser*/
22
(() => {
3-
require('/client-side/common.js')
4-
const assert = require('/lib/assert.js')
3+
require('./common')
4+
const assert = require('../lib/assert')
55
const base64 = require('base64-js')
6-
const r = require('/read.js')
6+
const r = require('../read')
77
const typeCache = {}
88
function saveTypeCache() {
99
const composedCache = {}

client-side/upload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*eslint-env browser*/
22
(() => {
3-
require('/client-side/common.js')
4-
const assert = require('/lib/assert.js')
3+
require('./common')
4+
const assert = require('../lib/assert')
55
/** @function
66
* @name upload
77
* @desc <b>(client-side only)</b>

client-test/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*eslint-disable no-console*/
2-
const assert = require(__dirname + '/../lib/assert.js')
2+
const assert = require('../lib/assert')
33
const fs = require('fs')
44
const http = require('http')
5-
const sb = require(__dirname + '/../index.js')
5+
const sb = require('../index')
66

77
const type = new sb.ArrayType(
88
new sb.StructType({

compile.js

Lines changed: 80 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,101 @@
11
#!/usr/bin/env node
2-
/*eslint-disable no-console*/
2+
/*eslint-disable no-console, camelcase*/
33
const browserify = require('browserify')
4-
const closure = require('google-closure-compiler-js').compile
4+
const ClosureCompiler = require('google-closure-compiler').compiler
55
const fs = require('fs')
6-
const ReplaceStream = require('./lib/replace-stream')
6+
const path = require('path')
77
const Simultaneity = require('simultaneity')
88

99
const uploadB = browserify()
10-
uploadB.add(__dirname + '/client-side/upload.js')
10+
uploadB.add(path.join(__dirname, 'client-side/upload.js'))
1111
const downloadB = browserify()
12-
downloadB.add(__dirname + '/client-side/download.js')
12+
downloadB.add(path.join(__dirname, 'client-side/download.js'))
1313
const uploadDownloadB = browserify()
14-
uploadDownloadB.add(__dirname + '/client-side/upload-download.js')
14+
uploadDownloadB.add(path.join(__dirname, 'client-side/upload-download.js'))
1515

16-
const s = new Simultaneity
17-
//Replace require('util'), which is only used for util.inspect(), to minimize file size
18-
for (const utilFile of ['/lib/assert', '/structure-types', '/read']) {
19-
s.addTask(s => {
20-
fs.createReadStream(__dirname + utilFile + '.js')
21-
.pipe(new ReplaceStream("require('util')", "require('/lib/util-inspect.js')"))
22-
.pipe(fs.createWriteStream(__dirname + utilFile + '-noutil.js')).on('finish', () => {
23-
s.taskFinished()
24-
})
25-
})
26-
}
27-
s.addTask(s => {
28-
//Load the upload and download code and append them to each other to make a combined include file
29-
//These files are not too big, so it is not terrible to load them into memory
30-
let uploadCode, downloadCode
31-
new Simultaneity()
32-
.addTask(s => {
33-
fs.readFile(__dirname + '/client-side/upload.js', (err, data) => {
34-
if (err) throw err
35-
uploadCode = data
36-
s.taskFinished()
37-
})
38-
})
39-
.addTask(s => {
40-
fs.readFile(__dirname + '/client-side/download.js', (err, data) => {
41-
if (err) throw err
42-
downloadCode = data
43-
s.taskFinished()
44-
})
45-
})
46-
.callback(() => {
47-
fs.writeFile(
48-
__dirname + '/client-side/upload-download.js',
49-
Buffer.concat([uploadCode, Buffer.from(';'), downloadCode]),
50-
err => {
51-
if (err) throw err
52-
s.taskFinished()
53-
}
54-
)
55-
})
56-
})
57-
console.log('Compiling: Replacing large dependencies')
58-
const uploadFiles = [
59-
'/client-side/binary-ajax.js',
60-
'/client-side/common.js',
61-
'/config.js',
62-
'/lib/bit-math.js',
63-
'/lib/buffer-string.js',
64-
'/lib/flex-int.js',
65-
'/lib/growable-buffer.js',
66-
'/lib/strint.js',
67-
'/lib/util-inspect.js',
68-
'/recursive-registry.js'
16+
const UPLOAD_FILES = [
17+
'./client-side/binary-ajax.js',
18+
'./client-side/common.js',
19+
'./config.js',
20+
'./lib/assert.js',
21+
'./lib/bit-math.js',
22+
'./lib/buffer-string.js',
23+
'./lib/flex-int.js',
24+
'./lib/growable-buffer.js',
25+
'./lib/strint.js',
26+
'./recursive-registry.js',
27+
'./structure-types.js'
6928
]
70-
const downloadFiles = uploadFiles.concat(['/constructor-registry.js'])
71-
s.callback(() => {
72-
//Include the file in the browserify result because it is require()d by other files
73-
function exposeFile(b, name, fileName = name) {
74-
b.require(__dirname + fileName, {expose: name})
29+
const DOWNLOAD_FILES = UPLOAD_FILES.concat([
30+
'./constructor-registry.js',
31+
'./read.js'
32+
])
33+
function initiateCompile() {
34+
//Include a file in the browserify result because it is require()d by other files
35+
function exposeFile(b, fileName) {
36+
b.require(fileName, {expose: fileName})
7537
}
76-
function compile(b, {modifiedFiles, exposeFiles, outputFile}) {
38+
function compile(b, {exposeFiles, outputFile}) {
7739
console.log('Compiling: Browserifying ' + outputFile)
78-
//Expose the files with require('util') removed in place of the true file
79-
for (const ending in modifiedFiles) { //eslint-disable-line guard-for-in
80-
for (const file of modifiedFiles[ending]) exposeFile(b, file + '.js', file + '-' + ending + '.js')
81-
}
82-
//Expose all the unmodified files as normal
40+
const absoluteOutputFile = path.join(__dirname, outputFile)
8341
for (const file of exposeFiles) exposeFile(b, file)
84-
const chunks = []
85-
b.bundle().on('data', chunk => chunks.push(chunk)).on('end', () => { //load output into memory
86-
console.log('Compiling: Minifying ' + outputFile)
87-
const minified = closure({
88-
assumeFunctionWrapper: true,
89-
jsCode: [{src: Buffer.concat(chunks).toString()}],
90-
rewritePolyfills: true
91-
}).compiledCode
92-
fs.writeFile(__dirname + outputFile, '!function(){' + minified + '}()', err => { //write out the minified code
93-
if (err) throw err
42+
const bundleOutputFile = absoluteOutputFile.replace(/.js$/, '-unminified.js')
43+
b.bundle().pipe(fs.createWriteStream(bundleOutputFile))
44+
.on('finish', () => {
45+
console.log('Compiling: Minifying ' + outputFile)
46+
new ClosureCompiler({
47+
js: [bundleOutputFile],
48+
js_output_file: absoluteOutputFile,
49+
output_wrapper: '!function(){%output%}()',
50+
language_out: 'ES5',
51+
jscomp_off: '*' //browserify generates some ugly code that can be ignored
52+
}).run((exitCode, stdOut, stdErr) => {
53+
stdOut = stdOut.trim()
54+
if (stdOut) console.log(stdOut)
55+
stdErr = stdErr.trim()
56+
if (stdErr) console.error(stdErr)
57+
})
9458
})
95-
})
9659
}
9760
compile(uploadB, {
98-
modifiedFiles: {
99-
noutil: ['/lib/assert', '/structure-types']
100-
},
101-
exposeFiles: uploadFiles,
102-
outputFile: '/compiled/upload.js'
61+
exposeFiles: UPLOAD_FILES,
62+
outputFile: 'compiled/upload.js'
10363
})
10464
compile(downloadB, {
105-
modifiedFiles: {
106-
noutil: ['/lib/assert', '/structure-types', '/read']
107-
},
108-
exposeFiles: downloadFiles,
109-
outputFile: '/compiled/download.js'
65+
exposeFiles: DOWNLOAD_FILES,
66+
outputFile: 'compiled/download.js'
11067
})
11168
compile(uploadDownloadB, {
112-
modifiedFiles: {
113-
noutil: ['/lib/assert', '/structure-types', '/read']
114-
},
115-
exposeFiles: downloadFiles,
116-
outputFile: '/compiled/upload-download.js'
69+
exposeFiles: DOWNLOAD_FILES,
70+
outputFile: 'compiled/upload-download.js'
11771
})
118-
})
72+
}
73+
74+
//Load the upload and download code and append them to each other to make a combined include file
75+
//These files are not too big, so it is not terrible to load them into memory
76+
let uploadCode, downloadCode
77+
new Simultaneity()
78+
.addTask(s => {
79+
fs.readFile(path.join(__dirname, 'client-side/upload.js'), (err, data) => {
80+
if (err) throw err
81+
uploadCode = data
82+
s.taskFinished()
83+
})
84+
})
85+
.addTask(s => {
86+
fs.readFile(path.join(__dirname, 'client-side/download.js'), (err, data) => {
87+
if (err) throw err
88+
downloadCode = data
89+
s.taskFinished()
90+
})
91+
})
92+
.callback(() => {
93+
fs.writeFile(
94+
path.join(__dirname, 'client-side/upload-download.js'),
95+
Buffer.concat([uploadCode, Buffer.from(';'), downloadCode]),
96+
err => {
97+
if (err) throw err
98+
initiateCompile()
99+
}
100+
)
101+
})

0 commit comments

Comments
 (0)