Skip to content

Commit

Permalink
perf(@angular-devkit/build-angular): only create one instance of post…
Browse files Browse the repository at this point in the history
…css when needed

Prior to this change a postcss instance was created for every component style. This can cause a performance overhead when using tailwindcss, since each tailwind instance would do multiple IO calls to get the list of files specified in the `content` field.

(cherry picked from commit 48b2732)
  • Loading branch information
alan-agius4 committed Nov 21, 2023
1 parent 8072b85 commit d998707
Showing 1 changed file with 15 additions and 5 deletions.
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

0 comments on commit d998707

Please sign in to comment.