Skip to content

Commit

Permalink
fix: leverage pkg.exports
Browse files Browse the repository at this point in the history
First of all: the `pkg.exports` spec defines that object key order is
being used when resolving for the targeted environment, see
uuidjs/uuid#462 (comment)

In order for `webpack@5` to pick up the `browser` overrides they must
come _before_ `import` and `require`, see
https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62 for
preliminary documentation.

To make full use of `pkg.exports` in node as well we can use package
self reference instead of local paths, see the discussion in
jkrems/proposal-pkg-exports#47

Unfortunately the browser testing tool polendina doesn't play nice with
self reference since it only looks for bare module specifiers in
`node_modules` and `node_modules/polendina/node_modules`, see
https://github.com/rvagg/polendina/blob/master/lib/webpack.config.js#L28-L39

We would need a way to set up an `resolve.alias` for webpack in
polendina to make package self reference work.
  • Loading branch information
ctavan committed Jun 24, 2020
1 parent cb71597 commit d878a3a
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion bases/_base64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { coerce } from '../bytes.js'
import { coerce } from 'multiformats/bytes.js'
const encode = o => Buffer.from(o).toString('base64')
const decode = s => coerce(Buffer.from(s, 'base64'))
const __browser = false
Expand Down
2 changes: 1 addition & 1 deletion bases/base58.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import baseX from 'base-x'
import { coerce } from '../bytes.js'
import { coerce } from 'multiformats/bytes.js'
import { Buffer } from 'buffer'

