@stylexswc/nextjs-plugin 0.17.0 → 0.17.1-rc.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.
Files changed (2) hide show
  1. package/README.md +125 -121
  2. package/package.json +50 -36
package/README.md CHANGED
@@ -1,55 +1,47 @@
1
- # Next.js plugin with NAPI-RS StyleX compiler integration
2
-
3
- > Part of the [StyleX SWC Plugin](https://github.com/Dwlad90/stylex-swc-plugin#readme) workspace
4
-
5
- Next.js plugin for an unofficial
6
- [`napi-rs`](https://github.com/dwlad90/stylex-swc-plugin/tree/develop/crates/stylex-rs-compiler)
7
- compiler that includes the StyleX SWC code transformation under the hood.
8
-
9
- ## Overview
10
-
11
- This package combines two solutions to enhance your Next.js development
12
- experience with StyleX:
13
-
14
- ### StyleX SWC Plugin
15
-
16
- - Integrates StyleX with the SWC compiler, potentially leading to faster build
17
- times compared to using Babel.
18
- - Maintains high compatibility with official StyleX tests, ensuring a reliable
19
- experience.
20
- - Integrates seamlessly with Next.js SWC Compiler for a streamlined workflow.
21
-
22
- ### StyleX NAPI-RS Compiler
23
-
24
- - Utilizes NAPI-RS to compile StyleX code, offering advantages over the SWC
25
- plugin approach.
26
- - Provides access to StyleX metadata and source maps, enabling advanced plugin
27
- and tool development.
28
-
29
- ## Why choose this approach?
30
-
31
- - Leverage SWC's speed: Benefit from Next.js's default SWC compiler for
32
- potentially faster build times.
33
- - Maintain StyleX familiarity: The usage of StyleX remains unchanged for
34
- developers.
1
+ # @stylexswc/nextjs-plugin
2
+
3
+ > StyleX plugin for Next.js, powered by a Rust compiler (NAPI-RS + SWC).
4
+ > Supports Webpack, Rspack, and Turbopack builds. Part of the
5
+ > [StyleX SWC Plugin](https://github.com/Dwlad90/stylex-swc-plugin#readme)
6
+ > workspace.
7
+
8
+ This plugin integrates [StyleX](https://stylexjs.com) into Next.js using
9
+ [`@stylexswc/rs-compiler`](https://www.npmjs.com/package/@stylexswc/rs-compiler),
10
+ a Rust implementation of the StyleX transform, instead of the official Babel
11
+ plugin. That means you keep Next.js's fast SWC toolchain — no `.babelrc`, no
12
+ Babel fallback — and your StyleX code stays exactly the same. Per-file
13
+ transforms are 2x to 5x faster than with Babel — see
14
+ [performance](https://github.com/Dwlad90/stylex-swc-plugin#performance).
15
+
16
+ This is a community project and is not affiliated with Meta. It tracks the
17
+ official StyleX releases
18
+ <!-- stylex-compatibility:start -->(currently compatible with StyleX v0.19.0)<!-- stylex-compatibility:end -->,
19
+ requires Node.js 20 or newer, and supports Next.js 15+ (App Router and Pages
20
+ Router).
35
21
 
36
22
  ## Installation
37
23
 
38
- To install the package, run the following command:
39
-
40
24
  ```bash
41
25
  npm install --save-dev @stylexswc/nextjs-plugin
42
26
  ```
43
27
 
28
+ Your application still needs the StyleX runtime:
29
+
30
+ ```bash
31
+ npm install @stylexjs/stylex
32
+ ```
33
+
44
34
  ## Usage
45
35
 
46
- This plugin supports Webpack, Rspack, and Turbopack configurations in Next.js.
36
+ The plugin supports all three Next.js bundlers. Pick the export that matches
37
+ your setup.
47
38
 
48
39
  ### Using with Webpack
49
40
 
50
41
  For standard Next.js Webpack builds, use the default import:
51
42
 
52
- ```javascript
43
+ ```js
44
+ // next.config.js
53
45
  const stylexPlugin = require('@stylexswc/nextjs-plugin');
54
46
 
55
47
  module.exports = stylexPlugin({
@@ -61,11 +53,12 @@ module.exports = stylexPlugin({
61
53
 
62
54
  ### Using with Rspack
63
55
 
64
- For Next.js with [Rspack](https://rspack.rs), use the `/rspack` export. It applies the
65
- experimental [`next-rspack`](https://www.npmjs.com/package/next-rspack) adapter for you —
66
- no manual `withRspack` composition needed:
56
+ For Next.js with [Rspack](https://rspack.rs), use the `/rspack` export. It
57
+ applies the experimental
58
+ [`next-rspack`](https://www.npmjs.com/package/next-rspack) adapter for you — no
59
+ manual `withRspack` composition needed:
67
60
 
68
- ```typescript
61
+ ```ts
69
62
  import stylexPlugin from '@stylexswc/nextjs-plugin/rspack';
70
63
 
71
64
  module.exports = stylexPlugin({
@@ -76,27 +69,33 @@ module.exports = stylexPlugin({
76
69
  ```
77
70
 
78
71
  > [!NOTE]
79
- > Run `next dev` and `next build` without the `--webpack`/`--turbopack` flags. For
80
- > `next start`, set `NEXT_RSPACK=true` in the environment (the production server only
81
- > serves prebuilt output, but it still evaluates the config).
82
-
83
- The Rspack integration extracts StyleX CSS from both Server and Client Components, with
84
- the same options as the Webpack plugin, plus `stylexPackages` from
72
+ > Run `next dev` and `next build` without the `--webpack`/`--turbopack`
73
+ > flags. For `next start`, set `NEXT_RSPACK=true` in the environment (the
74
+ > production server only serves prebuilt output, but it still evaluates the
75
+ > config).
76
+
77
+ The Rspack integration extracts StyleX CSS from both Server and Client
78
+ Components, with the same options as the Webpack plugin, plus `stylexPackages`
79
+ from
85
80
  [`@stylexswc/rspack-plugin`](https://github.com/Dwlad90/stylex-swc-plugin/tree/develop/packages/rspack-plugin).
86
81
 
87
82
  > [!NOTE]
88
- > Packages listed in `transpilePackages` are automatically added to the `stylexPackages`
89
- > allowlist, so StyleX source shipped in `node_modules` (e.g. `@stylexjs/open-props`) is
90
- > picked up without extra configuration.
83
+ > Packages listed in `transpilePackages` are automatically added to the
84
+ > `stylexPackages` allowlist, so StyleX source shipped in `node_modules` (e.g.
85
+ > `@stylexjs/open-props`) is picked up without extra configuration.
91
86
 
92
87
  ### Using with Turbopack
93
88
 
94
89
  > [!IMPORTANT]
95
- > **Turbopack Limitation**: Turbopack does not support webpack plugins ([see Next.js docs](https://nextjs.org/docs/app/api-reference/turbopack#webpack-plugins)). When using Turbopack, the loader only compiles StyleX code but **does not extract CSS**.
90
+ > Turbopack does not support webpack plugins
91
+ > ([see Next.js docs](https://nextjs.org/docs/app/api-reference/turbopack#webpack-plugins)).
92
+ > When using Turbopack, the loader only compiles StyleX code but does not
93
+ > extract CSS.
96
94
  >
97
- > **You must configure the PostCSS plugin for CSS extraction.** Install `@stylexswc/postcss-plugin` and configure it in `postcss.config.js`:
95
+ > You must configure the PostCSS plugin for CSS extraction. Install
96
+ > `@stylexswc/postcss-plugin` and configure it in `postcss.config.js`:
98
97
  >
99
- > ```javascript
98
+ > ```js
100
99
  > // postcss.config.js
101
100
  > module.exports = {
102
101
  > plugins: {
@@ -112,26 +111,22 @@ the same options as the Webpack plugin, plus `stylexPackages` from
112
111
 
113
112
  For Next.js with Turbopack, use the `/turbopack` export:
114
113
 
115
- ```typescript
114
+ ```ts
116
115
  import withStylexTurbopack from '@stylexswc/nextjs-plugin/turbopack';
117
116
 
118
117
  export default withStylexTurbopack({
119
- // StyleX options here same as postcss-plugin
118
+ // StyleX options here, same as postcss-plugin
120
119
  rsOptions: {
121
- dev: process.env.NODE_ENV === 'development',
120
+ dev: process.env.NODE_ENV === 'development',
122
121
  },
123
122
  })({
124
123
  // Next.js config here
125
- experimental: {
126
- turbopack: {
127
- // Additional Turbopack configuration if needed
128
- },
129
- },
130
124
  });
131
125
  ```
132
126
 
133
127
  > [!NOTE]
134
- > When using Turbopack, the following options are not supported and will be ignored:
128
+ > When using Turbopack, the following options are not supported and will
129
+ > be ignored:
135
130
  >
136
131
  > - `useCSSLayers`
137
132
  > - `nextjsMode`
@@ -146,20 +141,20 @@ export default withStylexTurbopack({
146
141
 
147
142
  - Type: `Partial<StyleXOptions>`
148
143
  - Optional
149
- - Description: StyleX compiler options that will be passed to the NAPI-RS
150
- compiler. For standard StyleX options, see the
144
+ - Description: StyleX compiler options passed to `@stylexswc/rs-compiler`. For
145
+ the standard options, see the
151
146
  [official StyleX documentation](https://stylexjs.com/docs/api/configuration/babel-plugin/).
152
147
 
153
148
  > [!NOTE]
154
- > **New Features:** The `include` and `exclude` options are exclusive to this NAPI-RS compiler implementation and are not available in the official StyleX Babel plugin.
149
+ > The `include` and `exclude` options are exclusive to the Rust compiler
150
+ > and are not available in the official StyleX Babel plugin.
155
151
 
156
152
  ##### `rsOptions.include`
157
153
 
158
154
  - Type: `(string | RegExp)[]`
159
155
  - Optional
160
- - Description: **RS-compiler Only** An array of glob patterns or regular
161
- expressions to include specific files for StyleX transformation. When
162
- specified, only files matching at least one of these patterns will be
156
+ - Description: Glob patterns or regular expressions selecting the files to
157
+ transform. When specified, only files matching at least one pattern are
163
158
  transformed. Patterns are matched against paths relative to the current
164
159
  working directory.
165
160
 
@@ -167,31 +162,32 @@ export default withStylexTurbopack({
167
162
 
168
163
  - Type: `(string | RegExp)[]`
169
164
  - Optional
170
- - Description: **RS-compiler Only** An array of glob patterns or regular
171
- expressions to exclude specific files from StyleX transformation. Files
172
- matching any of these patterns will not be transformed, even if they match an
173
- `include` pattern. Patterns are matched against paths relative to the current
174
- working directory.
165
+ - Description: Glob patterns or regular expressions excluding files from the
166
+ transform. A file matching any exclude pattern is skipped even if it matches
167
+ an `include` pattern. Patterns are matched against paths relative to the
168
+ current working directory.
175
169
 
176
170
  #### `stylexImports`
177
171
 
178
172
  - Type: `Array<string | { as: string, from: string }>`
179
173
  - Default: `['stylex', '@stylexjs/stylex']`
180
- - Description: Specifies where StyleX will be imported from. Supports both
181
- string paths and import aliases.
174
+ - Description: Specifies where StyleX is imported from. Supports both string
175
+ paths and import aliases.
182
176
 
183
177
  #### `useCSSLayers`
184
178
 
185
179
  - Type: `UseLayersType`
186
180
  - Default: `false`
187
- - Description: Enables CSS cascade layers support for better style isolation.
181
+ - Description: Wraps the generated CSS in cascade layers for better style
182
+ isolation.
188
183
 
189
184
  #### `extractCSS`
190
185
 
191
186
  - Type: `boolean`
192
187
  - Optional
193
188
  - Default: `true`
194
- - Description: Controls whether CSS should be extracted into a separate file
189
+ - Description: Controls whether the generated CSS is extracted into a separate
190
+ file. Set to `false` when `@stylexswc/postcss-plugin` owns extraction.
195
191
 
196
192
  ### Advanced Options
197
193
 
@@ -200,20 +196,19 @@ export default withStylexTurbopack({
200
196
  - Type:
201
197
  `(css: string, filePath: string | undefined) => string | Buffer | Promise<string | Buffer>`
202
198
  - Optional
203
- - Description: Custom CSS transformation function. Since the plugin injects CSS
204
- after all loaders, use this to apply PostCSS or other CSS transformations.
199
+ - Description: Custom CSS post-processing. The plugin injects CSS after all
200
+ loaders have run, so use this to apply PostCSS or other CSS transformations.
205
201
 
206
202
  ### Example Configuration
207
203
 
208
204
  #### Webpack Configuration
209
205
 
210
- ```javascript
206
+ ```js
211
207
  const path = require('path');
212
208
  const stylexPlugin = require('@stylexswc/nextjs-plugin');
213
209
  const rootDir = __dirname;
214
210
 
215
211
  module.exports = stylexPlugin({
216
- // Add any StyleX options here
217
212
  rsOptions: {
218
213
  dev: process.env.NODE_ENV !== 'production',
219
214
  // Include only specific directories
@@ -252,14 +247,13 @@ module.exports = stylexPlugin({
252
247
 
253
248
  #### Turbopack Configuration
254
249
 
255
- ```typescript
250
+ ```ts
256
251
  import path from 'path';
257
252
  import withStylexTurbopack from '@stylexswc/nextjs-plugin/turbopack';
258
253
 
259
254
  const rootDir = __dirname;
260
255
 
261
256
  export default withStylexTurbopack({
262
- // Add any StyleX options here
263
257
  rsOptions: {
264
258
  dev: process.env.NODE_ENV !== 'production',
265
259
  aliases: {
@@ -272,28 +266,20 @@ export default withStylexTurbopack({
272
266
  stylexImports: ['@stylexjs/stylex'],
273
267
  })({
274
268
  transpilePackages: ['@stylexjs/open-props'],
275
- experimental: {
276
- turbopack: {
277
- // Additional Turbopack configuration if needed
278
- },
279
- },
280
269
  // Optionally, add any other Next.js config below
281
270
  });
282
271
  ```
283
272
 
284
- ##### Required: PostCSS Configuration for CSS Extraction
273
+ Required PostCSS configuration for CSS extraction under Turbopack:
285
274
 
286
- ```javascript
275
+ ```js
287
276
  // postcss.config.js
288
277
  const path = require('path');
289
278
 
290
279
  module.exports = {
291
280
  plugins: {
292
281
  '@stylexswc/postcss-plugin': {
293
- include: [
294
- 'app/**/*.{js,jsx,ts,tsx}',
295
- 'components/**/*.{js,jsx,ts,tsx}',
296
- ],
282
+ include: ['app/**/*.{js,jsx,ts,tsx}', 'components/**/*.{js,jsx,ts,tsx}'],
297
283
  rsOptions: {
298
284
  aliases: {
299
285
  '@/*': [path.join(__dirname, '*')],
@@ -311,9 +297,9 @@ module.exports = {
311
297
 
312
298
  ### Path Filtering Examples
313
299
 
314
- **Include only specific directories:**
300
+ Include only specific directories:
315
301
 
316
- ```javascript
302
+ ```js
317
303
  stylexPlugin({
318
304
  rsOptions: {
319
305
  include: ['app/**/*.tsx', 'components/**/*.tsx'],
@@ -321,19 +307,19 @@ stylexPlugin({
321
307
  });
322
308
  ```
323
309
 
324
- **Exclude test and build files:**
310
+ Exclude test files and API routes:
325
311
 
326
- ```javascript
312
+ ```js
327
313
  stylexPlugin({
328
314
  rsOptions: {
329
- exclude: ['**/*.test.*', '**/*.spec.*', '**/dist/**', '**/node_modules/**'],
315
+ exclude: ['**/*.test.*', '**/*.stories.*', '**/__tests__/**', 'app/api/**'],
330
316
  },
331
317
  });
332
318
  ```
333
319
 
334
- **Using regular expressions:**
320
+ Using regular expressions (exclude always takes precedence over include):
335
321
 
336
- ```javascript
322
+ ```js
337
323
  stylexPlugin({
338
324
  rsOptions: {
339
325
  include: [/app\/.*\.tsx$/, /components\/.*\.tsx$/],
@@ -342,31 +328,19 @@ stylexPlugin({
342
328
  });
343
329
  ```
344
330
 
345
- **Combined include and exclude (exclude takes precedence):**
346
-
347
- ```javascript
348
- stylexPlugin({
349
- rsOptions: {
350
- include: ['app/**/*.{ts,tsx}', 'components/**/*.{ts,tsx}'],
351
- exclude: ['**/__tests__/**', '**/__mocks__/**', 'app/api/**'],
352
- },
353
- });
354
- ```
355
-
356
- **Exclude node_modules except specific packages:**
331
+ Exclude `node_modules` except specific packages (negative lookahead):
357
332
 
358
- ```javascript
333
+ ```js
359
334
  stylexPlugin({
360
335
  rsOptions: {
361
- // Exclude all node_modules except @stylexjs/open-props
362
336
  exclude: [/node_modules(?!\/@stylexjs\/open-props)/],
363
337
  },
364
338
  });
365
339
  ```
366
340
 
367
- **Transform only specific packages from node_modules:**
341
+ Transform only specific packages from `node_modules`:
368
342
 
369
- ```javascript
343
+ ```js
370
344
  stylexPlugin({
371
345
  rsOptions: {
372
346
  include: [
@@ -380,6 +354,35 @@ stylexPlugin({
380
354
  });
381
355
  ```
382
356
 
357
+ ## FAQ
358
+
359
+ ### Which bundler mode should I pick?
360
+
361
+ Webpack (the default export) is the most battle-tested path and supports every
362
+ option. Turbopack gives the fastest dev server but needs the PostCSS plugin for
363
+ CSS extraction. Rspack is experimental in Next.js itself but works end to end
364
+ through the `/rspack` export.
365
+
366
+ ### Do I still need `@stylexjs/babel-plugin` or a `.babelrc`?
367
+
368
+ No — that is the point of this plugin. StyleX is compiled by the Rust compiler
369
+ inside the SWC pipeline, so Next.js never falls back to Babel.
370
+
371
+ ### My styles from `@stylexjs/open-props` (or another library) are missing
372
+
373
+ Add the package to `transpilePackages` in your Next.js config. It is then
374
+ transpiled by Next.js and automatically allowlisted for the StyleX transform.
375
+
376
+ ### Does this work with React Server Components?
377
+
378
+ Yes. Styles are extracted at build time from both Server and Client Components
379
+ into static CSS, so there is no runtime cost either way.
380
+
381
+ ### Is this an official StyleX package?
382
+
383
+ No. It is a community-maintained alternative to the official tooling and is not
384
+ affiliated with or supported by Meta.
385
+
383
386
  ## Examples
384
387
 
385
388
  - [Example repo](https://github.com/Dwlad90/nextjs-app-dir-stylex)
@@ -387,8 +390,8 @@ stylexPlugin({
387
390
 
388
391
  ## Documentation
389
392
 
390
- - [StyleX Documentation](https://stylexjs.com)
391
- - [NAPI-RS compiler for StyleX](https://github.com/Dwlad90/stylex-swc-plugin/tree/develop/crates/stylex-rs-compiler)
393
+ - [StyleX documentation](https://stylexjs.com)
394
+ - [`@stylexswc/rs-compiler` compiler options](https://github.com/Dwlad90/stylex-swc-plugin/tree/develop/crates/stylex-rs-compiler)
392
395
 
393
396
  ## Acknowledgments
394
397
 
@@ -397,4 +400,5 @@ This plugin was inspired by
397
400
 
398
401
  ## License
399
402
 
400
- MIT — see [LICENSE](https://github.com/Dwlad90/stylex-swc-plugin/blob/develop/LICENSE)
403
+ MIT — see
404
+ [LICENSE](https://github.com/Dwlad90/stylex-swc-plugin/blob/develop/LICENSE)
package/package.json CHANGED
@@ -1,29 +1,10 @@
1
1
  {
2
2
  "name": "@stylexswc/nextjs-plugin",
3
- "description": "StyleX NextJS plugin with NAPI-RS compiler",
4
- "version": "0.17.0",
5
- "config": {
6
- "scripty": {
7
- "path": "../../scripts/packages"
8
- }
9
- },
10
- "dependencies": {
11
- "@stylexswc/rs-compiler": "0.17.0",
12
- "@stylexswc/rspack-plugin": "0.17.0",
13
- "@stylexswc/turbopack-plugin": "0.17.0",
14
- "@stylexswc/webpack-plugin": "0.17.0",
15
- "next-rspack": "^16.2.9"
16
- },
17
- "devDependencies": {
18
- "@stylexswc/eslint-config": "0.17.0",
19
- "@stylexswc/typescript-config": "0.17.0",
20
- "@types/node": "^26.1.0",
21
- "next": "^16.2.9",
22
- "postcss": "^8.5.16",
23
- "react": "^19.2.7",
24
- "react-dom": "^19.2.7",
25
- "webpack": "^5.108.3"
26
- },
3
+ "description": "StyleX plugin for Next.js powered by a Rust NAPI-RS/SWC compiler. Supports Webpack, Rspack, and Turbopack builds with CSS extraction.",
4
+ "version": "0.17.1-rc.1",
5
+ "private": false,
6
+ "license": "MIT",
7
+ "sideEffects": false,
27
8
  "exports": {
28
9
  "./turbopack": {
29
10
  "types": "./dist/turbopack.d.ts",
@@ -45,31 +26,64 @@
45
26
  "files": [
46
27
  "dist"
47
28
  ],
29
+ "publishConfig": {
30
+ "registry": "https://registry.npmjs.org/",
31
+ "access": "public"
32
+ },
33
+ "config": {
34
+ "scripty": {
35
+ "path": "../../scripts/packages"
36
+ }
37
+ },
38
+ "dependencies": {
39
+ "@stylexswc/rs-compiler": "0.17.1-rc.1",
40
+ "@stylexswc/rspack-plugin": "0.17.1-rc.1",
41
+ "@stylexswc/turbopack-plugin": "0.17.1-rc.1",
42
+ "@stylexswc/webpack-plugin": "0.17.1-rc.1",
43
+ "next-rspack": "^16.2.9"
44
+ },
45
+ "devDependencies": {
46
+ "@stylexswc/eslint-config": "0.17.1-rc.1",
47
+ "@stylexswc/typescript-config": "0.17.1-rc.1",
48
+ "@types/node": "^26.1.0",
49
+ "next": "^16.2.9",
50
+ "postcss": "^8.5.16",
51
+ "react": "^19.2.7",
52
+ "react-dom": "^19.2.7",
53
+ "webpack": "^5.108.3"
54
+ },
55
+ "peerDependencies": {
56
+ "next": ">=15.0.0",
57
+ "next-rspack": "^16.2.9"
58
+ },
59
+ "bugs": "https://github.com/Dwlad90/stylex-swc-plugin/issues",
60
+ "homepage": "https://github.com/Dwlad90/stylex-swc-plugin/tree/develop/packages/nextjs-plugin#readme",
48
61
  "keywords": [
62
+ "app-router",
63
+ "atomic-css",
64
+ "css-in-js",
65
+ "napi-rs",
49
66
  "next",
50
67
  "nextjs",
51
68
  "nextjs-plugin",
69
+ "react",
70
+ "rspack",
71
+ "rust",
52
72
  "stylex",
53
- "swc"
73
+ "swc",
74
+ "turbopack"
54
75
  ],
55
- "license": "MIT",
56
76
  "main": "dist/index.js",
57
- "peerDependencies": {
58
- "next": ">=15.0.0",
59
- "next-rspack": "^16.2.9"
60
- },
61
77
  "peerDependenciesMeta": {
62
78
  "next-rspack": {
63
79
  "optional": true
64
80
  }
65
81
  },
66
- "private": false,
67
- "publishConfig": {
68
- "registry": "https://registry.npmjs.org/",
69
- "access": "public"
82
+ "repository": {
83
+ "type": "git",
84
+ "url": "git+https://github.com/Dwlad90/stylex-swc-plugin.git",
85
+ "directory": "packages/nextjs-plugin"
70
86
  },
71
- "repository": "https://github.com/Dwlad90/stylex-swc-plugin",
72
- "sideEffects": false,
73
87
  "scripts": {
74
88
  "build": "scripty --ts",
75
89
  "check:artifacts": "scripty",