@vanilla-extract/vite-plugin 3.1.6 → 3.2.1

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
@@ -88,6 +88,7 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
88
88
  - [Test environments](#test-environments)
89
89
  - [Configuration](#configuration)
90
90
  - [identifiers](#identifiers)
91
+ - [esbuildOptions](#esbuildoptions)
91
92
  - [Styling API](#styling-api)
92
93
  - [style](#style)
93
94
  - [styleVariants](#stylevariants)
@@ -393,6 +394,12 @@ Different formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc)
393
394
 
394
395
  Each integration will set a default value based on the configuration options passed to the bundler.
395
396
 
397
+ ### esbuildOptions
398
+ > Only for `esbuild`, `vite` and `rollup` plugins
399
+
400
+ esbuild is used internally to compile `.css.ts` files before evaluating them to extract styles. You can pass additional options here to customize that process.
401
+ Accepts a subset of esbuild build options (`plugins`, `external`, `define` and `loader`), see https://esbuild.github.io/api/#build-api.
402
+
396
403
  ---
397
404
 
398
405
  ## Styling API
@@ -1,7 +1,8 @@
1
1
  import type { Plugin } from 'vite';
2
- import { IdentifierOption } from '@vanilla-extract/integration';
2
+ import { IdentifierOption, CompileOptions } from '@vanilla-extract/integration';
3
3
  interface Options {
4
4
  identifiers?: IdentifierOption;
5
+ esbuildOptions?: CompileOptions['esbuildOptions'];
5
6
  }
6
- export declare function vanillaExtractPlugin({ identifiers }?: Options): Plugin;
7
+ export declare function vanillaExtractPlugin({ identifiers, esbuildOptions, }?: Options): Plugin;
7
8
  export {};
@@ -69,7 +69,8 @@ const resolvePostcssConfig = async config => {
69
69
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
70
70
 
71
71
  function vanillaExtractPlugin({
72
- identifiers
72
+ identifiers,
73
+ esbuildOptions
73
74
  } = {}) {
74
75
  let config;
75
76
  let server;
@@ -77,6 +78,9 @@ function vanillaExtractPlugin({
77
78
  const cssMap = new Map();
78
79
  let virtualExt;
79
80
  let packageName;
81
+
82
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
83
+
80
84
  return {
81
85
  name: 'vanilla-extract',
82
86
  enforce: 'pre',
@@ -108,31 +112,34 @@ function vanillaExtractPlugin({
108
112
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
109
113
  },
110
114
 
111
- resolveId(id) {
112
- if (!id.endsWith(virtualExt)) {
115
+ resolveId(source) {
116
+ if (!source.endsWith(virtualExt)) {
113
117
  return;
114
- }
118
+ } // Absolute paths seem to occur often in monorepos, where files are
119
+ // imported from outside the config root.
120
+
115
121
 
116
- const normalizedId = id.startsWith('/') ? id.slice(1) : id;
122
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
123
+ // The only valid scenario for a missing one is if someone had written
124
+ // a file in their app using the .vanilla.js/.vanilla.css extension
117
125
 
118
- if (cssMap.has(normalizedId)) {
119
- return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
126
+ if (cssMap.has(absoluteId)) {
127
+ return absoluteId;
120
128
  }
121
129
  },
122
130
 
123
131
  load(id) {
124
- if (!id.endsWith(virtualExt)) {
132
+ if (!cssMap.has(id)) {
125
133
  return;
126
134
  }
127
135
 
128
- const cssFileId = id.slice(config.root.length + 1);
129
- const css = cssMap.get(cssFileId);
136
+ const css = cssMap.get(id);
130
137
 
131
138
  if (typeof css !== 'string') {
132
139
  return;
133
140
  }
134
141
 
135
- if (!server) {
142
+ if (!server || server.config.isProduction) {
136
143
  return css;
137
144
  }
138
145
 
@@ -141,14 +148,14 @@ function vanillaExtractPlugin({
141
148
 
142
149
  const inject = (css) => injectStyles({
143
150
  fileScope: ${JSON.stringify({
144
- filePath: cssFileId
151
+ filePath: id
145
152
  })},
146
153
  css
147
154
  });
148
155
 
149
156
  inject(${JSON.stringify(css)});
150
157
 
151
- import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
158
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
152
159
  inject(css);
153
160
  });
154
161
  `;
@@ -184,7 +191,8 @@ function vanillaExtractPlugin({
184
191
  watchFiles
185
192
  } = await integration.compile({
186
193
  filePath: validId,
187
- cwd: config.root
194
+ cwd: config.root,
195
+ esbuildOptions
188
196
  });
189
197
 
190
198
  for (const file of watchFiles) {
@@ -195,7 +203,7 @@ function vanillaExtractPlugin({
195
203
  }
196
204
  }
197
205
 
198
- return integration.processVanillaFile({
206
+ const output = await integration.processVanillaFile({
199
207
  source,
200
208
  filePath: validId,
201
209
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -203,7 +211,8 @@ function vanillaExtractPlugin({
203
211
  fileScope,
204
212
  source
205
213
  }) => {
206
- const id = `${fileScope.filePath}${virtualExt}`;
214
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
215
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
207
216
  let cssSource = source;
208
217
 
209
218
  if (postCssConfig) {
@@ -214,11 +223,11 @@ function vanillaExtractPlugin({
214
223
  cssSource = postCssResult.css;
215
224
  }
216
225
 
217
- if (server && cssMap.has(id) && cssMap.get(id) !== source) {
226
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
218
227
  const {
219
228
  moduleGraph
220
229
  } = server;
221
- const module = moduleGraph.getModuleById(id);
230
+ const module = moduleGraph.getModuleById(absoluteId);
222
231
 
223
232
  if (module) {
224
233
  moduleGraph.invalidateModule(module);
@@ -226,15 +235,23 @@ function vanillaExtractPlugin({
226
235
 
227
236
  server.ws.send({
228
237
  type: 'custom',
229
- event: styleUpdateEvent(id),
238
+ event: styleUpdateEvent(absoluteId),
230
239
  data: cssSource
231
240
  });
232
241
  }
233
242
 
234
- cssMap.set(id, cssSource);
235
- return `import "${id}";`;
243
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
244
+ // are consistent across build machines
245
+
246
+ return `import "${rootRelativeId}";`;
236
247
  }
237
248
  });
249
+ return {
250
+ code: output,
251
+ map: {
252
+ mappings: ''
253
+ }
254
+ };
238
255
  }
239
256
 
240
257
  };
@@ -69,7 +69,8 @@ const resolvePostcssConfig = async config => {
69
69
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
70
70
 
71
71
  function vanillaExtractPlugin({
72
- identifiers
72
+ identifiers,
73
+ esbuildOptions
73
74
  } = {}) {
74
75
  let config;
75
76
  let server;
@@ -77,6 +78,9 @@ function vanillaExtractPlugin({
77
78
  const cssMap = new Map();
78
79
  let virtualExt;
79
80
  let packageName;
81
+
82
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
83
+
80
84
  return {
81
85
  name: 'vanilla-extract',
82
86
  enforce: 'pre',
@@ -108,31 +112,34 @@ function vanillaExtractPlugin({
108
112
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
109
113
  },
110
114
 
111
- resolveId(id) {
112
- if (!id.endsWith(virtualExt)) {
115
+ resolveId(source) {
116
+ if (!source.endsWith(virtualExt)) {
113
117
  return;
114
- }
118
+ } // Absolute paths seem to occur often in monorepos, where files are
119
+ // imported from outside the config root.
120
+
115
121
 
116
- const normalizedId = id.startsWith('/') ? id.slice(1) : id;
122
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
123
+ // The only valid scenario for a missing one is if someone had written
124
+ // a file in their app using the .vanilla.js/.vanilla.css extension
117
125
 
118
- if (cssMap.has(normalizedId)) {
119
- return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
126
+ if (cssMap.has(absoluteId)) {
127
+ return absoluteId;
120
128
  }
121
129
  },
122
130
 
123
131
  load(id) {
124
- if (!id.endsWith(virtualExt)) {
132
+ if (!cssMap.has(id)) {
125
133
  return;
126
134
  }
127
135
 
128
- const cssFileId = id.slice(config.root.length + 1);
129
- const css = cssMap.get(cssFileId);
136
+ const css = cssMap.get(id);
130
137
 
131
138
  if (typeof css !== 'string') {
132
139
  return;
133
140
  }
134
141
 
135
- if (!server) {
142
+ if (!server || server.config.isProduction) {
136
143
  return css;
137
144
  }
138
145
 
@@ -141,14 +148,14 @@ function vanillaExtractPlugin({
141
148
 
142
149
  const inject = (css) => injectStyles({
143
150
  fileScope: ${JSON.stringify({
144
- filePath: cssFileId
151
+ filePath: id
145
152
  })},
146
153
  css
147
154
  });
148
155
 
149
156
  inject(${JSON.stringify(css)});
150
157
 
151
- import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
158
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
152
159
  inject(css);
153
160
  });
154
161
  `;
@@ -184,7 +191,8 @@ function vanillaExtractPlugin({
184
191
  watchFiles
185
192
  } = await integration.compile({
186
193
  filePath: validId,
187
- cwd: config.root
194
+ cwd: config.root,
195
+ esbuildOptions
188
196
  });
189
197
 
190
198
  for (const file of watchFiles) {
@@ -195,7 +203,7 @@ function vanillaExtractPlugin({
195
203
  }
196
204
  }
197
205
 
198
- return integration.processVanillaFile({
206
+ const output = await integration.processVanillaFile({
199
207
  source,
200
208
  filePath: validId,
201
209
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -203,7 +211,8 @@ function vanillaExtractPlugin({
203
211
  fileScope,
204
212
  source
205
213
  }) => {
206
- const id = `${fileScope.filePath}${virtualExt}`;
214
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
215
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
207
216
  let cssSource = source;
208
217
 
209
218
  if (postCssConfig) {
@@ -214,11 +223,11 @@ function vanillaExtractPlugin({
214
223
  cssSource = postCssResult.css;
215
224
  }
216
225
 
217
- if (server && cssMap.has(id) && cssMap.get(id) !== source) {
226
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
218
227
  const {
219
228
  moduleGraph
220
229
  } = server;
221
- const module = moduleGraph.getModuleById(id);
230
+ const module = moduleGraph.getModuleById(absoluteId);
222
231
 
223
232
  if (module) {
224
233
  moduleGraph.invalidateModule(module);
@@ -226,15 +235,23 @@ function vanillaExtractPlugin({
226
235
 
227
236
  server.ws.send({
228
237
  type: 'custom',
229
- event: styleUpdateEvent(id),
238
+ event: styleUpdateEvent(absoluteId),
230
239
  data: cssSource
231
240
  });
232
241
  }
233
242
 
234
- cssMap.set(id, cssSource);
235
- return `import "${id}";`;
243
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
244
+ // are consistent across build machines
245
+
246
+ return `import "${rootRelativeId}";`;
236
247
  }
237
248
  });
249
+ return {
250
+ code: output,
251
+ map: {
252
+ mappings: ''
253
+ }
254
+ };
238
255
  }
239
256
 
240
257
  };
@@ -42,7 +42,8 @@ const resolvePostcssConfig = async config => {
42
42
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
43
43
 
44
44
  function vanillaExtractPlugin({
45
- identifiers
45
+ identifiers,
46
+ esbuildOptions
46
47
  } = {}) {
47
48
  let config;
48
49
  let server;
@@ -50,6 +51,9 @@ function vanillaExtractPlugin({
50
51
  const cssMap = new Map();
51
52
  let virtualExt;
52
53
  let packageName;
54
+
55
+ const getAbsoluteVirtualFileId = source => normalizePath(path.join(config.root, source));
56
+
53
57
  return {
54
58
  name: 'vanilla-extract',
55
59
  enforce: 'pre',
@@ -81,31 +85,34 @@ function vanillaExtractPlugin({
81
85
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
82
86
  },
83
87
 
84
- resolveId(id) {
85
- if (!id.endsWith(virtualExt)) {
88
+ resolveId(source) {
89
+ if (!source.endsWith(virtualExt)) {
86
90
  return;
87
- }
91
+ } // Absolute paths seem to occur often in monorepos, where files are
92
+ // imported from outside the config root.
93
+
88
94
 
89
- const normalizedId = id.startsWith('/') ? id.slice(1) : id;
95
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
96
+ // The only valid scenario for a missing one is if someone had written
97
+ // a file in their app using the .vanilla.js/.vanilla.css extension
90
98
 
91
- if (cssMap.has(normalizedId)) {
92
- return normalizePath(path.join(config.root, normalizedId));
99
+ if (cssMap.has(absoluteId)) {
100
+ return absoluteId;
93
101
  }
94
102
  },
95
103
 
96
104
  load(id) {
97
- if (!id.endsWith(virtualExt)) {
105
+ if (!cssMap.has(id)) {
98
106
  return;
99
107
  }
100
108
 
101
- const cssFileId = id.slice(config.root.length + 1);
102
- const css = cssMap.get(cssFileId);
109
+ const css = cssMap.get(id);
103
110
 
104
111
  if (typeof css !== 'string') {
105
112
  return;
106
113
  }
107
114
 
108
- if (!server) {
115
+ if (!server || server.config.isProduction) {
109
116
  return css;
110
117
  }
111
118
 
@@ -114,14 +121,14 @@ function vanillaExtractPlugin({
114
121
 
115
122
  const inject = (css) => injectStyles({
116
123
  fileScope: ${JSON.stringify({
117
- filePath: cssFileId
124
+ filePath: id
118
125
  })},
119
126
  css
120
127
  });
121
128
 
122
129
  inject(${JSON.stringify(css)});
123
130
 
124
- import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
131
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
125
132
  inject(css);
126
133
  });
127
134
  `;
@@ -157,7 +164,8 @@ function vanillaExtractPlugin({
157
164
  watchFiles
158
165
  } = await compile({
159
166
  filePath: validId,
160
- cwd: config.root
167
+ cwd: config.root,
168
+ esbuildOptions
161
169
  });
162
170
 
163
171
  for (const file of watchFiles) {
@@ -168,7 +176,7 @@ function vanillaExtractPlugin({
168
176
  }
169
177
  }
170
178
 
171
- return processVanillaFile({
179
+ const output = await processVanillaFile({
172
180
  source,
173
181
  filePath: validId,
174
182
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -176,7 +184,8 @@ function vanillaExtractPlugin({
176
184
  fileScope,
177
185
  source
178
186
  }) => {
179
- const id = `${fileScope.filePath}${virtualExt}`;
187
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
188
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
180
189
  let cssSource = source;
181
190
 
182
191
  if (postCssConfig) {
@@ -187,11 +196,11 @@ function vanillaExtractPlugin({
187
196
  cssSource = postCssResult.css;
188
197
  }
189
198
 
190
- if (server && cssMap.has(id) && cssMap.get(id) !== source) {
199
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
191
200
  const {
192
201
  moduleGraph
193
202
  } = server;
194
- const module = moduleGraph.getModuleById(id);
203
+ const module = moduleGraph.getModuleById(absoluteId);
195
204
 
196
205
  if (module) {
197
206
  moduleGraph.invalidateModule(module);
@@ -199,15 +208,23 @@ function vanillaExtractPlugin({
199
208
 
200
209
  server.ws.send({
201
210
  type: 'custom',
202
- event: styleUpdateEvent(id),
211
+ event: styleUpdateEvent(absoluteId),
203
212
  data: cssSource
204
213
  });
205
214
  }
206
215
 
207
- cssMap.set(id, cssSource);
208
- return `import "${id}";`;
216
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
217
+ // are consistent across build machines
218
+
219
+ return `import "${rootRelativeId}";`;
209
220
  }
210
221
  });
222
+ return {
223
+ code: output,
224
+ map: {
225
+ mappings: ''
226
+ }
227
+ };
211
228
  }
212
229
 
213
230
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.1.6",
3
+ "version": "3.2.1",
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": "^4.0.1",
18
+ "@vanilla-extract/integration": "^5.0.0",
19
19
  "outdent": "^0.8.0",
20
20
  "postcss": "^8.3.6",
21
21
  "postcss-load-config": "^3.1.0"