Last active
November 3, 2016 07:22
-
-
Save smrq/11afa539cb7cd30402e1d358e513ad9d to your computer and use it in GitHub Desktop.
Minifier for hackmud
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
Usage: | |
* Start in your hackmud scripts folder. | |
* Make your folder structure look like this: | |
package.json | |
lib/compile.js | |
lib/watch.js | |
src/ | |
* Put source files in src/ | |
* npm install | |
(except wait, aren't the cool kids using Yarn now?) | |
* npm start | |
* Enjoy debugging minified files |
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
const babel = require('babel-core'); | |
module.exports = function compile(src, options = {}) { | |
let autos = /^function.*(\/\/.*)/.exec(src); | |
if (autos) { | |
autos = autos[1]; | |
} | |
src = src | |
.replace(/#s\.([\w\d_]+)\.([\w\d_]+)/g, 'SC$$$1$$$2') | |
.replace(/#db\.i/g, 'DB_insert') | |
.replace(/#db\.f/g, 'DB_find_cursor') | |
.replace(/#db\.u1/g, 'DB_update_one') | |
.replace(/#db\.u/g, 'DB_update') | |
.replace(/#db\.r/g, 'DB_remove') | |
.replace(/^function/, 'export default function'); | |
let { code } = babel.transform(src, { | |
minified: true, | |
shouldPrintComment: value => value.indexOf('@auto') >= 0, | |
plugins: [ | |
"babel-plugin-minify-constant-folding", | |
"babel-plugin-minify-dead-code-elimination", | |
"babel-plugin-minify-flip-comparisons", | |
"babel-plugin-minify-guarded-expressions", | |
"babel-plugin-minify-infinity", | |
"babel-plugin-minify-mangle-names", | |
"babel-plugin-minify-replace", | |
// "babel-plugin-minify-simplify", // waiting for release of https://github.com/babel/babili/pull/202 | |
"babel-plugin-minify-type-constructors", | |
"babel-plugin-transform-member-expression-literals", | |
"babel-plugin-transform-merge-sibling-variables", | |
"babel-plugin-transform-minify-booleans", | |
"babel-plugin-transform-property-literals", | |
"babel-plugin-transform-simplify-comparison-operators", | |
"babel-plugin-transform-undefined-to-void", | |
[replaceIdentifier, { | |
"Array": "ARRAY", | |
"Date": "DATE", | |
"Error": "ERROR", | |
"JSON": "JSON_", | |
"Object": "OBJECT", | |
"String": "STRING", | |
}] | |
] | |
}); | |
code = code | |
.replace(/SC\$([\w\d_]+)\$([\w\d_]+)/g, '#s.$1.$2') | |
.replace(/DB_insert/g, '#db.i') | |
.replace(/DB_find_cursor/g, '#db.f') | |
.replace(/DB_update_one/g, '#db.u1') | |
.replace(/DB_update/g, '#db.u') | |
.replace(/DB_remove/g, '#db.r') | |
.replace(/export default (function\s*\([^)]*\)\s*{)/, | |
autos ? '$1 ' + autos + '\n' : | |
'$1'); | |
return code; | |
} | |
function replaceIdentifier({ types: t }) { | |
return { | |
visitor: { | |
Identifier(path, state) { | |
const newId = state.opts[path.node.name] | |
if (newId) { | |
path.replaceWith(t.identifier(newId)) | |
} | |
} | |
} | |
}; | |
} |
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
{ | |
"name": "hackmud-scripts", | |
"version": "1.0.0", | |
"description": "", | |
"scripts": { | |
"start": "node lib/watch", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Greg Smith <[email protected]>", | |
"license": "MIT", | |
"devDependencies": { | |
"babel-core": "^6.18.0", | |
"babel-plugin-minify-constant-folding": "^0.0.1", | |
"babel-plugin-minify-dead-code-elimination": "^0.0.4", | |
"babel-plugin-minify-flip-comparisons": "^0.0.1", | |
"babel-plugin-minify-guarded-expressions": "^0.0.3", | |
"babel-plugin-minify-infinity": "^0.0.1", | |
"babel-plugin-minify-mangle-names": "^0.0.3", | |
"babel-plugin-minify-numeric-literals": "^0.0.1", | |
"babel-plugin-minify-replace": "^0.0.1", | |
"babel-plugin-minify-simplify": "^0.0.3", | |
"babel-plugin-minify-type-constructors": "^0.0.1", | |
"babel-plugin-transform-member-expression-literals": "^6.8.0", | |
"babel-plugin-transform-merge-sibling-variables": "^6.8.0", | |
"babel-plugin-transform-minify-booleans": "^6.8.0", | |
"babel-plugin-transform-property-literals": "^6.8.0", | |
"babel-plugin-transform-simplify-comparison-operators": "^6.8.0", | |
"babel-plugin-transform-undefined-to-void": "^6.8.0", | |
"chokidar": "^1.6.1", | |
"glob": "^7.1.1" | |
} | |
} |
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
const fs = require('fs'); | |
const path = require('path'); | |
const babel = require('babel-core'); | |
const chokidar = require('chokidar'); | |
const compile = require('./compile'); | |
chokidar.watch('src/*.js', { ignored: '**/*.test.js'}) | |
.on('add', compileFile) | |
.on('change', compileFile) | |
.on('unlink', removeCompiledFile); | |
function compileFile(file) { | |
console.log(`Compiling ${file}`); | |
let src = fs.readFileSync(file, 'utf-8'); | |
try { | |
let result = compile(src, {}); | |
fs.writeFileSync(path.basename(file), result); | |
console.log('Done.'); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
function removeCompiledFile(file) { | |
console.log(`Removing ${file}`); | |
try { | |
fs.unlinkSync(path.basename(file)); | |
console.log('Done.'); | |
} catch (err) { | |
console.error(err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment