@vanilla-extract/vite-plugin 3.1.2 → 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)
@@ -328,7 +329,33 @@ If you don't have a `.babelrc` file in the root of your project, create one. Add
328
329
 
329
330
  ### Gatsby
330
331
 
331
- To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/KyleAMathews/gatsby-plugin-vanilla-extract) plugin.
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.
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
+ ```
332
359
 
333
360
  ### Test environments
334
361
 
@@ -443,6 +470,15 @@ export const childClass = style({
443
470
  >
444
471
  > If you want to globally target child nodes within the current element (e.g. `'& a[href]'`), you should use [`globalStyle`](#globalstyle) instead.
445
472
 
473
+ For fallback styles you may simply pass an array of properties instead of a single prop.
474
+
475
+ ```ts
476
+ export const exampleStyle = style({
477
+ // in Firefox and IE the "overflow: overlay" will be ignored and the "overflow: auto" will be applied
478
+ overflow: ['auto', 'overlay'],
479
+ });
480
+ ```
481
+
446
482
  Multiple styles can be composed into a single rule by providing an array of styles.
447
483
 
448
484
  ```ts
@@ -837,7 +873,7 @@ import { createVar, fallbackVar, style } from '@vanilla-extract/css';
837
873
  export const colorVar = createVar();
838
874
 
839
875
  export const exampleStyle = style({
840
- color: fallbackVar(colorVar, 'blue');
876
+ color: fallbackVar(colorVar, 'blue'),
841
877
  });
842
878
  ```
843
879
 
@@ -850,7 +886,7 @@ export const primaryColorVar = createVar();
850
886
  export const secondaryColorVar = createVar();
851
887
 
852
888
  export const exampleStyle = style({
853
- color: fallbackVar(primaryColorVar, secondaryColorVar, 'blue');
889
+ color: fallbackVar(primaryColorVar, secondaryColorVar, 'blue'),
854
890
  });
855
891
  ```
856
892
 
@@ -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.indexOf(virtualPrefix) === 0) {
114
- return id;
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.indexOf(virtualPrefix) === 0) {
120
- const fileScopeId = id.slice(virtualPrefix.length, id.indexOf(virtualExt));
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
- const css = cssMap.get(fileScopeId);
126
+ const cssFileId = id.slice(config.root.length + 1);
127
+ const css = cssMap.get(cssFileId);
127
128
 
128
- if (!server) {
129
- return css;
130
- }
129
+ if (typeof css !== 'string') {
130
+ return;
131
+ }
131
132
 
132
- const fileScope = integration.parseFileScope(fileScopeId);
133
- return outdent__default["default"]`
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 null;
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(path__default["default"].relative(packageInfo.dirname, validId)),
172
- packageInfo
173
- }).source;
174
+ filePath: vite.normalizePath(validId),
175
+ rootPath: config.root
176
+ });
174
177
  }
175
178
 
176
179
  const {
@@ -197,8 +200,7 @@ function vanillaExtractPlugin({
197
200
  fileScope,
198
201
  source
199
202
  }) => {
200
- const fileId = integration.stringifyFileScope(fileScope);
201
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
203
+ const id = `${fileScope.filePath}${virtualExt}`;
202
204
  let cssSource = source;
203
205
 
204
206
  if (postCssConfig) {
@@ -209,7 +211,7 @@ function vanillaExtractPlugin({
209
211
  cssSource = postCssResult.css;
210
212
  }
211
213
 
212
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
214
+ if (server && cssMap.has(id) && cssMap.get(id) !== source) {
213
215
  const {
214
216
  moduleGraph
215
217
  } = server;
@@ -221,12 +223,12 @@ function vanillaExtractPlugin({
221
223
 
222
224
  server.ws.send({
223
225
  type: 'custom',
224
- event: styleUpdateEvent(fileId),
226
+ event: styleUpdateEvent(id),
225
227
  data: cssSource
226
228
  });
227
229
  }
228
230
 
229
- cssMap.set(fileId, cssSource);
231
+ cssMap.set(id, cssSource);
230
232
  return `import "${id}";`;
231
233
  }
232
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.indexOf(virtualPrefix) === 0) {
114
- return id;
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.indexOf(virtualPrefix) === 0) {
120
- const fileScopeId = id.slice(virtualPrefix.length, id.indexOf(virtualExt));
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
- const css = cssMap.get(fileScopeId);
126
+ const cssFileId = id.slice(config.root.length + 1);
127
+ const css = cssMap.get(cssFileId);
127
128
 
128
- if (!server) {
129
- return css;
130
- }
129
+ if (typeof css !== 'string') {
130
+ return;
131
+ }
131
132
 
132
- const fileScope = integration.parseFileScope(fileScopeId);
133
- return outdent__default["default"]`
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 null;
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(path__default["default"].relative(packageInfo.dirname, validId)),
172
- packageInfo
173
- }).source;
174
+ filePath: vite.normalizePath(validId),
175
+ rootPath: config.root
176
+ });
174
177
  }
