@esmbly/core
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

@esmbly/core

The core of Esmbly.

This package is used by @esmbly/cli to parse the input files into AST representations and to run each provided transformer. It can also be used to run Esmbly programmatically.

Installation

Note: If you want to run the CLI you should install @esmbly/cli instead.

# Using Yarn:
yarn add @esmbly/core

# Or, using NPM:
npm install @esmbly/core --save

Usage

const esmbly = require('@esmbly/core');
const Flow = require('@esmbly/transformer-flow');
const Wasm = require('@esmbly/transformer-wasm');

const compile = content => {
  return esmbly.run({
    input: [
      {
        content,
        dir: '/',
        name: 'input',
        type: '.js',
      },
    ],
    output: [{ format: 'WebAssembly' }],
    transformers: [
      Flow.createTransformer(),
      Wasm.createTransformer()
    ],
  });
};

const program = `
  // @flow

  export function add(a: number, b: number): number {
    return a + b;
  }
`;

compile(program)
  .then(([{ content }]) => WebAssembly.instantiate(content, {}))
  .then(({ instance }) => console.log('2 + 3 = ' + instance.exports.add(2, 3))) // => 2 + 3 = 5
  .catch(err => console.log(err));

Configuration

The @esmbly/core API is very minimal. It exposes a single function called run which accepts a config object (see the RunConfig interface) where the following properties must be provided.

Option Description Type
input An array of files that should be transformed File[]
transformers An array of transformer instances (created using the createTransformer() method, or any object/class that implements the Transformer interface). The transformers will run in the order that they are specified. Transformer[]
output An array of output objects. In other words - what kind of files do you want to output? Output[]
interface File {
  name: string;
  content: string | Buffer;
  dir: string;
  type: FileType;
}

interface Output {
  format: Format;
  outDir?: string;
  outFile?: string;
  rootDir?: string;
}

Contributing

All types of contributions are very much welcome. Check out our Contributing Guide for instructions on how to get started.

Package Sidebar

Install

npm i @esmbly/core

Weekly Downloads

2

Version

0.0.6

License

MIT

Unpacked Size

557 kB

Total Files

19

Last publish

Collaborators

  • ollelauribostrom