From 5915ad10c14888382a33114c5db226b6d81db510 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 15 Jul 2021 14:21:59 +0100 Subject: [PATCH] fix: work with jest Jest [doesn't support package exports](https://github.com/facebook/jest/issues/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 #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 --- src/package/index.js | 22 ++++++++++++++----- .../pkg-kitchensink/output-main/index.js | 1 + .../pkg-kitchensink/output-main/secondary | 1 + .../pkg-kitchensink/output-notests/index.js | 1 + .../pkg-kitchensink/output-notests/secondary | 1 + .../pkg-kitchensink/output-tests/index.js | 1 + .../pkg-kitchensink/output-tests/secondary | 1 + 7 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/package/index.js b/src/package/index.js index 48bf3e8..87ee4a2 100644 --- a/src/package/index.js +++ b/src/package/index.js @@ -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' @@ -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' } @@ -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`) }) ) } @@ -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 = { diff --git a/test/fixtures/pkg-kitchensink/output-main/index.js b/test/fixtures/pkg-kitchensink/output-main/index.js index e69de29..b3160ff 100644 --- a/test/fixtures/pkg-kitchensink/output-main/index.js +++ b/test/fixtures/pkg-kitchensink/output-main/index.js @@ -0,0 +1 @@ +module.exports = require('./cjs/src/browser.js') diff --git a/test/fixtures/pkg-kitchensink/output-main/secondary b/test/fixtures/pkg-kitchensink/output-main/secondary index e69de29..6ba4fc0 100644 --- a/test/fixtures/pkg-kitchensink/output-main/secondary +++ b/test/fixtures/pkg-kitchensink/output-main/secondary @@ -0,0 +1 @@ +module.exports = require('./cjs/src/secondary.js') diff --git a/test/fixtures/pkg-kitchensink/output-notests/index.js b/test/fixtures/pkg-kitchensink/output-notests/index.js index e69de29..b3160ff 100644 --- a/test/fixtures/pkg-kitchensink/output-notests/index.js +++ b/test/fixtures/pkg-kitchensink/output-notests/index.js @@ -0,0 +1 @@ +module.exports = require('./cjs/src/browser.js') diff --git a/test/fixtures/pkg-kitchensink/output-notests/secondary b/test/fixtures/pkg-kitchensink/output-notests/secondary index e69de29..6ba4fc0 100644 --- a/test/fixtures/pkg-kitchensink/output-notests/secondary +++ b/test/fixtures/pkg-kitchensink/output-notests/secondary @@ -0,0 +1 @@ +module.exports = require('./cjs/src/secondary.js') diff --git a/test/fixtures/pkg-kitchensink/output-tests/index.js b/test/fixtures/pkg-kitchensink/output-tests/index.js index e69de29..b3160ff 100644 --- a/test/fixtures/pkg-kitchensink/output-tests/index.js +++ b/test/fixtures/pkg-kitchensink/output-tests/index.js @@ -0,0 +1 @@ +module.exports = require('./cjs/src/browser.js') diff --git a/test/fixtures/pkg-kitchensink/output-tests/secondary b/test/fixtures/pkg-kitchensink/output-tests/secondary index e69de29..6ba4fc0 100644 --- a/test/fixtures/pkg-kitchensink/output-tests/secondary +++ b/test/fixtures/pkg-kitchensink/output-tests/secondary @@ -0,0 +1 @@ +module.exports = require('./cjs/src/secondary.js')