I have a node script where I want to compile a ts file with a exported function to javascript that can be used on the web / embedded into a script tag.
const ts = require("typescript");
// hard coded for demonstration
const typescriptCode = `
export function add(a: number, b: number): number {
return a + b;
}`;
let result = ts.transpile(typescriptCode, {
compilerOptions: {
module: ts.ModuleKind.None,
target: ts.ScriptTarget.ES5
}
});
However even with ts.ModuleKind.None used in the compile options the program still outputs a script that attempts to bind the function to exports. Exports is not valid on the web. Value of result:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function add(a, b) {
return a + b;
}
exports.add = add;
How can I only transpile to this / make result be:
function add(a, b) {
return a + b;
}
exportfrom the function ? Since you don't want modules you should not use module features such as imports and exportstransformersproperty onTranspileOptions).typescriptCodevia regex replace before transpile.