@vanilla-extract/vite-plugin 3.1.4 → 3.1.7

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.
@@ -68,16 +68,18 @@ 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;
80
+
81
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
82
+
81
83
  return {
82
84
  name: 'vanilla-extract',
83
85
  enforce: 'pre',
@@ -100,53 +102,62 @@ function vanillaExtractPlugin({
100
102
 
101
103
  async configResolved(resolvedConfig) {
102
104
  config = resolvedConfig;
105
+ packageName = integration.getPackageInfo(config.root).name;
103
106
 
104
107
  if (config.command === 'serve') {
105
108
  postCssConfig = await resolvePostcssConfig(config);
106
109
  }
107
110
 
108
111
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
109
- packageInfo = integration.getPackageInfo(config.root);
110
112
  },
111
113
 
112
- resolveId(id) {
113
- if (id.indexOf(virtualPrefix) === 0) {
114
- return id;
114
+ resolveId(source) {
115
+ if (!source.endsWith(virtualExt)) {
116
+ return;
117
+ } // Absolute paths seem to occur often in monorepos, where files are
118
+ // imported from outside the config root.
119
+
120
+
121
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
122
+ // The only valid scenario for a missing one is if someone had written
123
+ // a file in their app using the .vanilla.js/.vanilla.css extension
124
+
125
+ if (cssMap.has(absoluteId)) {
126
+ return absoluteId;
115
127
  }
116
128
  },
117
129
 
118
130
  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
- }
131
+ if (!cssMap.has(id)) {
132
+ return;
133
+ }
125
134
 
126
- const css = cssMap.get(fileScopeId);
135
+ const css = cssMap.get(id);
127
136
 
128
- if (!server) {
129
- return css;
130
- }
137
+ if (typeof css !== 'string') {
138
+ return;
139
+ }
131
140
 
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
- `;
141
+ if (!server) {
142
+ return css;
147
143
  }
148
144
 
149
- return null;
145
+ return outdent__default["default"]`
146
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
147
+
148
+ const inject = (css) => injectStyles({
149
+ fileScope: ${JSON.stringify({
150
+ filePath: id
151
+ })},
152
+ css
153
+ });
154
+
155
+ inject(${JSON.stringify(css)});
156
+
157
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
158
+ inject(css);
159
+ });
160
+ `;
150
161
  },
151
162
 
152
163
  async transform(code, id, ssrParam) {
@@ -168,9 +179,10 @@ function vanillaExtractPlugin({
168
179
  if (ssr) {
169
180
  return integration.addFileScope({
170
181
  source: code,
171
- filePath: vite.normalizePath(path__default["default"].relative(packageInfo.dirname, validId)),
172
- packageInfo
173
- }).source;
182
+ filePath: vite.normalizePath(validId),
183
+ rootPath: config.root,
184
+ packageName
185
+ });
174
186
  }
175
187
 
176
188
  const {
@@ -189,7 +201,7 @@ function vanillaExtractPlugin({
189
201
  }
190
202
  }
191
203
 
192
- return integration.processVanillaFile({
204
+ const output = await integration.processVanillaFile({
193
205
  source,
194
206
  filePath: validId,
195
207
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -197,9 +209,8 @@ function vanillaExtractPlugin({
197
209
  fileScope,
198
210
  source
199
211
  }) => {
200
- // This file id is requested through a URL where ".." isn't valid.
201
- const fileId = integration.stringifyFileScope(fileScope).replace(/\.\./g, '_dir_up_');
202
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
212
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
213
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
203
214
  let cssSource = source;
204
215
 
205
216
  if (postCssConfig) {
@@ -210,11 +221,11 @@ function vanillaExtractPlugin({
210
221
  cssSource = postCssResult.css;
211
222
  }
212
223
 
213
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
224
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
214
225
  const {
215
226
  moduleGraph
216
227
  } = server;
217
- const module = moduleGraph.getModuleById(id);
228
+ const module = moduleGraph.getModuleById(absoluteId);
218
229
 
219
230
  if (module) {
220
231
  moduleGraph.invalidateModule(module);
@@ -222,15 +233,23 @@ function vanillaExtractPlugin({
222
233
 
223
234
  server.ws.send({
224
235
  type: 'custom',
225
- event: styleUpdateEvent(fileId),
236
+ event: styleUpdateEvent(absoluteId),
226
237
  data: cssSource
227
238
  });
228
239
  }
229
240
 
230
- cssMap.set(fileId, cssSource);
231
- return `import "${id}";`;
241
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
242
+ // are consistent across build machines
243
+
244
+ return `import "${rootRelativeId}";`;
232
245
  }
233
246
  });
247
+ return {
248
+ code: output,
249
+ map: {
250
+ mappings: ''
251
+ }
252
+ };
234
253
  }
235
254
 
236
255
  };
@@ -68,16 +68,18 @@ 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;
80
+
81
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
82
+
81
83
  return {
82
84
  name: 'vanilla-extract',
83
85
  enforce: 'pre',
@@ -100,53 +102,62 @@ function vanillaExtractPlugin({
100
102
 
101
103
  async configResolved(resolvedConfig) {
102
104
  config = resolvedConfig;
105
+ packageName = integration.getPackageInfo(config.root).name;
103
106
 
104
107
  if (config.command === 'serve') {
105
108
  postCssConfig = await resolvePostcssConfig(config);
106
109
  }
107
110
 
108
111
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
109
- packageInfo = integration.getPackageInfo(config.root);
110
112
  },
111
113
 
112
- resolveId(id) {
113
- if (id.indexOf(virtualPrefix) === 0) {
114
- return id;
114
+ resolveId(source) {
115
+ if (!source.endsWith(virtualExt)) {
116
+ return;
117
+ } // Absolute paths seem to occur often in monorepos, where files are
118
+ // imported from outside the config root.
119
+
120
+
121
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
122
+ // The only valid scenario for a missing one is if someone had written
123
+ // a file in their app using the .vanilla.js/.vanilla.css extension
124
+
125
+ if (cssMap.has(absoluteId)) {
126
+ return absoluteId;
115
127
  }
116
128
  },
117
129
 
118
130
  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
- }
131
+ if (!cssMap.has(id)) {
132
+ return;
133
+ }
125
134
 
126
- const css = cssMap.get(fileScopeId);
135
+ const css = cssMap.get(id);
127
136
 
128
- if (!server) {
129
- return css;
130
- }
137
+ if (typeof css !== 'string') {
138
+ return;
139
+ }
131
140
 
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
- `;
141
+ if (!server) {
142
+ return css;
147
143
  }
148
144
 
149
- return null;
145
+ return outdent__default["default"]`
146
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
147
+
148
+ const inject = (css) => injectStyles({
149
+ fileScope: ${JSON.stringify({
150
+ filePath: id
151
+ })},
152
+ css
153
+ });
154
+
155
+ inject(${JSON.stringify(css)});
156
+
157
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
158
+ inject(css);
159
+ });
160
+ `;
150
161
  },
151
162
 
152
163
  async transform(code, id, ssrParam) {
@@ -168,9 +179,10 @@ function vanillaExtractPlugin({
168
179
  if (ssr) {
169
180
  return integration.addFileScope({
170
181
  source: code,
171
- filePath: vite.normalizePath(path__default["default"].relative(packageInfo.dirname, validId)),
172
- packageInfo
173
- }).source;
182
+ filePath: vite.normalizePath(validId),
183
+ rootPath: config.root,
184
+ packageName
185
+ });
174
186
  }
175
187
 
176
188
  const {
@@ -189,7 +201,7 @@ function vanillaExtractPlugin({
189
201
  }
190
202
  }
191
203
 
192
- return integration.processVanillaFile({
204
+ const output = await integration.processVanillaFile({
193
205
  source,
194
206
  filePath: validId,
195
207
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -197,9 +209,8 @@ function vanillaExtractPlugin({
197
209
  fileScope,
198
210
  source
199
211
  }) => {
200
- // This file id is requested through a URL where ".." isn't valid.
201
- const fileId = integration.stringifyFileScope(fileScope).replace(/\.\./g, '_dir_up_');
202
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
212
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
213
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
203
214
  let cssSource = source;
204
215
 
205
216
  if (postCssConfig) {
@@ -210,11 +221,11 @@ function vanillaExtractPlugin({
210
221
  cssSource = postCssResult.css;
211
222
  }
212
223
 
213
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
224
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
214
225
  const {
215
226
  moduleGraph
216
227
  } = server;
217
- const module = moduleGraph.getModuleById(id);
228
+ const module = moduleGraph.getModuleById(absoluteId);
218
229
 
219
230
  if (module) {
220
231
  moduleGraph.invalidateModule(module);
@@ -222,15 +233,23 @@ function vanillaExtractPlugin({
222
233
 
223
234
  server.ws.send({
224
235
  type: 'custom',
225
- event: styleUpdateEvent(fileId),
236
+ event: styleUpdateEvent(absoluteId),
226
237
  data: cssSource
227
238
  });
228
239
  }
229
240
 
230
- cssMap.set(fileId, cssSource);
231
- return `import "${id}";`;
241
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
242
+ // are consistent across build machines
243
+
244
+ return `import "${rootRelativeId}";`;
232
245
  }
233
246
  });
247
+ return {
248
+ code: output,
249
+ map: {
250
+ mappings: ''
251
+ }
252
+ };
234
253
  }
235
254
 
236
255
  };
@@ -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,18 @@ 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;
53
+
54
+ const getAbsoluteVirtualFileId = source => normalizePath(path.join(config.root, source));
55
+
54
56
  return {
55
57
  name: 'vanilla-extract',
56
58
  enforce: 'pre',
@@ -73,53 +75,62 @@ function vanillaExtractPlugin({
73
75
 
74
76
  async configResolved(resolvedConfig) {
75
77
  config = resolvedConfig;
78
+ packageName = getPackageInfo(config.root).name;
76
79
 
77
80
  if (config.command === 'serve') {
78
81
  postCssConfig = await resolvePostcssConfig(config);
79
82
  }
80
83
 
81
84
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
82
- packageInfo = getPackageInfo(config.root);
83
85
  },
84
86
 
85
- resolveId(id) {
86
- if (id.indexOf(virtualPrefix) === 0) {
87
- return id;
87
+ resolveId(source) {
88
+ if (!source.endsWith(virtualExt)) {
89
+ return;
90
+ } // Absolute paths seem to occur often in monorepos, where files are
91
+ // imported from outside the config root.
92
+
93
+
94
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(source); // There should always be an entry in the `cssMap` here.
95
+ // The only valid scenario for a missing one is if someone had written
96
+ // a file in their app using the .vanilla.js/.vanilla.css extension
97
+
98
+ if (cssMap.has(absoluteId)) {
99
+ return absoluteId;
88
100
  }
89
101
  },
90
102
 
91
103
  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
- }
104
+ if (!cssMap.has(id)) {
105
+ return;
106
+ }
98
107
 
99
- const css = cssMap.get(fileScopeId);
108
+ const css = cssMap.get(id);
100
109
 
101
- if (!server) {
102
- return css;
103
- }
110
+ if (typeof css !== 'string') {
111
+ return;
112
+ }
104
113
 
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
- `;
114
+ if (!server) {
115
+ return css;
120
116
  }
121
117
 
122
- return null;
118
+ return outdent`
119
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
120
+
121
+ const inject = (css) => injectStyles({
122
+ fileScope: ${JSON.stringify({
123
+ filePath: id
124
+ })},
125
+ css
126
+ });
127
+
128
+ inject(${JSON.stringify(css)});
129
+
130
+ import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
131
+ inject(css);
132
+ });
133
+ `;
123
134
  },
124
135
 
125
136
  async transform(code, id, ssrParam) {
@@ -141,9 +152,10 @@ function vanillaExtractPlugin({
141
152
  if (ssr) {
142
153
  return addFileScope({
143
154
  source: code,
144
- filePath: normalizePath(path.relative(packageInfo.dirname, validId)),
145
- packageInfo
146
- }).source;
155
+ filePath: normalizePath(validId),
156
+ rootPath: config.root,
157
+ packageName
158
+ });
147
159
  }
148
160
 
149
161
  const {
@@ -162,7 +174,7 @@ function vanillaExtractPlugin({
162
174
  }
163
175
  }
164
176
 
165
- return processVanillaFile({
177
+ const output = await processVanillaFile({
166
178
  source,
167
179
  filePath: validId,
168
180
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
@@ -170,9 +182,8 @@ function vanillaExtractPlugin({
170
182
  fileScope,
171
183
  source
172
184
  }) => {
173
- // This file id is requested through a URL where ".." isn't valid.
174
- const fileId = stringifyFileScope(fileScope).replace(/\.\./g, '_dir_up_');
175
- const id = `${virtualPrefix}${fileId}${virtualExt}`;
185
+ const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
186
+ const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);
176
187
  let cssSource = source;
177
188
 
178
189
  if (postCssConfig) {
@@ -183,11 +194,11 @@ function vanillaExtractPlugin({
183
194
  cssSource = postCssResult.css;
184
195
  }
185
196
 
186
- if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
197
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
187
198
  const {
188
199
  moduleGraph
189
200
  } = server;
190
- const module = moduleGraph.getModuleById(id);
201
+ const module = moduleGraph.getModuleById(absoluteId);
191
202
 
192
203
  if (module) {
193
204
  moduleGraph.invalidateModule(module);
@@ -195,15 +206,23 @@ function vanillaExtractPlugin({
195
206
 
196
207
  server.ws.send({
197
208
  type: 'custom',
198
- event: styleUpdateEvent(fileId),
209
+ event: styleUpdateEvent(absoluteId),
199
210
  data: cssSource
200
211
  });
201
212
  }
202
213
 
203
- cssMap.set(fileId, cssSource);
204
- return `import "${id}";`;
214
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
215
+ // are consistent across build machines
216
+
217
+ return `import "${rootRelativeId}";`;
205
218
  }
206
219
  });
220
+ return {
221
+ code: output,
222
+ map: {
223
+ mappings: ''
224
+ }
225
+ };
207
226
  }
208
227
 
209
228
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.1.4",
3
+ "version": "3.1.7",
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"