@vanilla-extract/vite-plugin 3.2.1 → 3.4.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/README.md CHANGED
@@ -74,7 +74,7 @@ document.write(`
74
74
 
75
75
  ---
76
76
 
77
- Want to work at a higher level while maximising style re-use? Check out 🍨 [Sprinkles](https://vanilla-extract.style/documentation/sprinkles-api), our official zero-runtime atomic CSS framework, built on top of vanilla-extract.
77
+ Want to work at a higher level while maximising style re-use? Check out 🍨 [Sprinkles](https://vanilla-extract.style/documentation/packages/sprinkles), our official zero-runtime atomic CSS framework, built on top of vanilla-extract.
78
78
 
79
79
  ---
80
80
 
@@ -639,7 +639,7 @@ export const themeB = createTheme(vars, {
639
639
  });
640
640
  ```
641
641
 
642
- > 💡 All theme variants must provide a value for every variable or it’s a type error.
642
+ > 💡 All theme values must be provided or it’s a type error.
643
643
 
644
644
  ### createGlobalTheme
645
645
 
@@ -662,7 +662,7 @@ export const vars = createGlobalTheme(':root', {
662
662
  });
663
663
  ```
664
664
 
665
- > 💡 All theme variants must provide a value for every variable or it’s a type error.
665
+ > 💡 All theme values must be provided or it’s a type error.
666
666
 
667
667
  If you want to implement an existing theme contract, you can pass it as the second argument.
668
668
 
@@ -857,7 +857,7 @@ export const exampleStyle = style({
857
857
  });
858
858
  ```
859
859
 
860
- Scoped variables can be set via the `vars` property on style objects.
860
+ Scoped variables can be set using the `vars` key.
861
861
 
862
862
  ```ts
863
863
  import { createVar, style } from '@vanilla-extract/css';
@@ -1044,7 +1044,7 @@ document.write(`
1044
1044
 
1045
1045
  Your recipe configuration can also make use of existing variables, classes and styles.
1046
1046
 
1047
- For example, you can pass in the result of your [`sprinkles`](https://vanilla-extract.style/documentation/sprinkles-api) function directly.
1047
+ For example, you can pass in the result of your [`sprinkles`](https://vanilla-extract.style/documentation/packages/sprinkles) function directly.
1048
1048
 
1049
1049
  ```ts
1050
1050
  import { recipe } from '@vanilla-extract/recipes';
@@ -68,6 +68,8 @@ const resolvePostcssConfig = async config => {
68
68
 
69
69
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
70
70
 
71
+ const virtualExtCss = '.vanilla.css';
72
+ const virtualExtJs = '.vanilla.js';
71
73
  function vanillaExtractPlugin({
72
74
  identifiers,
73
75
  esbuildOptions
@@ -76,7 +78,7 @@ function vanillaExtractPlugin({
76
78
  let server;
77
79
  let postCssConfig;
78
80
  const cssMap = new Map();
79
- let virtualExt;
81
+ let forceEmitCssInSsrBuild = !!process.env.VITE_RSC_BUILD;
80
82
  let packageName;
81
83
 
82
84
  const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
@@ -109,60 +111,71 @@ function vanillaExtractPlugin({
109
111
  postCssConfig = await resolvePostcssConfig(config);
110
112
  }
111
113
 
112
- virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
114
+ if (config.plugins.some(p => p.name === 'astro:build')) {
115
+ forceEmitCssInSsrBuild = true;
116
+ }
113
117
  },
114
118
 
115
119
  resolveId(source) {
116
- if (!source.endsWith(virtualExt)) {
120
+ const [validId, query] = source.split('?');
121
+
122
+ if (!validId.endsWith(virtualExtCss) && !validId.endsWith(virtualExtJs)) {
117
123
  return;
118
124
  } // Absolute paths seem to occur often in monorepos, where files are
119
125
  // imported from outside the config root.
120
126
 
121
127
 
122
- const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
128
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId); // There should always be an entry in the `cssMap` here.
123
129
  // The only valid scenario for a missing one is if someone had written
124
130
  // a file in their app using the .vanilla.js/.vanilla.css extension
125
131
 
126
132
  if (cssMap.has(absoluteId)) {
127
- return absoluteId;
133
+ // Keep the original query string for HMR.
134
+ return absoluteId + (query ? `?${query}` : '');
128
135
  }
129
136
  },
130
137
 
131
138
  load(id) {
132
- if (!cssMap.has(id)) {
139
+ const [validId] = id.split('?');
140
+
141
+ if (!cssMap.has(validId)) {
133
142
  return;
134
143
  }
135
144
 
136
- const css = cssMap.get(id);
145
+ const css = cssMap.get(validId);
137
146
 
138
147
  if (typeof css !== 'string') {
139
148
  return;
140
149
  }
141
150
 
142
- if (!server || server.config.isProduction) {
151
+ if (validId.endsWith(virtualExtCss)) {
143
152
  return css;
144
153
  }
145
154
 
146
155
  return outdent__default["default"]`
147
156
  import { injectStyles } from '@vanilla-extract/css/injectStyles';
148
-
157
+
149
158
  const inject = (css) => injectStyles({
150
159
  fileScope: ${JSON.stringify({
151
- filePath: id
160
+ filePath: validId
152
161
  })},
153
162
  css
154
163
  });
155
164
 
156
165
  inject(${JSON.stringify(css)});
157
166
 
158
- import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
159
- inject(css);
160
- });
167
+ if (import.meta.hot) {
168
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
169
+ inject(css);
170
+ });
171
+ }
161
172
  `;
162
173
  },
163
174
 
164
175
  async transform(code, id, ssrParam) {
165
- if (!integration.cssFileFilter.test(id)) {
176
+ const [validId] = id.split('?');
177
+
178
+ if (!integration.cssFileFilter.test(validId)) {
166
179
  return null;
167
180
  }
168
181
 
@@ -174,10 +187,7 @@ function vanillaExtractPlugin({
174
187
  ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
175
188
  }
176
189
 
177
- const index = id.indexOf('?');
178
- const validId = index === -1 ? id : id.substring(0, index);
179
-
180
- if (ssr) {
190
+ if (ssr && !forceEmitCssInSsrBuild) {
181
191
  return integration.addFileScope({
182
192
  source: code,
183
193
  filePath: vite.normalizePath(validId),
@@ -198,7 +208,7 @@ function vanillaExtractPlugin({
198
208
  for (const file of watchFiles) {
199
209
  // In start mode, we need to prevent the file from rewatching itself.
200
210
  // If it's a `build --watch`, it needs to watch everything.
201
- if (config.command === 'build' || file !== id) {
211
+ if (config.command === 'build' || file !== validId) {
202
212
  this.addWatchFile(file);
203
213
  }
204
214
  }
@@ -211,7 +221,7 @@ function vanillaExtractPlugin({
211
221
  fileScope,
212
222
  source
213
223
  }) => {
214
- const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
224
+ const rootRelativeId = `${fileScope.filePath}${config.command === 'build' || ssr && forceEmitCssInSsrBuild ? virtualExtCss : virtualExtJs}`;
215
225
  const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
216
226
  let cssSource = source;
217
227
 
@@ -227,10 +237,12 @@ function vanillaExtractPlugin({
227
237
  const {
228
238
  moduleGraph
229
239
  } = server;
230
- const module = moduleGraph.getModuleById(absoluteId);
240
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || []);
231
241
 
232
242
  if (module) {
233
- moduleGraph.invalidateModule(module);
243
+ moduleGraph.invalidateModule(module); // Vite uses this timestamp to add `?t=` query string automatically for HMR.
244
+
245
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
234
246
  }
235
247
 
236
248
  server.ws.send({
@@ -68,6 +68,8 @@ const resolvePostcssConfig = async config => {
68
68
 
69
69
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
70
70
 
71
+ const virtualExtCss = '.vanilla.css';
72
+ const virtualExtJs = '.vanilla.js';
71
73
  function vanillaExtractPlugin({
72
74
  identifiers,
73
75
  esbuildOptions
@@ -76,7 +78,7 @@ function vanillaExtractPlugin({
76
78
  let server;
77
79
  let postCssConfig;
78
80
  const cssMap = new Map();
79
- let virtualExt;
81
+ let forceEmitCssInSsrBuild = !!process.env.VITE_RSC_BUILD;
80
82
  let packageName;
81
83
 
82
84
  const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
@@ -109,60 +111,71 @@ function vanillaExtractPlugin({
109
111
  postCssConfig = await resolvePostcssConfig(config);
110
112
  }
111
113
 
112
- virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
114
+ if (config.plugins.some(p => p.name === 'astro:build')) {
115
+ forceEmitCssInSsrBuild = true;
116
+ }
113
117
  },
114
118
 
115
119
  resolveId(source) {
116
- if (!source.endsWith(virtualExt)) {
120
+ const [validId, query] = source.split('?');
121
+
122
+ if (!validId.endsWith(virtualExtCss) && !validId.endsWith(virtualExtJs)) {
117
123
  return;
118
124
  } // Absolute paths seem to occur often in monorepos, where files are
119
125
  // imported from outside the config root.
120
126
 
121
127
 
122
- const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
128
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId); // There should always be an entry in the `cssMap` here.
123
129
  // The only valid scenario for a missing one is if someone had written
124
130
  // a file in their app using the .vanilla.js/.vanilla.css extension
125
131
 
126
132
  if (cssMap.has(absoluteId)) {
127
- return absoluteId;
133
+ // Keep the original query string for HMR.
134
+ return absoluteId + (query ? `?${query}` : '');
128
135
  }
129
136
  },
130
137
 
131
138
  load(id) {
132
- if (!cssMap.has(id)) {
139
+ const [validId] = id.split('?');
140
+
141
+ if (!cssMap.has(validId)) {
133
142
  return;
134
143
  }
135
144
 
136
- const css = cssMap.get(id);
145
+ const css = cssMap.get(validId);
137
146
 
138
147
  if (typeof css !== 'string') {
139
148
  return;
140
149
  }
141
150
 
142
- if (!server || server.config.isProduction) {
151
+ if (validId.endsWith(virtualExtCss)) {
143
152
  return css;
144
153
  }
145
154
 
146
155
  return outdent__default["default"]`
