Skip to content

Commit

Permalink
add support for webpack config defined as function
Browse files Browse the repository at this point in the history
  • Loading branch information
lev875 committed Oct 28, 2023
1 parent f9e9a78 commit 41394a3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ async function isDirNotEmpty(dir) {
}
}

async function loadConfig(config) {
return typeof config === 'function' ? config() : config
}

export default [
{
async before(config) {
Expand Down Expand Up @@ -78,7 +82,8 @@ export default [
check.webpackOutput = join(tmpdir(), `size-limit-${nanoid()}`)
}
if (check.config) {
check.webpackConfig = (await import(check.config)).default
let configModule = await import(check.config)
check.webpackConfig = await loadConfig(configModule.default)
convertConfig(check.webpackConfig, config.configPath)
} else {
check.webpackConfig = await getConfig(
Expand Down
12 changes: 12 additions & 0 deletions packages/webpack/test/fixtures/cjs/webpack-func.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'node:path'

module.exports = (argv, env) => ({
entry: {
file: path.join(__dirname, 'file.js'),
small: path.join(__dirname, 'small.js')
},
output: {
filename: '[name].js'
},
mode: 'development'
})
12 changes: 12 additions & 0 deletions packages/webpack/test/fixtures/cjs/webpack-promise.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'node:path'

module.exports = async (argv, env) => ({
entry: {
file: path.join(__dirname, 'file.js'),
small: path.join(__dirname, 'small.js')
},
output: {
filename: '[name].js'
},
mode: 'development'
})
12 changes: 12 additions & 0 deletions packages/webpack/test/fixtures/esm/webpack-func.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'node:path'

export default (argv, env) => ({
entry: {
file: path.join(__dirname, 'file.js'),
small: path.join(__dirname, 'small.js')
},
output: {
filename: '[name].js'
},
mode: 'development'
})
12 changes: 12 additions & 0 deletions packages/webpack/test/fixtures/esm/webpack-promise.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'node:path'

export default async (argv, env) => ({
entry: {
file: path.join(__dirname, 'file.js'),
small: path.join(__dirname, 'small.js')
},
output: {
filename: '[name].js'
},
mode: 'development'
})
40 changes: 40 additions & 0 deletions packages/webpack/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ describe('supports custom webpack config', () => {
})
})

describe('supports custom webpack config defined as function', () => {
it('works with cjs', async () => {
let config = {
checks: [{ config: fixture('cjs/webpack-func.config.js') }],
configPath: ROOT_CONFIG
}
await run(config)
expect(config.checks[0].size).toBe(1160)
})

it('works with esm', async () => {
let config = {
checks: [{ config: fixture('esm/webpack-func.config.js') }],
configPath: ROOT_CONFIG
}
await run(config)
expect(config.checks[0].size).toBe(1605)
})
})

describe('supports custom webpack config defined as async function', () => {
it('works with cjs', async () => {
let config = {
checks: [{ config: fixture('cjs/webpack-promise.config.js') }],
configPath: ROOT_CONFIG
}
await run(config)
expect(config.checks[0].size).toBe(1160)
})

it('works with esm', async () => {
let config = {
checks: [{ config: fixture('esm/webpack-promise.config.js') }],
configPath: ROOT_CONFIG
}
await run(config)
expect(config.checks[0].size).toBe(1605)
})
})

describe('supports custom entry', () => {
it('works with commonjs config', async () => {
let config = {
Expand Down

0 comments on commit 41394a3

Please sign in to comment.