next-style 2.0.0 → 2.0.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.
@@ -65,6 +65,16 @@ export declare class StyleCollector {
65
65
  */
66
66
  getAllStyles(): string;
67
67
  private extractMinWidth;
68
+ /**
69
+ * Writes all collected styles to a temp file so the PostCSS plugin
70
+ * (which runs in a separate process) can read them.
71
+ *
72
+ * Call this after all `css()` / `global()` calls have been evaluated,
73
+ * e.g. at the end of a build-time entry point.
74
+ *
75
+ * @param filePath - Destination file path. Defaults to `os.tmpdir()/next-style.css`.
76
+ */
77
+ flush(filePath?: string): void;
68
78
  /**
69
79
  * Returns a snapshot of the internal style map.
70
80
  * Intended for inspection and testing — not for direct mutation.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/compiler/index.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC7B,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAA;IACjB,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACpC,iEAAiE;IACjE,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACrC,iEAAiE;IACjE,SAAS,EAAE,MAAM,CAAA;IACjB,qDAAqD;IACrD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChC,6CAA6C;IAC7C,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC9B;AAED;;;;;;;;;;GAUG;AACH,qBAAa,cAAc;IAC1B,OAAO,CAAC,MAAM,CAAmC;IAEjD;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,GAAG,GAAG,MAAM;IAW/B;;;;;;OAMG;IACH,cAAc,CAAC,QAAQ,EAAE,MAAM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IAAI,MAAM;IAwBtB,OAAO,CAAC,eAAe;IAKvB;;;;;;;;OAQG;IACH,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAQ9B;;;OAGG;IACH,WAAW;CAGX;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB;;IAI/B;;;;OAIG;+BACwB,GAAG,GAAG,MAAM;EAIxC"}
@@ -193,6 +193,22 @@ export class StyleCollector {
193
193
  const match = query.match(/min-width:\s*(\d+)px/);
194
194
  return match ? parseInt(match[1], 10) : 0;
195
195
  }
196
+ /**
197
+ * Writes all collected styles to a temp file so the PostCSS plugin
198
+ * (which runs in a separate process) can read them.
199
+ *
200
+ * Call this after all `css()` / `global()` calls have been evaluated,
201
+ * e.g. at the end of a build-time entry point.
202
+ *
203
+ * @param filePath - Destination file path. Defaults to `os.tmpdir()/next-style.css`.
204
+ */
205
+ flush(filePath) {
206
+ const fs = require('node:fs');
207
+ const os = require('node:os');
208
+ const path = require('node:path');
209
+ const dest = filePath ?? path.join(os.tmpdir(), 'next-style.css');
210
+ fs.writeFileSync(dest, this.getAllStyles(), 'utf-8');
211
+ }
196
212
  /**
197
213
  * Returns a snapshot of the internal style map.
198
214
  * Intended for inspection and testing — not for direct mutation.
@@ -1,23 +1,12 @@
1
1
  import postcss from 'postcss';
2
- import type { StyleCollector } from '../compiler';
3
- /**
4
- * PostCSS plugin for next-style.
5
- *
6
- * Replaces `@import "next-style";` (or `@import 'next-style';`) in CSS files
7
- * with the compiled CSS collected by the StyleCollector.
8
- *
9
- * The collector is populated by the runtime `css()` / `global()` calls that
10
- * run during the build-time module evaluation (SWC / tsc transform pass).
11
- * At that point all `css({})` calls have been executed and their styles are
12
- * sitting in the collector — the PostCSS plugin just needs to drain it.
13
- *
14
- * Usage in postcss.config.js:
15
- * import nextStylePlugin from 'next-style/plugin'
16
- * export default { plugins: { 'next-style/plugin': {} } }
17
- */
2
+ /** Path to the temp file used as IPC bridge between runtime and PostCSS. */
3
+ export declare const CACHE_FILE: string;
18
4
  interface PluginOptions {
19
- /** Provide a custom collector (useful for testing / server integration). */
20
- collector?: StyleCollector;
5
+ /**
6
+ * Override the cache file path.
7
+ * Defaults to `os.tmpdir()/next-style.css`.
8
+ */
9
+ cacheFile?: string;
21
10
  }
22
11
  declare function nextStylePlugin(opts?: PluginOptions): postcss.Plugin;
23
12
  declare namespace nextStylePlugin {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/postcss-plugin/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAA;AAC7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAIjD;;;;;;;;;;;;;;GAcG;AAEH,UAAU,aAAa;IACtB,4EAA4E;IAC5E,SAAS,CAAC,EAAE,cAAc,CAAA;CAC1B;AAED,iBAAS,eAAe,CAAC,IAAI,GAAE,aAAkB,kBA6ChD;kBA7CQ,eAAe;;;AAiDxB,eAAe,eAAe,CAAA;AAC9B,eAAO,MAAM,MAAM,wBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/postcss-plugin/index.ts"],"names":[],"mappings":"AAGA,OAAO,OAAO,MAAM,SAAS,CAAA;AAoB7B,4EAA4E;AAC5E,eAAO,MAAM,UAAU,QAA2C,CAAA;AAElE,UAAU,aAAa;IACtB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,iBAAS,eAAe,CAAC,IAAI,GAAE,aAAkB,kBAqChD;kBArCQ,eAAe;;;AAyCxB,eAAe,eAAe,CAAA;AAC9B,eAAO,MAAM,MAAM,wBAAkB,CAAA"}
@@ -1,43 +1,56 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
1
4
  import postcss from 'postcss';
5
+ /**
6
+ * PostCSS plugin for next-style.
7
+ *
8
+ * How it works with Turbopack:
9
+ * 1. `css()` / `global()` calls in component files write compiled CSS to a
10
+ * temp file (`os.tmpdir()/next-style.css`) via `styleCollector.flush()`.
11
+ * 2. This plugin reads that temp file and replaces `@import "next-style"`
12
+ * with its contents at PostCSS processing time.
13
+ *
14
+ * Because PostCSS runs in a separate process from the module graph,
15
+ * in-memory collectors cannot be shared. The temp file is the IPC bridge.
16
+ *
17
+ * Usage in postcss.config.js:
18
+ * export default { plugins: { "next-style/plugin": {} } }
19
+ */
2
20
  const IMPORT_RE = /^next-style$/;
21
+ /** Path to the temp file used as IPC bridge between runtime and PostCSS. */
22
+ export const CACHE_FILE = path.join(os.tmpdir(), 'next-style.css');
3
23
  function nextStylePlugin(opts = {}) {
4
- // Lazy-import the runtime collector so the plugin works even when
5
- // the runtime module hasn't been evaluated yet (returns empty string then).
6
- let collector = opts.collector ?? null;
24
+ const cacheFile = opts.cacheFile ?? CACHE_FILE;
7
25
  const plugin = {
8
26
  postcssPlugin: 'next-style',
9
27
  Once(root) {
10
- // Resolve collector from runtime if not provided
11
- if (!collector) {
12
- try {
13
- // Dynamic require so the plugin can be loaded before the
14
- // runtime module is on disk (e.g. during type-check only builds).
15
- // eslint-disable-next-line @typescript-eslint/no-var-requires
16
- const runtime = require('../runtime');
17
- collector = runtime.styleCollector;
18
- }
19
- catch {
20
- // Runtime not available — skip injection silently
21
- return;
22
- }
28
+ // Read compiled CSS from the temp file written by the runtime
29
+ let cssContent = '';
30
+ try {
31
+ cssContent = fs.readFileSync(cacheFile, 'utf-8');
23
32
  }
24
- const cssContent = collector.getAllStyles();
25
- if (!cssContent.trim())
26
- return;
27
- // Walk @import "next-style" / @import 'next-style' declarations
28
- let injected = false;
33
+ catch {
34
+ // Cache file doesn't exist yet (first cold boot before any css() call)
35
+ // — leave the @import in place so PostCSS doesn't error, just remove it
36
+ }
37
+ // Replace @import "next-style" with compiled CSS (or remove if empty)
38
+ let replaced = false;
29
39
  root.walkAtRules('import', atRule => {
30
40
  const val = atRule.params.replace(/['"]/g, '').trim();
31
41
  if (!IMPORT_RE.test(val))
32
42
  return;
33
- const parsed = postcss.parse(cssContent);
34
- atRule.replaceWith(parsed);
35
- injected = true;
43
+ if (cssContent.trim()) {
44
+ atRule.replaceWith(postcss.parse(cssContent));
45
+ }
46
+ else {
47
+ atRule.remove();
48
+ }
49
+ replaced = true;
36
50
  });
37
- // If there was no @import directive, prepend styles at the top
38
- if (!injected) {
39
- const parsed = postcss.parse(cssContent);
40
- root.prepend(parsed);
51
+ // No @import directive prepend at top if there's content
52
+ if (!replaced && cssContent.trim()) {
53
+ root.prepend(postcss.parse(cssContent));
41
54
  }
42
55
  }
43
56
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,CAAA;AAEvD,4FAA4F;AAC5F,KAAK,YAAY,GAAG;KAAG,CAAC,IAAI,OAAO,CAAC,CAAC,EAAE,aAAa;CAAE,CAAA;AAEtD,iFAAiF;AACjF,KAAK,YAAY,GAAG;IACnB,KAAK,CAAC,EAAE,aAAa,CAAA;IACrB,KAAK,CAAC,EAAE,aAAa,CAAA;IACrB,KAAK,CAAC,EAAE,aAAa,CAAA;IACrB,KAAK,CAAC,EAAE,aAAa,CAAA;IACrB,MAAM,CAAC,EAAE,aAAa,CAAA;IACtB,CAAC,KAAK,EAAE,UAAU,MAAM,EAAE,GAAG,aAAa,CAAA;IAC1C,CAAC,SAAS,EAAE,cAAc,MAAM,EAAE,GAAG,aAAa,CAAA;IAClD,CAAC,QAAQ,EAAE,aAAa,MAAM,EAAE,GAAG,aAAa,CAAA;IAChD,CAAC,KAAK,EAAE,UAAU,MAAM,EAAE,GAAG,aAAa,CAAA;IAC1C,CAAC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AA;IACtB,CAAC,KAAK,EAAE,UAAU,MAAM,EAAE,GAAG,aAAa,CAAA;IAC1C,CAAC,SAAS,EAAE,cAAc,MAAM,EAAE,GAAG,aAAa,CAAA;IAClD,CAAC,QAAQ,EAAE,aAAa,MAAM,EAAE,GAAG,aAAa,CAAA;IAChD,CAAC,KAAK,EAAE,UAAU,MAAM,EAAE,GAAG,aAAa,CAAA;IAC1C,CAAC,SAAS,EAAE,cAAc,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;CAClE,CAAA;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,SAAS,GAAG,aAAa,GACpC,YAAY,GACZ,YAAY,GAAG;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAClB,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,GAAG,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAI7C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,IAAI,CAK9D;AAED;;;;;;;GAOG;AACH,OAAO,EAAE,SAAS,IAAI,cAAc,EAAE,CAAA;AAEtC,QAAA,MAAM,SAAS,gBAAuB,CAAA;AAEtC,gDAAgD;AAChD,eAAO,MAAM,aAAa;eACd,MAAM;;CAEjB,CAAA"}
@@ -23,7 +23,9 @@ import { StyleCollector } from '../compiler';
23
23
  * }
24
24
  */
25
25
  export function css(styles) {
26
- return collector.addStyle(styles);
26
+ const className = collector.addStyle(styles);
27
+ collector.flush();
28
+ return className;
27
29
  }
28
30
  /**
29
31
  * Registers global CSS styles applied directly to selectors (no scoped class).
@@ -44,6 +46,7 @@ export function global(styles) {
44
46
  Object.entries(styles).forEach(([selector, styleObj]) => {
45
47
  collector.addGlobalStyle(selector, styleObj);
46
48
  });
49
+ collector.flush();
47
50
  }
48
51
  /**
49
52
  * The shared `StyleCollector` instance.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-style",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Zero-Runtime CSS-in-JS for Next.js + Turbopack",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -41,11 +41,10 @@
41
41
  "@types/bun": "^1.3.14",
42
42
  "@types/node": "^25.7.0",
43
43
  "csstype": "^3.2.3",
44
- "postcss": "^8.5.14",
45
44
  "typescript": "^5"
46
45
  },
47
46
  "peerDependencies": {
48
- "postcss": "^8.4.31"
47
+ "postcss": ">=8.4.31"
49
48
  },
50
49
  "keywords": ["css-in-js", "zero-runtime", "next.js", "turbopack", "postcss", "style", "next-style", "css"],
51
50
  "author": "TiwPhiraphan",