Unverified Commit 7035a036 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

lazy load postcss plugins (#25317)

Improves startup performance

for vercel/front by ~1s
parent 1f9e958e
...@@ -42,6 +42,23 @@ function isIgnoredPlugin(pluginPath: string): boolean { ...@@ -42,6 +42,23 @@ function isIgnoredPlugin(pluginPath: string): boolean {
return true 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( async function loadPlugin(
dir: string, dir: string,
pluginName: string, pluginName: string,
...@@ -60,13 +77,13 @@ async function loadPlugin( ...@@ -60,13 +77,13 @@ async function loadPlugin(
if (isIgnoredPlugin(pluginPath)) { if (isIgnoredPlugin(pluginPath)) {
return false return false
} else if (options === true) { } else if (options === true) {
return require(pluginPath) return createLazyPostCssPlugin(() => require(pluginPath))
} else { } else {
const keys = Object.keys(options) const keys = Object.keys(options)
if (keys.length === 0) { if (keys.length === 0) {
return require(pluginPath) return createLazyPostCssPlugin(() => require(pluginPath))
} }
return require(pluginPath)(options) return createLazyPostCssPlugin(() => require(pluginPath)(options))
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment