From 724c8965aedf6258560e5eed232b646ba3b564a1 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 01:35:46 -0500 Subject: [PATCH 01/27] Add `jiti` to dependencies --- packages/size-limit/package.json | 1 + pnpm-lock.yaml | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/packages/size-limit/package.json b/packages/size-limit/package.json index 3eed7a24..514cd74e 100644 --- a/packages/size-limit/package.json +++ b/packages/size-limit/package.json @@ -26,6 +26,7 @@ "bytes-iec": "^3.1.1", "chokidar": "^3.6.0", "globby": "^14.0.1", + "jiti": "^1.21.0", "lilconfig": "^3.1.1", "nanospinner": "^1.1.0", "picocolors": "^1.0.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ce1c47f8..039dec0e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -166,6 +166,9 @@ importers: globby: specifier: ^14.0.1 version: 14.0.1 + jiti: + specifier: ^1.21.0 + version: 1.21.0 lilconfig: specifier: ^3.1.1 version: 3.1.1 @@ -3675,6 +3678,11 @@ packages: supports-color: 8.1.1 dev: false + /jiti@1.21.0: + resolution: {integrity: sha512-gFqAIbuKyyso/3G2qhiO2OM6shY6EPP/R0+mkDbyspxKazh8BXDC5FiFsUjlczgdNz/vfra0da2y+aHrusLG/Q==} + hasBin: true + dev: false + /jora@1.0.0-beta.8: resolution: {integrity: sha512-f3WpYwfDTlhfSdyCkAlAXSKRpwZYBgCDnyWmA9D0yyItCTFnFefKtvFpaczrj/FItkgDkHiewgFuHsgh4TmokA==} engines: {node: ^10.12.0 || ^12.20.0 || ^14.13.0 || >=15.0.0} From cd4418f80d06f66ad56d7dc53f33f4637029fb72 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 01:36:39 -0500 Subject: [PATCH 02/27] Add support for loading TypeScript config files dynamically --- packages/size-limit/get-config.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index 6cbd6509..c9422d8b 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -1,5 +1,6 @@ import bytes from 'bytes-iec' import { globby } from 'globby' +import _jiti from 'jiti' import { lilconfig } from 'lilconfig' import { createRequire } from 'node:module' import { dirname, isAbsolute, join, relative } from 'node:path' @@ -7,6 +8,7 @@ import { dirname, isAbsolute, join, relative } from 'node:path' import { SizeLimitError } from './size-limit-error.js' const require = createRequire(import.meta.url) +const jiti = _jiti(__filename, { interopDefault: true }) let OPTIONS = { brotli: 'file', @@ -91,6 +93,18 @@ function toName(files, cwd) { */ const dynamicImport = async filePath => (await import(filePath)).default +/** + * Loads a TypeScript file from a given file path using the + * {@linkcode jiti} function. This loader function simplifies the + * process of dynamically importing TypeScript modules at runtime, + * offering a way to execute or import TypeScript files directly + * without pre-compilation. + * + * @param {string} filePath - The path to the TypeScript file to be loaded. + * @returns {any} The module exports from the loaded TypeScript file. + */ +const tsLoader = filePath => jiti(filePath) + export default async function getConfig(plugins, process, args, pkg) { let config = { cwd: process.cwd() @@ -123,7 +137,11 @@ export default async function getConfig(plugins, process, args, pkg) { let explorer = lilconfig('size-limit', { loaders: { '.js': dynamicImport, - '.mjs': dynamicImport + '.mjs': dynamicImport, + '.cjs': dynamicImport, + '.ts': tsLoader, + '.mts': tsLoader, + '.cts': tsLoader }, searchPlaces: [ 'package.json', @@ -131,7 +149,10 @@ export default async function getConfig(plugins, process, args, pkg) { '.size-limit', '.size-limit.js', '.size-limit.mjs', - '.size-limit.cjs' + '.size-limit.cjs', + '.size-limit.ts', + '.size-limit.mts', + '.size-limit.cts' ] }) let result = await explorer.search(process.cwd()) From 9cc2224dd51f3c544f70477f91bcf4e590657473 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 01:39:11 -0500 Subject: [PATCH 03/27] Test all config file extension/module type combos with fixtures. --- fixtures/cjs-config-file-cjs/.size-limit.cjs | 5 + fixtures/cjs-config-file-cjs/index.js | 5 + fixtures/cjs-config-file-cjs/package.json | 9 ++ fixtures/cjs-config-file-esm/.size-limit.cjs | 5 + fixtures/cjs-config-file-esm/index.js | 5 + fixtures/cjs-config-file-esm/package.json | 9 ++ fixtures/cts-config-file-cjs/.size-limit.cts | 5 + fixtures/cts-config-file-cjs/index.js | 5 + fixtures/cts-config-file-cjs/package.json | 9 ++ fixtures/cts-config-file-esm/.size-limit.cts | 5 + fixtures/cts-config-file-esm/index.js | 5 + fixtures/cts-config-file-esm/package.json | 9 ++ fixtures/cts-config-file/.size-limit.cts | 5 + fixtures/cts-config-file/index.js | 5 + fixtures/cts-config-file/package.json | 9 ++ fixtures/js-config-file-cjs/.size-limit.js | 5 + fixtures/js-config-file-cjs/index.js | 5 + fixtures/js-config-file-cjs/package.json | 9 ++ fixtures/js-config-file-esm/.size-limit.js | 5 + fixtures/js-config-file-esm/index.js | 5 + fixtures/js-config-file-esm/package.json | 9 ++ fixtures/mjs-config-file-cjs/.size-limit.mjs | 5 + fixtures/mjs-config-file-cjs/index.js | 5 + fixtures/mjs-config-file-cjs/package.json | 9 ++ fixtures/mjs-config-file-esm/.size-limit.mjs | 5 + fixtures/mjs-config-file-esm/index.js | 5 + fixtures/mjs-config-file-esm/package.json | 9 ++ fixtures/mts-config-file-cjs/.size-limit.mts | 5 + fixtures/mts-config-file-cjs/index.js | 5 + fixtures/mts-config-file-cjs/package.json | 9 ++ fixtures/mts-config-file-esm/.size-limit.mts | 5 + fixtures/mts-config-file-esm/index.js | 5 + fixtures/mts-config-file-esm/package.json | 9 ++ fixtures/mts-config-file/.size-limit.mts | 5 + fixtures/mts-config-file/index.js | 5 + fixtures/mts-config-file/package.json | 9 ++ fixtures/ts-config-file-cjs/.size-limit.ts | 5 + fixtures/ts-config-file-cjs/index.js | 5 + fixtures/ts-config-file-cjs/package.json | 9 ++ fixtures/ts-config-file-esm/.size-limit.ts | 5 + fixtures/ts-config-file-esm/index.js | 5 + fixtures/ts-config-file-esm/package.json | 9 ++ .../.size-limit.ts | 5 + .../ts-config-file-with-type-module/index.js | 5 + .../package.json | 10 ++ fixtures/ts-config-file/.size-limit.ts | 5 + fixtures/ts-config-file/index.js | 5 + fixtures/ts-config-file/package.json | 9 ++ packages/size-limit/test/get-config.test.js | 144 +++++++++++++++++- 49 files changed, 448 insertions(+), 1 deletion(-) create mode 100644 fixtures/cjs-config-file-cjs/.size-limit.cjs create mode 100644 fixtures/cjs-config-file-cjs/index.js create mode 100644 fixtures/cjs-config-file-cjs/package.json create mode 100644 fixtures/cjs-config-file-esm/.size-limit.cjs create mode 100644 fixtures/cjs-config-file-esm/index.js create mode 100644 fixtures/cjs-config-file-esm/package.json create mode 100644 fixtures/cts-config-file-cjs/.size-limit.cts create mode 100644 fixtures/cts-config-file-cjs/index.js create mode 100644 fixtures/cts-config-file-cjs/package.json create mode 100644 fixtures/cts-config-file-esm/.size-limit.cts create mode 100644 fixtures/cts-config-file-esm/index.js create mode 100644 fixtures/cts-config-file-esm/package.json create mode 100644 fixtures/cts-config-file/.size-limit.cts create mode 100644 fixtures/cts-config-file/index.js create mode 100644 fixtures/cts-config-file/package.json create mode 100644 fixtures/js-config-file-cjs/.size-limit.js create mode 100644 fixtures/js-config-file-cjs/index.js create mode 100644 fixtures/js-config-file-cjs/package.json create mode 100644 fixtures/js-config-file-esm/.size-limit.js create mode 100644 fixtures/js-config-file-esm/index.js create mode 100644 fixtures/js-config-file-esm/package.json create mode 100644 fixtures/mjs-config-file-cjs/.size-limit.mjs create mode 100644 fixtures/mjs-config-file-cjs/index.js create mode 100644 fixtures/mjs-config-file-cjs/package.json create mode 100644 fixtures/mjs-config-file-esm/.size-limit.mjs create mode 100644 fixtures/mjs-config-file-esm/index.js create mode 100644 fixtures/mjs-config-file-esm/package.json create mode 100644 fixtures/mts-config-file-cjs/.size-limit.mts create mode 100644 fixtures/mts-config-file-cjs/index.js create mode 100644 fixtures/mts-config-file-cjs/package.json create mode 100644 fixtures/mts-config-file-esm/.size-limit.mts create mode 100644 fixtures/mts-config-file-esm/index.js create mode 100644 fixtures/mts-config-file-esm/package.json create mode 100644 fixtures/mts-config-file/.size-limit.mts create mode 100644 fixtures/mts-config-file/index.js create mode 100644 fixtures/mts-config-file/package.json create mode 100644 fixtures/ts-config-file-cjs/.size-limit.ts create mode 100644 fixtures/ts-config-file-cjs/index.js create mode 100644 fixtures/ts-config-file-cjs/package.json create mode 100644 fixtures/ts-config-file-esm/.size-limit.ts create mode 100644 fixtures/ts-config-file-esm/index.js create mode 100644 fixtures/ts-config-file-esm/package.json create mode 100644 fixtures/ts-config-file-with-type-module/.size-limit.ts create mode 100644 fixtures/ts-config-file-with-type-module/index.js create mode 100644 fixtures/ts-config-file-with-type-module/package.json create mode 100644 fixtures/ts-config-file/.size-limit.ts create mode 100644 fixtures/ts-config-file/index.js create mode 100644 fixtures/ts-config-file/package.json diff --git a/fixtures/cjs-config-file-cjs/.size-limit.cjs b/fixtures/cjs-config-file-cjs/.size-limit.cjs new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/cjs-config-file-cjs/.size-limit.cjs @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/cjs-config-file-cjs/index.js b/fixtures/cjs-config-file-cjs/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/cjs-config-file-cjs/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/cjs-config-file-cjs/package.json b/fixtures/cjs-config-file-cjs/package.json new file mode 100644 index 00000000..1f2bada2 --- /dev/null +++ b/fixtures/cjs-config-file-cjs/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "cjs-config-file-cjs", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/cjs-config-file-esm/.size-limit.cjs b/fixtures/cjs-config-file-esm/.size-limit.cjs new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/cjs-config-file-esm/.size-limit.cjs @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/cjs-config-file-esm/index.js b/fixtures/cjs-config-file-esm/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/cjs-config-file-esm/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/cjs-config-file-esm/package.json b/fixtures/cjs-config-file-esm/package.json new file mode 100644 index 00000000..71520bbb --- /dev/null +++ b/fixtures/cjs-config-file-esm/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "cjs-config-file-esm", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/cts-config-file-cjs/.size-limit.cts b/fixtures/cts-config-file-cjs/.size-limit.cts new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/cts-config-file-cjs/.size-limit.cts @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/cts-config-file-cjs/index.js b/fixtures/cts-config-file-cjs/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/cts-config-file-cjs/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/cts-config-file-cjs/package.json b/fixtures/cts-config-file-cjs/package.json new file mode 100644 index 00000000..59971b5d --- /dev/null +++ b/fixtures/cts-config-file-cjs/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "cts-config-file-cjs", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/cts-config-file-esm/.size-limit.cts b/fixtures/cts-config-file-esm/.size-limit.cts new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/cts-config-file-esm/.size-limit.cts @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/cts-config-file-esm/index.js b/fixtures/cts-config-file-esm/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/cts-config-file-esm/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/cts-config-file-esm/package.json b/fixtures/cts-config-file-esm/package.json new file mode 100644 index 00000000..c39c32d8 --- /dev/null +++ b/fixtures/cts-config-file-esm/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "cts-config-file-esm", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/cts-config-file/.size-limit.cts b/fixtures/cts-config-file/.size-limit.cts new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/cts-config-file/.size-limit.cts @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/cts-config-file/index.js b/fixtures/cts-config-file/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/cts-config-file/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/cts-config-file/package.json b/fixtures/cts-config-file/package.json new file mode 100644 index 00000000..0189ba5c --- /dev/null +++ b/fixtures/cts-config-file/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "cts-config-file", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/js-config-file-cjs/.size-limit.js b/fixtures/js-config-file-cjs/.size-limit.js new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/js-config-file-cjs/.size-limit.js @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/js-config-file-cjs/index.js b/fixtures/js-config-file-cjs/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/js-config-file-cjs/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/js-config-file-cjs/package.json b/fixtures/js-config-file-cjs/package.json new file mode 100644 index 00000000..be46cbd1 --- /dev/null +++ b/fixtures/js-config-file-cjs/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "js-config-file-cjs", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/js-config-file-esm/.size-limit.js b/fixtures/js-config-file-esm/.size-limit.js new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/js-config-file-esm/.size-limit.js @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/js-config-file-esm/index.js b/fixtures/js-config-file-esm/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/js-config-file-esm/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/js-config-file-esm/package.json b/fixtures/js-config-file-esm/package.json new file mode 100644 index 00000000..6825c6d1 --- /dev/null +++ b/fixtures/js-config-file-esm/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "js-config-file-esm", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/mjs-config-file-cjs/.size-limit.mjs b/fixtures/mjs-config-file-cjs/.size-limit.mjs new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/mjs-config-file-cjs/.size-limit.mjs @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/mjs-config-file-cjs/index.js b/fixtures/mjs-config-file-cjs/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/mjs-config-file-cjs/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/mjs-config-file-cjs/package.json b/fixtures/mjs-config-file-cjs/package.json new file mode 100644 index 00000000..d357038c --- /dev/null +++ b/fixtures/mjs-config-file-cjs/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "mjs-config-file-cjs", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/mjs-config-file-esm/.size-limit.mjs b/fixtures/mjs-config-file-esm/.size-limit.mjs new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/mjs-config-file-esm/.size-limit.mjs @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/mjs-config-file-esm/index.js b/fixtures/mjs-config-file-esm/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/mjs-config-file-esm/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/mjs-config-file-esm/package.json b/fixtures/mjs-config-file-esm/package.json new file mode 100644 index 00000000..3ff83294 --- /dev/null +++ b/fixtures/mjs-config-file-esm/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "mjs-config-file-esm", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/mts-config-file-cjs/.size-limit.mts b/fixtures/mts-config-file-cjs/.size-limit.mts new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/mts-config-file-cjs/.size-limit.mts @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/mts-config-file-cjs/index.js b/fixtures/mts-config-file-cjs/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/mts-config-file-cjs/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/mts-config-file-cjs/package.json b/fixtures/mts-config-file-cjs/package.json new file mode 100644 index 00000000..bd5f91b3 --- /dev/null +++ b/fixtures/mts-config-file-cjs/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "mts-config-file-cjs", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/mts-config-file-esm/.size-limit.mts b/fixtures/mts-config-file-esm/.size-limit.mts new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/mts-config-file-esm/.size-limit.mts @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/mts-config-file-esm/index.js b/fixtures/mts-config-file-esm/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/mts-config-file-esm/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/mts-config-file-esm/package.json b/fixtures/mts-config-file-esm/package.json new file mode 100644 index 00000000..39671c99 --- /dev/null +++ b/fixtures/mts-config-file-esm/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "mts-config-file-esm", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/mts-config-file/.size-limit.mts b/fixtures/mts-config-file/.size-limit.mts new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/mts-config-file/.size-limit.mts @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/mts-config-file/index.js b/fixtures/mts-config-file/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/mts-config-file/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/mts-config-file/package.json b/fixtures/mts-config-file/package.json new file mode 100644 index 00000000..a15a9469 --- /dev/null +++ b/fixtures/mts-config-file/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "mts-config-file", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/ts-config-file-cjs/.size-limit.ts b/fixtures/ts-config-file-cjs/.size-limit.ts new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/ts-config-file-cjs/.size-limit.ts @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/ts-config-file-cjs/index.js b/fixtures/ts-config-file-cjs/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/ts-config-file-cjs/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/ts-config-file-cjs/package.json b/fixtures/ts-config-file-cjs/package.json new file mode 100644 index 00000000..c532dc5d --- /dev/null +++ b/fixtures/ts-config-file-cjs/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "ts-config-file-cjs", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/ts-config-file-esm/.size-limit.ts b/fixtures/ts-config-file-esm/.size-limit.ts new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/ts-config-file-esm/.size-limit.ts @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/ts-config-file-esm/index.js b/fixtures/ts-config-file-esm/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/ts-config-file-esm/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/ts-config-file-esm/package.json b/fixtures/ts-config-file-esm/package.json new file mode 100644 index 00000000..d7316959 --- /dev/null +++ b/fixtures/ts-config-file-esm/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "ts-config-file-esm", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/ts-config-file-with-type-module/.size-limit.ts b/fixtures/ts-config-file-with-type-module/.size-limit.ts new file mode 100644 index 00000000..31503c8c --- /dev/null +++ b/fixtures/ts-config-file-with-type-module/.size-limit.ts @@ -0,0 +1,5 @@ +export default [ + { + path: 'index.js' + } +] diff --git a/fixtures/ts-config-file-with-type-module/index.js b/fixtures/ts-config-file-with-type-module/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/ts-config-file-with-type-module/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/ts-config-file-with-type-module/package.json b/fixtures/ts-config-file-with-type-module/package.json new file mode 100644 index 00000000..33d7002e --- /dev/null +++ b/fixtures/ts-config-file-with-type-module/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "name": "ts-config-file-with-type-module", + "type": "module", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/fixtures/ts-config-file/.size-limit.ts b/fixtures/ts-config-file/.size-limit.ts new file mode 100644 index 00000000..a086cba0 --- /dev/null +++ b/fixtures/ts-config-file/.size-limit.ts @@ -0,0 +1,5 @@ +module.exports = [ + { + path: 'index.js' + } +] diff --git a/fixtures/ts-config-file/index.js b/fixtures/ts-config-file/index.js new file mode 100644 index 00000000..d1bdfc55 --- /dev/null +++ b/fixtures/ts-config-file/index.js @@ -0,0 +1,5 @@ +const first = 'first' + +const second = 'second' + +export { first, second } diff --git a/fixtures/ts-config-file/package.json b/fixtures/ts-config-file/package.json new file mode 100644 index 00000000..f80c1f10 --- /dev/null +++ b/fixtures/ts-config-file/package.json @@ -0,0 +1,9 @@ +{ + "private": true, + "name": "ts-config-file", + "devDependencies": { + "@size-limit/webpack": ">= 0.0.0", + "@size-limit/webpack-why": ">= 0.0.0", + "size-limit": ">= 0.0.0" + } +} diff --git a/packages/size-limit/test/get-config.test.js b/packages/size-limit/test/get-config.test.js index 46417811..9b4bf981 100644 --- a/packages/size-limit/test/get-config.test.js +++ b/packages/size-limit/test/get-config.test.js @@ -1,5 +1,5 @@ import { join } from 'node:path' -import { beforeEach, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import calc from '../calc' import run from '../run' @@ -237,6 +237,118 @@ it('should work with .js config file and "type": "module"', async () => { }) }) +it('should work with .js config file and `export default` without "type": "module"', async () => { + expect(await check('js-config-file-esm')).toEqual({ + checks: [ + { + files: [fixture('js-config-file-esm', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.js', + cwd: fixture('js-config-file-esm') + }) +}) + +it('should work with .cjs config file and `export default` without "type": "module"', async () => { + expect(await check('cjs-config-file-esm')).toEqual({ + checks: [ + { + files: [fixture('cjs-config-file-esm', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.cjs', + cwd: fixture('cjs-config-file-esm') + }) +}) + +it('should work with .cjs config file and `module.exports` without "type": "module"', async () => { + expect(await check('cjs-config-file-cjs')).toEqual({ + checks: [ + { + files: [fixture('cjs-config-file-cjs', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.cjs', + cwd: fixture('cjs-config-file-cjs') + }) +}) + +it('should work with .ts config file and `export default` without "type": "module"', async () => { + expect(await check('ts-config-file-esm')).toEqual({ + checks: [ + { + files: [fixture('ts-config-file-esm', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.ts', + cwd: fixture('ts-config-file-esm') + }) +}) + +it('should work with .ts config file', async () => { + expect(await check('ts-config-file')).toEqual({ + checks: [ + { + files: [fixture('ts-config-file', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.ts', + cwd: fixture('ts-config-file') + }) +}) + +it('should work with .cts config file', async () => { + expect(await check('cts-config-file')).toEqual({ + checks: [ + { + files: [fixture('cts-config-file', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.cts', + cwd: fixture('cts-config-file') + }) +}) + +it('should work with .mts config file', async () => { + expect(await check('mts-config-file')).toEqual({ + checks: [ + { + files: [fixture('mts-config-file', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.mts', + cwd: fixture('mts-config-file') + }) +}) + +it('should work with .ts config file and "type": "module"', async () => { + expect(await check('ts-config-file-with-type-module')).toEqual({ + checks: [ + { + files: [fixture('ts-config-file-with-type-module', 'index.js')], + name: 'index.js', + path: 'index.js' + } + ], + configPath: '.size-limit.ts', + cwd: fixture('ts-config-file-with-type-module') + }) +}) + it('uses peerDependencies as ignore option', async () => { expect(await check('peer')).toEqual({ checks: [ @@ -316,3 +428,33 @@ it('normalizes import', async () => { cwd: fixture('integration-esm') }) }) + +const allConfigFileExtensions = ['mjs', 'js', 'cjs', 'ts', 'mts', 'cts'] +const exportTypes = [ + { moduleType: 'esm', exportSyntax: 'export default' }, + { moduleType: 'cjs', exportSyntax: 'module.exports' } +] + +describe.each(allConfigFileExtensions)( + 'config file with `.%s` extension', + extension => { + it.each(exportTypes)( + 'should work with $moduleType module syntax ($exportSyntax)', + async ({ moduleType }) => { + expect(await check(`${extension}-config-file-${moduleType}`)).toEqual({ + checks: [ + { + files: [ + fixture(`${extension}-config-file-${moduleType}`, 'index.js') + ], + name: 'index.js', + path: 'index.js' + } + ], + configPath: `.size-limit.${extension}`, + cwd: fixture(`${extension}-config-file-${moduleType}`) + }) + } + ) + } +) From 99b3d506b21761bfd8e6b5ea11f38fb0eae24754 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 02:04:34 -0500 Subject: [PATCH 04/27] Manually define `__filename` --- packages/size-limit/get-config.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index c9422d8b..95c660c3 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -4,10 +4,13 @@ import _jiti from 'jiti' import { lilconfig } from 'lilconfig' import { createRequire } from 'node:module' import { dirname, isAbsolute, join, relative } from 'node:path' - +import { fileURLToPath } from 'node:url' import { SizeLimitError } from './size-limit-error.js' const require = createRequire(import.meta.url) + +const __filename = fileURLToPath(import.meta.url) + const jiti = _jiti(__filename, { interopDefault: true }) let OPTIONS = { From 3556e71d2e80c2100c51639a8517be768b2eca2d Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 02:07:05 -0500 Subject: [PATCH 05/27] Increase the `timeout` to accommodate the new added number of tests --- vite.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vite.config.ts b/vite.config.ts index 96f0bfb8..6d379915 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -18,7 +18,7 @@ export default defineConfig({ 'packages/preset-app/index.js' ] }, - testTimeout: 10000, + testTimeout: 20_000, watchExclude: ['**/fixtures', '**/dist', '**/out'] } }) From fa1d20a3092df44c0e8f2f81a4e4798bbcbd3b7e Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 03:53:56 -0500 Subject: [PATCH 06/27] Shorten test descriptions in `get-config.test.js` --- packages/size-limit/test/get-config.test.js | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/size-limit/test/get-config.test.js b/packages/size-limit/test/get-config.test.js index 9b4bf981..d586c992 100644 --- a/packages/size-limit/test/get-config.test.js +++ b/packages/size-limit/test/get-config.test.js @@ -195,7 +195,7 @@ it('normalizes bundle and webpack arguments with --why and ui-reports', async () }) }) -it('should work with .mjs config file', async () => { +it('works with .mjs config file', async () => { expect(await check('mjs-config-file')).toEqual({ checks: [ { @@ -209,7 +209,7 @@ it('should work with .mjs config file', async () => { }) }) -it('should work with .js config file', async () => { +it('works with .js config file', async () => { expect(await check('js-config-file')).toEqual({ checks: [ { @@ -223,7 +223,7 @@ it('should work with .js config file', async () => { }) }) -it('should work with .js config file and "type": "module"', async () => { +it('works with .js config file and "type": "module"', async () => { expect(await check('js-config-file-with-type-module')).toEqual({ checks: [ { @@ -237,7 +237,7 @@ it('should work with .js config file and "type": "module"', async () => { }) }) -it('should work with .js config file and `export default` without "type": "module"', async () => { +it('works with .js config file and `export default` without "type": "module"', async () => { expect(await check('js-config-file-esm')).toEqual({ checks: [ { @@ -251,7 +251,7 @@ it('should work with .js config file and `export default` without "type": "modul }) }) -it('should work with .cjs config file and `export default` without "type": "module"', async () => { +it('works with .cjs config file and `export default` without "type": "module"', async () => { expect(await check('cjs-config-file-esm')).toEqual({ checks: [ { @@ -265,7 +265,7 @@ it('should work with .cjs config file and `export default` without "type": "modu }) }) -it('should work with .cjs config file and `module.exports` without "type": "module"', async () => { +it('works with .cjs config file and `module.exports` without "type": "module"', async () => { expect(await check('cjs-config-file-cjs')).toEqual({ checks: [ { @@ -279,7 +279,7 @@ it('should work with .cjs config file and `module.exports` without "type": "modu }) }) -it('should work with .ts config file and `export default` without "type": "module"', async () => { +it('works with .ts config file and `export default` without "type": "module"', async () => { expect(await check('ts-config-file-esm')).toEqual({ checks: [ { @@ -293,7 +293,7 @@ it('should work with .ts config file and `export default` without "type": "modul }) }) -it('should work with .ts config file', async () => { +it('works with .ts config file', async () => { expect(await check('ts-config-file')).toEqual({ checks: [ { @@ -307,7 +307,7 @@ it('should work with .ts config file', async () => { }) }) -it('should work with .cts config file', async () => { +it('works with .cts config file', async () => { expect(await check('cts-config-file')).toEqual({ checks: [ { @@ -321,7 +321,7 @@ it('should work with .cts config file', async () => { }) }) -it('should work with .mts config file', async () => { +it('works with .mts config file', async () => { expect(await check('mts-config-file')).toEqual({ checks: [ { @@ -335,7 +335,7 @@ it('should work with .mts config file', async () => { }) }) -it('should work with .ts config file and "type": "module"', async () => { +it('works with .ts config file and "type": "module"', async () => { expect(await check('ts-config-file-with-type-module')).toEqual({ checks: [ { @@ -439,7 +439,7 @@ describe.each(allConfigFileExtensions)( 'config file with `.%s` extension', extension => { it.each(exportTypes)( - 'should work with $moduleType module syntax ($exportSyntax)', + 'works with $moduleType module syntax ($exportSyntax)', async ({ moduleType }) => { expect(await check(`${extension}-config-file-${moduleType}`)).toEqual({ checks: [ From 9bc614904184eb9639c199cef76145f7f7409120 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 04:18:07 -0500 Subject: [PATCH 07/27] Add very loose type definitions --- packages/size-limit/index.d.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/size-limit/index.d.ts diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts new file mode 100644 index 00000000..0783c982 --- /dev/null +++ b/packages/size-limit/index.d.ts @@ -0,0 +1,21 @@ +export type Config = { + brotli?: boolean + compareWith?: string + config?: string + disableModuleConcatenation?: boolean + entry?: string | string[] + gzip?: boolean + hidePassed?: boolean + highlightLess?: boolean + ignore?: string[] + import?: string | Record + limit?: string + modifyEsbuildConfig?: (config?: any) => any + modifyWebpackConfig?: (config?: any) => any + module?: boolean + name?: string + path: string | string[] + running?: boolean + uiReports?: object + webpack?: boolean +} From 27ebf6f545137c1815724a45d4ea10d5d13a2cab Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 04:18:41 -0500 Subject: [PATCH 08/27] Add types definition file to `package.json` --- packages/size-limit/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/size-limit/package.json b/packages/size-limit/package.json index 514cd74e..919a1cbf 100644 --- a/packages/size-limit/package.json +++ b/packages/size-limit/package.json @@ -3,6 +3,7 @@ "version": "11.0.3", "description": "CLI tool for Size Limit", "type": "module", + "types": "index.d.ts", "keywords": [ "size-limit", "cli", From cdd617d48cbd087c6ee6ab0675e42924605d314c Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 04:29:28 -0500 Subject: [PATCH 09/27] Copy documentation from `README.md` and paste as JSDocs --- packages/size-limit/index.d.ts | 69 ++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index 0783c982..11819aae 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -1,21 +1,90 @@ export type Config = { + /** + * With `false` it will disable any compression. + */ brotli?: boolean + + /** + * Path to `stats.json` from another build to compare (when `--why` is using). + */ compareWith?: string + + /** + * A path to a custom webpack config. + */ config?: string disableModuleConcatenation?: boolean + + /** + * When using a custom webpack config, a webpack entry could be given. + * It could be a string or an array of strings. By default, + * the total size of all entry points will be checked. + */ entry?: string | string[] + + /** + * With `true` it will use Gzip compression and disable Brotli compression. + */ gzip?: boolean hidePassed?: boolean highlightLess?: boolean + + /** + * An array of files and dependencies to exclude from + * the project size calculation. + */ ignore?: string[] + /** + * Partial import to test tree-shaking. It could be `"{ lib }"` to test + * `import { lib } from 'lib'`, `*` to test all exports, or + * `{ "a.js": "{ a }", "b.js": "{ b }" }` to test multiple files. + */ import?: string | Record + + /** + * Size or time limit for files from the path option. + * It should be a string with a number and unit, separated by a space. + * Format: `100 B`, `10 kB`, `500 ms`, `1 s`. + */ limit?: string modifyEsbuildConfig?: (config?: any) => any + + /** + * (`.size-limit.js` only) Function that can be used to do last-minute + * changes to the webpack config, like adding a plugin. + */ modifyWebpackConfig?: (config?: any) => any module?: boolean + + /** + * The name of the current section. + * It will only be useful if you have multiple sections. + */ name?: string + + /** + * Relative paths to files. The only mandatory option. + * It could be a path `"index.js"`, a + * {@link https://github.com/sindresorhus/globby#globbing-patterns pattern} + * `"dist/app-*.js"` or an array + * `["index.js", "dist/app-*.js", "!dist/app-exclude.js"]`. + */ path: string | string[] + + /** + * With `false` it will disable calculating running time. + */ running?: boolean + + /** + * Custom UI reports list. + * + * @see {@link https://github.com/statoscope/statoscope/tree/master/packages/webpack-plugin#optionsreports-report Statoscope docs} + */ uiReports?: object + + /** + * With `false` it will disable webpack. + */ webpack?: boolean } From 0cc29e10c4c138b3e7c1a60ddf2a06d50fd88295 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 04:32:04 -0500 Subject: [PATCH 10/27] Add `SizeLimitConfig` type to `index.d.ts` --- packages/size-limit/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index 11819aae..399ee49d 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -88,3 +88,5 @@ export type Config = { */ webpack?: boolean } + +export type SizeLimitConfig = Config[] From ee94b70228668898f3d02f857156a4adaa438660 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 04:32:20 -0500 Subject: [PATCH 11/27] Fix minor type issue in JSDocs --- packages/size-limit/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/size-limit/index.js b/packages/size-limit/index.js index 8e88cb43..a9bd321e 100644 --- a/packages/size-limit/index.js +++ b/packages/size-limit/index.js @@ -8,7 +8,7 @@ export { SizeLimitError } from './size-limit-error.js' /** * Run Size Limit and return the result * - * @param {functions[]} plugins The list of plugins like `@size-limit/time` + * @param {Function[]} plugins The list of plugins like `@size-limit/time` * @param {string[]|object} files Path to files or internal config * @return {Promise} Project size */ From 97c5eaa55eedf73070a2c627a0a6c20a2b0368c8 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:09:20 -0500 Subject: [PATCH 12/27] Add `satisfies SizeLimitConfig` to TS fixtures --- fixtures/cts-config-file-cjs/.size-limit.cts | 4 +++- fixtures/cts-config-file-esm/.size-limit.cts | 4 +++- fixtures/cts-config-file/.size-limit.cts | 4 +++- fixtures/mts-config-file-cjs/.size-limit.mts | 4 +++- fixtures/mts-config-file-esm/.size-limit.mts | 4 +++- fixtures/mts-config-file/.size-limit.mts | 4 +++- fixtures/ts-config-file-cjs/.size-limit.ts | 4 +++- fixtures/ts-config-file-esm/.size-limit.ts | 4 +++- fixtures/ts-config-file-with-type-module/.size-limit.ts | 4 +++- fixtures/ts-config-file/.size-limit.ts | 4 +++- 10 files changed, 30 insertions(+), 10 deletions(-) diff --git a/fixtures/cts-config-file-cjs/.size-limit.cts b/fixtures/cts-config-file-cjs/.size-limit.cts index a086cba0..a759eaa9 100644 --- a/fixtures/cts-config-file-cjs/.size-limit.cts +++ b/fixtures/cts-config-file-cjs/.size-limit.cts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + module.exports = [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/cts-config-file-esm/.size-limit.cts b/fixtures/cts-config-file-esm/.size-limit.cts index 31503c8c..99ffbf69 100644 --- a/fixtures/cts-config-file-esm/.size-limit.cts +++ b/fixtures/cts-config-file-esm/.size-limit.cts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + export default [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/cts-config-file/.size-limit.cts b/fixtures/cts-config-file/.size-limit.cts index a086cba0..a759eaa9 100644 --- a/fixtures/cts-config-file/.size-limit.cts +++ b/fixtures/cts-config-file/.size-limit.cts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + module.exports = [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/mts-config-file-cjs/.size-limit.mts b/fixtures/mts-config-file-cjs/.size-limit.mts index a086cba0..a759eaa9 100644 --- a/fixtures/mts-config-file-cjs/.size-limit.mts +++ b/fixtures/mts-config-file-cjs/.size-limit.mts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + module.exports = [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/mts-config-file-esm/.size-limit.mts b/fixtures/mts-config-file-esm/.size-limit.mts index 31503c8c..99ffbf69 100644 --- a/fixtures/mts-config-file-esm/.size-limit.mts +++ b/fixtures/mts-config-file-esm/.size-limit.mts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + export default [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/mts-config-file/.size-limit.mts b/fixtures/mts-config-file/.size-limit.mts index 31503c8c..99ffbf69 100644 --- a/fixtures/mts-config-file/.size-limit.mts +++ b/fixtures/mts-config-file/.size-limit.mts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + export default [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/ts-config-file-cjs/.size-limit.ts b/fixtures/ts-config-file-cjs/.size-limit.ts index a086cba0..a759eaa9 100644 --- a/fixtures/ts-config-file-cjs/.size-limit.ts +++ b/fixtures/ts-config-file-cjs/.size-limit.ts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + module.exports = [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/ts-config-file-esm/.size-limit.ts b/fixtures/ts-config-file-esm/.size-limit.ts index 31503c8c..99ffbf69 100644 --- a/fixtures/ts-config-file-esm/.size-limit.ts +++ b/fixtures/ts-config-file-esm/.size-limit.ts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + export default [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/ts-config-file-with-type-module/.size-limit.ts b/fixtures/ts-config-file-with-type-module/.size-limit.ts index 31503c8c..99ffbf69 100644 --- a/fixtures/ts-config-file-with-type-module/.size-limit.ts +++ b/fixtures/ts-config-file-with-type-module/.size-limit.ts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + export default [ { path: 'index.js' } -] +] satisfies SizeLimitConfig diff --git a/fixtures/ts-config-file/.size-limit.ts b/fixtures/ts-config-file/.size-limit.ts index a086cba0..a759eaa9 100644 --- a/fixtures/ts-config-file/.size-limit.ts +++ b/fixtures/ts-config-file/.size-limit.ts @@ -1,5 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + module.exports = [ { path: 'index.js' } -] +] satisfies SizeLimitConfig From b5971a454a90fa12d55abaf9035bee9653c30e4e Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:11:03 -0500 Subject: [PATCH 13/27] Rename `_jiti` to `buildJiti` in `get-config.js` --- packages/size-limit/get-config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index 95c660c3..d9969744 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -1,6 +1,6 @@ import bytes from 'bytes-iec' import { globby } from 'globby' -import _jiti from 'jiti' +import buildJiti from 'jiti' import { lilconfig } from 'lilconfig' import { createRequire } from 'node:module' import { dirname, isAbsolute, join, relative } from 'node:path' @@ -11,7 +11,7 @@ const require = createRequire(import.meta.url) const __filename = fileURLToPath(import.meta.url) -const jiti = _jiti(__filename, { interopDefault: true }) +const jiti = buildJiti(__filename, { interopDefault: true }) let OPTIONS = { brotli: 'file', From 881cb12913a1d4797f86829d506749dde53aed49 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:17:19 -0500 Subject: [PATCH 14/27] Remove manually defined `__filename` --- packages/size-limit/get-config.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index d9969744..5ca244d5 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -9,9 +9,7 @@ import { SizeLimitError } from './size-limit-error.js' const require = createRequire(import.meta.url) -const __filename = fileURLToPath(import.meta.url) - -const jiti = buildJiti(__filename, { interopDefault: true }) +const jiti = buildJiti(fileURLToPath(import.meta.url), { interopDefault: true }) let OPTIONS = { brotli: 'file', @@ -139,12 +137,12 @@ export default async function getConfig(plugins, process, args, pkg) { } else { let explorer = lilconfig('size-limit', { loaders: { + '.cjs': dynamicImport, + '.cts': tsLoader, '.js': dynamicImport, '.mjs': dynamicImport, - '.cjs': dynamicImport, - '.ts': tsLoader, '.mts': tsLoader, - '.cts': tsLoader + '.ts': tsLoader }, searchPlaces: [ 'package.json', From 15064e6bfa6440017621e92f210ed7c3acd9fda9 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:17:52 -0500 Subject: [PATCH 15/27] Run ESLint fix on all files --- packages/size-limit/test/get-config.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/size-limit/test/get-config.test.js b/packages/size-limit/test/get-config.test.js index d586c992..fb627b1c 100644 --- a/packages/size-limit/test/get-config.test.js +++ b/packages/size-limit/test/get-config.test.js @@ -431,8 +431,8 @@ it('normalizes import', async () => { const allConfigFileExtensions = ['mjs', 'js', 'cjs', 'ts', 'mts', 'cts'] const exportTypes = [ - { moduleType: 'esm', exportSyntax: 'export default' }, - { moduleType: 'cjs', exportSyntax: 'module.exports' } + { exportSyntax: 'export default', moduleType: 'esm' }, + { exportSyntax: 'module.exports', moduleType: 'cjs' } ] describe.each(allConfigFileExtensions)( From 23c47b78123dff64cf7893f66ec0fa5120b0a0ee Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:20:30 -0500 Subject: [PATCH 16/27] Change `Config` from a `type` to an `interface` --- packages/size-limit/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index 399ee49d..eccf3d36 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -1,4 +1,4 @@ -export type Config = { +export interface Config { /** * With `false` it will disable any compression. */ From 105588044076dd8e09c24d953a68812325aeb70e Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:21:50 -0500 Subject: [PATCH 17/27] Rename `Config` interface to `Check` --- packages/size-limit/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index eccf3d36..3f291d12 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -1,4 +1,4 @@ -export interface Config { +export interface Check { /** * With `false` it will disable any compression. */ @@ -89,4 +89,4 @@ export interface Config { webpack?: boolean } -export type SizeLimitConfig = Config[] +export type SizeLimitConfig = Check[] From d778dd817f3c665bf6cc65150639a708e806dd03 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:25:57 -0500 Subject: [PATCH 18/27] Add new lines between properties of `Check` interface --- packages/size-limit/index.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index 3f291d12..bdf6504c 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -13,6 +13,7 @@ export interface Check { * A path to a custom webpack config. */ config?: string + disableModuleConcatenation?: boolean /** @@ -26,7 +27,9 @@ export interface Check { * With `true` it will use Gzip compression and disable Brotli compression. */ gzip?: boolean + hidePassed?: boolean + highlightLess?: boolean /** @@ -34,6 +37,7 @@ export interface Check { * the project size calculation. */ ignore?: string[] + /** * Partial import to test tree-shaking. It could be `"{ lib }"` to test * `import { lib } from 'lib'`, `*` to test all exports, or @@ -47,6 +51,7 @@ export interface Check { * Format: `100 B`, `10 kB`, `500 ms`, `1 s`. */ limit?: string + modifyEsbuildConfig?: (config?: any) => any /** @@ -54,6 +59,7 @@ export interface Check { * changes to the webpack config, like adding a plugin. */ modifyWebpackConfig?: (config?: any) => any + module?: boolean /** From afc19983f7f37a9318adfef0f11ca3ddfcfcc501 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:26:19 -0500 Subject: [PATCH 19/27] Run ESLint fix on all files --- packages/size-limit/get-config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index 5ca244d5..98e4f640 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -5,6 +5,7 @@ import { lilconfig } from 'lilconfig' import { createRequire } from 'node:module' import { dirname, isAbsolute, join, relative } from 'node:path' import { fileURLToPath } from 'node:url' + import { SizeLimitError } from './size-limit-error.js' const require = createRequire(import.meta.url) From c6174ba3152fc999e21d9024b159a379c2c148a4 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:45:38 -0500 Subject: [PATCH 20/27] Add `sizeLimitAPI` function to `index.d.ts` --- packages/size-limit/index.d.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index bdf6504c..d44e8428 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -96,3 +96,22 @@ export interface Check { } export type SizeLimitConfig = Check[] + +/** + * Any function with any arguments. + */ +type AnyFunction = (...args: any[]) => any + +/** + * Run Size Limit and return the result. + * + * @param plugins The list of plugins like `@size-limit/time` + * @param files Path to files or internal config + * @return Project size + */ +declare function sizeLimitAPI( + plugins: AnyFunction, + files: string[] | object +): Promise + +export default sizeLimitAPI From 8a407089b508f2ee6e5182947fa6fa11109ac5af Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 16:51:11 -0500 Subject: [PATCH 21/27] Dynamically import `jiti` - Dynamically import `jiti` to avoid unnecessary initialization for non TS config files --- packages/size-limit/get-config.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index 98e4f640..df18e00f 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -1,6 +1,5 @@ import bytes from 'bytes-iec' import { globby } from 'globby' -import buildJiti from 'jiti' import { lilconfig } from 'lilconfig' import { createRequire } from 'node:module' import { dirname, isAbsolute, join, relative } from 'node:path' @@ -10,8 +9,6 @@ import { SizeLimitError } from './size-limit-error.js' const require = createRequire(import.meta.url) -const jiti = buildJiti(fileURLToPath(import.meta.url), { interopDefault: true }) - let OPTIONS = { brotli: 'file', compareWith: 'webpack', @@ -103,9 +100,14 @@ const dynamicImport = async filePath => (await import(filePath)).default * without pre-compilation. * * @param {string} filePath - The path to the TypeScript file to be loaded. - * @returns {any} The module exports from the loaded TypeScript file. + * @returns {Promise} The module exports from the loaded TypeScript file. */ -const tsLoader = filePath => jiti(filePath) +const tsLoader = async filePath => { + const jiti = (await import('jiti')).default(fileURLToPath(import.meta.url), { + interopDefault: true + }) + return jiti(filePath) +} export default async function getConfig(plugins, process, args, pkg) { let config = { From 42c17574a440b4aea8e7acbcec63e1cb00f1a84c Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 20:33:10 -0500 Subject: [PATCH 22/27] Exclude `.d.ts` files from coverage --- vite.config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vite.config.ts b/vite.config.ts index 6d379915..72198c49 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from 'vitest/config' +import { coverageConfigDefaults, defineConfig } from 'vitest/config' export default defineConfig({ test: { @@ -11,6 +11,7 @@ export default defineConfig({ skipFull: true, clean: true, exclude: [ + ...coverageConfigDefaults.exclude, '**/fixtures', '**/scripts', '**/test', From 453dbdf09f4faa48208ba6acf2988fd3b75c8ba0 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 21:57:09 -0500 Subject: [PATCH 23/27] Replace the deprecated `test.watchExclude` with `server.watch.ignored` --- vite.config.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 72198c49..007661df 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -19,7 +19,7 @@ export default defineConfig({ 'packages/preset-app/index.js' ] }, - testTimeout: 20_000, - watchExclude: ['**/fixtures', '**/dist', '**/out'] - } + testTimeout: 20_000 + }, + server: { watch: { ignored: ['**/fixtures', '**/dist', '**/out'] } } }) From 26903bbcc8e31df97af5a065af80de7ca7a7110f Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 21:58:27 -0500 Subject: [PATCH 24/27] Retry failed tests during CI --- vite.config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vite.config.ts b/vite.config.ts index 007661df..3a0548d5 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -19,7 +19,8 @@ export default defineConfig({ 'packages/preset-app/index.js' ] }, - testTimeout: 20_000 + testTimeout: 20_000, + retry: process.env.CI ? 1 : 0 }, server: { watch: { ignored: ['**/fixtures', '**/dist', '**/out'] } } }) From 37f412e5c5005163cf5bbe1dccd7f76b2cafb7fd Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 22:00:27 -0500 Subject: [PATCH 25/27] Add JSDocs for the `Check` interface --- packages/size-limit/index.d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts index d44e8428..0caa2262 100644 --- a/packages/size-limit/index.d.ts +++ b/packages/size-limit/index.d.ts @@ -1,3 +1,6 @@ +/** + * Represents the options for the size-limit check. + */ export interface Check { /** * With `false` it will disable any compression. From 76348aa241067b5de27ee491b3acaa3e2b341e43 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 22:02:07 -0500 Subject: [PATCH 26/27] Disable `fileParallelism` during CI --- vite.config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vite.config.ts b/vite.config.ts index 3a0548d5..34e7b873 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -20,7 +20,8 @@ export default defineConfig({ ] }, testTimeout: 20_000, - retry: process.env.CI ? 1 : 0 + retry: process.env.CI ? 1 : 0, + fileParallelism: !process.env.CI }, server: { watch: { ignored: ['**/fixtures', '**/dist', '**/out'] } } }) From ee68efb31dfe4ec756dfd59197db2b82a4b7729a Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Tue, 12 Mar 2024 22:06:05 -0500 Subject: [PATCH 27/27] Run ESLint fix on all files --- packages/size-limit/get-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/size-limit/get-config.js b/packages/size-limit/get-config.js index df18e00f..7b4e2c8c 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -103,7 +103,7 @@ const dynamicImport = async filePath => (await import(filePath)).default * @returns {Promise} The module exports from the loaded TypeScript file. */ const tsLoader = async filePath => { - const jiti = (await import('jiti')).default(fileURLToPath(import.meta.url), { + let jiti = (await import('jiti')).default(fileURLToPath(import.meta.url), { interopDefault: true }) return jiti(filePath)