Skip to content

Commit 5915ad1

Browse files
committed
fix: work with jest
Jest [doesn't support package exports](jestjs/jest#9771), nor does it support `browser` overrides out of the box (though it [can be configured](https://jestjs.io/docs/configuration#resolver-string)). This means it parses the stubbed files introduced in mikeal#13 as javascript, so let's just require and export the file that the stub is stubbing. This has the added bonus of also supporting old nodes that don't understand package exports. Fixes achingbrain/uint8arrays#21
1 parent c101763 commit 5915ad1

File tree

7 files changed

+23
-5
lines changed

7 files changed

+23
-5
lines changed

src/package/index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import file from './file.js'
44
import testFile from './testFile.js'
55
import path from '../path-to-url.js'
66
import { fileURLToPath } from 'url'
7-
import { join, dirname } from 'path'
7+
import { join, dirname, resolve, relative } from 'path'
88
import rmtree from '@tgrajewski/rmtree'
99
import preserveShebangs from 'rollup-plugin-preserve-shebangs'
1010

@@ -163,9 +163,11 @@ class Package {
163163
await unlink(new URL(dist + '/cjs/_ipjsInput.js'))
164164
}
165165

166-
async stubFiles (dist, files) {
166+
async stubFiles (dist, overrides) {
167167
await Promise.all(
168-
files.map(async (file) => {
168+
Object.keys(overrides).map(async (file) => {
169+
const target = overrides[file]
170+
169171
if (file === '.') {
170172
file = 'index.js'
171173
}
@@ -189,7 +191,17 @@ class Package {
189191
return
190192
}
191193

192-
await writeFile(new URL(dist + '/' + file), '')
194+
const distPath = fileURLToPath(dist)
195+
const stubUrl = new URL(dist + '/' + file)
196+
const stubPath = fileURLToPath(stubUrl)
197+
const targetPath = resolve(join(distPath, target))
198+
let relativePath = relative(dirname(stubPath), targetPath)
199+
200+
if (!relativePath.startsWith('./')) {
201+
relativePath = `./${relativePath}`
202+
}
203+
204+
await writeFile(stubUrl, `module.exports = require('${relativePath}')\n`)
193205
})
194206
)
195207
}
@@ -241,7 +253,7 @@ class Package {
241253
json.exports = json.exports.import
242254
json.browser = json.browser.import
243255
}
244-
await this.stubFiles(dist, Object.keys(json.browser))
256+
await this.stubFiles(dist, json.browser)
245257
let files = Promise.all(pending)
246258
pending.push(writeFile(new URL(dist + '/package.json'), JSON.stringify(json, null, 2)))
247259
const typeModule = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./cjs/src/browser.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./cjs/src/secondary.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./cjs/src/browser.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./cjs/src/secondary.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./cjs/src/browser.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./cjs/src/secondary.js')

0 commit comments

Comments
 (0)