Skip to content

Commit

Permalink
Look up for plugins in parent package.json (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
exah authored Jan 30, 2021
1 parent b801ca1 commit f7ddd3b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/size-limit/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Plugins {
}

module.exports = function loadPlugins (pkg) {
if (!pkg || !pkg.packageJson) return []
if (!pkg || !pkg.packageJson) return new Plugins([])

let list = toArray(pkg.packageJson.dependencies)
.concat(toArray(pkg.packageJson.devDependencies))
Expand Down
14 changes: 13 additions & 1 deletion packages/size-limit/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let readPkgUp = require('read-pkg-up')
let ora = require('ora')
let path = require('path')
let chokidar = require('chokidar')

let SizeLimitError = require('./size-limit-error')
Expand All @@ -23,6 +24,17 @@ function throttle (fn) {
}
}

async function findPlugins (parentPkg) {
let plugins = loadPlugins(parentPkg)

if (!parentPkg || !plugins.isEmpty) return plugins

let cwd = path.resolve(parentPkg.path, '..', '..')
let pkg = await readPkgUp({ cwd })

return findPlugins(pkg)
}

module.exports = async process => {
function hasArg (arg) {
return process.argv.some(i => i === arg)
Expand All @@ -38,7 +50,7 @@ module.exports = async process => {
}

let pkg = await readPkgUp({ cwd: process.cwd() })
let plugins = loadPlugins(pkg)
let plugins = await findPlugins(pkg)

if (hasArg('--help')) {
return help.showHelp(plugins)
Expand Down
8 changes: 8 additions & 0 deletions packages/size-limit/test/__snapshots__/run.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`run allows to use peer dependencies in import 1`] = `
"
`;

exports[`run find plugins in parent package.json 1`] = `
"
Size limit: 1 KB
Size: 20 B gzipped
"
`;

exports[`run returns zero bytes for empty file 1`] = `
"
Size limit: 0 B
Expand Down
8 changes: 8 additions & 0 deletions packages/size-limit/test/fixtures/nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "file",
"private": true,
"devDependencies": {
"@size-limit/file": ">= 0.0.0",
"size-limit": ">= 0.0.0"
}
}
Empty file.
10 changes: 10 additions & 0 deletions packages/size-limit/test/fixtures/nested/package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "package",
"private": true,
"size-limit": [
{
"name": "index",
"limit": "1 KB"
}
]
}
6 changes: 5 additions & 1 deletion packages/size-limit/test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function createProcess (cwd, args = []) {
if (cwd.includes('/')) {
return cwd
} else {
return fixture(cwd)
return fixture(...(Array.isArray(cwd) ? cwd : [cwd]))
}
},
exit (code) {
Expand Down Expand Up @@ -291,4 +291,8 @@ describe(`run`, () => {
it('shows debug on error', async () => {
expect(clean(await error('internal-error', ['--debug']))).toMatchSnapshot()
})

it('find plugins in parent package.json', async () => {
expect(clean(await check(['nested', 'package']))).toMatchSnapshot()
})
})

0 comments on commit f7ddd3b

Please sign in to comment.