Skip to content

Commit

Permalink
Finished fixing #48; had to rewrite compilation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
calebsander committed Apr 15, 2017
1 parent 286d88a commit 70139a3
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 1,014 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
**/*-noutil.js
client-side/upload-download.js
client-side/upload-download.js
compiled/*-unminified.js
8 changes: 4 additions & 4 deletions client-side/common.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*eslint-env browser*/
const assert = require('/lib/assert.js')
const assert = require('../lib/assert')
assert.instanceOf(window.Map, Function)
assert.instanceOf(window.Set, Function)
assert.instanceOf(window.ArrayBuffer, Function)
assert.instanceOf(window.Uint8Array, Function)
assert.instanceOf(window.Symbol, Function)
require('/client-side/binary-ajax.js')
require('./binary-ajax')
if (!window.sb) {
window.sb = require('/structure-types.js')
const recursiveRegistry = require('/recursive-registry.js')
window.sb = require('../structure-types')
const recursiveRegistry = require('../recursive-registry')
for (const key in recursiveRegistry) {
if ({}.hasOwnProperty.call(recursiveRegistry, key)) window.sb[key] = recursiveRegistry[key]
}
Expand Down
6 changes: 3 additions & 3 deletions client-side/download.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*eslint-env browser*/
(() => {
require('/client-side/common.js')
const assert = require('/lib/assert.js')
require('./common')
const assert = require('../lib/assert')
const base64 = require('base64-js')
const r = require('/read.js')
const r = require('../read')
const typeCache = {}
function saveTypeCache() {
const composedCache = {}
Expand Down
4 changes: 2 additions & 2 deletions client-side/upload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*eslint-env browser*/
(() => {
require('/client-side/common.js')
const assert = require('/lib/assert.js')
require('./common')
const assert = require('../lib/assert')
/** @function
* @name upload
* @desc <b>(client-side only)</b>
Expand Down
4 changes: 2 additions & 2 deletions client-test/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*eslint-disable no-console*/
const assert = require(__dirname + '/../lib/assert.js')
const assert = require('../lib/assert')
const fs = require('fs')
const http = require('http')
const sb = require(__dirname + '/../index.js')
const sb = require('../index')

const type = new sb.ArrayType(
new sb.StructType({
Expand Down
177 changes: 80 additions & 97 deletions compile.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,101 @@
#!/usr/bin/env node
/*eslint-disable no-console*/
/*eslint-disable no-console, camelcase*/
const browserify = require('browserify')
const closure = require('google-closure-compiler-js').compile
const ClosureCompiler = require('google-closure-compiler').compiler
const fs = require('fs')
const ReplaceStream = require('./lib/replace-stream')
const path = require('path')
const Simultaneity = require('simultaneity')

const uploadB = browserify()
uploadB.add(__dirname + '/client-side/upload.js')
uploadB.add(path.join(__dirname, 'client-side/upload.js'))
const downloadB = browserify()
downloadB.add(__dirname + '/client-side/download.js')
downloadB.add(path.join(__dirname, 'client-side/download.js'))
const uploadDownloadB = browserify()
uploadDownloadB.add(__dirname + '/client-side/upload-download.js')
uploadDownloadB.add(path.join(__dirname, 'client-side/upload-download.js'))

const s = new Simultaneity
//Replace require('util'), which is only used for util.inspect(), to minimize file size
for (const utilFile of ['/lib/assert', '/structure-types', '/read']) {
s.addTask(s => {
fs.createReadStream(__dirname + utilFile + '.js')
.pipe(new ReplaceStream("require('util')", "require('/lib/util-inspect.js')"))
.pipe(fs.createWriteStream(__dirname + utilFile + '-noutil.js')).on('finish', () => {
s.taskFinished()
})
})
}
s.addTask(s => {
//Load the upload and download code and append them to each other to make a combined include file
//These files are not too big, so it is not terrible to load them into memory
let uploadCode, downloadCode
new Simultaneity()
.addTask(s => {
fs.readFile(__dirname + '/client-side/upload.js', (err, data) => {
if (err) throw err
uploadCode = data
s.taskFinished()
})
})
.addTask(s => {
fs.readFile(__dirname + '/client-side/download.js', (err, data) => {
if (err) throw err
downloadCode = data
s.taskFinished()
})
})
.callback(() => {
fs.writeFile(
__dirname + '/client-side/upload-download.js',
Buffer.concat([uploadCode, Buffer.from(';'), downloadCode]),
err => {
if (err) throw err
s.taskFinished()
}
)
})
})
console.log('Compiling: Replacing large dependencies')
const uploadFiles = [
'/client-side/binary-ajax.js',
'/client-side/common.js',
'/config.js',
'/lib/bit-math.js',
'/lib/buffer-string.js',
'/lib/flex-int.js',
'/lib/growable-buffer.js',
'/lib/strint.js',
'/lib/util-inspect.js',
'/recursive-registry.js'
const UPLOAD_FILES = [
'./client-side/binary-ajax.js',
'./client-side/common.js',
'./config.js',
'./lib/assert.js',
'./lib/bit-math.js',
'./lib/buffer-string.js',
'./lib/flex-int.js',
'./lib/growable-buffer.js',
'./lib/strint.js',
'./recursive-registry.js',
'./structure-types.js'
]
const downloadFiles = uploadFiles.concat(['/constructor-registry.js'])
s.callback(() => {
//Include the file in the browserify result because it is require()d by other files
function exposeFile(b, name, fileName = name) {
b.require(__dirname + fileName, {expose: name})
const DOWNLOAD_FILES = UPLOAD_FILES.concat([
'./constructor-registry.js',
'./read.js'
])
function initiateCompile() {
//Include a file in the browserify result because it is require()d by other files
function exposeFile(b, fileName) {
b.require(fileName, {expose: fileName})
}
function compile(b, {modifiedFiles, exposeFiles, outputFile}) {
function compile(b, {exposeFiles, outputFile}) {
console.log('Compiling: Browserifying ' + outputFile)
//Expose the files with require('util') removed in place of the true file
for (const ending in modifiedFiles) { //eslint-disable-line guard-for-in
for (const file of modifiedFiles[ending]) exposeFile(b, file + '.js', file + '-' + ending + '.js')
}
//Expose all the unmodified files as normal
const absoluteOutputFile = path.join(__dirname, outputFile)
for (const file of exposeFiles) exposeFile(b, file)
const chunks = []
b.bundle().on('data', chunk => chunks.push(chunk)).on('end', () => { //load output into memory
console.log('Compiling: Minifying ' + outputFile)
const minified = closure({
assumeFunctionWrapper: true,
jsCode: [{src: Buffer.concat(chunks).toString()}],
rewritePolyfills: true
}).compiledCode
fs.writeFile(__dirname + outputFile, '!function(){' + minified + '}()', err => { //write out the minified code
if (err) throw err
const bundleOutputFile = absoluteOutputFile.replace(/.js$/, '-unminified.js')
b.bundle().pipe(fs.createWriteStream(bundleOutputFile))
.on('finish', () => {
console.log('Compiling: Minifying ' + outputFile)
new ClosureCompiler({
js: [bundleOutputFile],
js_output_file: absoluteOutputFile,
output_wrapper: '!function(){%output%}()',
language_out: 'ES5',
jscomp_off: '*' //browserify generates some ugly code that can be ignored
}).run((exitCode, stdOut, stdErr) => {
stdOut = stdOut.trim()
if (stdOut) console.log(stdOut)
stdErr = stdErr.trim()
if (stdErr) console.error(stdErr)
})
})
})
}
compile(uploadB, {
modifiedFiles: {
noutil: ['/lib/assert', '/structure-types']
},
exposeFiles: uploadFiles,
outputFile: '/compiled/upload.js'
exposeFiles: UPLOAD_FILES,
outputFile: 'compiled/upload.js'
})
compile(downloadB, {
modifiedFiles: {
noutil: ['/lib/assert', '/structure-types', '/read']
},
exposeFiles: downloadFiles,
outputFile: '/compiled/download.js'
exposeFiles: DOWNLOAD_FILES,
outputFile: 'compiled/download.js'
})
compile(uploadDownloadB, {
modifiedFiles: {
noutil: ['/lib/assert', '/structure-types', '/read']
},
exposeFiles: downloadFiles,
outputFile: '/compiled/upload-download.js'
exposeFiles: DOWNLOAD_FILES,
outputFile: 'compiled/upload-download.js'
})
})
}

//Load the upload and download code and append them to each other to make a combined include file
//These files are not too big, so it is not terrible to load them into memory
let uploadCode, downloadCode
new Simultaneity()
.addTask(s => {
fs.readFile(path.join(__dirname, 'client-side/upload.js'), (err, data) => {
if (err) throw err
uploadCode = data
s.taskFinished()
})
})
.addTask(s => {
fs.readFile(path.join(__dirname, 'client-side/download.js'), (err, data) => {
if (err) throw err
downloadCode = data
s.taskFinished()
})
})
.callback(() => {
fs.writeFile(
path.join(__dirname, 'client-side/upload-download.js'),
Buffer.concat([uploadCode, Buffer.from(';'), downloadCode]),
err => {
if (err) throw err
initiateCompile()
}
)
})
Loading

0 comments on commit 70139a3

Please sign in to comment.