Skip to content

Commit

Permalink
allow to provide postcss plugin options as a string (#35173)
Browse files Browse the repository at this point in the history
fixes #35117

@balazsorban44 this needs a test case

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`


Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
  • Loading branch information
sokra and balazsorban44 committed Apr 8, 2022
1 parent 3d4f4c6 commit f4fbb83
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/next/build/webpack/config/blocks/css/plugins.ts
Expand Up @@ -9,7 +9,7 @@ type CssPluginCollection =
| CssPluginCollection_Array
| CssPluginCollection_Object

type CssPluginShape = [string, object | boolean]
type CssPluginShape = [string, object | boolean | string]

const genericErrorText = 'Malformed PostCSS Configuration'

Expand Down Expand Up @@ -62,7 +62,7 @@ const createLazyPostCssPlugin = (
async function loadPlugin(
dir: string,
pluginName: string,
options: boolean | object
options: boolean | object | string
): Promise<import('postcss').AcceptedPlugin | false> {
if (options === false || isIgnoredPlugin(pluginName)) {
return false
Expand All @@ -79,8 +79,7 @@ async function loadPlugin(
} else if (options === true) {
return createLazyPostCssPlugin(() => require(pluginPath))
} else {
const keys = Object.keys(options)
if (keys.length === 0) {
if (typeof options === 'object' && Object.keys(options).length === 0) {
return createLazyPostCssPlugin(() => require(pluginPath))
}
return createLazyPostCssPlugin(() => require(pluginPath)(options))
Expand Down Expand Up @@ -187,7 +186,9 @@ export async function getPostCssPlugins(
const pluginConfig = plugin[1]
if (
typeof pluginName === 'string' &&
(typeof pluginConfig === 'boolean' || typeof pluginConfig === 'object')
(typeof pluginConfig === 'boolean' ||
typeof pluginConfig === 'object' ||
typeof pluginConfig === 'string')
) {
parsed.push([pluginName, pluginConfig])
} else {
Expand Down
54 changes: 54 additions & 0 deletions test/production/postcss-plugin-config-as-string/index.test.ts
@@ -0,0 +1,54 @@
import { createNext } from 'e2e-utils'
import { renderViaHTTP } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'

describe('PostCSS plugin config as string', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'global.css': `
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
`,
'pages/_app.js': `
import "../global.css"
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
`,
'postcss.config.js': `
module.exports = {
plugins: {
'tailwindcss/nesting': 'postcss-nesting',
tailwindcss: {},
},
}
`,
'tailwind.config.js': `
module.exports = {
content: ['./pages/**/*'],
}
`,
},
dependencies: {
'postcss-nesting': '10.1.3',
tailwindcss: '3.0.23',
},
})
})
afterAll(() => next.destroy())

it('should work', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('hello world')
})
})

0 comments on commit f4fbb83

Please sign in to comment.