Files
rocket/scripts/update-esm-entrypoints.mjs
2021-01-03 16:20:47 +01:00

27 lines
980 B
JavaScript

import fs from 'fs';
import path from 'path';
import { createRequire } from 'module';
import { packages } from '../workspace-packages.mjs';
const require = createRequire(import.meta.url);
for (const pkg of packages) {
if (pkg.type === 'ts' && pkg.environment === 'node') {
const pkgPath = path.join(process.cwd(), 'packages', pkg.name);
const cjsModule = require(path.join(pkgPath, 'dist', 'index.js'));
const namedExports = Object.keys(cjsModule)
.filter(name => name !== 'default' && !name.startsWith('_'))
.join(', ');
const esmEntrypoint = `// this file is autogenerated with the update-esm-entrypoints script
import cjsEntrypoint from './dist/index.js';
const { ${namedExports} } = cjsEntrypoint;
export { ${namedExports} }`;
const esmEntrypointDts = "export * from './dist/index.js'";
fs.writeFileSync(path.join(pkgPath, 'index.mjs'), esmEntrypoint);
fs.writeFileSync(path.join(pkgPath, 'index.d.ts'), esmEntrypointDts);
}
}