actual-css 0.19.0 → 0.19.1

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 CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.19.1] - 2026-11-13
6
+ ### Changed
7
+ - `actual-css bundle` rewrites relative `url()` references to resolve from the `--out` directory, without copying the assets.
8
+ - `actual-css bundle` refuses relative `url()` in `node_modules` stylesheets and relative `url()` with CSS escapes.
9
+ - `inlineImports(file, { outDir })` applies the same rewrite; without `outDir`, URLs stay as written.
10
+
5
11
  ## [0.19.0] - 2026-11-13
6
12
  ### Added
7
13
  - `.reveal` and `.reveal-group`: one-shot entrance animations that play on first render, with `--reveal-duration`, `--reveal-distance`, and `--reveal-stagger`.
@@ -315,13 +315,29 @@ Use an existing CSS pipeline when these features are required.
315
315
 
316
316
  One further case is refused rather than guessed: a remote import inside a layered subtree, because hoisting it before ordinary rules would move it out of its layer.
317
317
 
318
- The CLI also does not process or rebase asset URLs such as:
318
+ The CLI rewrites relative `url()` references so they still point at the same file from the output's directory. With `--out public/build/app.css`, a stylesheet at `src/fonts/sans.css` that declares:
319
319
 
320
320
  ```css
321
- background-image: url("../images/card.svg");
321
+ src: url("files/sans.woff2");
322
322
  ```
323
323
 
324
- Those paths remain exactly as written, so projects with relative assets should generally let their existing build pipeline handle CSS imports.
324
+ ends up in the bundle as:
325
+
326
+ ```css
327
+ src: url("../../src/fonts/files/sans.woff2");
328
+ ```
329
+
330
+ Only the reference changes: the assets are not copied, so the emitted URL still leads to the source location. The bundle and every referenced file must therefore sit in the tree your server publishes. In the example above, `src/` itself would have to be served; when only `public/` is published, keep the referenced files under `public/` too — fonts in `public/fonts/` referenced from a bundle in `public/build/` come out as `url("../fonts/…")`.
331
+
332
+ Left as written: `data:` and other URLs with a scheme, root-relative (`/fonts/sans.woff2`) and protocol-relative paths, a bare query or fragment (`url(?v=1)`, `url(#clip)`), and anything inside a comment or a string, such as a `url()` nested in a quoted data URI or a `content` value.
333
+
334
+ Refused rather than guessed:
335
+
336
+ - a relative `url()` in a stylesheet under `node_modules`, which is never served: copy those assets into your published tree and reference them there;
337
+ - a relative `url()` containing CSS escapes (`url(a\ b.png)`);
338
+ - a target on another Windows drive than the output, which no relative path can reach.
339
+
340
+ A `url()` split over several lines is not rewritten, and neither are the bare-string URLs of `image-set()`, `-webkit-image-set()` or `src()`: they break when the bundle moves.
325
341
 
326
342
  Remote and absolute imports are preserved rather than fetched and are moved before ordinary rules so they remain valid CSS.
327
343
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "actual-css",
3
- "version": "0.19.0",
3
+ "version": "0.19.1",
4
4
  "description": "Plain CSS component framework with semantic classes, universal variants, small tokens, theme hooks, and progressive enhancements.",
5
5
  "type": "module",
6
6
  "packageManager": "bun@1.4.4",
