@ecopages/postcss-processor 0.2.0-alpha.1 → 0.2.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +31 -67
- package/package.json +38 -32
- package/src/index.d.ts +2 -0
- package/src/index.js +2 -0
- package/src/plugin.d.ts +154 -0
- package/src/plugin.js +469 -0
- package/src/postcss-processor.d.ts +48 -0
- package/src/postcss-processor.js +69 -0
- package/src/presets/{index.ts → index.d.ts} +2 -3
- package/src/presets/index.js +6 -0
- package/src/presets/tailwind-v3.d.ts +34 -0
- package/src/presets/tailwind-v3.js +26 -0
- package/src/presets/tailwind-v4.d.ts +47 -0
- package/src/presets/tailwind-v4.js +57 -0
- package/src/runtime/css-loader-plugin.d.ts +9 -0
- package/src/runtime/css-loader-plugin.js +28 -0
- package/src/runtime/css-loader.bun.d.ts +9 -0
- package/src/runtime/css-loader.bun.js +22 -0
- package/src/runtime/{css-runtime-contract.ts → css-runtime-contract.d.ts} +2 -3
- package/src/runtime/css-runtime-contract.js +0 -0
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -354
- package/src/postcss-processor.ts +0 -157
- package/src/presets/tailwind-v3.ts +0 -61
- package/src/presets/tailwind-v4.ts +0 -113
- package/src/runtime/css-loader-plugin.ts +0 -37
- package/src/runtime/css-loader.bun.ts +0 -30
- package/src/test/css/base.css +0 -3
- package/src/test/css/correct.css +0 -3
- package/src/test/css/error.css +0 -3
- package/src/test/css/external-plugins.css +0 -13
- package/src/test/css/import.css +0 -4
- package/src/test/plugin.test.ts +0 -74
- package/src/test/postcss-processor.test.ts +0 -106
- package/src/test/presets.test.ts +0 -140
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tailwind CSS v4 Preset for PostCSS Processor
|
|
3
|
+
* @module @ecopages/postcss-processor/presets/tailwind-v4
|
|
4
|
+
*
|
|
5
|
+
* Requires: @tailwindcss/postcss, cssnano
|
|
6
|
+
* Install: bun add @tailwindcss/postcss cssnano
|
|
7
|
+
*/
|
|
8
|
+
import type { PostCssProcessorPluginConfig } from '../plugin.js';
|
|
9
|
+
/**
|
|
10
|
+
* Options for Tailwind v4 preset
|
|
11
|
+
*/
|
|
12
|
+
export interface TailwindV4PresetOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Absolute path to the main Tailwind CSS file containing `@import "tailwindcss"`.
|
|
15
|
+
* Used to calculate relative @reference paths for CSS files using @apply.
|
|
16
|
+
*/
|
|
17
|
+
referencePath: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a PostCSS processor config preset for Tailwind CSS v4.
|
|
21
|
+
*
|
|
22
|
+
* Features:
|
|
23
|
+
* - Uses `@tailwindcss/postcss` plugin (v4)
|
|
24
|
+
* - Automatically injects `@reference` headers for `@apply` support
|
|
25
|
+
* - Includes cssnano for CSS minification
|
|
26
|
+
* - Returns both `plugins` for immediate use and `pluginFactories` so Ecopages
|
|
27
|
+
* can recreate fresh Tailwind/PostCSS plugin instances on dependency-driven rebuilds
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { postcssProcessorPlugin } from '@ecopages/postcss-processor';
|
|
32
|
+
* import { tailwindV4Preset } from '@ecopages/postcss-processor/presets';
|
|
33
|
+
*
|
|
34
|
+
* // Basic usage
|
|
35
|
+
* postcssProcessorPlugin(tailwindV4Preset({
|
|
36
|
+
* referencePath: path.resolve(import.meta.dir, 'src/styles/tailwind.css'),
|
|
37
|
+
* }))
|
|
38
|
+
*
|
|
39
|
+
* // Extend with additional plugins
|
|
40
|
+
* const preset = tailwindV4Preset({ referencePath });
|
|
41
|
+
* postcssProcessorPlugin({
|
|
42
|
+
* ...preset,
|
|
43
|
+
* plugins: { ...preset.plugins, myPlugin: myPlugin() },
|
|
44
|
+
* })
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function tailwindV4Preset(options: TailwindV4PresetOptions): PostCssProcessorPluginConfig;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import tailwindcss from "@tailwindcss/postcss";
|
|
2
|
+
import autoprefixer from "autoprefixer";
|
|
3
|
+
import browserslist from "browserslist";
|
|
4
|
+
import cssnano from "cssnano";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import postcssImport from "postcss-import";
|
|
7
|
+
import postcssNested from "postcss-nested";
|
|
8
|
+
function tailwindV4Preset(options) {
|
|
9
|
+
const { referencePath } = options;
|
|
10
|
+
const browserslistConfig = browserslist.loadConfig({ path: process.cwd() });
|
|
11
|
+
const autoprefixerOptions = browserslistConfig ? {} : {
|
|
12
|
+
overrideBrowserslist: [">0.3%", "not ie 11", "not dead", "not op_mini all"]
|
|
13
|
+
};
|
|
14
|
+
const pluginFactories = {
|
|
15
|
+
"postcss-import": () => postcssImport(),
|
|
16
|
+
"postcss-nested": () => postcssNested(),
|
|
17
|
+
"@tailwindcss/postcss": () => tailwindcss({ optimize: false }),
|
|
18
|
+
autoprefixer: () => autoprefixer(autoprefixerOptions),
|
|
19
|
+
cssnano: () => cssnano()
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
/**
|
|
23
|
+
* Instantiate the initial plugin list for the active processor instance.
|
|
24
|
+
* Fresh instances can later be recreated from `pluginFactories`.
|
|
25
|
+
*/
|
|
26
|
+
plugins: Object.fromEntries(Object.entries(pluginFactories).map(([name, factory]) => [name, factory()])),
|
|
27
|
+
pluginFactories,
|
|
28
|
+
transformInput: async (contents, filePath) => {
|
|
29
|
+
const css = contents instanceof Buffer ? contents.toString("utf-8") : contents;
|
|
30
|
+
const normalizedFilePath = path.resolve(filePath);
|
|
31
|
+
const normalizedReferencePath = path.resolve(referencePath);
|
|
32
|
+
if (normalizedFilePath === normalizedReferencePath) {
|
|
33
|
+
return css;
|
|
34
|
+
}
|
|
35
|
+
if (/^\s*@reference\s+/m.test(css)) {
|
|
36
|
+
return css;
|
|
37
|
+
}
|
|
38
|
+
const relativePath = path.relative(path.dirname(filePath), referencePath);
|
|
39
|
+
if (css.includes(`@import '${relativePath}'`) || css.includes(`@import "${relativePath}"`)) {
|
|
40
|
+
return css;
|
|
41
|
+
}
|
|
42
|
+
const tailwindImportPattern = /^@import\s+['"]tailwindcss(?:\/[^'"]*)?['"];?\s*$/m;
|
|
43
|
+
if (tailwindImportPattern.test(css)) {
|
|
44
|
+
return css.replace(tailwindImportPattern, `@import '${relativePath}';`);
|
|
45
|
+
}
|
|
46
|
+
if (css.includes("@apply")) {
|
|
47
|
+
return `@reference "${relativePath}";
|
|
48
|
+
|
|
49
|
+
${css}`;
|
|
50
|
+
}
|
|
51
|
+
return css;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
tailwindV4Preset
|
|
57
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EcoBuildPlugin } from '@ecopages/core/build/build-types';
|
|
2
|
+
import type { CssTransform } from './css-runtime-contract.js';
|
|
3
|
+
type CssLoaderOptions = {
|
|
4
|
+
name: string;
|
|
5
|
+
filter: RegExp;
|
|
6
|
+
transform: CssTransform;
|
|
7
|
+
};
|
|
8
|
+
export declare const createCssLoaderPlugin: ({ name, filter, transform }: CssLoaderOptions) => EcoBuildPlugin;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getFileAsBuffer } from "../postcss-processor.js";
|
|
2
|
+
const createCssLoaderPlugin = ({ name, filter, transform }) => ({
|
|
3
|
+
name,
|
|
4
|
+
setup(build) {
|
|
5
|
+
build.onLoad({ filter }, (args) => {
|
|
6
|
+
const rawFile = getFileAsBuffer(args.path);
|
|
7
|
+
const css = transform({
|
|
8
|
+
contents: rawFile,
|
|
9
|
+
filePath: args.path
|
|
10
|
+
});
|
|
11
|
+
if (css instanceof Promise) {
|
|
12
|
+
return css.then((resolved) => ({
|
|
13
|
+
exports: { default: resolved },
|
|
14
|
+
loader: "object"
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
exports: {
|
|
19
|
+
default: css
|
|
20
|
+
},
|
|
21
|
+
loader: "object"
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
export {
|
|
27
|
+
createCssLoaderPlugin
|
|
28
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EcoBuildPlugin } from '@ecopages/core/build/build-types';
|
|
2
|
+
import type { CssTransform } from './css-runtime-contract.js';
|
|
3
|
+
type BunCssLoaderOptions = {
|
|
4
|
+
name: string;
|
|
5
|
+
filter: RegExp;
|
|
6
|
+
transform: CssTransform;
|
|
7
|
+
};
|
|
8
|
+
export declare const createBunCssLoaderPlugin: ({ name, filter, transform }: BunCssLoaderOptions) => EcoBuildPlugin;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getFileAsBuffer } from "../postcss-processor.js";
|
|
2
|
+
const createBunCssLoaderPlugin = ({ name, filter, transform }) => ({
|
|
3
|
+
name,
|
|
4
|
+
setup(build) {
|
|
5
|
+
build.onLoad({ filter }, async (args) => {
|
|
6
|
+
const rawFile = await getFileAsBuffer(args.path);
|
|
7
|
+
const css = await transform({
|
|
8
|
+
contents: rawFile,
|
|
9
|
+
filePath: args.path
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
exports: {
|
|
13
|
+
default: css
|
|
14
|
+
},
|
|
15
|
+
loader: "object"
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
export {
|
|
21
|
+
createBunCssLoaderPlugin
|
|
22
|
+
};
|
|
File without changes
|
package/src/index.ts
DELETED
package/src/plugin.ts
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PostCssProcessorPlugin
|
|
3
|
-
* @module @ecopages/postcss-processor
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import path from 'node:path';
|
|
7
|
-
import type { IClientBridge } from '@ecopages/core';
|
|
8
|
-
import { fileSystem } from '@ecopages/file-system';
|
|
9
|
-
import { Processor, type ProcessorConfig } from '@ecopages/core/plugins/processor';
|
|
10
|
-
import type { EcoBuildPlugin } from '@ecopages/core/build/build-types';
|
|
11
|
-
import { Logger } from '@ecopages/logger';
|
|
12
|
-
import type postcss from 'postcss';
|
|
13
|
-
import { PostCssProcessor } from './postcss-processor';
|
|
14
|
-
import { createCssLoaderPlugin } from './runtime/css-loader-plugin';
|
|
15
|
-
import type { CssTransformInput } from './runtime/css-runtime-contract';
|
|
16
|
-
|
|
17
|
-
const logger = new Logger('[@ecopages/postcss-processor]', {
|
|
18
|
-
debug: process.env.ECOPAGES_LOGGER_DEBUG === 'true',
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Record of PostCSS plugins keyed by name
|
|
23
|
-
*/
|
|
24
|
-
export type PluginsRecord = Record<string, postcss.AcceptedPlugin>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Configuration for the PostCSS processor
|
|
28
|
-
*/
|
|
29
|
-
export interface PostCssProcessorPluginConfig {
|
|
30
|
-
/**
|
|
31
|
-
* Regex filter to match files to process
|
|
32
|
-
*/
|
|
33
|
-
filter?: RegExp;
|
|
34
|
-
/**
|
|
35
|
-
* Function to transform the contents of the file.
|
|
36
|
-
* It can be handy to add a custom header or footer to the file.
|
|
37
|
-
* Useful for injecting Tailwind v4 `@reference` directives.
|
|
38
|
-
* @param contents The contents of the file
|
|
39
|
-
* @param filePath The absolute path to the CSS file being processed
|
|
40
|
-
* @returns The transformed contents
|
|
41
|
-
*/
|
|
42
|
-
transformInput?: (contents: string | Buffer, filePath: string) => string | Promise<string>;
|
|
43
|
-
/**
|
|
44
|
-
* Function to transform the output CSS after PostCSS processing.
|
|
45
|
-
* It can be handy to add a custom header or footer to the processed CSS.
|
|
46
|
-
* @param css The processed CSS
|
|
47
|
-
* @returns The transformed CSS
|
|
48
|
-
*/
|
|
49
|
-
transformOutput?: (css: string) => Promise<string> | string;
|
|
50
|
-
/**
|
|
51
|
-
* Custom PostCSS plugins to use instead of the default ones
|
|
52
|
-
* @default undefined (uses default plugins)
|
|
53
|
-
*/
|
|
54
|
-
plugins?: PluginsRecord;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* PostCssProcessorPlugin
|
|
59
|
-
* A Processor for transforming CSS files.
|
|
60
|
-
*/
|
|
61
|
-
export class PostCssProcessorPlugin extends Processor<PostCssProcessorPluginConfig> {
|
|
62
|
-
static DEFAULT_OPTIONS: Required<Pick<PostCssProcessorPluginConfig, 'filter'>> = {
|
|
63
|
-
filter: /\.css$/,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
private postcssPlugins: postcss.AcceptedPlugin[] = [];
|
|
67
|
-
private readonly runtimeCssCache = new Map<string, string>();
|
|
68
|
-
|
|
69
|
-
private getCssFilter(): RegExp {
|
|
70
|
-
return this.options?.filter ?? PostCssProcessorPlugin.DEFAULT_OPTIONS.filter;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private resolveProcessedCssPath(filePath: string): string | null {
|
|
74
|
-
if (!this.context) {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const relativePath = path.relative(this.context.srcDir, filePath);
|
|
79
|
-
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return path.join(this.context.distDir, 'assets', relativePath);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private readProcessedCssFromDist(filePath: string): string | null {
|
|
87
|
-
const outputPath = this.resolveProcessedCssPath(filePath);
|
|
88
|
-
if (!outputPath || !fileSystem.exists(outputPath)) {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return fileSystem.readFileAsBuffer(outputPath).toString('utf-8');
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
private async persistProcessedCss(filePath: string, css: string): Promise<void> {
|
|
96
|
-
const outputPath = this.resolveProcessedCssPath(filePath);
|
|
97
|
-
if (!outputPath) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
fileSystem.ensureDir(path.dirname(outputPath));
|
|
102
|
-
fileSystem.write(outputPath, css);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private async prewarmRuntimeCssCache(): Promise<void> {
|
|
106
|
-
if (!this.context) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const sourceFiles = await fileSystem.glob(['**/*.{css,scss,sass,less}'], {
|
|
111
|
-
cwd: this.context.srcDir,
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
for (const relativePath of sourceFiles) {
|
|
115
|
-
const filePath = path.join(this.context.srcDir, relativePath);
|
|
116
|
-
if (!this.matchesFileFilter(filePath)) {
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const rawContents = await fileSystem.readFile(filePath);
|
|
121
|
-
let transformedInput = rawContents;
|
|
122
|
-
|
|
123
|
-
if (this.options?.transformInput) {
|
|
124
|
-
transformedInput = await this.options.transformInput(rawContents, filePath);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const processed = await this.process(transformedInput, filePath);
|
|
128
|
-
this.runtimeCssCache.set(filePath, processed);
|
|
129
|
-
await this.persistProcessedCss(filePath, processed);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
private transformCssSync(input: CssTransformInput): string {
|
|
134
|
-
const cached = this.runtimeCssCache.get(input.filePath);
|
|
135
|
-
if (cached) {
|
|
136
|
-
return cached;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const persisted = this.readProcessedCssFromDist(input.filePath);
|
|
140
|
-
if (persisted) {
|
|
141
|
-
this.runtimeCssCache.set(input.filePath, persisted);
|
|
142
|
-
return persisted;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const { contents } = input;
|
|
146
|
-
return typeof contents === 'string' ? contents : contents.toString('utf-8');
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private async transformCssAsync(input: CssTransformInput): Promise<string> {
|
|
150
|
-
const { contents, filePath } = input;
|
|
151
|
-
let transformed: string = typeof contents === 'string' ? contents : contents.toString('utf-8');
|
|
152
|
-
|
|
153
|
-
if (this.options?.transformInput) {
|
|
154
|
-
const result = this.options.transformInput(contents, filePath);
|
|
155
|
-
transformed =
|
|
156
|
-
typeof (result as unknown as Record<string, unknown>).then === 'function'
|
|
157
|
-
? await (result as Promise<string>)
|
|
158
|
-
: (result as string);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const processed = await this.process(transformed, filePath);
|
|
162
|
-
this.runtimeCssCache.set(filePath, processed);
|
|
163
|
-
await this.persistProcessedCss(filePath, processed);
|
|
164
|
-
return processed;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
override matchesFileFilter(filepath: string): boolean {
|
|
168
|
-
const filter = this.options?.filter ?? PostCssProcessorPlugin.DEFAULT_OPTIONS.filter;
|
|
169
|
-
return filter.test(filepath);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
constructor(
|
|
173
|
-
config: Omit<ProcessorConfig<PostCssProcessorPluginConfig>, 'name' | 'description'> = {
|
|
174
|
-
options: PostCssProcessorPlugin.DEFAULT_OPTIONS,
|
|
175
|
-
},
|
|
176
|
-
) {
|
|
177
|
-
super({
|
|
178
|
-
name: 'ecopages-postcss-processor',
|
|
179
|
-
description: 'A Processor for transforming CSS files using PostCSS.',
|
|
180
|
-
capabilities: [
|
|
181
|
-
{
|
|
182
|
-
kind: 'stylesheet',
|
|
183
|
-
extensions: ['*.css'],
|
|
184
|
-
},
|
|
185
|
-
],
|
|
186
|
-
watch: {
|
|
187
|
-
paths: [],
|
|
188
|
-
extensions: ['.css', '.scss', '.sass', '.less'],
|
|
189
|
-
onChange: async ({ path, bridge }) => {
|
|
190
|
-
await this.handleCssChange(path, bridge);
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
...config,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Handles CSS file changes during development.
|
|
199
|
-
* Processes the file and broadcasts a css-update event for hot reloading.
|
|
200
|
-
*/
|
|
201
|
-
private async handleCssChange(filePath: string, bridge: IClientBridge): Promise<void> {
|
|
202
|
-
if (!this.context) return;
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
let content = await fileSystem.readFile(filePath);
|
|
206
|
-
|
|
207
|
-
if (this.options?.transformInput) {
|
|
208
|
-
content = await this.options.transformInput(content, filePath);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const processed = await this.process(content, filePath);
|
|
212
|
-
this.runtimeCssCache.set(filePath, processed);
|
|
213
|
-
await this.persistProcessedCss(filePath, processed);
|
|
214
|
-
|
|
215
|
-
bridge.cssUpdate(filePath);
|
|
216
|
-
|
|
217
|
-
logger.debug(`Processed CSS: ${filePath}`);
|
|
218
|
-
} catch (error) {
|
|
219
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
220
|
-
logger.error(`Failed to process CSS: ${filePath}`, errorMessage);
|
|
221
|
-
bridge.error(errorMessage);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
get buildPlugins(): EcoBuildPlugin[] {
|
|
226
|
-
return [
|
|
227
|
-
createCssLoaderPlugin({
|
|
228
|
-
name: 'postcss-processor-build-loader',
|
|
229
|
-
filter: this.getCssFilter(),
|
|
230
|
-
transform: this.transformCssAsync.bind(this),
|
|
231
|
-
}),
|
|
232
|
-
];
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
get plugins(): EcoBuildPlugin[] {
|
|
236
|
-
return [
|
|
237
|
-
createCssLoaderPlugin({
|
|
238
|
-
name: 'postcss-processor-runtime-loader',
|
|
239
|
-
filter: this.getCssFilter(),
|
|
240
|
-
transform: this.transformCssSync.bind(this),
|
|
241
|
-
}),
|
|
242
|
-
];
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Setup the PostCSS processor.
|
|
247
|
-
*/
|
|
248
|
-
async setup(): Promise<void> {
|
|
249
|
-
await this.collectPostcssPlugins();
|
|
250
|
-
await this.prewarmRuntimeCssCache();
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Get the PostCSS plugins from the options or a config file.
|
|
255
|
-
* Searches for postcss.config.{js,cjs,mjs,ts} in the root directory.
|
|
256
|
-
*/
|
|
257
|
-
private async collectPostcssPlugins(): Promise<void> {
|
|
258
|
-
if (!this.context) {
|
|
259
|
-
throw new Error('Context must be set');
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const configExtensions = ['js', 'cjs', 'mjs', 'ts'];
|
|
263
|
-
let foundConfigPath: string | undefined;
|
|
264
|
-
let loadedPlugins: postcss.AcceptedPlugin[] | undefined;
|
|
265
|
-
|
|
266
|
-
for (const ext of configExtensions) {
|
|
267
|
-
const configPath = path.join(this.context.rootDir, `postcss.config.${ext}`);
|
|
268
|
-
if (fileSystem.exists(configPath)) {
|
|
269
|
-
foundConfigPath = configPath;
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (foundConfigPath) {
|
|
275
|
-
try {
|
|
276
|
-
logger.debug(`Loading PostCSS config from: ${foundConfigPath}`);
|
|
277
|
-
|
|
278
|
-
const postcssConfigModule = await import(foundConfigPath);
|
|
279
|
-
const postcssConfig = postcssConfigModule.default || postcssConfigModule;
|
|
280
|
-
|
|
281
|
-
if (postcssConfig && typeof postcssConfig.plugins === 'object' && postcssConfig.plugins !== null) {
|
|
282
|
-
if (Array.isArray(postcssConfig.plugins)) {
|
|
283
|
-
loadedPlugins = postcssConfig.plugins;
|
|
284
|
-
} else {
|
|
285
|
-
loadedPlugins = Object.values(postcssConfig.plugins as PluginsRecord);
|
|
286
|
-
}
|
|
287
|
-
logger.debug(`Successfully loaded ${loadedPlugins?.length ?? 0} plugins from config file.`);
|
|
288
|
-
} else {
|
|
289
|
-
logger.warn(
|
|
290
|
-
`PostCSS config file found (${foundConfigPath}), but no valid 'plugins' export detected.`,
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
} catch (error: any) {
|
|
294
|
-
logger.error(`Error loading PostCSS config from ${foundConfigPath}: ${error.message}`, error);
|
|
295
|
-
loadedPlugins = undefined;
|
|
296
|
-
}
|
|
297
|
-
} else {
|
|
298
|
-
logger.debug('No PostCSS config file found in root directory.');
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (loadedPlugins) {
|
|
302
|
-
this.postcssPlugins = loadedPlugins;
|
|
303
|
-
} else if (this.options?.plugins) {
|
|
304
|
-
logger.debug('Using PostCSS plugins provided in processor options.');
|
|
305
|
-
this.postcssPlugins = Object.values(this.options.plugins);
|
|
306
|
-
} else {
|
|
307
|
-
logger.warn(
|
|
308
|
-
'No PostCSS plugins configured. Use a preset like tailwindV3Preset() or tailwindV4Preset(), ' +
|
|
309
|
-
'provide plugins via options, or create a postcss.config file.',
|
|
310
|
-
);
|
|
311
|
-
this.postcssPlugins = [];
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
if (!this.postcssPlugins || this.postcssPlugins.length === 0) {
|
|
315
|
-
logger.warn('No PostCSS plugins configured or loaded. CSS processing might be minimal.');
|
|
316
|
-
this.postcssPlugins = [];
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Process CSS content
|
|
322
|
-
* @param fileAsString CSS content as string
|
|
323
|
-
* @param filePath Optional file path for resolving relative imports
|
|
324
|
-
* @returns Processed CSS
|
|
325
|
-
*/
|
|
326
|
-
async process(fileAsString: string, filePath?: string): Promise<string> {
|
|
327
|
-
return await PostCssProcessor.processStringOrBuffer(fileAsString, {
|
|
328
|
-
filePath,
|
|
329
|
-
plugins: this.postcssPlugins,
|
|
330
|
-
transformOutput: this.options?.transformOutput,
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
processSync(fileAsString: string, filePath?: string): string {
|
|
335
|
-
return PostCssProcessor.processStringOrBufferSync(fileAsString, {
|
|
336
|
-
filePath,
|
|
337
|
-
plugins: this.postcssPlugins,
|
|
338
|
-
transformOutput: this.options?.transformOutput,
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Teardown the PostCSS processor.
|
|
344
|
-
*/
|
|
345
|
-
async teardown(): Promise<void> {
|
|
346
|
-
logger.debug('Tearing down PostCSS processor');
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
export const postcssProcessorPlugin = (config?: PostCssProcessorPluginConfig): PostCssProcessorPlugin => {
|
|
351
|
-
return new PostCssProcessorPlugin({
|
|
352
|
-
options: config,
|
|
353
|
-
});
|
|
354
|
-
};
|