147
156
  import { injectStyles } from '@vanilla-extract/css/injectStyles';
148
-
157
+
149
158
  const inject = (css) => injectStyles({
150
159
  fileScope: ${JSON.stringify({
151
- filePath: id
160
+ filePath: validId
152
161
  })},
153
162
  css
154
163
  });
155
164
 
156
165
  inject(${JSON.stringify(css)});
157
166
 
158
- import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
159
- inject(css);
160
- });
167
+ if (import.meta.hot) {
168
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
169
+ inject(css);
170
+ });
171
+ }
161
172
  `;
162
173
  },
163
174
 
164
175
  async transform(code, id, ssrParam) {
165
- if (!integration.cssFileFilter.test(id)) {
176
+ const [validId] = id.split('?');
177
+
178
+ if (!integration.cssFileFilter.test(validId)) {
166
179
  return null;
167
180
  }
168
181
 
@@ -174,10 +187,7 @@ function vanillaExtractPlugin({
174
187
  ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
175
188
  }
176
189
 
177
- const index = id.indexOf('?');
178
- const validId = index === -1 ? id : id.substring(0, index);
179
-
180
- if (ssr) {
190
+ if (ssr && !forceEmitCssInSsrBuild) {
181
191
  return integration.addFileScope({
182
192
  source: code,
183
193
  filePath: vite.normalizePath(validId),
@@ -198,7 +208,7 @@ function vanillaExtractPlugin({
198
208
  for (const file of watchFiles) {
199
209
  // In start mode, we need to prevent the file from rewatching itself.
200
210
  // If it's a `build --watch`, it needs to watch everything.
201
- if (config.command === 'build' || file !== id) {
211
+ if (config.command === 'build' || file !== validId) {
202
212
  this.addWatchFile(file);
203
213
  }
204
214
  }
@@ -211,7 +221,7 @@ function vanillaExtractPlugin({
211
221
  fileScope,
212
222
  source
213
223
  }) => {
214
- const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
224
+ const rootRelativeId = `${fileScope.filePath}${config.command === 'build' || ssr && forceEmitCssInSsrBuild ? virtualExtCss : virtualExtJs}`;
215
225
  const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
216
226
  let cssSource = source;
217
227
 
@@ -227,10 +237,12 @@ function vanillaExtractPlugin({
227
237
  const {
228
238
  moduleGraph
229
239
  } = server;
230
- const module = moduleGraph.getModuleById(absoluteId);
240
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || []);
231
241
 
232
242
  if (module) {
233
- moduleGraph.invalidateModule(module);
243
+ moduleGraph.invalidateModule(module); // Vite uses this timestamp to add `?t=` query string automatically for HMR.
244
+
245
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
234
246
  }
235
247
 
236
248
  server.ws.send({
@@ -41,6 +41,8 @@ const resolvePostcssConfig = async config => {
41
41
 
42
42
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
43
43
 
44
+ const virtualExtCss = '.vanilla.css';
45
+ const virtualExtJs = '.vanilla.js';
44
46
  function vanillaExtractPlugin({
45
47
  identifiers,
46
48
  esbuildOptions
@@ -49,7 +51,7 @@ function vanillaExtractPlugin({
49
51
  let server;
50
52
  let postCssConfig;
51
53
  const cssMap = new Map();
52
- let virtualExt;
54
+ let forceEmitCssInSsrBuild = !!process.env.VITE_RSC_BUILD;
53
55
  let packageName;
54
56
 
55
57
  const getAbsoluteVirtualFileId = source => normalizePath(path.join(config.root, source));
@@ -82,60 +84,71 @@ function vanillaExtractPlugin({
82
84
  postCssConfig = await resolvePostcssConfig(config);
83
85
  }
84
86
 
85
- virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
87
+ if (config.plugins.some(p => p.name === 'astro:build')) {
88
+ forceEmitCssInSsrBuild = true;
89
+ }
86
90
  },
87
91
 
88
92
  resolveId(source) {
89
- if (!source.endsWith(virtualExt)) {
93
+ const [validId, query] = source.split('?');
94
+
95
+ if (!validId.endsWith(virtualExtCss) && !validId.endsWith(virtualExtJs)) {
90
96
  return;
91
97
  } // Absolute paths seem to occur often in monorepos, where files are
92
98
  // imported from outside the config root.
93
99
 
94
100
 
95
- const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
101
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId); // There should always be an entry in the `cssMap` here.
96
102
  // The only valid scenario for a missing one is if someone had written
97
103
  // a file in their app using the .vanilla.js/.vanilla.css extension
98
104
 
99
105
  if (cssMap.has(absoluteId)) {
100
- return absoluteId;
106
+ // Keep the original query string for HMR.
107
+ return absoluteId + (query ? `?${query}` : '');
101
108
  }
102
109
  },
103
110
 
104
111
  load(id) {
105
- if (!cssMap.has(id)) {
112
+ const [validId] = id.split('?');
113
+
114
+ if (!cssMap.has(validId)) {
106
115
  return;
107
116
  }
108
117
 
109
- const css = cssMap.get(id);
118
+ const css = cssMap.get(validId);
110
119
 
111
120
  if (typeof css !== 'string') {
112
121
  return;
113
122
  }
114
123
 
115
- if (!server || server.config.isProduction) {
124
+ if (validId.endsWith(virtualExtCss)) {
116
125
  return css;
117
126
  }
118
127
 
119
128
  return outdent`
