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

perf(@angular-devkit/build-angular): only create one instance of postcss when needed #26391

Merged
merged 1 commit into from
Nov 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export interface StylesheetLanguage {
): OnLoadResult | Promise<OnLoadResult>;
}

/**
* Cached postcss instances that can be re-used between various StylesheetPluginFactory instances.
*/
const postcssProcessor = new Map<string, WeakRef<import('postcss').Processor>>();

export class StylesheetPluginFactory {
private postcssProcessor?: import('postcss').Processor;

Expand All @@ -94,11 +99,16 @@ export class StylesheetPluginFactory {
}

if (options.tailwindConfiguration) {
postcss ??= (await import('postcss')).default;
const tailwind = await import(options.tailwindConfiguration.package);
this.postcssProcessor = postcss().use(
tailwind.default({ config: options.tailwindConfiguration.file }),
);
const { package: tailwindPackage, file: config } = options.tailwindConfiguration;
const postCssInstanceKey = tailwindPackage + ':' + config;
this.postcssProcessor = postcssProcessor.get(postCssInstanceKey)?.deref();

if (!this.postcssProcessor) {
postcss ??= (await import('postcss')).default;
const tailwind = await import(tailwindPackage);
this.postcssProcessor = postcss().use(tailwind.default({ config }));
postcssProcessor.set(postCssInstanceKey, new WeakRef(this.postcssProcessor));
}
}

return this.postcssProcessor;
Expand Down