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

allow to provide postcss plugin options as a string #35173

Merged
merged 7 commits into from Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})
})