Skip to content

Commit

Permalink
fix: work with jest
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain committed Jul 15, 2021
1 parent c101763 commit 5915ad1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import file from './file.js'
import testFile from './testFile.js'
import path from '../path-to-url.js'
import { fileURLToPath } from 'url'
import { join, dirname } from 'path'
import { join, dirname, resolve, relative } from 'path'
import rmtree from '@tgrajewski/rmtree'
import preserveShebangs from 'rollup-plugin-preserve-shebangs'

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

async stubFiles (dist, files) {
async stubFiles (dist, overrides) {
await Promise.all(
files.map(async (file) => {
Object.keys(overrides).map(async (file) => {
const target = overrides[file]

if (file === '.') {
file = 'index.js'
}
Expand All @@ -189,7 +191,17 @@ class Package {
return
}

await writeFile(new URL(dist + '/' + file), '')
const distPath = fileURLToPath(dist)
const stubUrl = new URL(dist + '/' + file)
const stubPath = fileURLToPath(stubUrl)
const targetPath = resolve(join(distPath, target))
let relativePath = relative(dirname(stubPath), targetPath)

if (!relativePath.startsWith('./')) {
relativePath = `./${relativePath}`
}

await writeFile(stubUrl, `module.exports = require('${relativePath}')\n`)
})
)
}
Expand Down Expand Up @@ -241,7 +253,7 @@ class Package {
json.exports = json.exports.import
json.browser = json.browser.import
}
await this.stubFiles(dist, Object.keys(json.browser))
await this.stubFiles(dist, json.browser)
let files = Promise.all(pending)
pending.push(writeFile(new URL(dist + '/package.json'), JSON.stringify(json, null, 2)))
const typeModule = {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./cjs/src/browser.js')
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-main/secondary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./cjs/src/secondary.js')
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-notests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./cjs/src/browser.js')
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-notests/secondary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./cjs/src/secondary.js')
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./cjs/src/browser.js')
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-tests/secondary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./cjs/src/secondary.js')

0 comments on commit 5915ad1

Please sign in to comment.