| 1 |
import * as uvu from 'uvu'; |
| 2 |
import * as assert from 'uvu/assert'; |
| 3 |
import { legacy } from '../src/legacy'; |
| 4 |
|
| 5 |
import type { Package } from 'resolve.exports'; |
| 6 |
|
| 7 |
function describe( |
| 8 |
name: string, |
| 9 |
cb: (it: uvu.Test) => void |
| 10 |
) { |
| 11 |
let t = uvu.suite(name); |
| 12 |
cb(t); |
| 13 |
t.run(); |
| 14 |
} |
| 15 |
|
| 16 |
describe('lib.legacy', it => { |
| 17 |
it('should be a function', () => { |
| 18 |
assert.type(legacy, 'function'); |
| 19 |
}); |
| 20 |
|
| 21 |
it('should prefer "module" > "main" entry', () => { |
| 22 |
let pkg: Package = { |
| 23 |
"name": "foobar", |
| 24 |
"module": "build/module.js", |
| 25 |
"main": "build/main.js", |
| 26 |
}; |
| 27 |
|
| 28 |
let output = legacy(pkg); |
| 29 |
assert.is(output, './build/module.js'); |
| 30 |
}); |
| 31 |
|
| 32 |
it('should read "main" field', () => { |
| 33 |
let pkg: Package = { |
| 34 |
"name": "foobar", |
| 35 |
"main": "build/main.js", |
| 36 |
}; |
| 37 |
|
| 38 |
let output = legacy(pkg); |
| 39 |
assert.is(output, './build/main.js'); |
| 40 |
}); |
| 41 |
|
| 42 |
it('should return nothing when no fields', () => { |
| 43 |
let pkg: Package = { |
| 44 |
"name": "foobar" |
| 45 |
}; |
| 46 |
|
| 47 |
let output = legacy(pkg); |
| 48 |
assert.is(output, undefined); |
| 49 |
}); |
| 50 |
|
| 51 |
it('should ignore boolean-type field values', () => { |
| 52 |
let pkg = { |
| 53 |
"module": true, |
| 54 |
"main": "main.js" |
| 55 |
}; |
| 56 |
|
| 57 |
let output = legacy(pkg as any); |
| 58 |
assert.is(output, './main.js'); |
| 59 |
}); |
| 60 |
}); |
| 61 |
|
| 62 |
describe('options.fields', it => { |
| 63 |
let pkg: Package = { |
| 64 |
"name": "foobar", |
| 65 |
"module": "build/module.js", |
| 66 |
"browser": "build/browser.js", |
| 67 |
"custom": "build/custom.js", |
| 68 |
"main": "build/main.js", |
| 69 |
}; |
| 70 |
|
| 71 |
it('should customize field search order', () => { |
| 72 |
let output = legacy(pkg); |
| 73 |
assert.is(output, './build/module.js', 'default: module'); |
| 74 |
|
| 75 |
output = legacy(pkg, { fields: ['main'] }); |
| 76 |
assert.is(output, './build/main.js', 'custom: main only'); |
| 77 |
|
| 78 |
output = legacy(pkg, { fields: ['custom', 'main', 'module'] }); |
| 79 |
assert.is(output, './build/custom.js', 'custom: custom > main > module'); |
| 80 |
}); |
| 81 |
|
| 82 |
it('should return first *resolved* field', () => { |
| 83 |
let output = legacy(pkg, { |
| 84 |
fields: ['howdy', 'partner', 'hello', 'world', 'main'] |
| 85 |
}); |
| 86 |
|
| 87 |
assert.is(output, './build/main.js'); |
| 88 |
}); |
| 89 |
}); |
| 90 |
|
| 91 |
describe('options.browser', it => { |
| 92 |
let pkg: Package = { |
| 93 |
"name": "foobar", |
| 94 |
"module": "build/module.js", |
| 95 |
"browser": "build/browser.js", |
| 96 |
"unpkg": "build/unpkg.js", |
| 97 |
"main": "build/main.js", |
| 98 |
}; |
| 99 |
|
| 100 |
it('should prioritize "browser" field when defined', () => { |
| 101 |
let output = legacy(pkg); |
| 102 |
assert.is(output, './build/module.js'); |
| 103 |
|
| 104 |
output = legacy(pkg, { browser: true }); |
| 105 |
assert.is(output, './build/browser.js'); |
| 106 |
}); |
| 107 |
|
| 108 |
it('should respect existing "browser" order in custom fields', () => { |
| 109 |
let output = legacy(pkg, { |
| 110 |
fields: ['main', 'browser'], |
| 111 |
browser: true, |
| 112 |
}); |
| 113 |
|
| 114 |
assert.is(output, './build/main.js'); |
| 115 |
}); |
| 116 |
|
| 117 |
// https://github.com/defunctzombie/package-browser-field-spec |
| 118 |
it('should resolve object format', () => { |
| 119 |
let pkg: Package = { |
| 120 |
"name": "foobar", |
| 121 |
"browser": { |
| 122 |
"module-a": "./shims/module-a.js", |
| 123 |
"./server/only.js": "./shims/client-only.js" |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
assert.is( |
| 128 |
legacy(pkg, { browser: 'module-a' }), |
| 129 |
'./shims/module-a.js' |
| 130 |
); |
| 131 |
|
| 132 |
assert.is( |
| 133 |
legacy(pkg, { browser: './server/only.js' }), |
| 134 |
'./shims/client-only.js' |
| 135 |
); |
| 136 |
|
| 137 |
assert.is( |
| 138 |
legacy(pkg, { browser: 'foobar/server/only.js' }), |
| 139 |
'./shims/client-only.js' |
| 140 |
); |
| 141 |
}); |
| 142 |
|
| 143 |
it('should allow object format to "ignore" modules/files :: string', () => { |
| 144 |
let pkg: Package = { |
| 145 |
"name": "foobar", |
| 146 |
"browser": { |
| 147 |
"module-a": false, |
| 148 |
"./foo.js": false, |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
assert.is( |
| 153 |
legacy(pkg, { browser: 'module-a' }), |
| 154 |
false |
| 155 |
); |
| 156 |
|
| 157 |
assert.is( |
| 158 |
legacy(pkg, { browser: './foo.js' }), |
| 159 |
false |
| 160 |
); |
| 161 |
|
| 162 |
assert.is( |
| 163 |
legacy(pkg, { browser: 'foobar/foo.js' }), |
| 164 |
false |
| 165 |
); |
| 166 |
}); |
| 167 |
|
| 168 |
it('should return the `browser` string (entry) if no custom mapping :: string', () => { |
| 169 |
let pkg: Package = { |
| 170 |
"name": "foobar", |
| 171 |
"browser": { |
| 172 |
// |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
assert.is( |
| 177 |
legacy(pkg, { |
| 178 |
browser: './hello.js' |
| 179 |
}), |
| 180 |
'./hello.js' |
| 181 |
); |
| 182 |
|
| 183 |
assert.is( |
| 184 |
legacy(pkg, { |
| 185 |
browser: 'foobar/hello.js' |
| 186 |
}), |
| 187 |
'./hello.js' |
| 188 |
); |
| 189 |
}); |
| 190 |
|
| 191 |
it('should return the full "browser" object :: true', () => { |
| 192 |
let pkg: Package = { |
| 193 |
"name": "foobar", |
| 194 |
"browser": { |
| 195 |
"./other.js": "./world.js" |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
let output = legacy(pkg, { |
| 200 |
browser: true |
| 201 |
}); |
| 202 |
|
| 203 |
assert.equal(output, pkg.browser); |
| 204 |
}); |
| 205 |
|
| 206 |
it('still ensures string output is made relative', () => { |
| 207 |
let pkg: Package = { |
| 208 |
"name": "foobar", |
| 209 |
"browser": { |
| 210 |
"./foo.js": "bar.js", |
| 211 |
} |
| 212 |
} as any; |
| 213 |
|
| 214 |
assert.is( |
| 215 |
legacy(pkg, { |
| 216 |
browser: './foo.js' |
| 217 |
}), |
| 218 |
'./bar.js' |
| 219 |
); |
| 220 |
|
| 221 |
assert.is( |
| 222 |
legacy(pkg, { |
| 223 |
browser: 'foobar/foo.js' |
| 224 |
}), |
| 225 |
'./bar.js' |
| 226 |
); |
| 227 |
}); |
| 228 |
}); |