build: some fixes + made @mtcute/test esm-only

This commit is contained in:
alina 🌸 2024-04-03 18:08:07 +03:00
parent ae8ab45a74
commit c5ce2c321d
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 47 additions and 23 deletions

View file

@ -66,7 +66,7 @@ export async function startTest(
let dcId = await client.getPrimaryDcId() let dcId = await client.getPrimaryDcId()
if (params.dcId) { if (params.dcId) {
if (!availableDcs.find((dc) => dc.id === params!.dcId)) { if (!availableDcs.find((dc) => dc.id === params.dcId)) {
throw new MtArgumentError(`DC ID is invalid (${dcId})`) throw new MtArgumentError(`DC ID is invalid (${dcId})`)
} }
dcId = params.dcId dcId = params.dcId
@ -85,7 +85,7 @@ export async function startTest(
code: () => code, code: () => code,
codeSentCallback: (sent) => { codeSentCallback: (sent) => {
for (let i = 0; i < sent.length; i++) { for (let i = 0; i < sent.length; i++) {
code += phone![5] code += phone[5]
} }
}, },
}) })

View file

@ -95,7 +95,7 @@ export function batchedQuery<T, U, K extends string | number>(params: {
} }
return new Promise<U | null>((resolve, reject) => { return new Promise<U | null>((resolve, reject) => {
arr!.push([item, resolve, reject]) arr.push([item, resolve, reject])
}) })
} }

View file

@ -0,0 +1 @@
module.exports = () => ({ buildCjs: false })

View file

@ -37,10 +37,7 @@
}, },
"distOnlyFields": { "distOnlyFields": {
"exports": { "exports": {
".": { ".": "./esm/index.js"
"import": "./esm/index.js",
"require": "./cjs/index.js"
}
} }
} }
} }

View file

@ -8,7 +8,8 @@ if (process.argv.length < 3) {
process.exit(0) process.exit(0)
} }
const packageDir = path.join(__dirname, '../packages', process.argv[2]) const packagesDir = path.join(__dirname, '../packages')
const packageDir = path.join(packagesDir, process.argv[2])
const outDir = path.join(packageDir, 'dist') const outDir = path.join(packageDir, 'dist')
function exec(cmd, params) { function exec(cmd, params) {
@ -178,30 +179,55 @@ if (buildConfig.buildTs) {
console.log('[i] Building typescript (CJS)...') console.log('[i] Building typescript (CJS)...')
const originalFiles = {} const originalFiles = {}
// todo - get rid of these, use @esm-replace-import instead for (const f of glob.sync(path.join(packagesDir, '**/*.ts'))) {
if (buildConfig.esmOnlyDirectives) { const content = fs.readFileSync(f, 'utf8')
for (const f of glob.sync(path.join(packageDir, '**/*.ts'))) { if (!content.includes('@only-if-esm')) continue
const content = fs.readFileSync(f, 'utf8') originalFiles[f] = content
if (!content.includes('@only-if-esm')) continue
originalFiles[f] = content
fs.writeFileSync(f, content.replace(/@only-if-esm.*?@\/only-if-esm/gs, '')) fs.writeFileSync(f, content.replace(/@only-if-esm.*?@\/only-if-esm/gs, ''))
}
} }
if (buildConfig.esmImportDirectives) { for (const f of glob.sync(path.join(packagesDir, '**/*.ts'))) {
for (const f of glob.sync(path.join(packageDir, '**/*.ts'))) { const content = fs.readFileSync(f, 'utf8')
const content = fs.readFileSync(f, 'utf8') if (!content.includes('@esm-replace-import')) continue
if (!content.includes('@esm-replace-import')) continue originalFiles[f] = content
originalFiles[f] = content
fs.writeFileSync(f, content.replace(/@esm-replace-import.*?await import/gs, 'require')) fs.writeFileSync(f, content.replace(/@esm-replace-import.*?await import/gs, 'require'))
}
// set type=commonjs in all package.json-s
for (const pkg of fs.readdirSync(packagesDir)) {
const pkgJson = path.join(packagesDir, pkg, 'package.json')
if (!fs.existsSync(pkgJson)) continue
const orig = fs.readFileSync(pkgJson, 'utf8')
originalFiles[pkgJson] = orig
fs.writeFileSync(pkgJson, JSON.stringify({
...JSON.parse(orig),
type: 'commonjs',
}, null, 2))
// maybe also dist/package.json
const distPkgJson = path.join(packagesDir, pkg, 'dist/package.json')
if (fs.existsSync(distPkgJson)) {
const orig = fs.readFileSync(distPkgJson, 'utf8')
originalFiles[distPkgJson] = orig
fs.writeFileSync(distPkgJson, JSON.stringify({
...JSON.parse(orig),
type: 'commonjs',
}, null, 2))
} }
} }
let error = false let error = false
try { try {
exec('pnpm exec tsc --module commonjs --outDir dist/cjs', { cwd: packageDir, stdio: 'inherit' }) exec('pnpm exec tsc --outDir dist/cjs', {
cwd: packageDir,
stdio: 'inherit',
})
} catch (e) { } catch (e) {
error = e error = e
} }