-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
286d88a
commit 70139a3
Showing
13 changed files
with
501 additions
and
1,014 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
) | ||
}) |
Oops, something went wrong.