Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not generate files with esm type #543

Open
XiLaiTL opened this issue Feb 11, 2023 · 0 comments
Open

Could not generate files with esm type #543

XiLaiTL opened this issue Feb 11, 2023 · 0 comments

Comments

@XiLaiTL
Copy link

XiLaiTL commented Feb 11, 2023

#525

So I just use script to turn it to esm ( add .js to the import path as extension )

import { open,lstat, readFile, readdir, writeFile } from "node:fs/promises";
import { extname, join } from "node:path";


const regexpNames = /(from\s+)(["'])(?!.*\.js)(((\.?\.\/)|antlr4ts\/).*)(["'])/gm

async function addJsExt(path: string) {
    const files = await readdir(path, 'utf-8')
    for (const fileName of files) {
        const pathNew = join(path, fileName)
        const stat = await lstat(pathNew)
        if (stat.isDirectory()) {
            await addJsExt(pathNew)
        }
        else if (stat.isFile() && extname(fileName) == ".ts") {
            const codes = await readFile(pathNew, { encoding: 'utf-8' }).catch(err=>{throw err})
            await writeFile(pathNew, codes.replace(regexpNames,"$1$2$3.js$6"), {encoding: "utf8"}).catch(err=>{throw err})
        }
    }
}
const path = join(process.cwd(), process.argv[2])
await addJsExt(path)

export {}

usage:
"scripts": {
"convert-to-esm": " ts-node-esm ./src/lib/JSExtUtils.ts ./src/antlr"
}

@XiLaiTL XiLaiTL changed the title Could not generator files with esm type Could not generate files with esm type Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant