ECMAScript Generator

Supported features:
Choose what you need. If strings concatenation is just enough - that's definitely the way to go.
See API Documentation for the details.
Simple Usage
import { esGenerate } from 'esgen';
const text = await esGenerate(code => {
code
.write(`function print(text) {`)
.indent(`console.log(text);`)
.write('}')
.write(`const greeting = 'Hello, World!';`)
.write(`print(greeting);`);
});
The following code will be emitted:
function print(text) {
console.log(text);
}
const greeting = 'Hello, World!';
print(greeting);
Symbols And Functions
Symbols used to avoid naming conflicts. If the same name requested for two different symbols, one of them will be
automatically renamed.
The example above can utilize symbols:
import { EsFunction, EsVarSymbol, esGenerate, esStringLiteral, esline } from 'esgen';
const print = new EsFunction(
'print',
{
text: {},
},
{
declare: {
at: 'bundle',
body: fn => code => {
code.write(
esline`console.log(${fn.args.text /* Refer declared argument symbol */});`,
);
},
},
},
);
const text = await esGenerate(code => {
const greeting = new EsVarSymbol('greeting');
code
.write(
greeting.declare({
value: () => esStringLiteral('Hello, World!'),
}),
)
.write(
esline`${print.call({
text: greeting /* Pass variable as argument. */,
})};`,
);
});
Symbol Exports And Code Evaluation
Symbols can be exported from the bundle. In this case it is possible to evaluate emitted code immediately and obtain
the exported symbols.
For example, to export the function print() from the example above, the following can be done:
import { EsFunction, esEvaluate, esline } from 'esgen';
const printFn = new EsFunction(
'print',
{
text: {},
},
{
declare: {
at: 'exports',
body: fn => code => {
code.write(
esline`console.log(${fn.args.text /* Refer declared argument symbol */});`,
);
},
},
},
);
const { print } = (await esEvaluate((_, { ns }) => {
ns.refer(printFn);
})) as { print: (text: string) => void };
print('Hello, World!');
Classes
Classes represented by EsClass instances.
Class may have a base class, constructor, and members.
import { EsClass, EsField, EsMemberVisibility, EsMethod, esEvaluate, esline } from 'esgen';
const printer = new EsClass('Printer', {
classConstructor: {
args: {
'initialText?': {
comment: 'Default text to print',
},
},
},
declare: {
at: 'exports',
},
});
const defaultText = new EsField('defaultText', {
visibility: EsMemberVisibility.Private,
}).declareIn(printer);
printer.declareConstructor({
args: {
initialText: {
declare: naming => esline`${naming} = 'Hello, World!'`,
},
},
body: ({
member: {
args: { initialText },
},
}) => esline`${defaultText.set('this', initialText)};`,
});
new EsMethod('print', {
args: { 'text?': { comment: 'Text to print' } },
}).declareIn(printer, {
args: {
text: {
declare: naming => esline`${naming} = ${defaultText.get('this')}`,
},
},
body: ({
member: {
args: { text },
},
}) => esline`console.log(${text});`,
});
const { Printer } = (await esEvaluate((_, { ns }) => {
ns.refer(printer);
})) as {
Printer: new (initialText?: string) => { print(text?: string): void };
};
const instance = new Printer();
instance.print();
instance.print('My text');