@vanilla-extract/vite-plugin 3.1.4 → 3.1.5
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/README.md
CHANGED
|
@@ -84,6 +84,7 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
|
|
|
84
84
|
- [Vite](#vite)
|
|
85
85
|
- [Next.js](#nextjs)
|
|
86
86
|
- [Gatsby](#gatsby)
|
|
87
|
+
- [Rollup](#rollup)
|
|
87
88
|
- [Test environments](#test-environments)
|
|
88
89
|
- [Configuration](#configuration)
|
|
89
90
|
- [identifiers](#identifiers)
|
|
@@ -330,6 +331,32 @@ If you don't have a `.babelrc` file in the root of your project, create one. Add
|
|
|
330
331
|
|
|
331
332
|
To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/gatsby-uc/plugins/tree/main/packages/gatsby-plugin-vanilla-extract) plugin.
|
|
332
333
|
|
|
334
|
+
### Rollup
|
|
335
|
+
|
|
336
|
+
> Note: This option is useful for library development but not suitable for application bundles.
|
|
337
|
+
> Rollup has no built-in CSS bundling, so this plugin just outputs styles as individual CSS assets.
|
|
338
|
+
> For applications we instead recommend to use Vite
|
|
339
|
+
> (which itself uses Rollup under the hood but comes with its own CSS bundling).
|
|
340
|
+
|
|
341
|
+
1. Install the dependencies.
|
|
342
|
+
|
|
343
|
+
```bash
|
|
344
|
+
npm install @vanilla-extract/css @vanilla-extract/rollup-plugin
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
2. Add the [Rollup](https://rollupjs.org/) plugin to your Rollup config.
|
|
348
|
+
|
|
349
|
+
> 💡 This plugin accepts an optional [configuration object](#configuration).
|
|
350
|
+
|
|
351
|
+
```js
|
|
352
|
+
import { vanillaExtractPlugin } from '@vanilla-extract/rollup-plugin';
|
|
353
|
+
|
|
354
|
+
// rollup.config.js
|
|
355
|
+
export default {
|
|
356
|
+
plugins: [vanillaExtractPlugin()]
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
333
360
|
### Test environments
|
|
334
361
|
|
|
335
362
|
1. Install the dependencies.
|
|
@@ -68,12 +68,10 @@ const resolvePostcssConfig = async config => {
|
|
|
68
68
|
|
|
69
69
|
const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
|
|
70
70
|
|
|
71
|
-
const virtualPrefix = 'virtual:vanilla-extract:';
|
|
72
71
|
function vanillaExtractPlugin({
|
|
73
72
|
identifiers
|
|
74
73
|
} = {}) {
|
|
75
74
|
let config;
|
|
76
|
-
let packageInfo;
|
|
77
75
|
let server;
|
|
78
76
|
let postCssConfig;
|
|
79
77
|
const cssMap = new Map();
|
|
@@ -106,47 +104,52 @@ function vanillaExtractPlugin({
|
|
|
106
104
|
}
|
|
107
105
|
|
|
108
106
|
virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
|
|
109
|
-
packageInfo = integration.getPackageInfo(config.root);
|
|
110
107
|
},
|
|
111
108
|
|
|
112
109
|
resolveId(id) {
|
|
113
|
-
if (id.
|
|
114
|
-
return
|
|
110
|
+
if (!id.endsWith(virtualExt)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const normalizedId = id.startsWith('/') ? id.slice(1) : id;
|
|
115
|
+
|
|
116
|
+
if (cssMap.has(normalizedId)) {
|
|
117
|
+
return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
|
|
115
118
|
}
|
|
116
119
|
},
|
|
117
120
|
|
|
118
121
|
load(id) {
|
|
119
|
-
if (id.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (!cssMap.has(fileScopeId)) {
|
|
123
|
-
throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
|
|
124
|
-
}
|
|
122
|
+
if (!id.endsWith(virtualExt)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
const cssFileId = id.slice(config.root.length + 1);
|
|
127
|
+
const css = cssMap.get(cssFileId);
|
|
127
128
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
if (typeof css !== 'string') {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
131
132
|
|
|
132
|
-
|
|
133
|
-
return
|
|
134
|
-
import { injectStyles } from '@vanilla-extract/css/injectStyles';
|
|
135
|
-
|
|
136
|
-
const inject = (css) => injectStyles({
|
|
137
|
-
fileScope: ${JSON.stringify(fileScope)},
|
|
138
|
-
css
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
inject(${JSON.stringify(css)});
|
|
142
|
-
|
|
143
|
-
import.meta.hot.on('${styleUpdateEvent(fileScopeId)}', (css) => {
|
|
144
|
-
inject(css);
|
|
145
|
-
});
|
|
146
|
-
`;
|
|
133
|
+
if (!server) {
|
|
134
|
+
return css;
|
|
147
135
|
}
|
|
148
136
|
|
|
149
|
-
return
|
|
137
|
+
return outdent__default["default"]`
|
|
138
|
+
import { injectStyles } from '@vanilla-extract/css/injectStyles';
|
|
139
|
+
|
|
140
|
+
const inject = (css) => injectStyles({
|
|
141
|
+
fileScope: ${JSON.stringify({
|
|
142
|
+
filePath: cssFileId
|
|
143
|
+
})},
|
|
144
|
+
css
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
inject(${JSON.stringify(css)});
|
|
148
|
+
|
|
149
|
+
import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
|
|
150
|
+
inject(css);
|
|
151
|
+
});
|
|
152
|
+
`;
|
|
150
153
|
},
|
|
151
154
|
|
|
152
155
|
async transform(code, id, ssrParam) {
|
|
@@ -168,9 +171,9 @@ function vanillaExtractPlugin({
|
|
|
168
171
|
if (ssr) {
|
|
169
172
|
return integration.addFileScope({
|
|
170
173
|
source: code,
|
|
171
|
-
filePath: vite.normalizePath(
|
|
172
|
-
|
|
173
|
-
})
|
|
174
|
+
filePath: vite.normalizePath(validId),
|
|
175
|
+
rootPath: config.root
|
|
176
|
+
});
|
|
174
177
|
}
|
|
175
178
|
|
|
176
179
|
const {
|
|
@@ -197,9 +200,7 @@ function vanillaExtractPlugin({
|
|
|
197
200
|
fileScope,
|
|
198
201
|
source
|
|
199
202
|
}) => {
|
|
200
|
-
|
|
201
|
-
const fileId = integration.stringifyFileScope(fileScope).replace(/\.\./g, '_dir_up_');
|
|
202
|
-
const id = `${virtualPrefix}${fileId}${virtualExt}`;
|
|
203
|
+
const id = `${fileScope.filePath}${virtualExt}`;
|
|
203
204
|
let cssSource = source;
|
|
204
205
|
|
|
205
206
|
if (postCssConfig) {
|
|
@@ -210,7 +211,7 @@ function vanillaExtractPlugin({
|
|
|
210
211
|
cssSource = postCssResult.css;
|
|
211
212
|
}
|
|
212
213
|
|
|
213
|
-
if (server && cssMap.has(
|
|
214
|
+
if (server && cssMap.has(id) && cssMap.get(id) !== source) {
|
|
214
215
|
const {
|
|
215
216
|
moduleGraph
|
|
216
217
|
} = server;
|
|
@@ -222,12 +223,12 @@ function vanillaExtractPlugin({
|
|
|
222
223
|
|
|
223
224
|
server.ws.send({
|
|
224
225
|
type: 'custom',
|
|
225
|
-
event: styleUpdateEvent(
|
|
226
|
+
event: styleUpdateEvent(id),
|
|
226
227
|
data: cssSource
|
|
227
228
|
});
|
|
228
229
|
}
|
|
229
230
|
|
|
230
|
-
cssMap.set(
|
|
231
|
+
cssMap.set(id, cssSource);
|
|
231
232
|
return `import "${id}";`;
|
|
232
233
|
}
|
|
233
234
|
});
|
|
@@ -68,12 +68,10 @@ const resolvePostcssConfig = async config => {
|
|
|
68
68
|
|
|
69
69
|
const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
|
|
70
70
|
|
|
71
|
-
const virtualPrefix = 'virtual:vanilla-extract:';
|
|
72
71
|
function vanillaExtractPlugin({
|
|
73
72
|
identifiers
|
|
74
73
|
} = {}) {
|
|
75
74
|
let config;
|
|
76
|
-
let packageInfo;
|
|
77
75
|
let server;
|
|
78
76
|
let postCssConfig;
|
|
79
77
|
const cssMap = new Map();
|
|
@@ -106,47 +104,52 @@ function vanillaExtractPlugin({
|
|
|
106
104
|
}
|
|
107
105
|
|
|
108
106
|
virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
|
|
109
|
-
packageInfo = integration.getPackageInfo(config.root);
|
|
110
107
|
},
|
|
111
108
|
|
|
112
109
|
resolveId(id) {
|
|
113
|
-
if (id.
|
|
114
|
-
return
|
|
110
|
+
if (!id.endsWith(virtualExt)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const normalizedId = id.startsWith('/') ? id.slice(1) : id;
|
|
115
|
+
|
|
116
|
+
if (cssMap.has(normalizedId)) {
|
|
117
|
+
return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
|
|
115
118
|
}
|
|
116
119
|
},
|
|
117
120
|
|
|
118
121
|
load(id) {
|
|
119
|
-
if (id.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (!cssMap.has(fileScopeId)) {
|
|
123
|
-
throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
|
|
124
|
-
}
|
|
122
|
+
if (!id.endsWith(virtualExt)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
const cssFileId = id.slice(config.root.length + 1);
|
|
127
|
+
const css = cssMap.get(cssFileId);
|
|
127
128
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
if (typeof css !== 'string') {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
131
132
|
|
|
132
|
-
|
|
133
|
-
return
|
|
134
|
-
import { injectStyles } from '@vanilla-extract/css/injectStyles';
|
|
135
|
-
|
|
136
|
-
const inject = (css) => injectStyles({
|
|
137
|
-
fileScope: ${JSON.stringify(fileScope)},
|
|
138
|
-
css
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
inject(${JSON.stringify(css)});
|
|
142
|
-
|
|
143
|
-
import.meta.hot.on('${styleUpdateEvent(fileScopeId)}', (css) => {
|
|
144
|
-
inject(css);
|
|
145
|
-
});
|
|
146
|
-
`;
|
|
133
|
+
if (!server) {
|
|
134
|
+
return css;
|
|
147
135
|
}
|
|
148
136
|
|
|
149
|
-
return
|
|
137
|
+
return outdent__default["default"]`
|
|
138
|
+
import { injectStyles } from '@vanilla-extract/css/injectStyles';
|
|
139
|
+
|
|
140
|
+
const inject = (css) => injectStyles({
|
|
141
|
+
fileScope: ${JSON.stringify({
|
|
142
|
+
filePath: cssFileId
|
|
143
|
+
})},
|
|
144
|
+
css
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
inject(${JSON.stringify(css)});
|
|
148
|
+
|
|
149
|
+
import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
|
|
150
|
+
inject(css);
|
|
151
|
+
});
|
|
152
|
+
`;
|
|
150
153
|
},
|
|
151
154
|
|
|
152
155
|
async transform(code, id, ssrParam) {
|
|
@@ -168,9 +171,9 @@ function vanillaExtractPlugin({
|
|
|
168
171
|
if (ssr) {
|
|
169
172
|
return integration.addFileScope({
|
|
170
173
|
source: code,
|
|
171
|
-
filePath: vite.normalizePath(
|
|
172
|
-
|
|
173
|
-
})
|
|
174
|
+
filePath: vite.normalizePath(validId),
|
|
175
|
+
rootPath: config.root
|
|
176
|
+
});
|
|
174
177
|
}
|
|
175
178
|
|
|
176
179
|
const {
|
|
@@ -197,9 +200,7 @@ function vanillaExtractPlugin({
|
|
|
197
200
|
fileScope,
|
|
198
201
|
source
|
|
199
202
|
}) => {
|
|
200
|
-
|
|
201
|
-
const fileId = integration.stringifyFileScope(fileScope).replace(/\.\./g, '_dir_up_');
|
|
202
|
-
const id = `${virtualPrefix}${fileId}${virtualExt}`;
|
|
203
|
+
const id = `${fileScope.filePath}${virtualExt}`;
|
|
203
204
|
let cssSource = source;
|
|
204
205
|
|
|
205
206
|
if (postCssConfig) {
|
|
@@ -210,7 +211,7 @@ function vanillaExtractPlugin({
|
|
|
210
211
|
cssSource = postCssResult.css;
|
|
211
212
|
}
|
|
212
213
|
|
|
213
|
-
if (server && cssMap.has(
|
|
214
|
+
if (server && cssMap.has(id) && cssMap.get(id) !== source) {
|
|
214
215
|
const {
|
|
215
216
|
moduleGraph
|
|
216
217
|
} = server;
|
|
@@ -222,12 +223,12 @@ function vanillaExtractPlugin({
|
|
|
222
223
|
|
|
223
224
|
server.ws.send({
|
|
224
225
|
type: 'custom',
|
|
225
|
-
event: styleUpdateEvent(
|
|
226
|
+
event: styleUpdateEvent(id),
|
|
226
227
|
data: cssSource
|
|
227
228
|
});
|
|
228
229
|
}
|
|
229
230
|
|
|
230
|
-
cssMap.set(
|
|
231
|
+
cssMap.set(id, cssSource);
|
|
231
232
|
return `import "${id}";`;
|
|
232
233
|
}
|
|
233
234
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { normalizePath } from 'vite';
|
|
3
3
|
import outdent from 'outdent';
|
|
4
|
-
import {
|
|
4
|
+
import { cssFileFilter, addFileScope, compile, processVanillaFile } from '@vanilla-extract/integration';
|
|
5
5
|
|
|
6
6
|
// Mostly copied from vite's implementation
|
|
7
7
|
// https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
|
|
@@ -41,12 +41,10 @@ const resolvePostcssConfig = async config => {
|
|
|
41
41
|
|
|
42
42
|
const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
|
|
43
43
|
|
|
44
|
-
const virtualPrefix = 'virtual:vanilla-extract:';
|
|
45
44
|
function vanillaExtractPlugin({
|
|
46
45
|
identifiers
|
|
47
46
|
} = {}) {
|
|
48
47
|
let config;
|
|
49
|
-
let packageInfo;
|
|
50
48
|
let server;
|
|
51
49
|
let postCssConfig;
|
|
52
50
|
const cssMap = new Map();
|
|
@@ -79,47 +77,52 @@ function vanillaExtractPlugin({
|
|
|
79
77
|
}
|
|
80
78
|
|
|
81
79
|
virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
|
|
82
|
-
packageInfo = getPackageInfo(config.root);
|
|
83
80
|
},
|
|
84
81
|
|
|
85
82
|
resolveId(id) {
|
|
86
|
-
if (id.
|
|
87
|
-
return
|
|
83
|
+
if (!id.endsWith(virtualExt)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const normalizedId = id.startsWith('/') ? id.slice(1) : id;
|
|
88
|
+
|
|
89
|
+
if (cssMap.has(normalizedId)) {
|
|
90
|
+
return normalizePath(path.join(config.root, normalizedId));
|
|
88
91
|
}
|
|
89
92
|
},
|
|
90
93
|
|
|
91
94
|
load(id) {
|
|
92
|
-
if (id.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (!cssMap.has(fileScopeId)) {
|
|
96
|
-
throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
|
|
97
|
-
}
|
|
95
|
+
if (!id.endsWith(virtualExt)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
const cssFileId = id.slice(config.root.length + 1);
|
|
100
|
+
const css = cssMap.get(cssFileId);
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
if (typeof css !== 'string') {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
104
105
|
|
|
105
|
-
|
|
106
|
-
return
|
|
107
|
-
import { injectStyles } from '@vanilla-extract/css/injectStyles';
|
|
108
|
-
|
|
109
|
-
const inject = (css) => injectStyles({
|
|
110
|
-
fileScope: ${JSON.stringify(fileScope)},
|
|
111
|
-
css
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
inject(${JSON.stringify(css)});
|
|
115
|
-
|
|
116
|
-
import.meta.hot.on('${styleUpdateEvent(fileScopeId)}', (css) => {
|
|
117
|
-
inject(css);
|
|
118
|
-
});
|
|
119
|
-
`;
|
|
106
|
+
if (!server) {
|
|
107
|
+
return css;
|
|
120
108
|
}
|
|
121
109
|
|
|
122
|
-
return
|
|
110
|
+
return outdent`
|
|
111
|
+
import { injectStyles } from '@vanilla-extract/css/injectStyles';
|
|
112
|
+
|
|
113
|
+
const inject = (css) => injectStyles({
|
|
114
|
+
fileScope: ${JSON.stringify({
|
|
115
|
+
filePath: cssFileId
|
|
116
|
+
})},
|
|
117
|
+
css
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
inject(${JSON.stringify(css)});
|
|
121
|
+
|
|
122
|
+
import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
|
|
123
|
+
inject(css);
|
|
124
|
+
});
|
|
125
|
+
`;
|
|
123
126
|
},
|
|
124
127
|
|
|
125
128
|
async transform(code, id, ssrParam) {
|
|
@@ -141,9 +144,9 @@ function vanillaExtractPlugin({
|
|
|
141
144
|
if (ssr) {
|
|
142
145
|
return addFileScope({
|
|
143
146
|
source: code,
|
|
144
|
-
filePath: normalizePath(
|
|
145
|
-
|
|
146
|
-
})
|
|
147
|
+
filePath: normalizePath(validId),
|
|
148
|
+
rootPath: config.root
|
|
149
|
+
});
|
|
147
150
|
}
|
|
148
151
|
|
|
149
152
|
const {
|
|
@@ -170,9 +173,7 @@ function vanillaExtractPlugin({
|
|
|
170
173
|
fileScope,
|
|
171
174
|
source
|
|
172
175
|
}) => {
|
|
173
|
-
|
|
174
|
-
const fileId = stringifyFileScope(fileScope).replace(/\.\./g, '_dir_up_');
|
|
175
|
-
const id = `${virtualPrefix}${fileId}${virtualExt}`;
|
|
176
|
+
const id = `${fileScope.filePath}${virtualExt}`;
|
|
176
177
|
let cssSource = source;
|
|
177
178
|
|
|
178
179
|
if (postCssConfig) {
|
|
@@ -183,7 +184,7 @@ function vanillaExtractPlugin({
|
|
|
183
184
|
cssSource = postCssResult.css;
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
if (server && cssMap.has(
|
|
187
|
+
if (server && cssMap.has(id) && cssMap.get(id) !== source) {
|
|
187
188
|
const {
|
|
188
189
|
moduleGraph
|
|
189
190
|
} = server;
|
|
@@ -195,12 +196,12 @@ function vanillaExtractPlugin({
|
|
|
195
196
|
|
|
196
197
|
server.ws.send({
|
|
197
198
|
type: 'custom',
|
|
198
|
-
event: styleUpdateEvent(
|
|
199
|
+
event: styleUpdateEvent(id),
|
|
199
200
|
data: cssSource
|
|
200
201
|
});
|
|
201
202
|
}
|
|
202
203
|
|
|
203
|
-
cssMap.set(
|
|
204
|
+
cssMap.set(id, cssSource);
|
|
204
205
|
return `import "${id}";`;
|
|
205
206
|
}
|
|
206
207
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vanilla-extract/vite-plugin",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.5",
|
|
4
4
|
"description": "Zero-runtime Stylesheets-in-TypeScript",
|
|
5
5
|
"main": "dist/vanilla-extract-vite-plugin.cjs.js",
|
|
6
6
|
"module": "dist/vanilla-extract-vite-plugin.esm.js",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"author": "SEEK",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@vanilla-extract/integration": "^
|
|
18
|
+
"@vanilla-extract/integration": "^4.0.0",
|
|
19
19
|
"outdent": "^0.8.0",
|
|
20
20
|
"postcss": "^8.3.6",
|
|
21
21
|
"postcss-load-config": "^3.1.0"
|