Skip to content

Commit f87a56f

Browse files
achingbrainvasco-santos
authored andcommitted
fix: work with jest (mikeal#14)
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 f87a56f

File tree

9 files changed

+32
-8
lines changed

9 files changed

+32
-8
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
"tempy": "^1.0.0"
3434
},
3535
"dependencies": {
36-
"acorn": "^8.0.5",
3736
"@tgrajewski/rmtree": "^1.0.1",
37+
"acorn": "^8.0.5",
38+
"escodegen": "^2.0.0",
3839
"esm-ast-to-cjs": "^0.0.2",
39-
"rollup-plugin-preserve-shebangs": "^0.2.0",
40+
"file-url": "^4.0.0",
4041
"rollup": "^2.38.1",
41-
"escodegen": "^2.0.0"
42+
"rollup-plugin-preserve-shebangs": "^0.2.0"
4243
}
4344
}

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 = {

src/path-to-url.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fileToUrl from 'file-url'
2+
13
// adapted from https://nodejs.org/api/path.html#path_path_resolve_paths
24
const CHAR_FORWARD_SLASH = '/'
35
const percentRegEx = /%/g
@@ -24,5 +26,8 @@ export default (filepath, cwd) => {
2426
if (resolved.includes('\r')) { resolved = resolved.replace(carriageReturnRegEx, '%0D') }
2527
if (resolved.includes('\t')) { resolved = resolved.replace(tabRegEx, '%09') }
2628
outURL.pathname = resolved
29+
30+
console.log('__resolved__', resolved)
31+
console.log('__what__', fileToUrl(resolved))
2732
return outURL
2833
}
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)