@vanilla-extract/vite-plugin 3.1.3 → 3.1.6

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.
@@ -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,16 +68,15 @@ 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();
80
78
  let virtualExt;
79
+ let packageName;
81
80
  return {
82
81
  name: 'vanilla-extract',
83
82
  enforce: 'pre',
@@ -100,53 +99,59 @@ function vanillaExtractPlugin({
100
99
 
101
100
  async configResolved(resolvedConfig) {
102
101
  config = resolvedConfig;
102
+ packageName = integration.getPackageInfo(config.root).name;
103
103
 
104
104
  if (config.command === 'serve') {
105
105
  postCssConfig = await resolvePostcssConfig(config);
106
106
  }
107
107
 
108
108
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
109
- packageInfo = integration.getPackageInfo(config.root);
110
109
  },
111
110
 
112
111
  resolveId(id) {
113
- if (id.indexOf(virtualPrefix) === 0) {
114
- return id;
112
+ if (!id.endsWith(virtualExt)) {
113
+ return;
114
+ }
115
+
116
+ const normalizedId = id.startsWith('/') ? id.slice(1) : id;
117
+
118
+ if (cssMap.has(normalizedId)) {
119
+ return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
115
120
  }
116
121
  },
117
122
 
118
123
  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
- }
124
+ if (!id.endsWith(virtualExt)) {
125
+ return;
126
+ }
125
127
 
126
- const css = cssMap.get(fileScopeId);
128
+ const cssFileId = id.slice(config.root.length + 1);
129
+ const css = cssMap.get(cssFileId);
127
130
 
128
- if (!server) {
129
- return css;
130
- }
131
+ if (typeof css !== 'string') {
132
+ return;
133
+ }
131
134
 
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
- `;
135
+ if (!server) {
136
+ return css;
147
137
  }
148
138
 
149
- return null;
139
+ return outdent__default["default"]`
140
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
141
+
142
+ const inject = (css) => injectStyles({
143
+ fileScope: ${JSON.stringify({
144
+ filePath: cssFileId
145
+ })},
146
+ css
147
+ });
148
+
149
+ inject(${JSON.stringify(css)});
150
+
151
+ import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
152
+ inject(css);
153
+ });
154
+ `;
150
155
  },
151
156
 
152
157
  async transform(code, id, ssrParam) {
@@ -168,9 +173,10 @@ function vanillaExtractPlugin({
168
173
  if (ssr) {
169
174
  return integration.addFileScope({
170
175
  source: code,
171
- filePath: vite.normalizePath(path__default["default"].relative(packageInfo.dirname, validId)),
172
- packageInfo
173
- }).source;
176
+ filePath: vite.normalizePath(validId),
177
+ rootPath: config.root,
178
+ packageName
179
+ });
174
180
  }
175
181
 
176
182
  const {
@@ -197,8 +203,7 @@ function vanillaExtractPlugin({
197
203
  fileScope,
198
204
  source
199
205
  }) => {
200
- const fileId = integration.stringifyFileScope(fileScope);
201
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
206
+ const id = `${fileScope.filePath}${virtualExt}`;
202
207
  let cssSource = source;
203
208
 
204
209
  if (postCssConfig) {
@@ -209,7 +214,7 @@ function vanillaExtractPlugin({
209
214
  cssSource = postCssResult.css;
210
215
  }
211
216
 
212
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
217
+ if (server && cssMap.has(id) && cssMap.get(id) !== source) {
213
218
  const {
214
219
  moduleGraph
215
220
  } = server;
@@ -221,12 +226,12 @@ function vanillaExtractPlugin({
221
226
 
222
227
  server.ws.send({
223
228
  type: 'custom',
224
- event: styleUpdateEvent(fileId),
229
+ event: styleUpdateEvent(id),
225
230
  data: cssSource
226
231
  });
227
232
  }
228
233
 
229
- cssMap.set(fileId, cssSource);
234
+ cssMap.set(id, cssSource);
230
235
  return `import "${id}";`;
231
236
  }
232
237
  });
@@ -68,16 +68,15 @@ 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();
80
78
  let virtualExt;
79
+ let packageName;
81
80
  return {
82
81
  name: 'vanilla-extract',
83
82
  enforce: 'pre',
@@ -100,53 +99,59 @@ function vanillaExtractPlugin({
100
99
 
101
100
  async configResolved(resolvedConfig) {
102
101
  config = resolvedConfig;
102
+ packageName = integration.getPackageInfo(config.root).name;
103
103
 
104
104
  if (config.command === 'serve') {
105
105
  postCssConfig = await resolvePostcssConfig(config);
106
106
  }
107
107
 
108
108
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
109
- packageInfo = integration.getPackageInfo(config.root);
110
109
  },
111
110
 
112
111
  resolveId(id) {
113
- if (id.indexOf(virtualPrefix) === 0) {
114
- return id;
112
+ if (!id.endsWith(virtualExt)) {
113
+ return;
114
+ }
115
+
116
+ const normalizedId = id.startsWith('/') ? id.slice(1) : id;
117
+
118
+ if (cssMap.has(normalizedId)) {
119
+ return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
115
120
  }
116
121
  },
117
122
 
118
123
  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
- }
124
+ if (!id.endsWith(virtualExt)) {
125
+ return;
126
+ }
125
127
 
126
- const css = cssMap.get(fileScopeId);
128
+ const cssFileId = id.slice(config.root.length + 1);
129
+ const css = cssMap.get(cssFileId);
127
130
 
128
- if (!server) {
129
- return css;
130
- }
131
+ if (typeof css !== 'string') {
132
+ return;
133
+ }
131
134
 
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
- `;
135
+ if (!server) {
136
+ return css;
147
137
  }
148
138
 
149
- return null;
139
+ return outdent__default["default"]`
140
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
141
+
142
+ const inject = (css) => injectStyles({
143
+ fileScope: ${JSON.stringify({
144
+ filePath: cssFileId
145
+ })},
146
+ css
147
+ });
148
+
149
+ inject(${JSON.stringify(css)});
150
+
151
+ import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
152
+ inject(css);
153
+ });
154
+ `;
150
155
  },
