Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit c61f314

Browse files
authored
fix: support cjs stubbing (#220)
1 parent 638fe75 commit c61f314

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/core/package/index.spec.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,33 @@ describe('package class', () => {
5959
// TODO: move to fixture
6060
test('should generate package stub', async () => {
6161
const defaultPath = getFixturePath('default')
62-
const files = [
63-
resolve(defaultPath, 'dist/index.js'),
64-
resolve(defaultPath, 'dist/index.es.js'),
65-
resolve(defaultPath, 'dist/index.d.ts'),
66-
]
67-
for (const file of files) {
62+
const files = {
63+
[resolve(defaultPath, 'dist/index.js')]: [
64+
`const jiti = require('jiti')()`,
65+
`module.exports = jiti('./../src/index')`,
66+
].join('\n'),
67+
[resolve(
68+
defaultPath,
69+
'dist/index.es.js'
70+
)]: `export * from './../src/index'`,
71+
[resolve(
72+
defaultPath,
73+
'dist/index.d.ts'
74+
)]: `export * from './../src/index'`,
75+
}
76+
for (const file in files) {
6877
await remove(file)
6978
expect(existsSync(file)).toBeFalsy()
7079
}
7180

7281
await core.createStubs()
7382

74-
for (const file of files) {
83+
for (const file in files) {
7584
expect(
7685
readFileSync(file)
7786
.toString()
78-
.replace(/from '.*\/fixture/, "from '/fixture")
79-
).toBe(`export * from './../src/index'`)
87+
.replace(/'.*\/fixture/, "'/fixture")
88+
).toBe(files[file])
8089
}
8190
})
8291

src/core/package/index.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -342,29 +342,40 @@ export class Package {
342342
const absPath = entrypoint.replace(/(\.[jt]s)$/, '')
343343
await writeFile(
344344
binary,
345-
`#!/usr/bin/env node\nconst jiti = require('jiti')()\nmodule.exports = jiti('${absPath}')`
345+
[
346+
`#!/usr/bin/env node`,
347+
`const jiti = require('jiti')()`,
348+
`module.exports = jiti('${absPath}')`,
349+
].join('\n')
346350
)
347351
await this.setBinaryPermissions()
348352
})
349353
}
350354

351-
async createStub(path: string | undefined) {
355+
async createStub(path: string | undefined, cjs = false) {
352356
if (!path || !this.entrypoint || !this.options.build) return
353357

354358
const outFile = this.resolvePath(path)
355359
const outDir = dirname(outFile)
356360
if (!existsSync(outDir)) await mkdirp(outDir)
357361
const relativeEntrypoint = relative(outDir, this.entrypoint).replace(
358-
/(\.[jt]s)$/,
362+
/(\.[cm]?[jt]s)$/,
359363
''
360364
)
361-
await writeFile(outFile, `export * from './${relativeEntrypoint}'`)
365+
const stub = cjs
366+
? [
367+
`const jiti = require('jiti')()`,
368+
`module.exports = jiti('./${relativeEntrypoint}')`,
369+
].join('\n')
370+
: `export * from './${relativeEntrypoint}'`
371+
372+
await writeFile(outFile, stub)
362373
}
363374

364375
async createStubs() {
365376
return Promise.all([
366377
this.createBinaryStubs(),
367-
this.createStub(this.pkg.main),
378+
this.createStub(this.pkg.main, true),
368379
this.createStub(this.pkg.module),
369380
this.createStub(this.pkg.types),
370381
])

0 commit comments

Comments
 (0)