Skip to content

Commit

Permalink
Add support .mjs config files (#353)
Browse files Browse the repository at this point in the history
* Add support for loading `.mjs` files in size-limit configuration

* Bump `lilconfig`

* Add `mjs-config-file` fixture

* Add `js-config-file` fixture

* Add tests for `.mjs` and `.js` config files

* Add `dynamicImport` function to help load ESM `.js` config files

* Add `js-config-file-with-type-module` fixture files

* Add test for `.js` config file with "type": "module"

* Format files
  • Loading branch information
aryaemami59 committed Mar 11, 2024
1 parent e70fd90 commit a94908e
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 19 deletions.
5 changes: 5 additions & 0 deletions fixtures/js-config-file-with-type-module/.size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default [
{
path: 'index.js'
}
]
5 changes: 5 additions & 0 deletions fixtures/js-config-file-with-type-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const first = 'first'

const second = 'second'

export { first, second }
10 changes: 10 additions & 0 deletions fixtures/js-config-file-with-type-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"private": true,
"name": "js-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"
}
}
5 changes: 5 additions & 0 deletions fixtures/js-config-file/.size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = [
{
path: 'index.js'
}
]
5 changes: 5 additions & 0 deletions fixtures/js-config-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const first = 'first'

const second = 'second'

export { first, second }
9 changes: 9 additions & 0 deletions fixtures/js-config-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"name": "js-config-file",
"devDependencies": {
"@size-limit/webpack": ">= 0.0.0",
"@size-limit/webpack-why": ">= 0.0.0",
"size-limit": ">= 0.0.0"
}
}
5 changes: 5 additions & 0 deletions fixtures/mjs-config-file/.size-limit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default [
{
path: 'index.js'
}
]
5 changes: 5 additions & 0 deletions fixtures/mjs-config-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const first = 'first'

const second = 'second'

export { first, second }
9 changes: 9 additions & 0 deletions fixtures/mjs-config-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"name": "mjs-config-file",
"devDependencies": {
"@size-limit/webpack": ">= 0.0.0",
"@size-limit/webpack-why": ">= 0.0.0",
"size-limit": ">= 0.0.0"
}
}
14 changes: 14 additions & 0 deletions packages/size-limit/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ function toName(files, cwd) {
return files.map(i => (i.startsWith(cwd) ? relative(cwd, i) : i)).join(', ')
}

/**
* Dynamically imports a module from a given file path
* and returns its default export.
*
* @param {string} filePath - The path to the module file to be imported.
* @returns {Promise<any>} A promise that resolves with the default export of the module.
*/
const dynamicImport = async filePath => (await import(filePath)).default

export default async function getConfig(plugins, process, args, pkg) {
let config = {
cwd: process.cwd()
Expand Down Expand Up @@ -112,11 +121,16 @@ export default async function getConfig(plugins, process, args, pkg) {
config.checks = [{ files: args.files }]
} else {
let explorer = lilconfig('size-limit', {
loaders: {
'.js': dynamicImport,
'.mjs': dynamicImport
},
searchPlaces: [
'package.json',
'.size-limit.json',
'.size-limit',
'.size-limit.js',
'.size-limit.mjs',
'.size-limit.cjs'
]
})
Expand Down
2 changes: 1 addition & 1 deletion packages/size-limit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"bytes-iec": "^3.1.1",
"chokidar": "^3.5.3",
"globby": "^14.0.0",
"lilconfig": "^3.0.0",
"lilconfig": "^3.1.1",
"nanospinner": "^1.1.0",
"picocolors": "^1.0.0"
},
Expand Down
42 changes: 42 additions & 0 deletions packages/size-limit/test/get-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,48 @@ it('normalizes bundle and webpack arguments with --why and ui-reports', async ()
})
})

it('should work with .mjs config file', async () => {
expect(await check('mjs-config-file')).toEqual({
checks: [
{
files: [fixture('mjs-config-file', 'index.js')],
name: 'index.js',
path: 'index.js'
}
],
configPath: '.size-limit.mjs',
cwd: fixture('mjs-config-file')
})
})

it('should work with .js config file', async () => {
expect(await check('js-config-file')).toEqual({
checks: [
{
files: [fixture('js-config-file', 'index.js')],
name: 'index.js',
path: 'index.js'
}
],
configPath: '.size-limit.js',
cwd: fixture('js-config-file')
})
})

it('should work with .js config file and "type": "module"', async () => {
expect(await check('js-config-file-with-type-module')).toEqual({
checks: [
{
files: [fixture('js-config-file-with-type-module', 'index.js')],
name: 'index.js',
path: 'index.js'
}
],
configPath: '.size-limit.js',
cwd: fixture('js-config-file-with-type-module')
})
})

it('uses peerDependencies as ignore option', async () => {
expect(await check('peer')).toEqual({
checks: [
Expand Down
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a94908e

Please sign in to comment.