175
178
 
176
179
  const {
@@ -197,8 +200,7 @@ function vanillaExtractPlugin({
197
200
  fileScope,
198
201
  source
199
202
  }) => {
200
- const fileId = integration.stringifyFileScope(fileScope);
201
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
203
+ const id = `${fileScope.filePath}${virtualExt}`;
202
204
  let cssSource = source;
203
205
 
204
206
  if (postCssConfig) {
@@ -209,7 +211,7 @@ function vanillaExtractPlugin({
209
211
  cssSource = postCssResult.css;
210
212
  }
211
213
 
212
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
214
+ if (server && cssMap.has(id) && cssMap.get(id) !== source) {
213
215
  const {
214
216
  moduleGraph
215
217
  } = server;
@@ -221,12 +223,12 @@ function vanillaExtractPlugin({
221
223
 
222
224
  server.ws.send({
223
225
  type: 'custom',
224
- event: styleUpdateEvent(fileId),
226
+ event: styleUpdateEvent(id),
225
227
  data: cssSource
226
228
  });
227
229
  }
228
230
 
229
- cssMap.set(fileId, cssSource);
231
+ cssMap.set(id, cssSource);
230
232
  return `import "${id}";`;
231
233
  }
232
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 { getPackageInfo, parseFileScope, cssFileFilter, addFileScope, compile, processVanillaFile, stringifyFileScope } from '@vanilla-extract/integration';
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.indexOf(virtualPrefix) === 0) {
87
- return id;
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.indexOf(virtualPrefix) === 0) {
93
- const fileScopeId = id.slice(virtualPrefix.length, id.indexOf(virtualExt));
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
- const css = cssMap.get(fileScopeId);
99
+ const cssFileId = id.slice(config.root.length + 1);
100
+ const css = cssMap.get(cssFileId);
100
101
 
101
- if (!server) {
102
- return css;
103
- }
102
+ if (typeof css !== 'string') {
103
+ return;
104
+ }
104
105
 
105
- const fileScope = parseFileScope(fileScopeId);
106
- return outdent`
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 null;
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(path.relative(packageInfo.dirname, validId)),
145
- packageInfo
146
- }).source;
147
+ filePath: normalizePath(validId),
148
+ rootPath: config.root
149
+ });
147
150
  }
148
151
 
149
152
  const {
@@ -170,8 +173,7 @@ function vanillaExtractPlugin({
170
173
  fileScope,
171
174
  source
172
175
  }) => {
173
- const fileId = stringifyFileScope(fileScope);
174
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
176
+ const id = `${fileScope.filePath}${virtualExt}`;
175
177
  let cssSource = source;
176
178
 
177
179
  if (postCssConfig) {
@@ -182,7 +184,7 @@ function vanillaExtractPlugin({
182
184
  cssSource = postCssResult.css;
183
185
  }
184
186
 
185
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
187
+ if (server && cssMap.has(id) && cssMap.get(id) !== source) {
186
188
  const {
187
189
  moduleGraph
188
190
  } = server;
@@ -194,12 +196,12 @@ function vanillaExtractPlugin({
194
196
 
195
197
  server.ws.send({
196
198
  type: 'custom',
197
- event: styleUpdateEvent(fileId),
199
+ event: styleUpdateEvent(id),
198
200
  data: cssSource
199
201
  });
200
202
  }
201
203
 
202
- cssMap.set(fileId, cssSource);
204
+ cssMap.set(id, cssSource);
203
205
  return `import "${id}";`;
204
206
  }
205
207
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.1.2",
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": "^2.0.1",
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"