@vanilla-extract/vite-plugin 3.0.0 → 3.1.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 SEEK
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -82,7 +82,6 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
82
82
  - [webpack](#webpack)
83
83
  - [esbuild](#esbuild)
84
84
  - [Vite](#vite)
85
- - [Snowpack](#snowpack)
86
85
  - [Next.js](#nextjs)
87
86
  - [Gatsby](#gatsby)
88
87
  - [Test environments](#test-environments)
@@ -95,6 +94,7 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
95
94
  - [createTheme](#createtheme)
96
95
  - [createGlobalTheme](#createglobaltheme)
97
96
  - [createThemeContract](#createthemecontract)
97
+ - [createGlobalThemeContract](#createglobalthemecontract)
98
98
  - [assignVars](#assignvars)
99
99
  - [createVar](#createvar)
100
100
  - [fallbackVar](#fallbackvar)
@@ -271,27 +271,6 @@ export default {
271
271
 
272
272
  > Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
273
273
 
274
- ### Snowpack
275
-
276
- 1. Install the dependencies.
277
-
278
- ```bash
279
- npm install @vanilla-extract/css @vanilla-extract/snowpack-plugin
280
- ```
281
-
282
- 2. Add the [Snowpack](https://www.snowpack.dev/) plugin to your snowpack config.
283
-
284
- > 💡 This plugin accepts an optional [configuration object](#configuration).
285
-
286
- ```js
287
- // snowpack.config.json
288
- {
289
- "plugins": ["@vanilla-extract/snowpack-plugin"]
290
- }
291
- ```
292
-
293
- > Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
294
-
295
274
  ### Next.js
296
275
 
297
276
  1. Install the dependencies.
@@ -300,16 +279,7 @@ npm install @vanilla-extract/css @vanilla-extract/snowpack-plugin
300
279
  npm install @vanilla-extract/css @vanilla-extract/babel-plugin @vanilla-extract/next-plugin
301
280
  ```
302
281
 
303
- 2. If you don't have a `.babelrc` file in the root of your project, create one. Add the [Babel](https://babeljs.io) plugin to your `.babelrc` file, ensuring that you're also including `"next/babel"` in your `presets` array.
304
-
305
- ```json
306
- {
307
- "presets": ["next/babel"],
308
- "plugins": ["@vanilla-extract/babel-plugin"]
309
- }
310
- ```
311
-
312
- 3. If you don't have a `next.config.js` file in the root of your project, create one. Add the [Next.js](https://nextjs.org) plugin to your `next.config.js` file.
282
+ 2. If you don't have a `next.config.js` file in the root of your project, create one. Add the [Next.js](https://nextjs.org) plugin to your `next.config.js` file.
313
283
 
314
284
  > 💡 This plugin accepts an optional [configuration object](#configuration).
315
285
 
@@ -319,6 +289,7 @@ const {
319
289
  } = require('@vanilla-extract/next-plugin');
320
290
  const withVanillaExtract = createVanillaExtractPlugin();
321
291
 
292
+ /** @type {import('next').NextConfig} */
322
293
  const nextConfig = {};
323
294
 
324
295
  module.exports = withVanillaExtract(nextConfig);
@@ -336,11 +307,25 @@ const withMDX = require('@next/mdx')({
336
307
  extension: /\.mdx$/
337
308
  });
338
309
 
310
+ /** @type {import('next').NextConfig} */
339
311
  const nextConfig = {};
340
312
 
341
313
  module.exports = withVanillaExtract(withMDX(nextConfig));
342
314
  ```
343
315
 
316
+ 3. (Optional) If you want to automatically generate debug IDs during development, you can add the [Babel](https://babeljs.io) plugin. Note that this step will cause Next.js to switch from [SWC](https://github.com/swc-project/swc) to Babel, increasing build times. This may or may not be an issue depending on the size of your project.
317
+
318
+ > Note: While optional for Next.js, the Babel plugin is still required when trying to run `.css.ts` files in Node for unit testing since the files are no longer being processed by a bundler.
319
+
320
+ If you don't have a `.babelrc` file in the root of your project, create one. Add the Babel plugin to your `.babelrc` file, ensuring that you're also including `"next/babel"` in your `presets` array.
321
+
322
+ ```json
323
+ {
324
+ "presets": ["next/babel"],
325
+ "plugins": ["@vanilla-extract/babel-plugin"]
326
+ }
327
+ ```
328
+
344
329
  ### Gatsby
345
330
 
346
331
  To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/KyleAMathews/gatsby-plugin-vanilla-extract) plugin.
@@ -1,7 +1,7 @@
1
- import type { Plugin } from 'vite';
2
- import { IdentifierOption } from '@vanilla-extract/integration';
3
- interface Options {
4
- identifiers?: IdentifierOption;
5
- }
6
- export declare function vanillaExtractPlugin({ identifiers }?: Options): Plugin;
7
- export {};
1
+ import type { Plugin } from 'vite';
2
+ import { IdentifierOption } from '@vanilla-extract/integration';
3
+ interface Options {
4
+ identifiers?: IdentifierOption;
5
+ }
6
+ export declare function vanillaExtractPlugin({ identifiers }?: Options): Plugin;
7
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { ResolvedConfig } from 'vite';
2
+ import type { ProcessOptions, Plugin } from 'postcss';
3
+ export interface PostCSSConfigResult {
4
+ options: ProcessOptions;
5
+ plugins: Plugin[];
6
+ }
7
+ export declare const resolvePostcssConfig: (config: ResolvedConfig) => Promise<PostCSSConfigResult | null>;
@@ -9,17 +9,73 @@ var integration = require('@vanilla-extract/integration');
9
9
 
10
10
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
11
11
 
12
+ function _interopNamespace(e) {
13
+ if (e && e.__esModule) return e;
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n["default"] = e;
27
+ return Object.freeze(n);
28
+ }
29
+
12
30
  var path__default = /*#__PURE__*/_interopDefault(path);
13
31
  var outdent__default = /*#__PURE__*/_interopDefault(outdent);
14
32
 
33
+ // Mostly copied from vite's implementation
34
+ // https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
35
+ const resolvePostcssConfig = async config => {
36
+ var _config$css;
37
+
38
+ // inline postcss config via vite config
39
+ const inlineOptions = (_config$css = config.css) === null || _config$css === void 0 ? void 0 : _config$css.postcss;
40
+ const inlineOptionsIsString = typeof inlineOptions === 'string';
41
+
42
+ if (inlineOptions && !inlineOptionsIsString) {
43
+ const options = { ...inlineOptions
44
+ };
45
+ delete options.plugins;
46
+ return {
47
+ options,
48
+ plugins: inlineOptions.plugins || []
49
+ };
50
+ } else {
51
+ try {
52
+ const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
53
+ const postCssConfig = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss-load-config')); })).default({}, searchPath);
54
+ return {
55
+ options: postCssConfig.options,
56
+ // @ts-expect-error - The postcssrc options don't agree with real postcss, but it should be ok
57
+ plugins: postCssConfig.plugins
58
+ };
59
+ } catch (e) {
60
+ if (!/No PostCSS Config found/.test(e.message)) {
61
+ throw e;
62
+ }
63
+
64
+ return null;
65
+ }
66
+ }
67
+ };
68
+
15
69
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
16
70
 
71
+ const virtualPrefix = 'virtual:vanilla-extract:';
17
72
  function vanillaExtractPlugin({
18
73
  identifiers
19
74
  } = {}) {
20
75
  let config;
21
76
  let packageInfo;
22
77
  let server;
78
+ let postCssConfig;
23
79
  const cssMap = new Map();
24
80
  let virtualExt;
25
81
  return {
@@ -42,23 +98,26 @@ function vanillaExtractPlugin({
42
98
  };
43
99
  },
44
100
 
45
- configResolved(resolvedConfig) {
101
+ async configResolved(resolvedConfig) {
46
102
  config = resolvedConfig;
103
+
104
+ if (config.command === 'serve') {
105
+ postCssConfig = await resolvePostcssConfig(config);
106
+ }
107
+
47
108
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
48
109
  packageInfo = integration.getPackageInfo(config.root);
49
110
  },
50
111
 
51
112
  resolveId(id) {
52
- if (id.indexOf(virtualExt) > 0) {
113
+ if (id.indexOf(virtualPrefix) === 0) {
53
114
  return id;
54
115
  }
55
116
  },
56
117
 
57
118
  load(id) {
58
- const extensionIndex = id.indexOf(virtualExt);
59
-
60
- if (extensionIndex > 0) {
61
- const fileScopeId = id.substring(0, extensionIndex);
119
+ if (id.indexOf(virtualPrefix) === 0) {
120
+ const fileScopeId = id.slice(virtualPrefix.length, id.indexOf(virtualExt));
62
121
 
63
122
  if (!cssMap.has(fileScopeId)) {
64
123
  throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
@@ -71,7 +130,7 @@ function vanillaExtractPlugin({
71
130
  }
72
131
 
73
132
  const fileScope = integration.parseFileScope(fileScopeId);
74
- return outdent__default['default']`
133
+ return outdent__default["default"]`
75
134
  import { injectStyles } from '@vanilla-extract/css/injectStyles';
76
135
 
77
136
  const inject = (css) => injectStyles({
@@ -90,18 +149,26 @@ function vanillaExtractPlugin({
90
149
  return null;
91
150
  },
92
151
 
93
- async transform(code, id, ssr) {
152
+ async transform(code, id, ssrParam) {
94
153
  if (!integration.cssFileFilter.test(id)) {
95
154
  return null;
96
155
  }
97
156
 
157
+ let ssr;
158
+
159
+ if (typeof ssrParam === 'boolean') {
160
+ ssr = ssrParam;
161
+ } else {
162
+ ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
163
+ }
164
+
98
165
  const index = id.indexOf('?');
99
166
  const validId = index === -1 ? id : id.substring(0, index);
100
167
 
101
168
  if (ssr) {
102
169
  return integration.addFileScope({
103
170
  source: code,
104
- filePath: vite.normalizePath(path__default['default'].relative(packageInfo.dirname, validId)),
171
+ filePath: vite.normalizePath(path__default["default"].relative(packageInfo.dirname, validId)),
105
172
  packageInfo
106
173
  }).source;
107
174
  }
@@ -126,12 +193,21 @@ function vanillaExtractPlugin({
126
193
  source,
127
194
  filePath: validId,
128
195
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
129
- serializeVirtualCssPath: ({
196
+ serializeVirtualCssPath: async ({
130
197
  fileScope,
131
198
  source
132
199
  }) => {
133
200
  const fileId = integration.stringifyFileScope(fileScope);
134
- const id = `${fileId}${virtualExt}`;
201
+ const id = `${virtualPrefix}${fileId}${virtualExt}`;
202
+ let cssSource = source;
203
+
204
+ if (postCssConfig) {
205
+ const postCssResult = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss')); })).default(postCssConfig.plugins).process(source, { ...postCssConfig.options,
206
+ from: undefined,
207
+ map: false
208
+ });
209
+ cssSource = postCssResult.css;
210
+ }
135
211
 
136
212
  if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
137
213
  const {
@@ -146,11 +222,11 @@ function vanillaExtractPlugin({
146
222
  server.ws.send({
147
223
  type: 'custom',
148
224
  event: styleUpdateEvent(fileId),
149
- data: source
225
+ data: cssSource
150
226
  });
151
227
  }
152
228
 
153
- cssMap.set(fileId, source);
229
+ cssMap.set(fileId, cssSource);
154
230
  return `import "${id}";`;
155
231
  }
156
232
  });
@@ -9,17 +9,73 @@ var integration = require('@vanilla-extract/integration');
9
9
 
10
10
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
11
11
 
12
+ function _interopNamespace(e) {
13
+ if (e && e.__esModule) return e;
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n["default"] = e;
27
+ return Object.freeze(n);
28
+ }
29
+
12
30
  var path__default = /*#__PURE__*/_interopDefault(path);
13
31
  var outdent__default = /*#__PURE__*/_interopDefault(outdent);
14
32
 
33
+ // Mostly copied from vite's implementation
34
+ // https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
35
+ const resolvePostcssConfig = async config => {
36
+ var _config$css;
37
+
38
+ // inline postcss config via vite config
39
+ const inlineOptions = (_config$css = config.css) === null || _config$css === void 0 ? void 0 : _config$css.postcss;
40
+ const inlineOptionsIsString = typeof inlineOptions === 'string';
41
+
42
+ if (inlineOptions && !inlineOptionsIsString) {
43
+ const options = { ...inlineOptions
44
+ };
45
+ delete options.plugins;
46
+ return {
47
+ options,
48
+ plugins: inlineOptions.plugins || []
49
+ };
50
+ } else {
51
+ try {
52
+ const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
53
+ const postCssConfig = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss-load-config')); })).default({}, searchPath);
54
+ return {
55
+ options: postCssConfig.options,
56
+ // @ts-expect-error - The postcssrc options don't agree with real postcss, but it should be ok
57
+ plugins: postCssConfig.plugins
58
+ };
59
+ } catch (e) {
60
+ if (!/No PostCSS Config found/.test(e.message)) {
61
+ throw e;
62
+ }
63
+
64
+ return null;
65
+ }
66
+ }
67
+ };
68
+
15
69
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
16
70
 
71
+ const virtualPrefix = 'virtual:vanilla-extract:';
17
72
  function vanillaExtractPlugin({
18
73
  identifiers
19
74
  } = {}) {
20
75
  let config;
21
76
  let packageInfo;
22
77
  let server;
78
+ let postCssConfig;
23
79
  const cssMap = new Map();
24
80
  let virtualExt;
25
81
  return {
@@ -42,23 +98,26 @@ function vanillaExtractPlugin({
42
98
  };
43
99
  },
44
100
 
45
- configResolved(resolvedConfig) {
101
+ async configResolved(resolvedConfig) {
46
102
  config = resolvedConfig;
103
+
104
+ if (config.command === 'serve') {
105
+ postCssConfig = await resolvePostcssConfig(config);
106
+ }
107
+
47
108
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
48
109
  packageInfo = integration.getPackageInfo(config.root);
49
110
  },
50
111
 
51
112
  resolveId(id) {
52
- if (id.indexOf(virtualExt) > 0) {
113
+ if (id.indexOf(virtualPrefix) === 0) {
53
114
  return id;
54
115
  }
55
116
  },
56
117
 
57
118
  load(id) {
58
- const extensionIndex = id.indexOf(virtualExt);
59
-
60
- if (extensionIndex > 0) {
61
- const fileScopeId = id.substring(0, extensionIndex);
119
+ if (id.indexOf(virtualPrefix) === 0) {
120
+ const fileScopeId = id.slice(virtualPrefix.length, id.indexOf(virtualExt));
62
121
 
63
122
  if (!cssMap.has(fileScopeId)) {
64
123
  throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
@@ -71,7 +130,7 @@ function vanillaExtractPlugin({
71
130
  }
72
131
 
73
132
  const fileScope = integration.parseFileScope(fileScopeId);
74
- return outdent__default['default']`
133
+ return outdent__default["default"]`
75
134
  import { injectStyles } from '@vanilla-extract/css/injectStyles';
76
135
 
77
136
  const inject = (css) => injectStyles({
@@ -90,18 +149,26 @@ function vanillaExtractPlugin({
90
149
  return null;
91
150
  },
92
151
 
93
- async transform(code, id, ssr) {
152
+ async transform(code, id, ssrParam) {
94
153
  if (!integration.cssFileFilter.test(id)) {
95
154
  return null;
96
155
  }
97
156
 
157
+ let ssr;
158
+
159
+ if (typeof ssrParam === 'boolean') {
160
+ ssr = ssrParam;
161
+ } else {
162
+ ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
163
+ }
164
+
98
165
  const index = id.indexOf('?');
99
166
  const validId = index === -1 ? id : id.substring(0, index);
100
167
 
101
168
  if (ssr) {
102
169
  return integration.addFileScope({
103
170
  source: code,
104
- filePath: vite.normalizePath(path__default['default'].relative(packageInfo.dirname, validId)),
171
+ filePath: vite.normalizePath(path__default["default"].relative(packageInfo.dirname, validId)),
105
172
  packageInfo
106
173
  }).source;
107
174
  }
@@ -126,12 +193,21 @@ function vanillaExtractPlugin({
126
193
  source,
127
194
  filePath: validId,
128
195
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
129
- serializeVirtualCssPath: ({
196
+ serializeVirtualCssPath: async ({
130
197
  fileScope,
131
198
  source
132
199
  }) => {
133
200
  const fileId = integration.stringifyFileScope(fileScope);
134
- const id = `${fileId}${virtualExt}`;
201
+ const id = `${virtualPrefix}${fileId}${virtualExt}`;
202
+ let cssSource = source;
203
+
204
+ if (postCssConfig) {
205
+ const postCssResult = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss')); })).default(postCssConfig.plugins).process(source, { ...postCssConfig.options,
206
+ from: undefined,
207
+ map: false
208
+ });
209
+ cssSource = postCssResult.css;
210
+ }
135
211
 
136
212
  if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
137
213
  const {
@@ -146,11 +222,11 @@ function vanillaExtractPlugin({
146
222
  server.ws.send({
147
223
  type: 'custom',
148
224
  event: styleUpdateEvent(fileId),
149
- data: source
225
+ data: cssSource
150
226
  });
151
227
  }
152
228
 
153
- cssMap.set(fileId, source);
229
+ cssMap.set(fileId, cssSource);
154
230
  return `import "${id}";`;
155
231
  }
156
232
  });
@@ -3,14 +3,52 @@ import { normalizePath } from 'vite';
3
3
  import outdent from 'outdent';
4
4
  import { getPackageInfo, parseFileScope, cssFileFilter, addFileScope, compile, processVanillaFile, stringifyFileScope } from '@vanilla-extract/integration';
5
5
 
6
+ // Mostly copied from vite's implementation
7
+ // https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
8
+ const resolvePostcssConfig = async config => {
9
+ var _config$css;
10
+
11
+ // inline postcss config via vite config
12
+ const inlineOptions = (_config$css = config.css) === null || _config$css === void 0 ? void 0 : _config$css.postcss;
13
+ const inlineOptionsIsString = typeof inlineOptions === 'string';
14
+
15
+ if (inlineOptions && !inlineOptionsIsString) {
16
+ const options = { ...inlineOptions
17
+ };
18
+ delete options.plugins;
19
+ return {
20
+ options,
21
+ plugins: inlineOptions.plugins || []
22
+ };
23
+ } else {
24
+ try {
25
+ const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
26
+ const postCssConfig = await (await import('postcss-load-config')).default({}, searchPath);
27
+ return {
28
+ options: postCssConfig.options,
29
+ // @ts-expect-error - The postcssrc options don't agree with real postcss, but it should be ok
30
+ plugins: postCssConfig.plugins
31
+ };
32
+ } catch (e) {
33
+ if (!/No PostCSS Config found/.test(e.message)) {
34
+ throw e;
35
+ }
36
+
37
+ return null;
38
+ }
39
+ }
40
+ };
41
+
6
42
  const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
7
43
 
44
+ const virtualPrefix = 'virtual:vanilla-extract:';
8
45
  function vanillaExtractPlugin({
9
46
  identifiers
10
47
  } = {}) {
11
48
  let config;
12
49
  let packageInfo;
13
50
  let server;
51
+ let postCssConfig;
14
52
  const cssMap = new Map();
15
53
  let virtualExt;
16
54
  return {
@@ -33,23 +71,26 @@ function vanillaExtractPlugin({
33
71
  };
34
72
  },
35
73
 
36
- configResolved(resolvedConfig) {
74
+ async configResolved(resolvedConfig) {
37
75
  config = resolvedConfig;
76
+
77
+ if (config.command === 'serve') {
78
+ postCssConfig = await resolvePostcssConfig(config);
79
+ }
80
+
38
81
  virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
39
82
  packageInfo = getPackageInfo(config.root);
40
83
  },
41
84
 
42
85
  resolveId(id) {
43
- if (id.indexOf(virtualExt) > 0) {
86
+ if (id.indexOf(virtualPrefix) === 0) {
44
87
  return id;
45
88
  }
46
89
  },
47
90
 
48
91
  load(id) {
49
- const extensionIndex = id.indexOf(virtualExt);
50
-
51
- if (extensionIndex > 0) {
52
- const fileScopeId = id.substring(0, extensionIndex);
92
+ if (id.indexOf(virtualPrefix) === 0) {
93
+ const fileScopeId = id.slice(virtualPrefix.length, id.indexOf(virtualExt));
53
94
 
54
95
  if (!cssMap.has(fileScopeId)) {
55
96
  throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
@@ -81,11 +122,19 @@ function vanillaExtractPlugin({
81
122
  return null;
82
123
  },
83
124
 
84
- async transform(code, id, ssr) {
125
+ async transform(code, id, ssrParam) {
85
126
  if (!cssFileFilter.test(id)) {
86
127
  return null;
87
128
  }
88
129
 
130
+ let ssr;
131
+
132
+ if (typeof ssrParam === 'boolean') {
133
+ ssr = ssrParam;
134
+ } else {
135
+ ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
136
+ }
137
+
89
138
  const index = id.indexOf('?');
90
139
  const validId = index === -1 ? id : id.substring(0, index);
91
140
 
@@ -117,12 +166,21 @@ function vanillaExtractPlugin({
117
166
  source,
118
167
  filePath: validId,
119
168
  identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
120
- serializeVirtualCssPath: ({
169
+ serializeVirtualCssPath: async ({
121
170
  fileScope,
122
171
  source
123
172
  }) => {
124
173
  const fileId = stringifyFileScope(fileScope);
125
- const id = `${fileId}${virtualExt}`;
174
+ const id = `${virtualPrefix}${fileId}${virtualExt}`;
175
+ let cssSource = source;
176
+
177
+ if (postCssConfig) {
178
+ const postCssResult = await (await import('postcss')).default(postCssConfig.plugins).process(source, { ...postCssConfig.options,
179
+ from: undefined,
180
+ map: false
181
+ });
182
+ cssSource = postCssResult.css;
183
+ }
126
184
 
127
185
  if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
128
186
  const {
@@ -137,11 +195,11 @@ function vanillaExtractPlugin({
137
195
  server.ws.send({
138
196
  type: 'custom',
139
197
  event: styleUpdateEvent(fileId),
140
- data: source
198
+ data: cssSource
141
199
  });
142
200
  }
143
201
 
144
- cssMap.set(fileId, source);
202
+ cssMap.set(fileId, cssSource);
145
203
  return `import "${id}";`;
146
204
  }
147
205
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.0.0",
3
+ "version": "3.1.2",
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,13 +15,15 @@
15
15
  "author": "SEEK",
16
16
  "license": "MIT",
17
17
  "dependencies": {
18
- "@vanilla-extract/integration": "^1.4.3",
19
- "outdent": "^0.8.0"
18
+ "@vanilla-extract/integration": "^2.0.1",
19
+ "outdent": "^0.8.0",
20
+ "postcss": "^8.3.6",
21
+ "postcss-load-config": "^3.1.0"
20
22
  },
21
23
  "devDependencies": {
22
- "vite": "^2.6.0"
24
+ "vite": "^2.7.0"
23
25
  },
24
26
  "peerDependencies": {
25
27
  "vite": "^2.2.3"
26
28
  }
27
- }
29
+ }
package/CHANGELOG.md DELETED
@@ -1,185 +0,0 @@
1
- # @vanilla-extract/vite-plugin
2
-
3
- ## 3.0.0
4
-
5
- ### Major Changes
6
-
7
- - [#422](https://github.com/seek-oss/vanilla-extract/pull/422) [`4e5ba05`](https://github.com/seek-oss/vanilla-extract/commit/4e5ba05549bc0baae8d82b9e9da9e69f032a1191) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Remove the `devStyleRuntime` option
8
-
9
- ### Patch Changes
10
-
11
- - [#422](https://github.com/seek-oss/vanilla-extract/pull/422) [`4e5ba05`](https://github.com/seek-oss/vanilla-extract/commit/4e5ba05549bc0baae8d82b9e9da9e69f032a1191) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Fix HMR for pre-existing CSS
12
-
13
- Previously, styles would not hot reload correctly when returning to a previously cached version
14
-
15
- ## 2.1.3
16
-
17
- ### Patch Changes
18
-
19
- - [#409](https://github.com/seek-oss/vanilla-extract/pull/409) [`a9c5cb7`](https://github.com/seek-oss/vanilla-extract/commit/a9c5cb768ad10bd25dd1a31041733fc96cd467a0) Thanks [@benjervis](https://github.com/benjervis)! - Fix plugin for watch mode.
20
-
21
- The vite plugin previously relied on a one to one matching of resolve to load calls, and would clean up the CSS stored in memory after it was loaded.
22
-
23
- This isn't true in `--watch` mode, as the same file can be loaded on the rebuild without having to be resolved, which the plugin now handles.
24
-
25
- * [#409](https://github.com/seek-oss/vanilla-extract/pull/409) [`a9c5cb7`](https://github.com/seek-oss/vanilla-extract/commit/a9c5cb768ad10bd25dd1a31041733fc96cd467a0) Thanks [@benjervis](https://github.com/benjervis)! - Update the "vanilla-extract" devStyleRuntime option.
26
-
27
- When using the vanilla browser runtime in vite, it now operates on a new model where a .css.js file is generated, that uses @vanilla-extract/css/injectStyles to add the css to the browser.
28
-
29
- This allows for hot reloading of styles, and makes styles a bit easier to debug at dev time (because they're actually visible).
30
-
31
- - [#409](https://github.com/seek-oss/vanilla-extract/pull/409) [`a9c5cb7`](https://github.com/seek-oss/vanilla-extract/commit/a9c5cb768ad10bd25dd1a31041733fc96cd467a0) Thanks [@benjervis](https://github.com/benjervis)! - Handle vite 2.6.
32
-
33
- As of vite 2.6 and greater, `?used` gets appended to css imports, which causes the file imports to be not what we expected.
34
-
35
- This caused mismatching classnames in the vite plugin, and it caused the babel plugin to not add filescopes when it should have.
36
-
37
- * [#409](https://github.com/seek-oss/vanilla-extract/pull/409) [`a9c5cb7`](https://github.com/seek-oss/vanilla-extract/commit/a9c5cb768ad10bd25dd1a31041733fc96cd467a0) Thanks [@benjervis](https://github.com/benjervis)! - Automatically optimize deps.
38
-
39
- When using the new vanilla browser runtime, the new `injectStyles` dependency gets injected at runtime, so vite can't discover it ahead of time and pre-bundle it.
40
-
41
- The plugin will now add the dependency to optimizeDeps if the vanilla runtime is being used so that it's optimized up front.
42
- It also ensures that some relevant vanilla packages are externalised in SSR mode.
43
-
44
- * Updated dependencies [[`a9c5cb7`](https://github.com/seek-oss/vanilla-extract/commit/a9c5cb768ad10bd25dd1a31041733fc96cd467a0)]:
45
- - @vanilla-extract/integration@1.4.3
46
-
47
- ## 2.1.2
48
-
49
- ### Patch Changes
50
-
51
- - [#398](https://github.com/seek-oss/vanilla-extract/pull/398) [`63f4ed0`](https://github.com/seek-oss/vanilla-extract/commit/63f4ed0e67419df565f3f447cf27cec612afbb8d) Thanks [@benjervis](https://github.com/benjervis)! - Fixes a bug with vite where "?used" is appended to the file path of css files.
52
-
53
- This could cause different class name hashes to be generated between SSR and client builds.
54
- This was introduced in vite 2.6.0.
55
-
56
- ## 2.1.1
57
-
58
- ### Patch Changes
59
-
60
- - [#393](https://github.com/seek-oss/vanilla-extract/pull/393) [`2f379f1`](https://github.com/seek-oss/vanilla-extract/commit/2f379f118c2a2fe6dc08a1cc15a395dbc0f17ece) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Fix compilation errors in Vite 2.6
61
-
62
- - Updated dependencies [[`2f379f1`](https://github.com/seek-oss/vanilla-extract/commit/2f379f118c2a2fe6dc08a1cc15a395dbc0f17ece)]:
63
- - @vanilla-extract/integration@1.4.2
64
-
65
- ## 2.1.0
66
-
67
- ### Minor Changes
68
-
69
- - [#373](https://github.com/seek-oss/vanilla-extract/pull/373) [`a55d2cf`](https://github.com/seek-oss/vanilla-extract/commit/a55d2cfd7c4ca9175a2c86557888df90907bfd0f) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Add `devStyleRuntime` option to allow switching between dev style runtimes
70
-
71
- The built-in Vite dev style runtime (what adds styles to the page when running `vite serve`) doesn't seem to clean up old styles as expected. Passing `devStyleRuntime: 'vanilla-extract'` will instead use vanilla-extract's browser runtime. This pushes all style creation in development to the browser, but may give a better HMR experience.
72
-
73
- ## 2.0.2
74
-
75
- ### Patch Changes
76
-
77
- - [#341](https://github.com/seek-oss/vanilla-extract/pull/341) [`0b743e7`](https://github.com/seek-oss/vanilla-extract/commit/0b743e744447616f8daf0c6b88beff8ffef8d41b) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Refactor SSR file scoping to use centralised `addFileScope` implementation
78
-
79
- - Updated dependencies [[`50bae14`](https://github.com/seek-oss/vanilla-extract/commit/50bae14bf38c8a971ad1727cb8e827c86da06772), [`0b743e7`](https://github.com/seek-oss/vanilla-extract/commit/0b743e744447616f8daf0c6b88beff8ffef8d41b)]:
80
- - @vanilla-extract/integration@1.3.0
81
-
82
- ## 2.0.1
83
-
84
- ### Patch Changes
85
-
86
- - [#327](https://github.com/seek-oss/vanilla-extract/pull/327) [`73b55f8`](https://github.com/seek-oss/vanilla-extract/commit/73b55f8e7e205701abdb1cbb029c3f2f5080b6fd) Thanks [@benjervis](https://github.com/benjervis)! - Fix bug where precompiled .css.ts files (.css.js) were treated differently in SSR mode.
87
-
88
- ## 2.0.0
89
-
90
- ### Major Changes
91
-
92
- - [#323](https://github.com/seek-oss/vanilla-extract/pull/323) [`1e7d647`](https://github.com/seek-oss/vanilla-extract/commit/1e7d6470398a0fbcbdef4118e678150932cd9275) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc) can now be configured via the `identifiers` option which accepts either `short` or `debug`.
93
-
94
- - `short` identifiers are a 7+ character hash. e.g. `hnw5tz3`
95
- - `debug` identifiers contain human readable prefixes representing the owning filename and a potential rule level debug name. e.g. `somefile_mystyle_hnw5tz3`
96
-
97
- ```js
98
- import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
99
-
100
- vanillaExtractPlugin({ identifiers: 'short' });
101
- ```
102
-
103
- BREAKING CHANGE
104
-
105
- Previously identifiers were formatted as `short` when `process.env.NODE_ENV` was set to "production". By default, they will now be formatted according to Vite's [mode config](https://vitejs.dev/config/#mode).
106
-
107
- ### Patch Changes
108
-
109
- - Updated dependencies [[`1e7d647`](https://github.com/seek-oss/vanilla-extract/commit/1e7d6470398a0fbcbdef4118e678150932cd9275)]:
110
- - @vanilla-extract/integration@1.2.0
111
-
112
- ## 1.2.0
113
-
114
- ### Minor Changes
115
-
116
- - [#311](https://github.com/seek-oss/vanilla-extract/pull/311) [`416eb9a`](https://github.com/seek-oss/vanilla-extract/commit/416eb9ae99661597a8443594f4376aacf4d295cc) Thanks [@mattcompiles](https://github.com/mattcompiles)! - In SSR mode, move to runtime evaluation of vanilla-extract files
117
-
118
- This unlocks the potential for CSS extraction on the server.
119
-
120
- ## 1.1.2
121
-
122
- ### Patch Changes
123
-
124
- - [#290](https://github.com/seek-oss/vanilla-extract/pull/290) [`adc1d64`](https://github.com/seek-oss/vanilla-extract/commit/adc1d644635a1197edd36f522f78658a641027d4) Thanks [@ygj6](https://github.com/ygj6)! - Normalize path of generated CSS files
125
-
126
- ## 1.1.1
127
-
128
- ### Patch Changes
129
-
130
- - [#270](https://github.com/seek-oss/vanilla-extract/pull/270) [`fe19158`](https://github.com/seek-oss/vanilla-extract/commit/fe1915808b374a1bbf8f67eca85c0253153e2cb9) Thanks [@yyx990803](https://github.com/yyx990803)! - Fix watching of modules imported by `.css.ts` files
131
-
132
- ## 1.1.0
133
-
134
- ### Minor Changes
135
-
136
- - [#259](https://github.com/seek-oss/vanilla-extract/pull/259) [`b8a6441`](https://github.com/seek-oss/vanilla-extract/commit/b8a6441e3400be388a520e3acf94f3bb6519cf0a) Thanks [@markdalgleish](https://github.com/markdalgleish)! - Allow the result of `composeStyles` to be used in selectors
137
-
138
- When style compositions are used in selectors, they are now assigned an additional class so they can be uniquely identified. When selectors are processed internally, the composed classes are removed, only leaving behind the unique identifier classes. This allows you to treat them as if they were a single class within vanilla-extract selectors.
139
-
140
- ```ts
141
- import { style, globalStyle, composeStyles } from '@vanilla-extract/css';
142
-
143
- const background = style({ background: 'mintcream' });
144
- const padding = style({ padding: 12 });
145
-
146
- export const container = composeStyles(background, padding);
147
-
148
- globalStyle(`${container} *`, {
149
- boxSizing: 'border-box',
150
- });
151
- ```
152
-
153
- ### Patch Changes
154
-
155
- - Updated dependencies [[`b8a6441`](https://github.com/seek-oss/vanilla-extract/commit/b8a6441e3400be388a520e3acf94f3bb6519cf0a)]:
156
- - @vanilla-extract/integration@1.1.0
157
-
158
- ## 1.0.0
159
-
160
- ### Major Changes
161
-
162
- - [#171](https://github.com/seek-oss/vanilla-extract/pull/171) [`84a8611`](https://github.com/seek-oss/vanilla-extract/commit/84a8611972f32a00a6cbd85267a01dd2d31be869) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Release v1
163
-
164
- ### Patch Changes
165
-
166
- - Updated dependencies [[`84a8611`](https://github.com/seek-oss/vanilla-extract/commit/84a8611972f32a00a6cbd85267a01dd2d31be869)]:
167
- - @vanilla-extract/integration@1.0.0
168
-
169
- ## 0.1.1
170
-
171
- ### Patch Changes
172
-
173
- - [#96](https://github.com/seek-oss/vanilla-extract/pull/96) [`697a233`](https://github.com/seek-oss/vanilla-extract/commit/697a2332cdb34886af26224c13f1efb73b6d36b3) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Change vite-plugin to be a named export
174
-
175
- BREAKING CHANGE
176
-
177
- ```diff
178
- -import vanillaExtractPlugin from '@vanilla-extract/vite-plugin';
179
- +import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
180
-
181
- // vite.config.js
182
- export default {
183
- plugins: [vanillaExtractPlugin()]
184
- }
185
- ```