Commit b364cdea authored by Evan Hahn's avatar Evan Hahn

Initial commit

parents
/node_modules/
This diff is collapsed.
{
"private": true,
"scripts": {
"start": "node src/dev-server",
"test": "standard --fix"
},
"devDependencies": {
"babel-minify": "^0.3.0",
"ejs": "^2.5.7",
"express": "^4.16.2",
"regpack": "^5.0.1",
"standard": "^10.0.3"
}
}
const express = require('express')
const path = require('path')
const minify = require('./minify')
const app = express()
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.get('/', async (req, res) => {
const output = await minify()
const byteCount = Buffer.from(output, 'utf8').length
res.render('shim', { output, byteCount })
})
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log('App started on :' + port)
})
/*
* variable list
* a = canvas
* b = body
* c = context
* d = document
* e = array of entities (q is a method called on every tick)
* f = pressing Left?
* g = pressing Right?
* l = last tick time
* p = player
* q = tick function
* r = scalar relative to screen size
* s = ∆t
*/
/* eslint-disable no-undef, no-return-assign */
// Initial setup
a.style.background = '#0f073b'
r = Math.min(a.width, a.height) / 30
// The player
p = ((playerX, playerY, playerVelocity, playerAcceleration, playerDirection) => {
f = g = 0
onkeydown = event => {
if (event.keyCode === 65) { f = 1 }
if (event.keyCode === 68) { g = 1 }
}
onkeyup = event => {
if (event.keyCode === 65) { f = 0 }
if (event.keyCode === 68) { g = 0 }
}
return {
q: () => {
playerDirection += ((g * 5) - (f * 5)) * s
playerVelocity = Math.min(r, playerVelocity + playerAcceleration * s)
playerX += Math.cos(playerDirection) * playerVelocity * r * s
playerY += Math.sin(playerDirection) * playerVelocity * r * s
if (playerX < 0) { playerX += a.width }
if (playerY < 0) { playerY += a.height }
playerX = playerX % a.width
playerY = playerY % a.width
// this could probably be smaller ^
c.fillStyle = '#ffff00'
c.translate(playerX, playerY)
c.rotate(playerDirection)
c.beginPath()
c.moveTo(-r, -r)
c.lineTo(r, 0)
c.lineTo(-r, r)
c.fill()
c.setTransform(1, 0, 0, 1, 0, 0)
}
}
})(a.width / 2, a.height / 2, 0, 3, 0)
// Define the list of entities
e = [p]
// Tick function
l = 0
requestAnimationFrame(q = (currentTime) => {
s = (l ? currentTime - l : 0) / 1000
l = currentTime
a.width = a.width // clears the screen
e.map(entity => entity.q())
requestAnimationFrame(q)
})
const path = require('path')
const fs = require('fs')
const util = require('util')
const childProcess = require('child_process')
const RegPack = require('regpack').RegPack
const readFile = util.promisify(fs.readFile)
const execFile = util.promisify(childProcess.execFile)
const SOURCE_PATH = path.join(__dirname, 'js1k.js')
const BIN_PATH = path.join(__dirname, '..', 'node_modules', '.bin')
const BABEL_MINIFY_PATH = path.join(BIN_PATH, 'minify')
const BABEL_MINIFY_OPTIONS = ['--builtIns', '--booleans', '--mangle', '--infinity', '--flipComparisons', '--mangle.topLevel']
module.exports = async function minify () {
const rawSource = await readFile(SOURCE_PATH)
const babelResult = await babelMinify()
const regPackResult = regpackMinify(babelResult)
console.log()
console.log('Raw source size: ' + rawSource.length)
console.log('Babel minify size: ' + babelResult.length)
console.log('Regpack size: ' + regPackResult.length)
const results = [rawSource, babelResult, regPackResult]
return shortest(results).toString('utf8').trim()
}
function shortest (arr) {
return arr.reduce((result, value) => {
return result.length < value.length ? result : value
}, arr[0]).toString('utf8').trim()
}
async function babelMinify () {
const result = await execFile(BABEL_MINIFY_PATH, [SOURCE_PATH].concat(BABEL_MINIFY_OPTIONS))
const withoutTrailingSemicolon = result.stdout.replace(/;$/, '')
return Buffer.from(withoutTrailingSemicolon, 'utf8')
}
function regpackMinify (sourceBuffer) {
const source = sourceBuffer.toString('utf8')
const regpack = new RegPack()
const packerResults = regpack.runPacker(source, {
withMath: false,
hash2DContext: true,
contextVariableName: 'c',
reassignVars: true,
varsNotReassigned: ['a', 'b', 'c', 'd']
})
const resultStrings = packerResults.reduce((resultStrings, packerResult) => {
const packerResultStrings = packerResult.result.map((r) => r[1])
return resultStrings.concat(packerResultStrings)
}, [])
return Buffer.from(shortest(resultStrings))
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment