Skip to content

Commit a94908e

Browse files
authored
Add support .mjs config files (#353)
* 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
1 parent e70fd90 commit a94908e

File tree

13 files changed

+133
-19
lines changed

13 files changed

+133
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default [
2+
{
3+
path: 'index.js'
4+
}
5+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const first = 'first'
2+
3+
const second = 'second'
4+
5+
export { first, second }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"private": true,
3+
"name": "js-config-file-with-type-module",
4+
"type": "module",
5+
"devDependencies": {
6+
"@size-limit/webpack": ">= 0.0.0",
7+
"@size-limit/webpack-why": ">= 0.0.0",
8+
"size-limit": ">= 0.0.0"
9+
}
10+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = [
2+
{
3+
path: 'index.js'
4+
}
5+
]

fixtures/js-config-file/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const first = 'first'
2+
3+
const second = 'second'
4+
5+
export { first, second }

fixtures/js-config-file/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"private": true,
3+
"name": "js-config-file",
4+
"devDependencies": {
5+
"@size-limit/webpack": ">= 0.0.0",
6+
"@size-limit/webpack-why": ">= 0.0.0",
7+
"size-limit": ">= 0.0.0"
8+
}
9+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default [
2+
{
3+
path: 'index.js'
4+
}
5+
]

fixtures/mjs-config-file/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const first = 'first'
2+
3+
const second = 'second'
4+
5+
export { first, second }

fixtures/mjs-config-file/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"private": true,
3+
"name": "mjs-config-file",
4+
"devDependencies": {
5+
"@size-limit/webpack": ">= 0.0.0",
6+
"@size-limit/webpack-why": ">= 0.0.0",
7+
"size-limit": ">= 0.0.0"
8+
}
9+
}

packages/size-limit/get-config.js

+14
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ function toName(files, cwd) {
8282
return files.map(i => (i.startsWith(cwd) ? relative(cwd, i) : i)).join(', ')
8383
}
8484

85+
/**
86+
* Dynamically imports a module from a given file path
87+
* and returns its default export.
88+
*
89+
* @param {string} filePath - The path to the module file to be imported.
90+
* @returns {Promise<any>} A promise that resolves with the default export of the module.
91+
*/
92+
const dynamicImport = async filePath => (await import(filePath)).default
93+
8594
export default async function getConfig(plugins, process, args, pkg) {
8695
let config = {
8796
cwd: process.cwd()
@@ -112,11 +121,16 @@ export default async function getConfig(plugins, process, args, pkg) {
112121
config.checks = [{ files: args.files }]
113122
} else {
114123
let explorer = lilconfig('size-limit', {
124+
loaders: {
125+
'.js': dynamicImport,
126+
'.mjs': dynamicImport
127+
},
115128
searchPlaces: [
116129
'package.json',
117130
'.size-limit.json',
118131
'.size-limit',
119132
'.size-limit.js',
133+
'.size-limit.mjs',
120134
'.size-limit.cjs'
121135
]
122136
})

packages/size-limit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"bytes-iec": "^3.1.1",
2727
"chokidar": "^3.5.3",
2828
"globby": "^14.0.0",
29-
"lilconfig": "^3.0.0",
29+
"lilconfig": "^3.1.1",
3030
"nanospinner": "^1.1.0",
3131
"picocolors": "^1.0.0"
3232
},

packages/size-limit/test/get-config.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,48 @@ it('normalizes bundle and webpack arguments with --why and ui-reports', async ()
195195
})
196196
})
197197

198+
it('should work with .mjs config file', async () => {
199+
expect(await check('mjs-config-file')).toEqual({
200+
checks: [
201+
{
202+
files: [fixture('mjs-config-file', 'index.js')],
203+
name: 'index.js',
204+
path: 'index.js'
205+
}
206+
],
207+
configPath: '.size-limit.mjs',
208+
cwd: fixture('mjs-config-file')
209+
})
210+
})
211+
212+
it('should work with .js config file', async () => {
213+
expect(await check('js-config-file')).toEqual({
214+
checks: [
215+
{
216+
files: [fixture('js-config-file', 'index.js')],
217+
name: 'index.js',
218+
path: 'index.js'
219+
}
220+
],
221+
configPath: '.size-limit.js',
222+
cwd: fixture('js-config-file')
223+
})
224+
})
225+
226+
it('should work with .js config file and "type": "module"', async () => {
227+
expect(await check('js-config-file-with-type-module')).toEqual({
228+
checks: [
229+
{
230+
files: [fixture('js-config-file-with-type-module', 'index.js')],
231+
name: 'index.js',
232+
path: 'index.js'
233+
}
234+
],
235+
configPath: '.size-limit.js',
236+
cwd: fixture('js-config-file-with-type-module')
237+
})
238+
})
239+
198240
it('uses peerDependencies as ignore option', async () => {
199241
expect(await check('peer')).toEqual({
200242
checks: [

pnpm-lock.yaml

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)