typescript#ModuleKind TypeScript Examples
The following examples show how to use
typescript#ModuleKind.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: transpile.ts From elephize with MIT License | 4 votes |
export function transpile(options: CliOptions, baseDir: string, outDir: string, log: LogObj) {
const namespaces = {
root: options.rootNs,
builtins: options.builtinsNs || (options.rootNs ? options.rootNs + '\\Builtins' : 'Builtins'),
};
const builtinsRelativePath = ModuleRegistry.namespaceToPath(namespaces.builtins);
let builtinsPath: string;
if (options.rewriteBuiltinsRoot) {
if (!options.builtinsNs) {
log.warn('builtinsNs option should be provided if rewriteBuiltinsRoot is used', []);
}
builtinsPath = path.join(options.rewriteBuiltinsRoot, builtinsRelativePath);
} else {
builtinsPath = path.resolve(__dirname, '..', '..', '..', 'builtins');
}
const serverFilesRoot = options.serverBaseDir ?? options.baseDir;
let hooks: NodeHooks = {};
if (options.hooksIncludePath) {
try {
let result = transpileModule(
fs.readFileSync(options.hooksIncludePath, { encoding: 'utf-8' }),
{ compilerOptions: { module: ModuleKind.CommonJS } }
);
// eslint-disable-next-line no-eval
const data: ElephizeNodeHookEntry[] = eval(result.outputText);
hooks = {
...(data.reduce<NodeHooks>((acc, entry) => {
acc[entry.nodeKind] = { run: entry.hook };
return acc;
}, {})),
};
} catch (e) {
log.error('Failed to load AST hooks: %s', [e.toString()]);
hooks = {};
}
}
glob(options.src, (e: Error, matches: string[]) => {
if (e) {
log.error('%s', [e.toString()]);
process.exit(1);
return;
}
const compilerOptions = {
baseUrl: baseDir,
paths: options.tsPaths || {},
};
(options.watch ? translateCodeAndWatch : translateCode)(
matches.map((p) => path.resolve('./', p)),
options.ignoreImports,
options.replaceImports,
options.tsPaths,
log,
{
baseDir,
serverFilesRoot,
builtinsPath,
aliases: options.aliases,
namespaces,
printImportTree: options.printImportTree,
encoding: options.encoding || 'utf-8',
disableCodeElimination: options.noZap,
options: compilerOptions,
onData: (sourceFilename: string, targetFilename: string, content: string) => onData(targetFilename, content),
onFinish,
jsxPreferences: options.jsxPreferences || {},
hooks,
}
);
});
function onData(filename: string, content: string) {
const outputFilename = outDir + '/' + filename;
log.info('Emitting file: %s', [outputFilename]);
const outputDir = path.dirname(outputFilename);
mkdirpSync(outputDir);
fs.writeFileSync(outputFilename, iconv.encode(content, options.encoding || 'utf-8'));
}
function onFinish() {
if ((log.errCount || 0) > 0 && options.bail === 'error') {
process.exit(1);
}
if ((log.errCount || 0) + (log.warnCount || 0) > 0 && options.bail === 'warn') {
process.exit(1);
}
if (options.rewriteBuiltinsRoot) {
log.special('Skip builtins copy bacause rewriteBuiltinsRoot options is provided', []);
return;
}
const bTgt = path.join(outDir, ModuleRegistry.namespaceToPath(namespaces.builtins));
log.special('Copying builtins files', []);
log.special('From: %s', [builtinsPath]);
log.special('To: %s', [bTgt]);
ncp(builtinsPath, bTgt, {
transform: function(read, write) {
read.pipe(replace(/__ROOTNS__\\Builtins/g, namespaces.builtins)).pipe(write);
},
}, (err) => {
if (!err) {
log.special('Builtins base files successfully copied', []);
}
});
}
}