const wrap = obj => ({
Expand Down
2 changes: 1 addition & 1 deletion bases/base64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as b64 from './_base64.js'
import * as b64 from 'multiformats/bases/_base64.js'

const create = alphabet => {
// The alphabet is only used to know:
Expand Down
12 changes: 6 additions & 6 deletions basics.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { create } from './index.js'
import raw from './codecs/raw.js'
import json from './codecs/json.js'
import base32 from './bases/base32.js'
import base64 from './bases/base64.js'
import sha2 from './hashes/sha2.js'
import { create } from 'multiformats/index.js'
import raw from 'multiformats/codecs/raw.js'
import json from 'multiformats/codecs/json.js'
import base32 from 'multiformats/bases/base32.js'
import base64 from 'multiformats/bases/base64.js'
import sha2 from 'multiformats/hashes/sha2.js'

const multiformats = create()
multiformats.multihash.add(sha2)
Expand Down
2 changes: 1 addition & 1 deletion cid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as bytes from './bytes.js'
import * as bytes from 'multiformats/bytes.js'
import withIs from 'class-is'

const readonly = (object, key, value) => {
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import varints from 'varint'
import createCID from './cid.js'
import * as bytes from './bytes.js'
import createCID from 'multiformats/cid.js'
import * as bytes from 'multiformats/bytes.js'

const cache = new Map()
const varint = {
Expand Down
2 changes: 1 addition & 1 deletion legacy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CID from 'cids'
import * as bytes from './bytes.js'
import * as bytes from 'multiformats/bytes.js'
import { Buffer } from 'buffer'

const legacy = (multiformats, name) => {
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"import": "./index.js",
"require": "./dist/index.cjs"
},
"./index.js": {
"import": "./index.js",
"require": "./dist/index.cjs"
},
"./basics.js": {
"import": "./basics.js",
"require": "./dist/basics.cjs"
Expand All @@ -43,9 +47,9 @@
"require": "./dist/legacy.cjs"
},
"./bases/_base64.js": {
"browser": "./bases/_base64-browser.js",
"import": "./bases/_base64.js",
"require": "./dist/bases/_base64.cjs",
"browser": "./bases/_base64-browser.js"
"require": "./dist/bases/_base64.cjs"
},
"./bases/base16.js": {
"import": "./bases/base16.js",
Expand All @@ -63,21 +67,17 @@
"import": "./bases/base64.js",
"require": "./dist/bases/base64.cjs"
},
"./hashes/sha2-browser.js": {
"import": "./hashes/sha2-browser.js",
"require": "./dist/hashes/sha2-browser.cjs"
},
"./hashes/sha2.js": {
"browser": "./hashes/sha2-browser.js",
"import": "./hashes/sha2.js",
"require": "./dist/hashes/sha2.cjs",
"browser": "./hashes/sha2-browser.js"
"require": "./dist/hashes/sha2.cjs"
},
"./codecs/json.js": {
"import": "./codecs/json.js.js",
"import": "./codecs/json.js",
"require": "./dist/codecs/json.cjs"
},
"./codecs/raw.js": {
"import": "./codecs/raw.js.js",
"import": "./codecs/raw.js",
"require": "./dist/codecs/raw.cjs"
}
},
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'

let configs = []

const _filter = p => !p.includes('/_') && !p.includes('rollup.config')
const _filter = p => !p.includes('rollup.config')

const relativeToMain = name => ({
name: 'relative-to-main',
Expand Down
2 changes: 1 addition & 1 deletion test/test-bytes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* globals describe, it */
import * as bytes from '../bytes.js'
import * as bytes from 'multiformats/bytes.js'
import { deepStrictEqual } from 'assert'
const test = it
const same = deepStrictEqual
Expand Down
10 changes: 5 additions & 5 deletions test/test-cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import crypto from 'crypto'
import OLDCID from 'cids'
import assert from 'assert'
import { toHex } from '../bytes.js'
import multiformats from '../basics.js'
import base58 from '../bases/base58.js'
import base32 from '../bases/base32.js'
import base64 from '../bases/base64.js'
import { toHex } from 'multiformats/bytes.js'
import multiformats from 'multiformats/basics.js'
import base58 from 'multiformats/bases/base58.js'
import base32 from 'multiformats/bases/base32.js'
import base64 from 'multiformats/bases/base64.js'
import util from 'util'
const test = it
const same = assert.deepStrictEqual
Expand Down
2 changes: 1 addition & 1 deletion test/test-errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* globals describe, it */
import assert from 'assert'
import { create } from '../index.js'
import { create } from 'multiformats/index.js'
const multiformat = create()
const test = it

Expand Down
4 changes: 2 additions & 2 deletions test/test-legacy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals before, describe, it */
import { Buffer } from 'buffer'
import assert from 'assert'
import multiformats from '../basics.js'
import legacy from '../legacy.js'
import multiformats from 'multiformats/basics.js'
import legacy from 'multiformats/legacy.js'
const same = assert.deepStrictEqual
const test = it

Expand Down
16 changes: 8 additions & 8 deletions test/test-multibase.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* globals describe, it */
import * as bytes from '../bytes.js'
import * as bytes from 'multiformats/bytes.js'
import assert from 'assert'
import { create as multiformat } from '../index.js'
import base16 from '../bases/base16.js'
import base32 from '../bases/base32.js'
import base58 from '../bases/base58.js'
import base64 from '../bases/base64.js'
import basics from '../basics.js'
import { __browser } from '../bases/_base64.js'
import { create as multiformat } from 'multiformats/index.js'
import base16 from 'multiformats/bases/base16.js'
import base32 from 'multiformats/bases/base32.js'
import base58 from 'multiformats/bases/base58.js'
import base64 from 'multiformats/bases/base64.js'
import basics from 'multiformats/basics.js'
import { __browser } from 'multiformats/bases/_base64.js'
const basicsMultibase = basics.multibase
const same = assert.deepStrictEqual
const test = it
Expand Down
4 changes: 2 additions & 2 deletions test/test-multicodec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* globals describe, it */
import * as bytes from '../bytes.js'
import * as bytes from 'multiformats/bytes.js'
import assert from 'assert'
import multiformats from '../basics.js'
import multiformats from 'multiformats/basics.js'
const same = assert.deepStrictEqual
const test = it

Expand Down
6 changes: 3 additions & 3 deletions test/test-multihash.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* globals describe, it */
import * as bytes from '../bytes.js'
import * as bytes from 'multiformats/bytes.js'
import assert from 'assert'
import { create as multiformat } from '../index.js'
import { create as multiformat } from 'multiformats/index.js'
import intTable from 'multicodec/src/int-table.js'
import valid from './fixtures/valid-multihash.js'
import invalid from './fixtures/invalid-multihash.js'
import crypto from 'crypto'
import sha2 from '../hashes/sha2.js'
import sha2 from 'multiformats/hashes/sha2.js'
const same = assert.deepStrictEqual
const test = it
const encode = name => data => bytes.coerce(crypto.createHash(name).update(data).digest())
Expand Down

0 comments on commit d878a3a

Please sign in to comment.