Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support conditions with "!" #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` <br>
Default: `false`
Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ export function throws(name: string, entry: Entry, condition?: number): never {
}

export function conditions(options: t.Options): Set<t.Condition> {
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;
}

Expand Down
37 changes: 31 additions & 6 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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']);
});
});

Expand Down