Skip to content

Commit

Permalink
add test filterModuleRules for next-plugin-storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
ja0n committed Sep 23, 2020
1 parent 8fdb93d commit 8c61308
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/next-plugin-storybook/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,25 @@ async function webpackFinal(config) {
...nextWebpackConfig.resolve,
}

config.module.rules = [
config.module.rules = {
...filterModuleRules(config),
...nextWebpackConfig.module.rules.map((rule) => {
// we need to resolve next-babel-loader since it's not available
// relative with storybook's config
if (rule.use && rule.use.loader === 'next-babel-loader') {
rule.use.loader = require.resolve(
'next/dist/build/webpack/loaders/next-babel-loader'
)
}
return rule
}),
}

return config
}

function filterModuleRules(config) {
return [
...config.module.rules.filter((rule) => {
// the rules we're filtering use RegExp for the test
if (!rule.test instanceof RegExp) return true
Expand All @@ -42,21 +60,10 @@ async function webpackFinal(config) {
}
return true
}),
...nextWebpackConfig.module.rules.map((rule) => {
// we need to resolve next-babel-loader since it's not available
// relative with storybook's config
if (rule.use && rule.use.loader === 'next-babel-loader') {
rule.use.loader = require.resolve(
'next/dist/build/webpack/loaders/next-babel-loader'
)
}
return rule
}),
]

return config
}

module.exports = {
webpackFinal,
filterModuleRules,
}
13 changes: 13 additions & 0 deletions test/unit/webpack-config-overrides.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
/* eslint-env jest */
import { attachReactRefresh } from 'next/dist/build/webpack-config'
import * as storybookPlugin from '../../packages/next-plugin-storybook/preset'

describe('next-plugin-storybook filterModuleRules', () => {
it('should filter module rules correctly', async () => {
const input = {
module: { rules: [{ test: 'babel-loader' }, { test: /.*\.css/ }] },
}
const expected = [{ test: 'babel-loader' }]

const output = storybookPlugin.filterModuleRules(input)
expect(output).toEqual(expected)
})
})

describe('webpack-config attachReactRefresh', () => {
it('should skip adding when unrelated', () => {
Expand Down

0 comments on commit 8c61308

Please sign in to comment.