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

Fix and add test filterModuleRules for next-plugin-storybook #17306

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 20 additions & 13 deletions packages/next-plugin-storybook/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 @@ -43,21 +61,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.unit.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