120
129
  import { injectStyles } from '@vanilla-extract/css/injectStyles';
121
-
130
+
122
131
  const inject = (css) => injectStyles({
123
132
  fileScope: ${JSON.stringify({
124
- filePath: id
133
+ filePath: validId
125
134
  })},
126
135
  css
127
136
  });
128
137
 
129
138
  inject(${JSON.stringify(css)});
130
139
 
131
- import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
132
- inject(css);
133
- });
140
+ if (import.meta.hot) {
141
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
142
+ inject(css);
143
+ });
144
+ }
134
145
  `;
135
146
  },
136
147
 
137
148
  async transform(code, id, ssrParam) {
138
- if (!cssFileFilter.test(id)) {
149
+ const [validId] = id.split('?');
150
+
151
+ if (!cssFileFilter.test(validId)) {
139
152
  return null;
140
153
  }
141
154
 
@@ -147,10 +160,7 @@ function vanillaExtractPlugin({
147
160
  ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
148
161
  }
149
162
 
150
- const index = id.indexOf('?');
151
- const validId = index === -1 ? id : id.substring(0, index);
152
-
153
- if (ssr) {
163
+ if (ssr && !forceEmitCssInSsrBuild) {
154
164
  return addFileScope({
155
165
  source: code,
156
166
  filePath: normalizePath(validId),
@@ -171,7 +181,7 @@ function vanillaExtractPlugin({
171
181
  for (const file of watchFiles) {
172
182
  // In start mode, we need to prevent the file from rewatching itself.
173
183
  // If it's a `build --watch`, it needs to watch everything.
174
- if (config.command === 'build' || file !== id) {
184
+ if (config.command === 'build' || file !== validId) {
175
185
  this.addWatchFile(file);
176
186
  }
177
187
  }
@@ -184,7 +194,7 @@ function vanillaExtractPlugin({
184
194
  fileScope,
185
195
  source
186
196
  }) => {
187
- const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
197
+ const rootRelativeId = `${fileScope.filePath}${config.command === 'build' || ssr && forceEmitCssInSsrBuild ? virtualExtCss : virtualExtJs}`;
188
198
  const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
189
199
  let cssSource = source;
190
200
 
@@ -200,10 +210,12 @@ function vanillaExtractPlugin({
200
210
  const {
201
211
  moduleGraph
202
212
  } = server;
203
- const module = moduleGraph.getModuleById(absoluteId);
213
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || []);
204
214
 
205
215
  if (module) {
206
- moduleGraph.invalidateModule(module);
216
+ moduleGraph.invalidateModule(module); // Vite uses this timestamp to add `?t=` query string automatically for HMR.
217
+
218
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
207
219
  }
208
220
 
209
221
  server.ws.send({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.2.1",
3
+ "version": "3.4.0",
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",