|
| 1 | +import { createMatcher, getMatcher } from '../util' |
| 2 | +import * as test from 'fresh-tape' |
| 3 | + |
| 4 | +test('repeat, on-demand matcher', async t => { |
| 5 | + const pkg = { main: 'index.js' } |
| 6 | + const matcher = getMatcher('/root', pkg) |
| 7 | + t.deepEqual(matcher('.', 'commonjs'), { cause: '.main', location: '/root/index.js' }) |
| 8 | + t.equal(getMatcher('/root', pkg), matcher) |
| 9 | +}) |
| 10 | +test('simple main', async t => { |
| 11 | + const match = createMatcher('/root', { main: './test.js' }) |
| 12 | + for (const input of [ |
| 13 | + '', |
| 14 | + '.', |
| 15 | + './' |
| 16 | + ]) { |
| 17 | + t.deepEqual(match(input, 'commonjs'), { cause: '.main', location: '/root/test.js' }) |
| 18 | + } |
| 19 | + t.equal(match('', 'module'), undefined) |
| 20 | +}) |
| 21 | +test('export override string', async t => { |
| 22 | + const a = createMatcher('/root', { main: 'a.js', exports: './b.js' }) |
| 23 | + t.deepEqual(a('', 'commonjs'), { cause: '.exports[\'.\']', location: '/root/b.js' }) |
| 24 | + t.deepEqual(a('', 'module'), { cause: '.exports[\'.\']', location: '/root/b.js' }) |
| 25 | +}) |
| 26 | +test('export override object', async t => { |
| 27 | + const a = createMatcher('/root', { main: 'a.js', exports: { import: './b.js', require: './c.js' } }) |
| 28 | + t.deepEqual(a('', 'commonjs'), { cause: '.exports[\'.\'].require', location: '/root/c.js' }) |
| 29 | + t.deepEqual(a('', 'module'), { cause: '.exports[\'.\'].import', location: '/root/b.js' }) |
| 30 | +}) |
| 31 | +test('export deep override', async t => { |
| 32 | + const match = createMatcher('/root', { |
| 33 | + main: 'a.js', |
| 34 | + exports: { |
| 35 | + '.': { require: './b.js', import: './c.js' }, |
| 36 | + './c-a': { require: './d.js', import: './e.js' }, |
| 37 | + './c-b': { default: './f.js' }, |
| 38 | + './c-c': { node: './g.js', require: './h.js', default: './i.js' } |
| 39 | + } |
| 40 | + }) |
| 41 | + t.deepEqual(match('c-a', 'commonjs'), { cause: '.exports[\'./c-a\'].require', location: '/root/d.js' }) |
| 42 | + t.equals(match('c-a.js', 'commonjs'), undefined) |
| 43 | + t.deepEqual(match('c-a', 'module'), { cause: '.exports[\'./c-a\'].import', location: '/root/e.js' }) |
| 44 | + t.deepEqual(match('c-b', 'commonjs'), { cause: '.exports[\'./c-b\'].default', location: '/root/f.js' }) |
| 45 | + t.deepEqual(match('c-b', 'module'), { cause: '.exports[\'./c-b\'].default', location: '/root/f.js' }) |
| 46 | + t.deepEqual(match('c-c', 'commonjs'), { cause: '.exports[\'./c-c\'].node', location: '/root/g.js' }) |
| 47 | + t.deepEqual(match('c-c', 'module'), { cause: '.exports[\'./c-c\'].default', location: '/root/i.js' }) |
| 48 | +}) |
| 49 | +test('pattern', async t => { |
| 50 | + const match = createMatcher('/root', { |
| 51 | + main: 'a.js', |
| 52 | + exports: { |
| 53 | + './foo/*': null, |
| 54 | + './bar/*': { require: null }, |
| 55 | + '.': { require: './b.js', import: './c.js' }, |
| 56 | + './bak/*.ts': { require: './cjs/*.js' }, |
| 57 | + './*': { require: './cjs/*.js', import: './mjs/*' } |
| 58 | + } |
| 59 | + }) |
| 60 | + t.deepEqual(match('c-a', 'commonjs'), { cause: '.exports[\'./*\'].require', location: '/root/cjs/c-a.js' }) |
| 61 | + t.deepEqual(match('foo/c-a', 'commonjs'), { cause: '.exports[\'./foo/*\']', location: null }) |
| 62 | + t.deepEqual(match('bar/c-a', 'commonjs'), { cause: '.exports[\'./bar/*\'].require', location: null }) |
| 63 | + t.deepEqual(match('baz/c-a', 'module'), { cause: '.exports[\'./*\'].import', location: '/root/mjs/baz/c-a' }) |
| 64 | + t.deepEqual(match('bak/d.ts', 'commonjs'), { cause: '.exports[\'./bak/*.ts\'].require', location: '/root/cjs/d.js' }) |
| 65 | +}) |
| 66 | +test('deep require', async t => { |
| 67 | + const deep = createMatcher('/root', { exports: { './test': './a.js' } }) |
| 68 | + t.deepEqual(deep('test', 'commonjs'), { cause: '.exports[\'./test\']', location: '/root/a.js' }) |
| 69 | +}) |
| 70 | +test('main and type=module', async t => { |
| 71 | + const match = createMatcher('/root', { main: './test.js', type: 'module' }) |
| 72 | + t.equal(match('', 'commonjs'), undefined) |
| 73 | + t.deepEqual(match('', 'module'), { cause: '.main and .type=module', location: '/root/test.js' }) |
| 74 | +}) |
| 75 | +test('module', async t => { |
| 76 | + const match = createMatcher('/root', { main: './a.cjs', module: 'b.mjs' }) |
| 77 | + t.deepEqual(match('', 'commonjs'), { cause: '.main', location: '/root/a.cjs' }) |
| 78 | + t.deepEqual(match('', 'module'), { cause: '.module', location: '/root/b.mjs' }) |
| 79 | +}) |
| 80 | +test.skip('empty match', async t => { |
| 81 | + const match = createMatcher('/root', {}) |
| 82 | + t.deepEqual(match('', 'commonjs'), { cause: 'fs', location: '/root/indexs.js' }) |
| 83 | + t.equal(match('', 'module'), undefined) |
| 84 | +}) |
0 commit comments