1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311import { describe, it, expect, beforeAll, afterAll } from "vitest";
import path from "node:path";
import os from "node:os";
describe("resolvePostcssStringPlugins", () => {
let resolvePostcssStringPlugins: typeof import("../packages/vinext/src/index.js")["_resolvePostcssStringPlugins"];
beforeAll(async () => {
const mod = await import("../packages/vinext/src/index.js");
resolvePostcssStringPlugins = mod._resolvePostcssStringPlugins;
});
/**
* Creates a temporary project directory with a mock PostCSS plugin
* and a postcss config file. Returns the temp dir path.
*/
async function createTmpProject(
configFileName: string,
configContent: string,
opts?: { mockPluginContent?: string },
): Promise<string> {
const fsp = await import("node:fs/promises");
const dir = await fsp.mkdtemp(path.join(os.tmpdir(), "vinext-postcss-"));
// Create a mock PostCSS plugin that the config can reference
const pluginDir = path.join(dir, "node_modules", "mock-postcss-plugin");
await fsp.mkdir(pluginDir, { recursive: true });
const pluginContent = opts?.mockPluginContent ?? `
module.exports = function mockPlugin(opts) {
return {
postcssPlugin: "mock-postcss-plugin",
Once(root) {},
};
};
module.exports.postcss = true;
`;
await fsp.writeFile(path.join(pluginDir, "index.js"), pluginContent);
await fsp.writeFile(
path.join(pluginDir, "package.json"),
JSON.stringify({ name: "mock-postcss-plugin", version: "1.0.0", main: "index.js" }),
);
// Write the PostCSS config file
await fsp.writeFile(path.join(dir, configFileName), configContent);
return dir;
}
async function cleanupDir(dir: string) {
const fsp = await import("node:fs/promises");
await fsp.rm(dir, { recursive: true, force: true }).catch(() => {});
}
// --- No config file ---
it("returns undefined when no PostCSS config exists", async () => {
const fsp = await import("node:fs/promises");
const dir = await fsp.mkdtemp(path.join(os.tmpdir(), "vinext-postcss-none-"));
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeUndefined();
} finally {
await cleanupDir(dir);
}
});
// --- Array form with string plugin ---
it("resolves string plugin names in array-form postcss.config.cjs", async () => {
const dir = await createTmpProject(
"postcss.config.cjs",
`module.exports = { plugins: ["mock-postcss-plugin"] };`,
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeDefined();
expect(result!.plugins).toHaveLength(1);
// The resolved plugin should be an object (PostCSS plugin instance)
const plugin = result!.plugins[0];
expect(plugin).toBeDefined();
expect(typeof plugin === "object" || typeof plugin === "function").toBe(true);
} finally {
await cleanupDir(dir);
}
});
it("resolves string plugin names in array-form postcss.config.mjs", async () => {
const dir = await createTmpProject(
"postcss.config.mjs",
`export default { plugins: ["mock-postcss-plugin"] };`,
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeDefined();
expect(result!.plugins).toHaveLength(1);
} finally {
await cleanupDir(dir);
}
});
// --- Object form (should be skipped) ---
it("returns undefined for object-form plugins (postcss-load-config handles these)", async () => {
const dir = await createTmpProject(
"postcss.config.cjs",
`module.exports = { plugins: { "mock-postcss-plugin": {} } };`,
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeUndefined();
} finally {
await cleanupDir(dir);
}
});
// --- Array form without string entries (already resolved) ---
it("returns undefined when array plugins contain no strings", async () => {
const dir = await createTmpProject(
"postcss.config.cjs",
`const plugin = require("mock-postcss-plugin");
module.exports = { plugins: [plugin()] };`,
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeUndefined();
} finally {
await cleanupDir(dir);
}
});
// --- Array tuple form: ["plugin-name", { options }] ---
it("resolves array tuple form [name, options]", async () => {
const dir = await createTmpProject(
"postcss.config.cjs",
`module.exports = { plugins: [["mock-postcss-plugin", { foo: "bar" }]] };`,
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeDefined();
expect(result!.plugins).toHaveLength(1);
} finally {
await cleanupDir(dir);
}
});
// --- Mixed array with strings and already-resolved plugins ---
it("handles mixed array with strings and pre-resolved plugins", async () => {
const dir = await createTmpProject(
"postcss.config.cjs",
`const plugin = require("mock-postcss-plugin");
module.exports = { plugins: ["mock-postcss-plugin", plugin()] };`,
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeDefined();
expect(result!.plugins).toHaveLength(2);
} finally {
await cleanupDir(dir);
}
});
// --- Plugin that exports a default function ---
it("calls default export function for string plugin", async () => {
const dir = await createTmpProject(
"postcss.config.cjs",
`module.exports = { plugins: ["mock-postcss-plugin"] };`,
{
mockPluginContent: `
module.exports = function(opts) {
return {
postcssPlugin: "mock-plugin",
Once(root) {},
};
};
module.exports.postcss = true;
`,
},
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeDefined();
expect(result!.plugins).toHaveLength(1);
// Should be the result of calling the function (a plugin object)
expect(result!.plugins[0]).toHaveProperty("postcssPlugin", "mock-plugin");
} finally {
await cleanupDir(dir);
}
});
// --- JSON/YAML configs are skipped ---
it("returns undefined for .postcssrc.json (postcss-load-config handles these)", async () => {
const fsp = await import("node:fs/promises");
const dir = await fsp.mkdtemp(path.join(os.tmpdir(), "vinext-postcss-json-"));
await fsp.writeFile(
path.join(dir, ".postcssrc.json"),
JSON.stringify({ plugins: { autoprefixer: {} } }),
);
try {
const result = await resolvePostcssStringPlugins(dir);
expect(result).toBeUndefined();
} finally {
await cleanupDir(dir);
}
});
// --- Config file priority ---
it("picks postcss.config.js over .postcssrc", async () => {
const fsp = await import("node:fs/promises");
const dir = await createTmpProject(
"postcss.config.js",
`module.exports = { plugins: ["mock-postcss-plugin"] };`,
);
// Also create a .postcssrc with object form
await fsp.writeFile(
path.join(dir, ".postcssrc"),
JSON.stringify({ plugins: { autoprefixer: {} } }),
);
try {
const result = await resolvePostcssStringPlugins(dir);
// postcss.config.js has array-form with string โ should resolve
expect(result).toBeDefined();
expect(result!.plugins).toHaveLength(1);
} finally {
await cleanupDir(dir);
}
});
});
// ---------------------------------------------------------------------------
// Integration test: Vite config hook injects resolved PostCSS
// ---------------------------------------------------------------------------
describe("PostCSS string plugin resolution in Vite config", () => {
let tmpDir: string;
afterAll(async () => {
if (tmpDir) {
const fsp = await import("node:fs/promises");
await fsp.rm(tmpDir, { recursive: true, force: true }).catch(() => {});
}
});
it("injects resolved PostCSS plugins into Vite css.postcss config", async () => {
const fsp = await import("node:fs/promises");
tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "vinext-postcss-int-"));
// Symlink node_modules from project root
const rootNodeModules = path.resolve(import.meta.dirname, "../node_modules");
await fsp.symlink(rootNodeModules, path.join(tmpDir, "node_modules"), "junction");
// Create a mock PostCSS plugin in node_modules
const pluginDir = path.join(tmpDir, "node_modules", "mock-postcss-plugin");
await fsp.mkdir(pluginDir, { recursive: true });
await fsp.writeFile(
path.join(pluginDir, "index.js"),
`module.exports = function(opts) {
return { postcssPlugin: "mock-postcss-plugin", Once(root) {} };
};
module.exports.postcss = true;`,
);
await fsp.writeFile(
path.join(pluginDir, "package.json"),
JSON.stringify({ name: "mock-postcss-plugin", version: "1.0.0", main: "index.js" }),
);
// PostCSS config with array-form string plugin
await fsp.writeFile(
path.join(tmpDir, "postcss.config.cjs"),
`module.exports = { plugins: ["mock-postcss-plugin"] };`,
);
// Minimal pages directory
await fsp.mkdir(path.join(tmpDir, "pages"), { recursive: true });
await fsp.writeFile(
path.join(tmpDir, "pages", "index.tsx"),
`export default function Home() { return <h1>Home</h1>; }`,
);
// Create a Vite server to test the resolved config
const { createServer } = await import("vite");
const vinext = (await import("../packages/vinext/src/index.js")).default;
const server = await createServer({
root: tmpDir,
configFile: false,
plugins: [vinext()],
server: { port: 0 },
logLevel: "silent",
});
try {
// Check the resolved config has css.postcss set
const postcssConfig = server.config.css?.postcss;
expect(postcssConfig).toBeDefined();
expect(typeof postcssConfig).toBe("object");
const postcssObj = postcssConfig as { plugins: any[] };
expect(postcssObj.plugins).toHaveLength(1);
expect(postcssObj.plugins[0]).toHaveProperty("postcssPlugin", "mock-postcss-plugin");
} finally {
await server.close();
}
}, 30000);
});