Skip to content

Commit

Permalink
fix: patch cjs vars for esm scope
Browse files Browse the repository at this point in the history
fix #119, fix #122
  • Loading branch information
qmhc committed Sep 30, 2022
1 parent 05335e3 commit 57e093d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scripts/build.ts
Expand Up @@ -13,6 +13,11 @@ async function main() {
{ stdio: 'inherit' }
)

await patchCommomJs()
await patchESModule()
}

async function patchCommomJs() {
const indexPath = path.resolve(root, '../dist/index.cjs')

let indexCodes = await fs.readFile(indexPath, 'utf-8')
Expand All @@ -31,6 +36,24 @@ async function main() {
}
}

const cjsBridge = `
import __cjs_url__ from 'url';
import __cjs_path__ from 'path';
import __cjs_mod__ from 'module';
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
const __dirname = __cjs_path__.dirname(__filename);
const require = __cjs_mod__.createRequire(import.meta.url);
`

async function patchESModule() {
const indexPath = path.resolve(root, '../dist/index.mjs')

let indexCodes = await fs.readFile(indexPath, 'utf-8')
indexCodes = cjsBridge + indexCodes

await fs.writeFile(indexPath, indexCodes)
}

main().catch(error => {
console.error(error)
process.exit(1)
Expand Down

0 comments on commit 57e093d

Please sign in to comment.