Skip to content

Commit

Permalink
fix: work with browserify
Browse files Browse the repository at this point in the history
Browserify [does not support](browserify/resolve#224) `"exports"`
in a `package.json`, and nor can you use `"browser"` in `package.json`
[as a hint to Browserify to look in a certain place for a file](browserify/resolve#250 (comment)).

This means the output of `ipjs` is not compatible with Browserify since
it expects the runtime/bundler to use the `"exports"` field to look up
files paths.

If Browserify finds a file path, it will then use the `"browser"` values
to apply any overrides, which `ipjs` uses to direct it to the `/cjs` folder.

The problem is if it can't find the file in the first place it won't use
the `"browser"` map to get the overrides.

Handily we're generating the `"browser"` field values from the `"exports"`
values so we know we have the complete set of files that the user wants to
expose to the outside world, and the paths we want people to use to access
them.

The change in this PR is to use the `"browser"` field values to [mimc the `"exports"` structure in a CJS-compatible directory structure](browserify/resolve#250 (comment))
as per @ljharb's suggestion.

For any file that we are overriding with `"browser"` values, we create an
empty file (where a resolvable file does not already exist) a the path
Browserify expects it to be at, then it'll dutifully use the `"browser"`
field to pull the actual file in.
  • Loading branch information
achingbrain committed Jul 14, 2021
1 parent 729ffbf commit 0b076a5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/package/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { promises as fs } from 'fs'
import { promises as fs, existsSync } from 'fs'
import { rollup } from 'rollup'
import file from './file.js'
import testFile from './testFile.js'
import path from '../path-to-url.js'
import { fileURLToPath } from 'url'
import { join } from 'path'
import { join, dirname } from 'path'
import rmtree from '@tgrajewski/rmtree'
import preserveShebangs from 'rollup-plugin-preserve-shebangs'

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

async stubFiles (dist, files) {
await Promise.all(
files.map(async (file) => {
if (file === '.') {
file = 'index.js'
}
if (file.startsWith('./')) {
file = file.substring(2)
}
const dir = dirname(file)
if (dir !== '.') {
try {
await mkdir(new URL(dist + '/' + dir), {
recursive: true
})
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
}

if (existsSync(new URL(dist + '/' + file))) {
return
}

await writeFile(new URL(dist + '/' + file), '')
})
)
}

async deflate (dist) {
if (!(dist instanceof URL)) dist = path(dist)
rmtree(fileURLToPath(dist))
Expand Down Expand Up @@ -204,6 +235,7 @@ class Package {
json.exports = json.exports.import
json.browser = json.browser.import
}
await this.stubFiles(dist, Object.keys(json.browser))
let files = Promise.all(pending)
pending.push(writeFile(new URL(dist + '/package.json'), JSON.stringify(json, null, 2)))
const typeModule = '{ "type" : "module" }'
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 0b076a5

Please sign in to comment.