Skip to content
Merged
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
1 change: 1 addition & 0 deletions fixtures/exports/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1
20 changes: 20 additions & 0 deletions fixtures/exports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "exports",
"private": true,
"type": "module",
"exports": {
".": {
"import": "./lib/index.js"
}
},
"devDependencies": {
"@size-limit/file": ">= 0.0.0",
"size-limit": ">= 0.0.0"
},
"size-limit": [
{
"name": "index",
"limit": "1 kB"
}
]
}
17 changes: 16 additions & 1 deletion packages/size-limit/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ function toAbsolute(file, cwd) {
return isAbsolute(file) ? file : join(cwd, file)
}

// The package root file from `exports`: the field itself when it is a string,
// otherwise its `.` entry, with conditions resolved in `import`, `default`,
// `require` order
function exportsEntry(exports) {
if (typeof exports === 'string') return exports
if (typeof exports !== 'object' || exports === null) return undefined
if ('.' in exports) return exportsEntry(exports['.'])
for (let condition of ['import', 'default', 'require']) {
let entry = exportsEntry(exports[condition])
if (entry) return entry
}
return undefined
}

async function findFiles(patterns, cwd) {
// Node.js `glob` matches a bare directory as a single entry, while
// `tinyglobby` expanded it to the files inside. Add a `/**` sibling for
Expand Down Expand Up @@ -205,7 +219,8 @@ export default async function getConfig(plugins, process, args, pkg) {
require.resolve(join(dirname(pkg.path), pkg.packageJson.main))
]
} else {
processed.files = [join(dirname(pkg.path), 'index.js')]
let entry = exportsEntry(pkg.packageJson.exports) || 'index.js'
processed.files = [join(dirname(pkg.path), entry)]
}
}
return processed
Expand Down
15 changes: 15 additions & 0 deletions packages/size-limit/test/get-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ it('uses index.js by default', async () => {
})
})

it('uses the root entry of exports without main field', async () => {
expect(await check('exports')).toEqual({
checks: [
{
files: [fixture('exports', 'lib', 'index.js')],
limit: '1 kB',
name: 'index',
sizeLimit: 1000
}
],
configPath: 'package.json',
cwd: fixture('exports')
})
})

it('overrides limit by CLI arg', async () => {
expect(await check('simple', ['--limit', '10 kB'])).toEqual({
checks: [
Expand Down
Loading