electrum-compiler
Advanced tools
Comparing version
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -55,6 +57,6 @@ | ||
key: 'build', | ||
value: function build(name, source) { | ||
value: function build(name, source, locals) { | ||
var Electrum = imports.Electrum; | ||
var React = imports.React; | ||
var components = this._items; | ||
var components = _extends({}, this._items, locals); | ||
var sourceVar = components.justToMakeSureThisDoesNotGetOptimizedAway || Electrum.justToMakeSureThisDoesNotGetOptimizedAway || React.justToMakeSureThisDoesNotGetOptimizedAway || ''; | ||
@@ -61,0 +63,0 @@ |
{ | ||
"name": "electrum-compiler", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "Run-time compiler for Electrum-enabled React components.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -17,2 +17,6 @@ # electrum-compiler | ||
To transform JavaScript to ES5, use: | ||
* `transform(input)` → produces ES5 output based on the JavaScript `source`. | ||
```javascript | ||
@@ -27,2 +31,11 @@ let input = 'const greet = x => `Hello ${x}.`;'; | ||
To build an Electrum-enabled React component, use: | ||
* `build(name, input)` → produces a component description; the name of the | ||
compoment will be `name` and its source code defined by `input`, which must | ||
be a source containing a class definition. | ||
* `build(name, input, locals)` → same as above, but inject the key/value | ||
pairs found in `locals` into the compilation context. | ||
```javascript | ||
@@ -29,0 +42,0 @@ let input = 'class extends React.Component { render() { return <div>Hi.</div>; } }'; |
@@ -75,3 +75,3 @@ 'use strict'; | ||
it ('', () => { | ||
it ('builds Electrum component from class, using external component <Button> and variable \'text\'', () => { | ||
const source = 'class extends React.Component { render() { return <Button>{text}</Button>; }}'; | ||
@@ -87,2 +87,15 @@ const compiler = new Compiler (); | ||
}); | ||
it ('builds Electrum component from class, overriding external component', () => { | ||
const source = 'class extends React.Component { render() { return <Button message="x"/>; }}'; | ||
const compiler = new Compiler (); | ||
compiler.register ('Button', Button); | ||
// Replace Button with <Bar> | ||
const Foo = compiler.build ('Foo', source, {Button: Bar}).component; | ||
const html = ReactDOMServer.renderToStaticMarkup (<Foo />); | ||
expect (html).to.equal ('<span>x</span>'); | ||
}); | ||
}); | ||
@@ -89,0 +102,0 @@ }); |
@@ -33,6 +33,6 @@ 'use strict'; | ||
build (name, source) { | ||
build (name, source, locals) { | ||
var Electrum = imports.Electrum; | ||
var React = imports.React; | ||
var components = this._items; | ||
var components = {...this._items, ...locals}; | ||
var sourceVar = | ||
@@ -39,0 +39,0 @@ components.justToMakeSureThisDoesNotGetOptimizedAway || |
29671
5.15%488
2.09%105
14.13%