151
156
 
152
157
  async transform(code, id, ssrParam) {
@@ -168,9 +173,10 @@ function vanillaExtractPlugin({
168
173
  if (ssr) {
169
174
  return integration.addFileScope({
170
175
  source: code,
171
- filePath: vite.normalizePath(path__default["default"].relative(packageInfo.dirname, validId)),
172
- packageInfo
173
- }).source;
176
+ filePath: vite.normalizePath(validId),
177
+ rootPath: config.root,
178
+ packageName
179
+ });
174
180
  }
175
181
 
176
182
  const {
@@ -197,8 +203,7 @@ function vanillaExtractPlugin({
197
203
  fileScope,
198
204
  source
199
205
  }) => {
200
- const fileId = integration.stringifyFileScope(fileScope);
201
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
206
+ const id = `${fileScope.filePath}${virtualExt}`;
202
207
  let cssSource = source;
203
208
 
204
209
  if (postCssConfig) {
@@ -209,7 +214,7 @@ function vanillaExtractPlugin({
209
214
  cssSource = postCssResult.css;
210
215
  }
211
216
 
212
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
217
+ if (server && cssMap.has(id) && cssMap.get(id) !== source) {
213
218
  const {
214
219
  moduleGraph
215
220
  } = server;
@@ -221,12 +226,12 @@ function vanillaExtractPlugin({
221
226
 
222
227
  server.ws.send({
223
228
  type: 'custom',
224
- event: styleUpdateEvent(fileId),
229
+ event: styleUpdateEvent(id),
225
230
  data: cssSource
226
231
  });
227
232
  }
228
233
 
229
- cssMap.set(fileId, cssSource);
234
+ cssMap.set(id, cssSource);
230
235
  return `import "${id}";`;
231
236
  }
232
237
  });
@@ -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 { getPackageInfo, 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,16 +41,15 @@ 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();
53
51
  let virtualExt;
52
+ let packageName;
54
53
  return {
55
54
  name: 'vanilla-extract',
56
55
  enforce: 'pre',
@@ -73,53 +72,59 @@ function vanillaExtractPlugin({
73
72
 
74
73
  async configResolved(resolvedConfig) {
75
74
  config = resolvedConfig;
75
+ packageName = getPackageInfo(config.root).name;
76
76
 
77
77
  if (config.command === 'serve') {
78
78
  postCssConfig = await resolvePostcssConfig(config);
79
79
  }
80
80
 
81
81
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
82
- packageInfo = getPackageInfo(config.root);
83
82
  },
84
83
 
85
84
  resolveId(id) {
86
- if (id.indexOf(virtualPrefix) === 0) {
87
- return id;
85
+ if (!id.endsWith(virtualExt)) {
86
+ return;
87
+ }
88
+
89
+ const normalizedId = id.startsWith('/') ? id.slice(1) : id;
90
+
91
+ if (cssMap.has(normalizedId)) {
92
+ return normalizePath(path.join(config.root, normalizedId));
88
93
  }
89
94
  },
90
95
 
91
96
  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
- }
97
+ if (!id.endsWith(virtualExt)) {
98
+ return;
99
+ }
98
100
 
99
- const css = cssMap.get(fileScopeId);
101
+ const cssFileId = id.slice(config.root.length + 1);
102
+ const css = cssMap.get(cssFileId);
100
103
 
101
- if (!server) {
102
- return css;
103
- }
104
+ if (typeof css !== 'string') {
105
+ return;
106
+ }
104
107
 
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
- `;
108
+ if (!server) {
109
+ return css;
120
110
  }
121
111
 
122
- return null;
112
+ return outdent`
113
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
114
+
115
+ const inject = (css) => injectStyles({
116
+ fileScope: ${JSON.stringify({
117
+ filePath: cssFileId
118
+ })},
119
+ css
120
+ });
121
+
122
+ inject(${JSON.stringify(css)});
123
+
124
+ import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
125
+ inject(css);
126
+ });
127
+ `;
123
128
  },
124
129
 
125
130
  async transform(code, id, ssrParam) {
@@ -141,9 +146,10 @@ function vanillaExtractPlugin({
141
146
  if (ssr) {
142
147
  return addFileScope({
143
148
  source: code,
144
- filePath: normalizePath(path.relative(packageInfo.dirname, validId)),
145
- packageInfo
146
- }).source;
149
+ filePath: normalizePath(validId),
150
+ rootPath: config.root,
151
+ packageName
152
+ });
147
153
  }
148
154
 
149
155
  const {
@@ -170,8 +176,7 @@ function vanillaExtractPlugin({
170
176
  fileScope,
171
177
  source
172
178
  }) => {
173
- const fileId = stringifyFileScope(fileScope);
174
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
179
+ const id = `${fileScope.filePath}${virtualExt}`;
175
180
  let cssSource = source;
176
181
 
177
182
  if (postCssConfig) {
@@ -182,7 +187,7 @@ function vanillaExtractPlugin({
182
187
  cssSource = postCssResult.css;
183
188
  }
184
189
 
185
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
190
+ if (server && cssMap.has(id) && cssMap.get(id) !== source) {
186
191
  const {
187
192
  moduleGraph
188
193
  } = server;
@@ -194,12 +199,12 @@ function vanillaExtractPlugin({
194
199
 
195
200
  server.ws.send({
196
201
  type: 'custom',
197
- event: styleUpdateEvent(fileId),
202
+ event: styleUpdateEvent(id),
198
203
  data: cssSource
199
204
  });
200
205
  }
201
206
 
202
- cssMap.set(fileId, cssSource);
207
+ cssMap.set(id, cssSource);
203
208
  return `import "${id}";`;
204
209
  }
205
210
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.1.3",
3
+ "version": "3.1.6",
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": "^3.0.0",
18
+ "@vanilla-extract/integration": "^4.0.1",
19
19
  "outdent": "^0.8.0",
20
20
  "postcss": "^8.3.6",
21
21
  "postcss-load-config": "^3.1.0"