Skip to content

Instantly share code, notes, and snippets.

@tdresser
Forked from developit/*transpiler-lite.md
Last active April 26, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdresser/57f12d4cc0d8bdba9267f070d97133e9 to your computer and use it in GitHub Desktop.
Save tdresser/57f12d4cc0d8bdba9267f070d97133e9 to your computer and use it in GitHub Desktop.
Fix alphabetization
/node_modules
/*.log
/package-lock.json
/*.map
.DS_Store

transpiler-lite

So, you need to transpile classes to ES5 for some reason, but you need to do it in the browser.

Could we use a gross regular expression to get that done? Yes.

Should we? No, but it's far too late for that now.

Supported Features

  • Classes definitions (no inheritance)
  • Constructors (allows return)
  • Static methods
  • Getters / setters

Usage

import transpile from 'transpile-lite';
let es5Code = transpile(`
  class Foo {
    constructor(a, b) {
      console.log(a, b)
    }
    static get properties() { return [1,2,3] }
    render() {
      return 'stuff'
    }
  }
`)

Roadmap

  • Improve comment handling
  • Support getter/setter pairs (via memoized descriptor?)

License

Apache 2.0.

Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
{
"name": "transpiler-lite",
"version": "2.0.2",
"description": "Transpile classes to ES5 in the worst way possible.",
"module": "transpiler-lite.js",
"main": "transpiler-lite.umd.js",
"cjs:main": "transpiler-lite.min.js",
"umd:main": "transpiler-lite.umd.js",
"scripts": {
"build": "microbundle transpiler-lite.js -f cjs,umd && sed -i '' 's/module\\.exports\\s*=\\s*function/function transpilerLite/' transpiler-lite.min.js && sed -i '' -e '/^\\/\\/#.*/d' transpiler-lite.min.js && sed -i '' -e '/^\\/\\/#.*/d' transpiler-lite.umd.js"
},
"eslintConfig": {
"extends": "eslint-config-developit"
},
"keywords": [
"transpiler",
"scary regex"
],
"author": "Jason Miller <jason@developit.ca>",
"license": "apache-2.0",
"devDependencies": {
"eslint": "^4.17.0",
"eslint-config-developit": "^1.1.1",
"microbundle": "^0.4.3",
"strip-json-comments-cli": "^1.0.1"
}
}
export default function transpileLite(code, C) {
return (C = 0), code.replace(/(\/\*[\s\S]*?\*\/|\/\/.*)/g, '').replace(
/(^\s*|\n\s*)(static\s+)?((?:async\s+)?)((?:[sg]et\s+)?)((?:\*\s*)?)([a-zA-Z0-9$_]+)\(([^()]*?)\)\s*\{/g,
(s, out, isStatic, async, desc, gen, name, args) => {
let id = '', ctx = isStatic ? 'this' : 'this.prototype';
if (desc) out += `Object.defineProperty(${ctx},'${name}',{${desc}:${id=(' $t'+C++)}});`;
else out += `${ctx}.${name}=`;
return out + `${async}function${gen}${id}(${args}){`;
}).replace(/class\s+([a-zA-Z0-9$_]+)?\s*\{/g, (s, n) =>
`$t${C}.call(${n||'c'});function ${n||'c'}(){var x=${n||'c'}.prototype.constructor;if(x&&x!=${n||'c'})return x.apply(this,arguments)}function $t${C++}(){`
).replace(/(^|\s|;)(?:let|const)(\s)/gm, '$1var$2');
}
function transpilerLite(t,s){return s=0,t.replace(/(\/\*[\s\S]*?\*\/|\/\/.*)/g,"").replace(/(^\s*|\n\s*)(static\s+)?((?:async\s+)?)((?:[sg]et\s+)?)((?:\*\s*)?)([a-zA-Z0-9$_]+)\(([^()]*?)\)\s*\{/g,function(t,e,r,c,n,o,a,u){var i="",p=r?"this":"this.prototype";return(e+=n?"Object.defineProperty("+p+",'"+a+"',{"+n+":"+(i=" $t"+s++)+"});":p+"."+a+"=")+c+"function"+o+i+"("+u+"){"}).replace(/class\s+([a-zA-Z0-9$_]+)?\s*\{/g,function(t,e){return"$t"+s+".call("+(e||"c")+");function "+(e||"c")+"(){var x="+(e||"c")+".prototype.constructor;if(x&&x!="+(e||"c")+")return x.apply(this,arguments)}function $t"+s+++"(){"}).replace(/(^|\s|;)(?:let|const)(\s)/gm,"$1var$2")};
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.transpilerLite=e()}(this,function(){return function(t,e){return e=0,t.replace(/(\/\*[\s\S]*?\*\/|\/\/.*)/g,"").replace(/(^\s*|\n\s*)(static\s+)?((?:async\s+)?)((?:[sg]et\s+)?)((?:\*\s*)?)([a-zA-Z0-9$_]+)\(([^()]*?)\)\s*\{/g,function(t,n,r,s,c,o,i,u){var f="",a=r?"this":"this.prototype";return(n+=c?"Object.defineProperty("+a+",'"+i+"',{"+c+":"+(f=" $t"+e++)+"});":a+"."+i+"=")+s+"function"+o+f+"("+u+"){"}).replace(/class\s+([a-zA-Z0-9$_]+)?\s*\{/g,function(t,n){return"$t"+e+".call("+(n||"c")+");function "+(n||"c")+"(){var x="+(n||"c")+".prototype.constructor;if(x&&x!="+(n||"c")+")return x.apply(this,arguments)}function $t"+e+++"(){"}).replace(/(^|\s|;)(?:let|const)(\s)/gm,"$1var$2")}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment