From f7ddd3bdb75d5164da90e474b19a5d0b711be9c0 Mon Sep 17 00:00:00 2001 From: John Grishin Date: Sat, 30 Jan 2021 16:23:16 +0000 Subject: [PATCH] Look up for plugins in parent package.json (#211) --- packages/size-limit/load-plugins.js | 2 +- packages/size-limit/run.js | 14 +++++++++++++- .../size-limit/test/__snapshots__/run.test.js.snap | 8 ++++++++ .../size-limit/test/fixtures/nested/package.json | 8 ++++++++ .../test/fixtures/nested/package/index.js | 0 .../test/fixtures/nested/package/package.json | 10 ++++++++++ packages/size-limit/test/run.test.js | 6 +++++- 7 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 packages/size-limit/test/fixtures/nested/package.json create mode 100644 packages/size-limit/test/fixtures/nested/package/index.js create mode 100644 packages/size-limit/test/fixtures/nested/package/package.json diff --git a/packages/size-limit/load-plugins.js b/packages/size-limit/load-plugins.js index 79032152..a998d747 100644 --- a/packages/size-limit/load-plugins.js +++ b/packages/size-limit/load-plugins.js @@ -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)) diff --git a/packages/size-limit/run.js b/packages/size-limit/run.js index f49d28c4..7709238c 100644 --- a/packages/size-limit/run.js +++ b/packages/size-limit/run.js @@ -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') @@ -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) @@ -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) diff --git a/packages/size-limit/test/__snapshots__/run.test.js.snap b/packages/size-limit/test/__snapshots__/run.test.js.snap index d86717aa..030e18be 100644 --- a/packages/size-limit/test/__snapshots__/run.test.js.snap +++ b/packages/size-limit/test/__snapshots__/run.test.js.snap @@ -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 diff --git a/packages/size-limit/test/fixtures/nested/package.json b/packages/size-limit/test/fixtures/nested/package.json new file mode 100644 index 00000000..61a97591 --- /dev/null +++ b/packages/size-limit/test/fixtures/nested/package.json @@ -0,0 +1,8 @@ +{ + "name": "file", + "private": true, + "devDependencies": { + "@size-limit/file": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/packages/size-limit/test/fixtures/nested/package/index.js b/packages/size-limit/test/fixtures/nested/package/index.js new file mode 100644 index 00000000..e69de29b diff --git a/packages/size-limit/test/fixtures/nested/package/package.json b/packages/size-limit/test/fixtures/nested/package/package.json new file mode 100644 index 00000000..ca28cb27 --- /dev/null +++ b/packages/size-limit/test/fixtures/nested/package/package.json @@ -0,0 +1,10 @@ +{ + "name": "package", + "private": true, + "size-limit": [ + { + "name": "index", + "limit": "1 KB" + } + ] +} diff --git a/packages/size-limit/test/run.test.js b/packages/size-limit/test/run.test.js index 146b2637..61e45332 100644 --- a/packages/size-limit/test/run.test.js +++ b/packages/size-limit/test/run.test.js @@ -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) { @@ -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() + }) })