electrum-compiler
raw JSON → 1.9.0 verified Fri May 01 auth: no javascript
Run-time compiler for Electrum-enabled React components, based on babel-standalone. Version 1.9.0 is the latest stable release; the package has had a low release cadence with infrequent updates. It provides a Compiler class capable of transforming JavaScript to ES5 and building React components from source code at runtime. Key differentiators include integration with the Electrum framework, support for registering external symbols, and a built-in eval for ES6/JSX. Note that it compiles synchronously in the browser and depends on Electrum v4.3.0 as a peer dependency.
Common errors
error TypeError: Compiler is not a constructor ↓
cause Incorrect import: default import instead of named import.
fix
Use import { Compiler } from 'electrum-compiler'
error Error: Cannot find module 'electrum' ↓
cause Peer dependency electrum is not installed.
fix
Run npm install electrum@^4.3.0
Warnings
gotcha Error positions in build() are offset by one line due to prepended code. ↓
fix When interpreting error line numbers, subtract 1 from the reported line.
gotcha The compiler relies on a bundled babel-standalone; large bundle size may impact browser performance. ↓
fix Consider lazy-loading the compiler or using a server-side compilation approach for production.
gotcha The register() method stores items in a global-like catalog; multiple components may conflict if keys overlap. ↓
fix Use unique keys when registering symbols, or create separate Compiler instances for isolation.
Install
npm install electrum-compiler yarn add electrum-compiler pnpm add electrum-compiler Imports
- Compiler wrong
import Compiler from 'electrum-compiler'correctimport { Compiler } from 'electrum-compiler' - Compiler wrong
const Compiler = require('electrum-compiler')correctconst { Compiler } = require('electrum-compiler') - Compiler wrong
import { compile } from 'electrum-compiler'correctimport { Compiler } from 'electrum-compiler'
Quickstart
import { Compiler } from 'electrum-compiler';
const compiler = new Compiler();
// Transform ES6 to ES5
const es5Source = compiler.transform('const greet = x => `Hello ${x}.`;');
console.log(es5Source);
// Build a React component with Electrum support
const componentSource = `
class extends React.Component {
render() {
return <Button>{text}</Button>;
}
}`;
compiler.register('Button', Button); // Assume Button is a React component
compiler.register('text', 'Hello');
const output = compiler.build('MyComponent', componentSource);
if (output.error) {
console.error(output.error);
} else {
console.log('Component:', output.component);
}
// Evaluate an expression
const result = compiler.eval('2 + 3');
console.log(result); // 5