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..a759eaa9 --- /dev/null +++ b/fixtures/cts-config-file-cjs/.size-limit.cts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +module.exports = [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..99ffbf69 --- /dev/null +++ b/fixtures/cts-config-file-esm/.size-limit.cts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +export default [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..a759eaa9 --- /dev/null +++ b/fixtures/cts-config-file/.size-limit.cts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +module.exports = [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..a759eaa9 --- /dev/null +++ b/fixtures/mts-config-file-cjs/.size-limit.mts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +module.exports = [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..99ffbf69 --- /dev/null +++ b/fixtures/mts-config-file-esm/.size-limit.mts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +export default [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..99ffbf69 --- /dev/null +++ b/fixtures/mts-config-file/.size-limit.mts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +export default [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..a759eaa9 --- /dev/null +++ b/fixtures/ts-config-file-cjs/.size-limit.ts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +module.exports = [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..99ffbf69 --- /dev/null +++ b/fixtures/ts-config-file-esm/.size-limit.ts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +export default [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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..99ffbf69 --- /dev/null +++ b/fixtures/ts-config-file-with-type-module/.size-limit.ts @@ -0,0 +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/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..a759eaa9 --- /dev/null +++ b/fixtures/ts-config-file/.size-limit.ts @@ -0,0 +1,7 @@ +import { SizeLimitConfig } from '../../packages/size-limit' + +module.exports = [ + { + path: 'index.js' + } +] satisfies SizeLimitConfig 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/get-config.js b/packages/size-limit/get-config.js index 6cbd6509..7b4e2c8c 100644 --- a/packages/size-limit/get-config.js +++ b/packages/size-limit/get-config.js @@ -3,6 +3,7 @@ import { globby } from 'globby' 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' @@ -91,6 +92,23 @@ 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 {Promise} The module exports from the loaded TypeScript file. + */ +const tsLoader = async filePath => { + let 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 = { cwd: process.cwd() @@ -122,8 +140,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 + '.mjs': dynamicImport, + '.mts': tsLoader, + '.ts': tsLoader }, searchPlaces: [ 'package.json', @@ -131,7 +153,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()) diff --git a/packages/size-limit/index.d.ts b/packages/size-limit/index.d.ts new file mode 100644 index 00000000..0caa2262 --- /dev/null +++ b/packages/size-limit/index.d.ts @@ -0,0 +1,120 @@ +/** + * Represents the options for the size-limit check. + */ +export interface Check { + /** + * 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 +} + +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 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 */ diff --git a/packages/size-limit/package.json b/packages/size-limit/package.json index 3eed7a24..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", @@ -26,6 +27,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/packages/size-limit/test/get-config.test.js b/packages/size-limit/test/get-config.test.js index 46417811..fb627b1c 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' @@ -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,6 +237,118 @@ it('should work with .js config file and "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: [ + { + 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('works 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('works 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('works 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('works 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('works 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('works 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('works 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 = [ + { exportSyntax: 'export default', moduleType: 'esm' }, + { exportSyntax: 'module.exports', moduleType: 'cjs' } +] + +describe.each(allConfigFileExtensions)( + 'config file with `.%s` extension', + extension => { + it.each(exportTypes)( + 'works 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}`) + }) + } + ) + } +) 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} diff --git a/vite.config.ts b/vite.config.ts index 96f0bfb8..34e7b873 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', @@ -18,7 +19,9 @@ export default defineConfig({ 'packages/preset-app/index.js' ] }, - testTimeout: 10000, - watchExclude: ['**/fixtures', '**/dist', '**/out'] - } + testTimeout: 20_000, + retry: process.env.CI ? 1 : 0, + fileParallelism: !process.env.CI + }, + server: { watch: { ignored: ['**/fixtures', '**/dist', '**/out'] } } })