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

feat(@angular/build): support custom postcss config #28352

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,9 @@ export class StylesheetPluginFactory {
if (!postcssProcessor) {
postcss ??= (await import('postcss')).default;
postcssProcessor = postcss();
for (const [pluginName, pluginOptions] of options.postcssConfiguration.plugins) {
const { default: plugin } = await import(pluginName);
if (typeof plugin !== 'function' || plugin.postcss !== true) {
throw new Error(`Attempted to load invalid Postcss plugin: "${pluginName}"`);
}

postcssProcessor.use(plugin(pluginOptions));
for (const plugin of options.postcssConfiguration.plugins) {
postcssProcessor.use(plugin);
}

postcssProcessors.set(postCssInstanceKey, new WeakRef(postcssProcessor));
}

Expand Down
55 changes: 10 additions & 45 deletions packages/angular/build/src/utils/postcss-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@

import { readFile, readdir } from 'node:fs/promises';
import { join } from 'node:path';
import type { AcceptedPlugin } from 'postcss';

export interface PostcssConfiguration {
plugins: [name: string, options?: object | string][];
plugins: AcceptedPlugin[];
}

interface RawPostcssConfiguration {
plugins?: Record<string, object | boolean | string> | (string | [string, object])[];
}

const postcssConfigurationFiles: string[] = ['postcss.config.json', '.postcssrc.json'];
const postcssConfigurationFiles: string[] = [
'postcss.config.js',
'postcss.config.cjs',
'postcss.config.mjs',
'postcss.config.ts',
];
const tailwindConfigFiles: string[] = [
'tailwind.config.js',
'tailwind.config.cjs',
Expand Down Expand Up @@ -62,15 +68,6 @@ export function findTailwindConfiguration(
return findFile(searchDirectories, tailwindConfigFiles);
}

async function readPostcssConfiguration(
configurationFile: string,
): Promise<RawPostcssConfiguration> {
const data = await readFile(configurationFile, 'utf-8');
const config = JSON.parse(data) as RawPostcssConfiguration;

return config;
}

export async function loadPostcssConfiguration(
searchDirectories: SearchDirectory[],
): Promise<PostcssConfiguration | undefined> {
Expand All @@ -79,45 +76,13 @@ export async function loadPostcssConfiguration(
return undefined;
}

const raw = await readPostcssConfiguration(configPath);
const config = await import(configPath);

// If no plugins are defined, consider it equivalent to no configuration
if (!raw.plugins || typeof raw.plugins !== 'object') {
if (!config.plugins || !Array.isArray(config.plugins)) {
return undefined;
}

// Normalize plugin array form
if (Array.isArray(raw.plugins)) {
if (raw.plugins.length < 1) {
return undefined;
}

const config: PostcssConfiguration = { plugins: [] };
for (const element of raw.plugins) {
if (typeof element === 'string') {
config.plugins.push([element]);
} else {
config.plugins.push(element);
}
}

return config;
}

// Normalize plugin object map form
const entries = Object.entries(raw.plugins);
if (entries.length < 1) {
return undefined;
}

const config: PostcssConfiguration = { plugins: [] };
for (const [name, options] of entries) {
if (!options || (typeof options !== 'object' && typeof options !== 'string')) {
continue;
}

config.plugins.push([name, options]);
}

return config;
}