rsbuild-plugin-vanilla-extract 0.0.0-stage → 0.1.0
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 +29 -0
- package/LICENSE +21 -0
- package/README.md +155 -2
- package/THIRD_PARTY_NOTICES.md +46 -0
- package/dist/constants.d.ts +18 -0
- package/dist/constants.js +27 -0
- package/dist/css-loader.d.ts +33 -0
- package/dist/css-loader.js +69 -0
- package/dist/css-modules.d.ts +14 -0
- package/dist/css-modules.js +74 -0
- package/dist/css-store.d.ts +14 -0
- package/dist/css-store.js +55 -0
- package/dist/evaluate.d.ts +29 -0
- package/dist/evaluate.js +79 -0
- package/dist/externals.d.ts +63 -0
- package/dist/externals.js +161 -0
- package/dist/file-scope.d.ts +68 -0
- package/dist/file-scope.js +462 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +411 -0
- package/dist/layer-guard.d.ts +8 -0
- package/dist/layer-guard.js +23 -0
- package/dist/lib-css.d.ts +170 -0
- package/dist/lib-css.js +370 -0
- package/dist/loader.d.ts +32 -0
- package/dist/loader.js +217 -0
- package/dist/paths.d.ts +43 -0
- package/dist/paths.js +40 -0
- package/dist/process.d.ts +60 -0
- package/dist/process.js +62 -0
- package/dist/rename.d.ts +20 -0
- package/dist/rename.js +99 -0
- package/dist/runtime/collector.d.ts +8 -0
- package/dist/runtime/collector.js +23 -0
- package/dist/runtime/entry.js +3 -0
- package/dist/runtime/registry.d.ts +9 -0
- package/dist/runtime/registry.js +6 -0
- package/dist/serialize.d.ts +20 -0
- package/dist/serialize.js +142 -0
- package/dist/store.d.ts +49 -0
- package/dist/store.js +55 -0
- package/dist/swc-options.d.ts +12 -0
- package/dist/swc-options.js +76 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +1 -0
- package/package.json +63 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `pluginVanillaExtract()` for `@rsbuild/core` `^2.3.0` and `@vanilla-extract/css` `^1.21.0`.
|
|
8
|
+
Style files are evaluated with Rspack's `importModule` in a layer of the app's compilation,
|
|
9
|
+
so aliases, `source.define`, tsconfig paths, watching and the persistent cache apply. Their
|
|
10
|
+
CSS goes through Rsbuild's CSS pipeline. The only runtime dependency is `@emotion/hash`.
|
|
11
|
+
- Production class names are the same as with `@vanilla-extract/webpack-plugin`.
|
|
12
|
+
Development names come from export names.
|
|
13
|
+
- Options: `identifiers`, `externals` (packages loaded with Node's `require` during
|
|
14
|
+
evaluation), `include` and `exclude` (Rspack rule conditions on the style file's path).
|
|
15
|
+
- Options of the official plugins throw when their value would change the build
|
|
16
|
+
(`outputCss: false`, `emitCssInSsr: true`, a non-default `test`) and warn otherwise.
|
|
17
|
+
- Output of `@vanilla-extract/rollup-plugin` in `node_modules` is left to the normal JS and
|
|
18
|
+
CSS pipeline, so its CSS is kept.
|
|
19
|
+
- Modules imported by style files compile with the parser, decorator, class field and JSX
|
|
20
|
+
settings of the environment's SWC options.
|
|
21
|
+
- HMR with React Fast Refresh, without `splitChunks` or `reactRefreshOptions` workarounds.
|
|
22
|
+
- Asset URLs in style files resolve relative to the source file through css-loader.
|
|
23
|
+
- Node and web-worker environments get class names without CSS unless `output.emitCss`.
|
|
24
|
+
- Rslib bundle and bundleless mode. Bundleless output is `<name>_css.js` and
|
|
25
|
+
`<name>_css.css` per style file, with `<name>_css.d.ts` / `.d.cts` / `.d.mts` declaration
|
|
26
|
+
shims. Tested with Rstest, Rspress, storybook-rsbuild and Modern.js.
|
|
27
|
+
- Build errors for a second registration in one environment and for Module Federation
|
|
28
|
+
`shared` packages imported by style files; a warning for `tools.cssLoader.url: false`.
|
|
29
|
+
- Windows paths.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 YiCChi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rsbuild-plugin-vanilla-extract
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[vanilla-extract](https://vanilla-extract.style) for [Rsbuild](https://rsbuild.rs) 2.
|
|
4
|
+
|
|
5
|
+
Style files are evaluated with Rspack's `importModule` inside the app's own compilation.
|
|
6
|
+
Aliases, `source.define`, tsconfig paths, file watching and the persistent cache apply to
|
|
7
|
+
them, and their CSS goes through Rsbuild's CSS pipeline. The only runtime dependency is
|
|
8
|
+
`@emotion/hash`: no Babel, no esbuild. Production class names are the same as with
|
|
9
|
+
`@vanilla-extract/webpack-plugin`.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add -D rsbuild-plugin-vanilla-extract @vanilla-extract/css
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requires `@rsbuild/core` `^2.3.0`, `@vanilla-extract/css` `^1.21.0` and Node.js `>=22.22.0`.
|
|
18
|
+
`@vanilla-extract/recipes`, `sprinkles` and `dynamic` need no setup.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts file="getting-started/rsbuild.config.ts"
|
|
23
|
+
// rsbuild.config.ts
|
|
24
|
+
import { defineConfig } from '@rsbuild/core';
|
|
25
|
+
import { pluginVanillaExtract } from 'rsbuild-plugin-vanilla-extract';
|
|
26
|
+
|
|
27
|
+
export default defineConfig({
|
|
28
|
+
plugins: [pluginVanillaExtract()],
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```ts file="getting-started/src/button.css.ts"
|
|
33
|
+
// src/button.css.ts
|
|
34
|
+
import { style } from '@vanilla-extract/css';
|
|
35
|
+
|
|
36
|
+
export const button = style({
|
|
37
|
+
padding: '8px 16px',
|
|
38
|
+
border: 'none',
|
|
39
|
+
borderRadius: 6,
|
|
40
|
+
color: 'white',
|
|
41
|
+
background: 'rebeccapurple',
|
|
42
|
+
':hover': { background: 'indigo' },
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Import `button` from `./button.css` in the app. The class name is `button_button__<hash>` in
|
|
47
|
+
development and the hash alone in production.
|
|
48
|
+
|
|
49
|
+
Style files are the files matching `/\.css\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts)$/`. Register the
|
|
50
|
+
plugin once per environment, in `plugins` or in `environments.<name>.plugins`; a second
|
|
51
|
+
registration for the same environment is an error. Environments with `target: 'node'` or
|
|
52
|
+
`'web-worker'` get class names without CSS unless `output.emitCss` is `true`, so server and
|
|
53
|
+
client markup match.
|
|
54
|
+
|
|
55
|
+
## Options
|
|
56
|
+
|
|
57
|
+
| Option | Type | Default |
|
|
58
|
+
| ------------- | ------------------------------------------------------------------ | ------------------------------------------------- |
|
|
59
|
+
| `identifiers` | `'short' \| 'debug' \| ((args: IdentifierFunctionArgs) => string)` | `'debug'` in development, `'short'` in production |
|
|
60
|
+
| `externals` | `string \| RegExp \| Array<string \| RegExp>` | none |
|
|
61
|
+
| `include` | `Rspack.RuleSetCondition` | every style file, `node_modules` included |
|
|
62
|
+
| `exclude` | `Rspack.RuleSetCondition` | none |
|
|
63
|
+
|
|
64
|
+
`identifiers` takes the values of vanilla-extract's own option. `'short'` gives the class
|
|
65
|
+
names of the official plugins: the hash comes from the package name (the closest
|
|
66
|
+
`package.json` with a `name` at or above the project root) and the path relative to the root.
|
|
67
|
+
A function receives:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
interface IdentifierFunctionArgs {
|
|
71
|
+
hash: string; // the 'short' identifier
|
|
72
|
+
debugId?: string; // an id vanilla-extract has itself: style({}, 'id'), a styleVariants key, a recipe variant
|
|
73
|
+
filePath: string; // the style file, relative to the project root, posix
|
|
74
|
+
packageName?: string; // '' when no package.json has a name
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Its result must match `/^[A-Z_][0-9A-Z_-]+$/i`. It enters the persistent cache digest by its
|
|
79
|
+
source text, so it must not read outside state when `performance.buildCache` is on.
|
|
80
|
+
|
|
81
|
+
`externals` lists packages that style files load with Node's `require` while they run,
|
|
82
|
+
instead of having them bundled for evaluation. Use it for packages that read files next to
|
|
83
|
+
their own code or load native addons. A string must equal the request; a RegExp is tested
|
|
84
|
+
against it. They resolve from the project root. This option affects evaluation only; to keep
|
|
85
|
+
a package out of the build output, use Rsbuild's `output.externals`.
|
|
86
|
+
|
|
87
|
+
`include` and `exclude` are Rspack rule conditions on the path of the style files the plugin
|
|
88
|
+
compiles where the app imports them; the others go through the normal JS pipeline.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { pluginVanillaExtract } from 'rsbuild-plugin-vanilla-extract';
|
|
92
|
+
|
|
93
|
+
pluginVanillaExtract({
|
|
94
|
+
include: /[\\/]src[\\/]/,
|
|
95
|
+
exclude: /[\\/]node_modules[\\/]legacy-ui[\\/]/,
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
A style file outside `include` still runs with its own file scope when an evaluated style
|
|
100
|
+
file imports it. The plugin treats a file matching `exclude` as output that another tool has
|
|
101
|
+
already compiled. When a style file imports it, it runs without a file scope, and the
|
|
102
|
+
relative `.css` files it imports are added to the CSS of that style file (except in Rslib
|
|
103
|
+
bundleless builds). Output of `@vanilla-extract/rollup-plugin` is detected without
|
|
104
|
+
`exclude`.
|
|
105
|
+
|
|
106
|
+
Options of the official plugins are checked. `outputCss: false`, `emitCssInSsr: true` and a
|
|
107
|
+
`test` other than the default throw: use `output.emitCss` and `include` / `exclude`. Other
|
|
108
|
+
values of these keys, `esbuildOptions`, `processCss`, `allowRuntime`, `runtime`,
|
|
109
|
+
`unstable_mode` and unknown keys print a warning and are ignored.
|
|
110
|
+
|
|
111
|
+
Besides `pluginVanillaExtract`, the package exports `PLUGIN_NAME` (`'rsbuild:vanilla-extract'`)
|
|
112
|
+
and the types `PluginVanillaExtractOptions`, `IdentifierOption`, `IdentifierFunctionArgs` and
|
|
113
|
+
`LayerExternalsOption`.
|
|
114
|
+
|
|
115
|
+
## Other Rstack tools
|
|
116
|
+
|
|
117
|
+
| Tool | Where to register the plugin |
|
|
118
|
+
| ----------------- | -------------------------------------------------------------------------------------- |
|
|
119
|
+
| Rslib | `plugins` in `rslib.config.ts`, with `output.target: 'web'` |
|
|
120
|
+
| Rspress | `builderConfig.plugins` in `rspress.config.ts` |
|
|
121
|
+
| Modern.js | `builderPlugins` in `modern.config.ts` |
|
|
122
|
+
| storybook-rsbuild | the project's `rsbuild.config.ts`, or `rsbuildFinal` in `.storybook/main.ts`, not both |
|
|
123
|
+
| Rstest | `plugins` in `rstest.config.ts` |
|
|
124
|
+
|
|
125
|
+
Each tool is built through its CLI in this repository's tests. Configurations, and CSS in
|
|
126
|
+
Rstest DOM tests: [integrations](https://github.com/YiCChi/rsbuild-plugin-vanilla-extract/blob/main/docs/integrations.md).
|
|
127
|
+
Component libraries with Rslib, bundleless output included:
|
|
128
|
+
[Rslib](https://github.com/YiCChi/rsbuild-plugin-vanilla-extract/blob/main/docs/rslib.md).
|
|
129
|
+
|
|
130
|
+
## Known limits
|
|
131
|
+
|
|
132
|
+
- Development class names are named after exports, not variables. A value no export holds
|
|
133
|
+
keeps the file name only (`card__…` instead of `card_internal__…`). Production names are
|
|
134
|
+
not affected. All differences from the official plugin:
|
|
135
|
+
[differences](https://github.com/YiCChi/rsbuild-plugin-vanilla-extract/blob/main/docs/differences.md).
|
|
136
|
+
- Modules that style files import are compiled by the plugin's own rules, not by your
|
|
137
|
+
loaders. TypeScript, and JavaScript outside `node_modules`, go through SWC with the parser,
|
|
138
|
+
decorator, class field and JSX settings of your config, without SWC plugins or browser
|
|
139
|
+
targets. JavaScript in `node_modules` runs as it is. JSON is JSON.
|
|
140
|
+
Anything else is an asset: while the style file runs, its default export is a placeholder
|
|
141
|
+
string (the query, such as `?react`, is dropped) and its named exports are `undefined`.
|
|
142
|
+
- `import './reset.css'` inside a style file does not reach the output: import plain CSS
|
|
143
|
+
from the app. Top-level `await` in style files is not supported.
|
|
144
|
+
- Style files cannot import Module Federation `shared` packages.
|
|
145
|
+
- With Rspack 2.3, each style edit reloads every stylesheet of the page, and two updates
|
|
146
|
+
within 50 ms can end in a full reload. The webpack plugin does the same.
|
|
147
|
+
|
|
148
|
+
Moving from the webpack, Vite or Rollup plugin:
|
|
149
|
+
[migration](https://github.com/YiCChi/rsbuild-plugin-vanilla-extract/blob/main/docs/migration.md).
|
|
150
|
+
Error messages and fixes:
|
|
151
|
+
[troubleshooting](https://github.com/YiCChi/rsbuild-plugin-vanilla-extract/blob/main/docs/troubleshooting.md).
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
MIT. Parts are ported from [vanilla-extract](https://github.com/vanilla-extract-css/vanilla-extract)
|
|
156
|
+
(MIT, Copyright (c) 2021 SEEK); their notices are in `THIRD_PARTY_NOTICES.md` in this package.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
This package contains code derived from the following projects.
|
|
4
|
+
|
|
5
|
+
## vanilla-extract
|
|
6
|
+
|
|
7
|
+
https://github.com/vanilla-extract-css/vanilla-extract
|
|
8
|
+
|
|
9
|
+
Derived parts:
|
|
10
|
+
|
|
11
|
+
- `src/serialize.ts`: port of `serializeVanillaModule` (`packages/integration/src/processVanillaFile.ts`).
|
|
12
|
+
- `src/file-scope.ts`: port of `addFileScope` (`packages/integration/src/addFileScope.ts`).
|
|
13
|
+
- `src/process.ts`, `src/loader.ts`: CSS grouping by file scope and unused-composition handling of
|
|
14
|
+
`processVanillaFile` (`packages/integration/src`).
|
|
15
|
+
- `src/rename.ts`: `debugFileName`, a copy of `getDebugFileName` (`packages/css/src/getDebugFileName.ts`).
|
|
16
|
+
- Tests marked `[upstream]` and the tests citing an upstream file are adapted from the vanilla-extract test suite.
|
|
17
|
+
|
|
18
|
+
MIT License
|
|
19
|
+
|
|
20
|
+
Copyright (c) 2021 SEEK
|
|
21
|
+
|
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
23
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
24
|
+
in the Software without restriction, including without limitation the rights
|
|
25
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
26
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
27
|
+
furnished to do so, subject to the following conditions:
|
|
28
|
+
|
|
29
|
+
The above copyright notice and this permission notice shall be included in all
|
|
30
|
+
copies or substantial portions of the Software.
|
|
31
|
+
|
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
33
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
34
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
35
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
37
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
38
|
+
SOFTWARE.
|
|
39
|
+
|
|
40
|
+
## mlly
|
|
41
|
+
|
|
42
|
+
https://github.com/unjs/mlly
|
|
43
|
+
|
|
44
|
+
`src/file-scope.ts`: the ESM / CommonJS detection patterns follow mlly's `detectSyntax`
|
|
45
|
+
(which `@vanilla-extract/integration` uses), under the MIT License, Copyright (c) Pooya Parsa
|
|
46
|
+
<pooya@pi0.io>. The license text is the same as above.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const PLUGIN_NAME = "rsbuild:vanilla-extract";
|
|
2
|
+
export declare const LAYER = "vanilla-extract";
|
|
3
|
+
export declare const RUNTIME_DIR: string;
|
|
4
|
+
export declare const ENTRY: string;
|
|
5
|
+
export declare const REGISTRY: string;
|
|
6
|
+
export declare const COLLECTOR: string;
|
|
7
|
+
export declare const LOADER: string;
|
|
8
|
+
export declare const CSS_LOADER: string;
|
|
9
|
+
export declare const LIB_CSS_LOADER: string;
|
|
10
|
+
export declare const RUNTIME_DIR_RE: RegExp;
|
|
11
|
+
export declare const ENTRY_RE: RegExp;
|
|
12
|
+
/** The CSS loader in a request or module identifier, followed by its query. */
|
|
13
|
+
export declare const CSS_LOADER_QUERY_RE: RegExp;
|
|
14
|
+
export declare const STORE_NAME = "__RSBUILD_VANILLA_EXTRACT__";
|
|
15
|
+
export declare const STORE = "globalThis.__RSBUILD_VANILLA_EXTRACT__";
|
|
16
|
+
export declare const CSS_TS: RegExp;
|
|
17
|
+
export declare const JS: RegExp;
|
|
18
|
+
export declare const VIRTUAL_CSS: RegExp;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { pathPattern } from "./paths.js";
|
|
4
|
+
const here = node_path.dirname(fileURLToPath(import.meta.url));
|
|
5
|
+
const resolveHere = (relative)=>node_path.join(here, relative);
|
|
6
|
+
const PLUGIN_NAME = 'rsbuild:vanilla-extract';
|
|
7
|
+
const LAYER = 'vanilla-extract';
|
|
8
|
+
const RUNTIME_DIR = resolveHere('./runtime');
|
|
9
|
+
const ENTRY = resolveHere('./runtime/entry.js');
|
|
10
|
+
const REGISTRY = resolveHere('./runtime/registry.js');
|
|
11
|
+
const COLLECTOR = resolveHere('./runtime/collector.js');
|
|
12
|
+
const LOADER = resolveHere('./loader.js');
|
|
13
|
+
const CSS_LOADER = resolveHere('./css-loader.js');
|
|
14
|
+
const LIB_CSS_LOADER = resolveHere('./lib-css.js');
|
|
15
|
+
const RUNTIME_DIR_RE = pathPattern(RUNTIME_DIR, {
|
|
16
|
+
prefix: true
|
|
17
|
+
});
|
|
18
|
+
const ENTRY_RE = pathPattern(ENTRY);
|
|
19
|
+
const CSS_LOADER_QUERY_RE = new RegExp(`${pathPattern(CSS_LOADER, {
|
|
20
|
+
embedded: true
|
|
21
|
+
}).source}\\?([^!]*)`, pathPattern(CSS_LOADER).flags);
|
|
22
|
+
const STORE_NAME = '__RSBUILD_VANILLA_EXTRACT__';
|
|
23
|
+
const STORE = `globalThis.${STORE_NAME}`;
|
|
24
|
+
const CSS_TS = /\.css\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts)$/;
|
|
25
|
+
const JS = /\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts)$/;
|
|
26
|
+
const VIRTUAL_CSS = /\.vanilla\.css$/;
|
|
27
|
+
export { COLLECTOR, CSS_LOADER, CSS_LOADER_QUERY_RE, CSS_TS, ENTRY, ENTRY_RE, JS, LAYER, LIB_CSS_LOADER, LOADER, PLUGIN_NAME, REGISTRY, RUNTIME_DIR, RUNTIME_DIR_RE, STORE, STORE_NAME, VIRTUAL_CSS };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Rspack } from '@rsbuild/core';
|
|
2
|
+
import type { FileScope } from '@vanilla-extract/css';
|
|
3
|
+
/** 96-bit content hash of a scope's CSS. */
|
|
4
|
+
export declare const cssHash: (css: string) => string;
|
|
5
|
+
export interface CssRequestOptions {
|
|
6
|
+
/** Store key of the environment. */
|
|
7
|
+
key: string;
|
|
8
|
+
root: string;
|
|
9
|
+
/** Package name the plugin gives to the project's own style files. */
|
|
10
|
+
packageName: string;
|
|
11
|
+
/** The style file whose evaluation produced this scope. */
|
|
12
|
+
file: string;
|
|
13
|
+
hash: string;
|
|
14
|
+
}
|
|
15
|
+
/** The import request of a scope's virtual CSS module. */
|
|
16
|
+
export declare function cssRequest({ key, root, packageName, fileScope, file, hash, resource, out, }: CssRequestOptions & {
|
|
17
|
+
/** Rslib bundleless output: where the CSS file goes, relative to Rslib's root (lib-css.ts). */
|
|
18
|
+
out?: string;
|
|
19
|
+
fileScope: FileScope;
|
|
20
|
+
/**
|
|
21
|
+
* The module's resource, when it must not be the default: Rslib bundleless output gives
|
|
22
|
+
* every style file its own modules (a module shared by two entries goes into a chunk).
|
|
23
|
+
*/
|
|
24
|
+
resource?: string;
|
|
25
|
+
}): string;
|
|
26
|
+
export interface CssQuery {
|
|
27
|
+
key: string;
|
|
28
|
+
hash: string;
|
|
29
|
+
/** The scope to return; absent when it is the resource file's own scope. */
|
|
30
|
+
fileScope?: FileScope;
|
|
31
|
+
}
|
|
32
|
+
export declare function parseCssQuery(query: string): CssQuery;
|
|
33
|
+
export default function vanillaCssLoader(this: Rspack.LoaderContext): void;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { CSS_LOADER } from "./constants.js";
|
|
4
|
+
import { cssStore } from "./css-store.js";
|
|
5
|
+
import { cssResource, evaluateStyleFile, scopeKey } from "./evaluate.js";
|
|
6
|
+
import { relativePosix } from "./paths.js";
|
|
7
|
+
import { getEnvironmentStore } from "./store.js";
|
|
8
|
+
const cssHash = (css)=>createHash('sha256').update(css).digest('base64url').slice(0, 16);
|
|
9
|
+
function cssRequest({ key, root, packageName, fileScope, file, hash, resource = cssResource(fileScope, file, root, packageName), out }) {
|
|
10
|
+
const scopeFile = node_path.resolve(root, fileScope.filePath);
|
|
11
|
+
const query = new URLSearchParams({
|
|
12
|
+
key,
|
|
13
|
+
hash
|
|
14
|
+
});
|
|
15
|
+
if (resource !== scopeFile) {
|
|
16
|
+
query.set('scope', fileScope.filePath);
|
|
17
|
+
if (fileScope.packageName) query.set('pkg', fileScope.packageName);
|
|
18
|
+
}
|
|
19
|
+
if (out) query.set('out', out);
|
|
20
|
+
return `${scopeFile}.vanilla.css!=!${CSS_LOADER}?${query}!${resource}`;
|
|
21
|
+
}
|
|
22
|
+
function parseCssQuery(query) {
|
|
23
|
+
const q = new URLSearchParams(query.startsWith('?') ? query.slice(1) : query);
|
|
24
|
+
const key = q.get('key');
|
|
25
|
+
const hash = q.get('hash');
|
|
26
|
+
if (!key || !hash) throw new Error(`[vanilla-extract] malformed virtual CSS request: ${query}`);
|
|
27
|
+
const filePath = q.get('scope');
|
|
28
|
+
return filePath ? {
|
|
29
|
+
key,
|
|
30
|
+
hash,
|
|
31
|
+
fileScope: {
|
|
32
|
+
filePath,
|
|
33
|
+
packageName: q.get('pkg') ?? ''
|
|
34
|
+
}
|
|
35
|
+
} : {
|
|
36
|
+
key,
|
|
37
|
+
hash
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function vanillaCssLoader() {
|
|
41
|
+
const callback = this.async();
|
|
42
|
+
run(this).then((css)=>callback(null, css), (err)=>callback(err));
|
|
43
|
+
}
|
|
44
|
+
async function run(ctx) {
|
|
45
|
+
const query = parseCssQuery('string' == typeof ctx.query ? ctx.query : '');
|
|
46
|
+
const store = getEnvironmentStore(query.key);
|
|
47
|
+
const css = cssStore(ctx._compiler);
|
|
48
|
+
const stored = css.get(query.hash);
|
|
49
|
+
if (void 0 !== stored) return stored;
|
|
50
|
+
const file = ctx.resourcePath;
|
|
51
|
+
const text = await scopeCss(ctx, file, store, query.fileScope);
|
|
52
|
+
const hash = cssHash(text);
|
|
53
|
+
css.set(hash, text);
|
|
54
|
+
if (hash !== query.hash) ctx.cacheable(false);
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
async function scopeCss(ctx, file, store, fileScope) {
|
|
58
|
+
const wantedScope = fileScope ?? {
|
|
59
|
+
filePath: relativePosix(store.root, file),
|
|
60
|
+
packageName: store.packageName
|
|
61
|
+
};
|
|
62
|
+
const wanted = scopeKey(wantedScope);
|
|
63
|
+
const { scopes } = await evaluateStyleFile(ctx, file, store);
|
|
64
|
+
const scope = scopes.find((s)=>scopeKey(s.fileScope) === wanted);
|
|
65
|
+
if (!scope) ctx.emitWarning(new Error(`[vanilla-extract] evaluating ${relativePosix(store.root, file)} produced no CSS for file scope "${wantedScope.filePath}"${wantedScope.packageName ? ` (package "${wantedScope.packageName}")` : ''}; its virtual CSS module is empty.`));
|
|
66
|
+
return scope?.css ?? '';
|
|
67
|
+
}
|
|
68
|
+
export default vanillaCssLoader;
|
|
69
|
+
export { cssHash, cssRequest, parseCssQuery };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Rspack } from '@rsbuild/core';
|
|
2
|
+
/** Paths css-loader must treat as global CSS whatever `modules.auto` says. */
|
|
3
|
+
export declare const NEVER_CSS_MODULES: RegExp;
|
|
4
|
+
/** `auto` that leaves `NEVER_CSS_MODULES` out; `true` / `false` / undefined as they are. */
|
|
5
|
+
export declare function autoWithout(auto: unknown): unknown;
|
|
6
|
+
/** Rewrites `modules.auto` of every css-loader in `rules`. Returns how many it changed. */
|
|
7
|
+
export declare function guardCssModules(rules: Rspack.RuleSetRules | undefined): number;
|
|
8
|
+
/**
|
|
9
|
+
* Whether a css-loader in `rules` has `url: false` (`tools.cssLoader.url`). The plugin
|
|
10
|
+
* writes the URLs of assets that style files use as paths relative to the source file,
|
|
11
|
+
* for css-loader to resolve and emit; with URL handling off they stay unresolved.
|
|
12
|
+
* Rsbuild's guide sets it for @vanilla-extract/webpack-plugin.
|
|
13
|
+
*/
|
|
14
|
+
export declare function hasCssLoaderUrlFalse(rules: Rspack.RuleSetRules | undefined): boolean;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const NEVER_CSS_MODULES = /\.vanilla\.css(?:$|[?#])|_css\.css$|\.css\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts)(?:$|[?#])/;
|
|
2
|
+
const guarded = new WeakSet();
|
|
3
|
+
function autoWithout(auto) {
|
|
4
|
+
let out = auto;
|
|
5
|
+
if (auto instanceof RegExp) out = (p)=>{
|
|
6
|
+
auto.lastIndex = 0;
|
|
7
|
+
return !NEVER_CSS_MODULES.test(p) && auto.test(p);
|
|
8
|
+
};
|
|
9
|
+
else if ('function' == typeof auto && !guarded.has(auto)) {
|
|
10
|
+
const fn = auto;
|
|
11
|
+
out = (p, q, f)=>!NEVER_CSS_MODULES.test(p) && fn(p, q, f);
|
|
12
|
+
}
|
|
13
|
+
if ('function' == typeof out) guarded.add(out);
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
const CSS_LOADER_PATH = /[\\/]css-loader[\\/]/;
|
|
17
|
+
function guardCssModules(rules) {
|
|
18
|
+
let changed = 0;
|
|
19
|
+
const visitUse = (use)=>{
|
|
20
|
+
if (Array.isArray(use)) {
|
|
21
|
+
for (const u of use)visitUse(u);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!use || 'object' != typeof use) return;
|
|
25
|
+
const u = use;
|
|
26
|
+
if ('string' != typeof u.loader || !CSS_LOADER_PATH.test(u.loader)) return;
|
|
27
|
+
const modules = u.options?.modules;
|
|
28
|
+
if (!modules || 'object' != typeof modules) return;
|
|
29
|
+
const m = modules;
|
|
30
|
+
const next = autoWithout(m.auto);
|
|
31
|
+
if (next !== m.auto) {
|
|
32
|
+
m.auto = next;
|
|
33
|
+
changed++;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const visit = (list)=>{
|
|
37
|
+
if (!Array.isArray(list)) return;
|
|
38
|
+
for (const rule of list){
|
|
39
|
+
if (!rule || 'object' != typeof rule) continue;
|
|
40
|
+
const r = rule;
|
|
41
|
+
visitUse(r.use);
|
|
42
|
+
if (r.loader) visitUse({
|
|
43
|
+
loader: r.loader,
|
|
44
|
+
options: r.options
|
|
45
|
+
});
|
|
46
|
+
visit(r.oneOf);
|
|
47
|
+
visit(r.rules);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
visit(rules);
|
|
51
|
+
return changed;
|
|
52
|
+
}
|
|
53
|
+
function hasCssLoaderUrlFalse(rules) {
|
|
54
|
+
const visitUse = (use)=>{
|
|
55
|
+
if (Array.isArray(use)) return use.some(visitUse);
|
|
56
|
+
if (!use || 'object' != typeof use) return false;
|
|
57
|
+
const u = use;
|
|
58
|
+
if ('string' != typeof u.loader || !CSS_LOADER_PATH.test(u.loader)) return false;
|
|
59
|
+
return u.options?.url === false;
|
|
60
|
+
};
|
|
61
|
+
const visit = (list)=>{
|
|
62
|
+
if (!Array.isArray(list)) return false;
|
|
63
|
+
return list.some((rule)=>{
|
|
64
|
+
if (!rule || 'object' != typeof rule) return false;
|
|
65
|
+
const r = rule;
|
|
66
|
+
return visitUse(r.use) || !!r.loader && visitUse({
|
|
67
|
+
loader: r.loader,
|
|
68
|
+
options: r.options
|
|
69
|
+
}) || visit(r.oneOf) || visit(r.rules);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
return visit(rules);
|
|
73
|
+
}
|
|
74
|
+
export { NEVER_CSS_MODULES, autoWithout, guardCssModules, hasCssLoaderUrlFalse };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Rspack } from '@rsbuild/core';
|
|
2
|
+
/** The CSS map of `compiler` (the loader context's `_compiler`, or the plugin's compiler). */
|
|
3
|
+
export declare function cssStore(compiler: object | undefined): Map<string, string>;
|
|
4
|
+
/** Hashes named by the virtual CSS modules of a compilation. */
|
|
5
|
+
export declare function referencedHashes(compilation: Rspack.Compilation): Set<string>;
|
|
6
|
+
export interface PruneResult {
|
|
7
|
+
entries: number;
|
|
8
|
+
bytes: number;
|
|
9
|
+
removed: number;
|
|
10
|
+
}
|
|
11
|
+
/** Keep only `keep` in `map`. */
|
|
12
|
+
export declare function prune(map: Map<string, string>, keep: Set<string>): PruneResult;
|
|
13
|
+
/** Rspack plugin that prunes the compiler's CSS map after each successful compilation. */
|
|
14
|
+
export declare function cssStorePlugin(label: string): Rspack.RspackPluginInstance;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { CSS_LOADER_QUERY_RE } from "./constants.js";
|
|
2
|
+
const KEY = Symbol.for('rsbuild-plugin-vanilla-extract.css');
|
|
3
|
+
function stores() {
|
|
4
|
+
const g = globalThis;
|
|
5
|
+
g[KEY] ??= new WeakMap();
|
|
6
|
+
return g[KEY];
|
|
7
|
+
}
|
|
8
|
+
function cssStore(compiler) {
|
|
9
|
+
if (!compiler) throw new Error('[vanilla-extract] loader context without compiler');
|
|
10
|
+
const all = stores();
|
|
11
|
+
let map = all.get(compiler);
|
|
12
|
+
if (!map) {
|
|
13
|
+
map = new Map();
|
|
14
|
+
all.set(compiler, map);
|
|
15
|
+
}
|
|
16
|
+
return map;
|
|
17
|
+
}
|
|
18
|
+
const HASH_RE = /(?:^|&)hash=([\w-]+)/;
|
|
19
|
+
function referencedHashes(compilation) {
|
|
20
|
+
const out = new Set();
|
|
21
|
+
for (const m of compilation.modules){
|
|
22
|
+
const query = CSS_LOADER_QUERY_RE.exec(m.identifier())?.[1];
|
|
23
|
+
const hash = void 0 === query ? void 0 : HASH_RE.exec(query)?.[1];
|
|
24
|
+
if (hash) out.add(hash);
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function prune(map, keep) {
|
|
29
|
+
let removed = 0;
|
|
30
|
+
for (const hash of map.keys())if (!keep.has(hash)) {
|
|
31
|
+
map.delete(hash);
|
|
32
|
+
removed++;
|
|
33
|
+
}
|
|
34
|
+
let bytes = 0;
|
|
35
|
+
for (const css of map.values())bytes += css.length;
|
|
36
|
+
return {
|
|
37
|
+
entries: map.size,
|
|
38
|
+
bytes,
|
|
39
|
+
removed
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const debug = ()=>/\bstore\b/.test(process.env.RSBUILD_VANILLA_EXTRACT_DEBUG ?? '');
|
|
43
|
+
function cssStorePlugin(label) {
|
|
44
|
+
return {
|
|
45
|
+
apply (compiler) {
|
|
46
|
+
compiler.hooks.done.tap('rsbuild-plugin-vanilla-extract:css-store', (stats)=>{
|
|
47
|
+
const map = cssStore(compiler);
|
|
48
|
+
if (stats.hasErrors()) return;
|
|
49
|
+
const r = prune(map, referencedHashes(stats.compilation));
|
|
50
|
+
if (debug()) process.stderr.write(`[vanilla-extract] css store ${label}: entries=${r.entries} bytes=${r.bytes} removed=${r.removed}\n`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export { cssStore, cssStorePlugin, prune, referencedHashes };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Rspack } from '@rsbuild/core';
|
|
2
|
+
import type { FileScope } from '@vanilla-extract/css';
|
|
3
|
+
import { type LibScopeContext } from './lib-css.js';
|
|
4
|
+
import { type EvalResult, type Processed } from './process.js';
|
|
5
|
+
import type { EnvironmentStore } from './store.js';
|
|
6
|
+
export declare const ASSET_RE: RegExp;
|
|
7
|
+
export declare const assetMarker: (abs: string) => string;
|
|
8
|
+
export declare const fromHex: (h: string) => string;
|
|
9
|
+
/**
|
|
10
|
+
* The resource of a scope's virtual CSS module: the scope's own source file when the
|
|
11
|
+
* scope is one of the project's style files, so that every importer produces the same
|
|
12
|
+
* request (dedupe), and chunking / cacheGroups / url() see that file, not a plugin file.
|
|
13
|
+
* A scope set by hand (a library calling setFileScope) is evaluated
|
|
14
|
+
* through the file that reached it.
|
|
15
|
+
*/
|
|
16
|
+
export declare function cssResource(fileScope: FileScope, file: string, root: string, packageName: string): string;
|
|
17
|
+
/** Identity of a file scope, as used to group CSS. */
|
|
18
|
+
export declare const scopeKey: (fileScope: FileScope) => string;
|
|
19
|
+
export interface Evaluated extends Processed {
|
|
20
|
+
result: EvalResult;
|
|
21
|
+
/** Rslib bundleless output: where the scopes' CSS goes (lib-css.ts). */
|
|
22
|
+
lib?: LibScopeContext;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Runs `file` through `runtime/entry.js?ve-entry` in the eval layer (importModule
|
|
26
|
+
* also records every module it reached as a dependency of the calling module),
|
|
27
|
+
* then renames and transforms the collected CSS.
|
|
28
|
+
*/
|
|
29
|
+
export declare function evaluateStyleFile(ctx: Rspack.LoaderContext<unknown>, file: string, store: EnvironmentStore): Promise<Evaluated>;
|