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

lazy load postcss plugins #25317

Merged
merged 1 commit into from
May 21, 2021
Merged
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
23 changes: 20 additions & 3 deletions packages/next/build/webpack/config/blocks/css/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ function isIgnoredPlugin(pluginPath: string): boolean {
return true
}

const createLazyPostCssPlugin = (
fn: () => import('postcss').AcceptedPlugin
): import('postcss').AcceptedPlugin => {
let result: any = undefined
const plugin = (...args: any[]) => {
if (result === undefined) result = fn() as any
if (result.postcss === true) {
return result(...args)
} else if (result.postcss) {
return result.postcss
}
return result
}
plugin.postcss = true
return plugin
}

async function loadPlugin(
dir: string,
pluginName: string,
Expand All @@ -60,13 +77,13 @@ async function loadPlugin(
if (isIgnoredPlugin(pluginPath)) {
return false
} else if (options === true) {
return require(pluginPath)
return createLazyPostCssPlugin(() => require(pluginPath))
} else {
const keys = Object.keys(options)
if (keys.length === 0) {
return require(pluginPath)
return createLazyPostCssPlugin(() => require(pluginPath))
}
return require(pluginPath)(options)
return createLazyPostCssPlugin(() => require(pluginPath)(options))
}
}

Expand Down