Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .mjs config files #353

Merged
merged 9 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.