diff --git a/readme.md b/readme.md index ce762f1..9f68321 100644 --- a/readme.md +++ b/readme.md @@ -340,6 +340,15 @@ resolve.exports(pkg, '.', { //=> ["./$worker.js"] ``` +If you want to remove a condition, you can use the `!` prefix. For example, you only need `"types"`: + +```js +resolve.exports(pkg, '.', { + conditions: ['!default', '!import', '!node', 'types'] +}); +// Conditions: ["types"] +``` + #### options.unsafe Type: `boolean`
Default: `false` diff --git a/src/utils.ts b/src/utils.ts index ab74868..b2009d2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -13,9 +13,16 @@ export function throws(name: string, entry: Entry, condition?: number): never { } export function conditions(options: t.Options): Set { - let out = new Set([ 'default', ...options.conditions || [] ]); + let out = new Set(['default']); options.unsafe || out.add(options.require ? 'require' : 'import'); options.unsafe || out.add(options.browser ? 'browser' : 'node'); + for (const condition of options.conditions || []) { + if (condition.startsWith('!')) { + out.delete(condition.slice(1)); + } else { + out.add(condition); + } + } return out; } diff --git a/test/utils.ts b/test/utils.ts index 72f356d..784dc5f 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -77,12 +77,12 @@ describe('$.conditions', it => { it('option.conditions', () => { let output = run({ conditions: ['foo', 'bar'] }); - assert.equal(output, ['default', 'foo', 'bar', 'import', 'node']); + assert.equal(output, ['default', 'import', 'node', 'foo', 'bar']); }); it('option.conditions :: order', () => { let output = run({ conditions: ['node', 'import', 'foobar'] }); - assert.equal(output, ['default', 'node', 'import', 'foobar']); + assert.equal(output, ['default', 'import', 'node', 'foobar']); }); it('option.conditions :: unsafe', () => { @@ -92,22 +92,47 @@ describe('$.conditions', it => { it('option.conditions :: browser', () => { let output = run({ browser: true, conditions: ['foo', 'bar'] }); - assert.equal(output, ['default', 'foo', 'bar', 'import', 'browser']); + assert.equal(output, ['default', 'import', 'browser', 'foo', 'bar']); }); it('option.conditions :: browser :: order', () => { let output = run({ browser: true, conditions: ['browser', 'foobar'] }); - assert.equal(output, ['default', 'browser', 'foobar', 'import']); + assert.equal(output, ['default', 'import', 'browser', 'foobar']); }); it('option.conditions :: require', () => { let output = run({ require: true, conditions: ['foo', 'bar'] }); - assert.equal(output, ['default', 'foo', 'bar', 'require', 'node']); + assert.equal(output, ['default', 'require', 'node', 'foo', 'bar']); }); it('option.conditions :: require :: order', () => { let output = run({ require: true, conditions: ['require', 'foobar'] }); - assert.equal(output, ['default', 'require', 'foobar', 'node']); + assert.equal(output, ['default', 'require', 'node', 'foobar']); + }); + + it('option.conditions :: negate', () => { + let output = run({ conditions: ['!import'] }); + assert.equal(output, ['default', 'node']); + }); + + it('option.conditions :: negate :: all', () => { + let output = run({ conditions: ['!default', '!import', '!node', 'types'] }); + assert.equal(output, ['types']); + }); + + it('option.conditions :: negate :: require', () => { + let output = run({ require: true, conditions: ['!node', 'deno'] }); + assert.equal(output, ['default', 'require', 'deno']); + }); + + it('option.conditions :: negate :: require', () => { + let output = run({ browser: true, conditions: ['!browser'] }); + assert.equal(output, ['default', 'import']); + }); + + it('option.conditions :: negate :: unsafe', () => { + let output = run({ unsafe: true, conditions: ['!foo', 'bar'] }); + assert.equal(output, ['default', 'bar']); }); });