@vanilla-extract/vite-plugin 3.1.5 → 3.2.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
@@ -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,13 +69,18 @@ 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;
76
77
  let postCssConfig;
77
78
  const cssMap = new Map();
78
79
  let virtualExt;
80
+ let packageName;
81
+
82
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
83
+
79
84
  return {
80
85
  name: 'vanilla-extract',
81
86
  enforce: 'pre',
@@ -98,6 +103,7 @@ function vanillaExtractPlugin({
98
103
 
99
104
  async configResolved(resolvedConfig) {
100
105
  config = resolvedConfig;
106
+ packageName = integration.getPackageInfo(config.root).name;
101
107
 
102
108
  if (config.command === 'serve') {
103
109
  postCssConfig = await resolvePostcssConfig(config);
@@ -106,25 +112,28 @@ function vanillaExtractPlugin({
106
112
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
107
113
  },
108
114
 
109
- resolveId(id) {
110
- if (!id.endsWith(virtualExt)) {
115
+ resolveId(source) {
116
+ if (!source.endsWith(virtualExt)) {
111
117
  return;
112
- }
118
+ } // Absolute paths seem to occur often in monorepos, where files are
119
+ // imported from outside the config root.
120
+
113
121
 
114
- 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
115
125
 
116
- if (cssMap.has(normalizedId)) {
117
- return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
126
+ if (cssMap.has(absoluteId)) {
127
+ return absoluteId;
118
128
  }
119
129
  },
120
130
 
121
131
  load(id) {
122
- if (!id.endsWith(virtualExt)) {
132
+ if (!cssMap.has(id)) {
123
133
  return;
124
134
  }
125
135
 
126
- const cssFileId = id.slice(config.root.length + 1);
127
- const css = cssMap.get(cssFileId);
136
+ const css = cssMap.get(id);
128
137
 
129
138
  if (typeof css !== 'string') {
130
139
  return;
@@ -139,14 +148,14 @@ function vanillaExtractPlugin({
139
148
 
140
149
  const inject = (css) => injectStyles({
141
150
  fileScope: ${JSON.stringify({
142
- filePath: cssFileId
151
+ filePath: id
143
152
  })},
144
153
  css
145
154
  });
146
155
 
147
156
  inject(${JSON.stringify(css)});
148
157
 
149
- import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
158
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
150
159
  inject(css);
151
160
  });
152
161
  `;
@@ -172,7 +181,8 @@ function vanillaExtractPlugin({
172
181
  return integration.addFileScope({
173
182
  source: code,
174
183
  filePath: vite.normalizePath(validId),
175
- rootPath: config.root
184
+ rootPath: config.root,
185
+ packageName
176
186
  });
177
187
  }
178
188
 
@@ -181,7 +191,8 @@ function vanillaExtractPlugin({
181
191
  watchFiles
182
192
  } = await integration.compile({
183
193
  filePath: validId,
184
- cwd: config.root
194
+ cwd: config.root,
195
+ esbuildOptions
185
196
  });
186
197
 
187
198
  for (const file of watchFiles) {
@@ -192,7 +203,7 @@ function vanillaExtractPlugin({
192
203
  }
193
204
  }
194
205
 
195
- return integration.processVanillaFile({
206
+ const output = await integration.processVanillaFile({
196
207
  source,
197
208
  filePath: validId,
198
209
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -200,7 +211,8 @@ function vanillaExtractPlugin({
200
211
  fileScope,
201
212
  source
202
213
  }) => {
203
- const id = `${fileScope.filePath}${virtualExt}`;
214
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
215
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
204
216
  let cssSource = source;
205
217
 
206
218
  if (postCssConfig) {
@@ -211,11 +223,11 @@ function vanillaExtractPlugin({
211
223
  cssSource = postCssResult.css;
212
224
  }
213
225
 
214
- if (server && cssMap.has(id) && cssMap.get(id) !== source) {
226
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
215
227
  const {
216
228
  moduleGraph
217
229
  } = server;
218
- const module = moduleGraph.getModuleById(id);
230
+ const module = moduleGraph.getModuleById(absoluteId);
219
231
 
220
232
  if (module) {
221
233
  moduleGraph.invalidateModule(module);
@@ -223,15 +235,23 @@ function vanillaExtractPlugin({
223
235
 
224
236
  server.ws.send({
225
237
  type: 'custom',
226
- event: styleUpdateEvent(id),
238
+ event: styleUpdateEvent(absoluteId),
227
239
  data: cssSource
228
240
  });
229
241
  }
230
242
 
231
- cssMap.set(id, cssSource);
232
- 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}";`;
233
247
  }
234
248
  });
249
+ return {
250
+ code: output,
251
+ map: {
252
+ mappings: ''
253
+ }
254
+ };
235
255
  }
236
256
 
237
257
  };
@@ -69,13 +69,18 @@ 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;
76
77
  let postCssConfig;
77
78
  const cssMap = new Map();
78
79
  let virtualExt;
80
+ let packageName;
81
+
82
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
83
+
79
84
  return {
80
85
  name: 'vanilla-extract',
81
86
  enforce: 'pre',
@@ -98,6 +103,7 @@ function vanillaExtractPlugin({
98
103
 
99
104
  async configResolved(resolvedConfig) {
100
105
  config = resolvedConfig;
106
+ packageName = integration.getPackageInfo(config.root).name;
101
107
 
102
108
  if (config.command === 'serve') {
103
109
  postCssConfig = await resolvePostcssConfig(config);
@@ -106,25 +112,28 @@ function vanillaExtractPlugin({
106
112
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
107
113
  },
108
114
 
109
- resolveId(id) {
110
- if (!id.endsWith(virtualExt)) {
115
+ resolveId(source) {
116
+ if (!source.endsWith(virtualExt)) {
111
117
  return;
112
- }
118
+ } // Absolute paths seem to occur often in monorepos, where files are
119
+ // imported from outside the config root.
120
+
113
121
 
114
- 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
115
125
 
116
- if (cssMap.has(normalizedId)) {
117
- return vite.normalizePath(path__default["default"].join(config.root, normalizedId));
126
+ if (cssMap.has(absoluteId)) {
127
+ return absoluteId;
118
128
  }
119
129
  },
120
130
 
121
131
  load(id) {
122
- if (!id.endsWith(virtualExt)) {
132
+ if (!cssMap.has(id)) {
123
133
  return;
124
134
  }
125
135
 
126
- const cssFileId = id.slice(config.root.length + 1);
127
- const css = cssMap.get(cssFileId);
136
+ const css = cssMap.get(id);
128
137
 
129
138
  if (typeof css !== 'string') {
130
139
  return;
@@ -139,14 +148,14 @@ function vanillaExtractPlugin({
139
148
 
140
149
  const inject = (css) => injectStyles({
141
150
  fileScope: ${JSON.stringify({
142
- filePath: cssFileId
151
+ filePath: id
143
152
  })},
144
153
  css
145
154
  });
146
155
 
147
156
  inject(${JSON.stringify(css)});
148
157
 
149
- import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
158
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
150
159
  inject(css);
151
160
  });
152
161
  `;
@@ -172,7 +181,8 @@ function vanillaExtractPlugin({
172
181
  return integration.addFileScope({
173
182
  source: code,
174
183
  filePath: vite.normalizePath(validId),
175
- rootPath: config.root
184
+ rootPath: config.root,
185
+ packageName
176
186
  });
177
187
  }
178
188
 
@@ -181,7 +191,8 @@ function vanillaExtractPlugin({
181
191
  watchFiles
182
192
  } = await integration.compile({
183
193
  filePath: validId,
184
- cwd: config.root
194
+ cwd: config.root,
195
+ esbuildOptions
185
196
  });
186
197
 
187
198
  for (const file of watchFiles) {
@@ -192,7 +203,7 @@ function vanillaExtractPlugin({
192
203
  }
193
204
  }
194
205
 
195
- return integration.processVanillaFile({
206
+ const output = await integration.processVanillaFile({
196
207
  source,
197
208
  filePath: validId,
198
209
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -200,7 +211,8 @@ function vanillaExtractPlugin({
200
211
  fileScope,
201
212
  source
202
213
  }) => {
203
- const id = `${fileScope.filePath}${virtualExt}`;
214
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
215
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
204
216
  let cssSource = source;
205
217
 
206
218
  if (postCssConfig) {
@@ -211,11 +223,11 @@ function vanillaExtractPlugin({
211
223
  cssSource = postCssResult.css;
212
224
  }
213
225
 
214
- if (server && cssMap.has(id) && cssMap.get(id) !== source) {
226
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
215
227
  const {
216
228
  moduleGraph
217
229
  } = server;
218
- const module = moduleGraph.getModuleById(id);
230
+ const module = moduleGraph.getModuleById(absoluteId);
219
231
 
220
232
  if (module) {
221
233
  moduleGraph.invalidateModule(module);
@@ -223,15 +235,23 @@ function vanillaExtractPlugin({
223
235
 
224
236
  server.ws.send({
225
237
  type: 'custom',
226
- event: styleUpdateEvent(id),
238
+ event: styleUpdateEvent(absoluteId),
227
239
  data: cssSource
228
240
  });
229
241
  }
230
242
 
231
- cssMap.set(id, cssSource);
232
- 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}";`;
233
247
  }
234
248
  });
249
+ return {
250
+ code: output,
251
+ map: {
252
+ mappings: ''
253
+ }
254
+ };
235
255
  }
236
256
 
237
257
  };
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  import { normalizePath } from 'vite';
3
3
  import outdent from 'outdent';
4
- import { cssFileFilter, addFileScope, compile, processVanillaFile } 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
@@ -42,13 +42,18 @@ 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;
49
50
  let postCssConfig;
50
51
  const cssMap = new Map();
51
52
  let virtualExt;
53
+ let packageName;
54
+
55
+ const getAbsoluteVirtualFileId = source => normalizePath(path.join(config.root, source));
56
+
52
57
  return {
53
58
  name: 'vanilla-extract',
54
59
  enforce: 'pre',
@@ -71,6 +76,7 @@ function vanillaExtractPlugin({
71
76
 
72
77
  async configResolved(resolvedConfig) {
73
78
  config = resolvedConfig;
79
+ packageName = getPackageInfo(config.root).name;
74
80
 
75
81
  if (config.command === 'serve') {
76
82
  postCssConfig = await resolvePostcssConfig(config);
@@ -79,25 +85,28 @@ function vanillaExtractPlugin({
79
85
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
80
86
  },
81
87
 
82
- resolveId(id) {
83
- if (!id.endsWith(virtualExt)) {
88
+ resolveId(source) {
89
+ if (!source.endsWith(virtualExt)) {
84
90
  return;
85
- }
91
+ } // Absolute paths seem to occur often in monorepos, where files are
92
+ // imported from outside the config root.
93
+
86
94
 
87
- 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
88
98
 
89
- if (cssMap.has(normalizedId)) {
90
- return normalizePath(path.join(config.root, normalizedId));
99
+ if (cssMap.has(absoluteId)) {
100
+ return absoluteId;
91
101
  }
92
102
  },
93
103
 
94
104
  load(id) {
95
- if (!id.endsWith(virtualExt)) {
105
+ if (!cssMap.has(id)) {
96
106
  return;
97
107
  }
98
108
 
99
- const cssFileId = id.slice(config.root.length + 1);
100
- const css = cssMap.get(cssFileId);
109
+ const css = cssMap.get(id);
101
110
 
102
111
  if (typeof css !== 'string') {
103
112
  return;
@@ -112,14 +121,14 @@ function vanillaExtractPlugin({
112
121
 
113
122
  const inject = (css) => injectStyles({
114
123
  fileScope: ${JSON.stringify({
115
- filePath: cssFileId
124
+ filePath: id
116
125
  })},
117
126
  css
118
127
  });
119
128
 
120
129
  inject(${JSON.stringify(css)});
121
130
 
122
- import.meta.hot.on('${styleUpdateEvent(cssFileId)}', (css) => {
131
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
123
132
  inject(css);
124
133
  });
125
134
  `;
@@ -145,7 +154,8 @@ function vanillaExtractPlugin({
145
154
  return addFileScope({
146
155
  source: code,
147
156
  filePath: normalizePath(validId),
148
- rootPath: config.root
157
+ rootPath: config.root,
158
+ packageName
149
159
  });
150
160
  }
151
161
 
@@ -154,7 +164,8 @@ function vanillaExtractPlugin({
154
164
  watchFiles
155
165
  } = await compile({
156
166
  filePath: validId,
157
- cwd: config.root
167
+ cwd: config.root,
168
+ esbuildOptions
158
169
  });
159
170
 
160
171
  for (const file of watchFiles) {
@@ -165,7 +176,7 @@ function vanillaExtractPlugin({
165
176
  }
166
177
  }
167
178
 
168
- return processVanillaFile({
179
+ const output = await processVanillaFile({
169
180
  source,
170
181
  filePath: validId,
171
182
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -173,7 +184,8 @@ function vanillaExtractPlugin({
173
184
  fileScope,
174
185
  source
175
186
  }) => {
176
- const id = `${fileScope.filePath}${virtualExt}`;
187
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
188
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
177
189
  let cssSource = source;
178
190
 
179
191
  if (postCssConfig) {
@@ -184,11 +196,11 @@ function vanillaExtractPlugin({
184
196
  cssSource = postCssResult.css;
185
197
  }
186
198
 
187
- if (server && cssMap.has(id) && cssMap.get(id) !== source) {
199
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
188
200
  const {
189
201
  moduleGraph
190
202
  } = server;
191
- const module = moduleGraph.getModuleById(id);
203
+ const module = moduleGraph.getModuleById(absoluteId);
192
204
 
193
205
  if (module) {
194
206
  moduleGraph.invalidateModule(module);
@@ -196,15 +208,23 @@ function vanillaExtractPlugin({
196
208
 
197
209
  server.ws.send({
198
210
  type: 'custom',
199
- event: styleUpdateEvent(id),
211
+ event: styleUpdateEvent(absoluteId),
200
212
  data: cssSource
201
213
  });
202
214
  }
203
215
 
204
- cssMap.set(id, cssSource);
205
- 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}";`;
206
220
  }
207
221
  });
222
+ return {
223
+ code: output,
224
+ map: {
225
+ mappings: ''
226
+ }
227
+ };
208
228
  }
209
229
 
210
230
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.1.5",
3
+ "version": "3.2.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",
@@ -15,7 +15,7 @@
15
15
  "author": "SEEK",
16
16
  "license": "MIT",
17
17
  "dependencies": {
18
- "@vanilla-extract/integration": "^4.0.0",
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"