@@ -20,6 +20,8 @@ function usage() {
20
20
  "bundle: flatten plain CSS @import chains from relative files and actual-css/css/*",
21
21
  "subpaths. A layer or layer(name) import is flattened into the @layer block it",
22
22
  "stands for. Remote imports are kept and hoisted to the top of the bundle.",
23
+ "Relative url() references are rewritten to resolve from the output file; the",
24
+ "assets are not copied, so keep them and the bundle in the served tree.",
23
25
  "Leaves modern CSS syntax untouched; this is a bundler, not a transpiler.",
24
26
  "",
25
27
  "design: export a theme's tokens for Figma, Penpot and a portable DTCG profile,",
@@ -161,7 +163,7 @@ async function contrast({ themeFile, name }) {
161
163
  async function bundle({ input, out, minify }) {
162
164
  const inputPath = resolve(input);
163
165
  const outputPath = resolve(out);
164
- const css = await inlineImports(inputPath);
166
+ const css = await inlineImports(inputPath, { outDir: dirname(outputPath) });
165
167
  const code = minify ? minifyCss(css) : css;
166
168
 
167
169
  await mkdir(dirname(outputPath), { recursive: true });
@@ -6,7 +6,7 @@
6
6
  import { existsSync } from "node:fs";
7
7
  import { readFile } from "node:fs/promises";
8
8
  import { createRequire } from "node:module";
9
- import { dirname, resolve } from "node:path";
9
+ import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
10
10
  import { pathToFileURL } from "node:url";
11
11
 
12
12
  const IMPORT_PREFIX_RE = /^\s*@import\b/;
@@ -16,6 +16,12 @@ const EXTERNAL_SPECIFIER_RE = /^(?:[a-z][a-z0-9+.-]+:|\/\/|\/)/i;
16
16
  const CHARSET_RE = /^\s*@charset\s+["'][^"']*["']\s*;[^\n]*\n?/;
17
17
  const LAYER_TRAILER_RE = /^layer(?:\(\s*([A-Za-z_-][\w-]*(?:\.[A-Za-z_-][\w-]*)*)?\s*\))?$/;
18
18
  const RESOLVER_STUB = "__actual_css_resolve__.js";
19
+ // url() values that stay valid wherever the bundle lands: a scheme (data:,
20
+ // https:; two characters or more, as above), a root- or protocol-relative
21
+ // path, a bare query or fragment (same document).
22
+ const LOCATION_FREE_URL_RE = /^(?:[a-z][a-z0-9+.-]+:|\/|[?#])/i;
23
+ const IDENT_CHAR_RE = /[\w-]/;
24
+ const NODE_MODULES_SEGMENT = `${sep}node_modules${sep}`;
19
25
 
20
26
  function skipWhitespace(source, index) {
21
27
  let next = index;
@@ -157,7 +163,142 @@ function wrapInLayer(css, name) {
157
163
  return `${head}\n${body}\n}\n`;
158
164
  }
159
165
 
160
- async function inlineFile(path, stack, preserved, layered) {
166
+ // Index just past the string opened by the quote at `start` (escapes
167
+ // honoured), or the line end when the string is left open.
168
+ function stringEnd(line, start) {
169
+ const quote = line[start];
170
+
171
+ for (let i = start + 1; i < line.length; i += 1) {
172
+ if (line[i] === "\\") i += 1;
173
+ else if (line[i] === quote) return i + 1;
174
+ }
175
+
176
+ return line.length;
177
+ }
178
+
179
+ // Reads the url() whose "(" sits at `open`: { value, quote, end } with `end`
180
+ // just past ")", or null when the call does not close on this line.
181
+ function readUrlCall(line, open) {
182
+ let i = skipWhitespace(line, open + 1);
183
+ const quote = line[i] === '"' || line[i] === "'" ? line[i] : "";
184
+ let value;
185
+
186
+ if (quote) {
187
+ // A string left open runs to the line end, so no ")" follows and the
188
+ // check below returns null: the slice is never used then.
189
+ const end = stringEnd(line, i);
190
+ value = line.slice(i + 1, end - 1);
191
+ i = skipWhitespace(line, end);
192
+ } else {
193
+ const start = i;
194
+ while (i < line.length && line[i] !== ")" && !/\s/.test(line[i])) {
195
+ i += line[i] === "\\" ? 2 : 1;
196
+ }
197
+ value = line.slice(start, i);
198
+ i = skipWhitespace(line, i);
199
+ }
200
+
201
+ return line[i] === ")" ? { value, quote, end: i + 1 } : null;
202
+ }
203
+
204
+ // The url() value pointing at the same file as `value` written from `file`,
205
+ // seen from `base`; null when it does not depend on the stylesheet location.
206
+ function rebasedUrl(value, file, base) {
207
+ if (value === "" || LOCATION_FREE_URL_RE.test(value)) return null;
208
+
209
+ // Escapes would need decoding before the path means anything: refuse
210
+ // rather than rewrite a path that is not the one the browser reads.
211
+ if (value.includes("\\")) {
212
+ throw new Error(
213
+ `Escaped characters in a relative url() are not supported in ${file}: ${value}`,
214
+ );
215
+ }
216
+ // A package directory is never served: the rewritten reference would only
217
+ // ever 404. Copy such assets into the served tree and reference them there.
218
+ if (file.includes(NODE_MODULES_SEGMENT)) {
219
+ throw new Error(
220
+ `Relative url() in a node_modules stylesheet cannot be served from the bundle (${file}): ${value}`,
221
+ );
222
+ }
223
+
224
+ const cut = value.search(/[?#]/);
225
+ const path = cut === -1 ? value : value.slice(0, cut);
226
+ const suffix = cut === -1 ? "" : value.slice(cut);
227
+ const target = resolve(dirname(file), path);
228
+ const fromBase = relative(base, target);
229
+ // Another Windows drive has no relative path: fail rather than emit a URL
230
+ // the browser can never resolve.
231
+ if (isAbsolute(fromBase)) {
232
+ throw new Error(`Cannot reach ${target} by a relative url() from ${base} (${file})`);
233
+ }
234
+
235
+ const rebased = fromBase.split(sep).join("/");
236
+ return `${rebased.startsWith(".") ? "" : "./"}${rebased}${suffix}`;
237
+ }
238
+
239
+ // Rewrites the relative url() references of one line (see rebasedUrl).
240
+ // Comments and strings are copied verbatim — a url() quoted inside a data URI
241
+ // or a `content` string is text, not a reference; `state.inComment` carries a
242
+ // comment that spans lines. A url() split over several lines is left as is.
243
+ function rebaseUrls(line, file, base, state) {
244
+ let out = "";
245
+ let i = 0;
246
+
247
+ while (i < line.length) {
248
+ if (state.inComment) {
249
+ const close = line.indexOf("*/", i);
250
+ const end = close === -1 ? line.length : close + 2;
251
+ out += line.slice(i, end);
252
+ state.inComment = close === -1;
253
+ i = end;
254
+ continue;
255
+ }
256
+
257
+ const char = line[i];
258
+
259
+ if (char === "/" && line[i + 1] === "*") {
260
+ state.inComment = true;
261
+ out += "/*";
262
+ i += 2;
263
+ continue;
264
+ }
265
+
266
+ if (char === '"' || char === "'") {
267
+ const end = stringEnd(line, i);
268
+ out += line.slice(i, end);
269
+ i = end;
270
+ continue;
271
+ }
272
+
273
+ const call =
274
+ line.slice(i, i + 4).toLowerCase() === "url(" && !IDENT_CHAR_RE.test(line[i - 1] ?? "")
275
+ ? readUrlCall(line, i + 3)
276
+ : null;
277
+ if (!call) {
278
+ out += char;
279
+ i += 1;
280
+ continue;
281
+ }
282
+
283
+ const rebased = rebasedUrl(call.value, file, base);
284
+
285
+ if (rebased === null) {
286
+ out += line.slice(i, call.end);
287
+ i = call.end;
288
+ continue;
289
+ }
290
+
291
+ // Quote whatever an unquoted url() could not hold; escape the quote kept.
292
+ const quote = call.quote || (/[\s"'()]/.test(rebased) ? '"' : "");
293
+ const body = quote ? rebased.replaceAll(quote, `\\${quote}`) : rebased;
294
+ out += `url(${quote}${body}${quote})`;
295
+ i = call.end;
296
+ }
297
+
298
+ return out;
299
+ }
300
+
301
+ async function inlineFile(path, stack, preserved, layered, base) {
161
302
  if (stack.includes(path)) {
162
303
  const chain = [...stack, path].map((item) => item.replace(/.*[/\\]/, ""));
163
304
  throw new Error(`Circular CSS import: ${chain.join(" -> ")}`);
@@ -166,6 +307,7 @@ async function inlineFile(path, stack, preserved, layered) {
166
307
  const css = await readFile(path, "utf8");
167
308
  const nextStack = [...stack, path];
168
309
  const chunks = [];
310
+ const urlState = { inComment: false };
169
311
 
170
312
  for (const line of css.split(/\r?\n/)) {
171
313
  const parsed = parseImportLine(line);
@@ -174,7 +316,7 @@ async function inlineFile(path, stack, preserved, layered) {
174
316
  if (IMPORT_PREFIX_RE.test(line)) {
175
317
  throw new Error(`Unsupported CSS @import syntax in ${path}: ${line.trim()}`);
176
318
  }
177
- chunks.push(line);
319
+ chunks.push(base ? rebaseUrls(line, path, base, urlState) : line);
178
320
  continue;
179
321
  }
180
322
 
@@ -199,16 +341,21 @@ async function inlineFile(path, stack, preserved, layered) {
199
341
  }
200
342
 
201
343
  const target = resolveImportTarget(path, parsed.specifier);
202
- const inlined = await inlineFile(target, nextStack, preserved, layered || layer !== null);
344
+ const inlined = await inlineFile(target, nextStack, preserved, layered || layer !== null, base);
203
345
  chunks.push(layer ? wrapInLayer(inlined, layer.name) : inlined);
204
346
  }
205
347
 
206
348
  return `${chunks.join("\n").trim()}\n`;
207
349
  }
208
350
 
209
- export async function inlineImports(file) {
351
+ // `outDir`: the directory the bundle will be written to. When given, relative
352
+ // url() references are rewritten to keep pointing at their files from there
353
+ // (fonts, images next to an imported stylesheet); without it they are copied
354
+ // as written, which is only right when the bundle sits beside its entry.
355
+ export async function inlineImports(file, { outDir } = {}) {
210
356
  const preserved = [];
211
- const body = await inlineFile(resolve(file), [], preserved, false);
357
+ const base = outDir ? resolve(outDir) : null;
358
+ const body = await inlineFile(resolve(file), [], preserved, false, base);
212
359
 
213
360
  if (preserved.length === 0) return body;
214
361