@vanilla-extract/vite-plugin 0.0.0-filescope-urls-202281281439

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.
@@ -0,0 +1,8 @@
1
+ import type { Plugin } from 'vite';
2
+ import { IdentifierOption, CompileOptions } from '@vanilla-extract/integration';
3
+ interface Options {
4
+ identifiers?: IdentifierOption;
5
+ esbuildOptions?: CompileOptions['esbuildOptions'];
6
+ }
7
+ export declare function vanillaExtractPlugin({ identifiers, esbuildOptions, }?: Options): Plugin;
8
+ 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>;
@@ -0,0 +1 @@
1
+ export * from "./declarations/src/index";
@@ -0,0 +1,273 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var path = require('path');
6
+ var url = require('url');
7
+ var vite = require('vite');
8
+ var outdent = require('outdent');
9
+ var integration = require('@vanilla-extract/integration');
10
+
11
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
+
13
+ function _interopNamespace(e) {
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
+ }
30
+
31
+ var path__default = /*#__PURE__*/_interopDefault(path);
32
+ var outdent__default = /*#__PURE__*/_interopDefault(outdent);
33
+
34
+ // Mostly copied from vite's implementation
35
+ // https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
36
+ const resolvePostcssConfig = async config => {
37
+ var _config$css;
38
+
39
+ // inline postcss config via vite config
40
+ const inlineOptions = (_config$css = config.css) === null || _config$css === void 0 ? void 0 : _config$css.postcss;
41
+ const inlineOptionsIsString = typeof inlineOptions === 'string';
42
+
43
+ if (inlineOptions && !inlineOptionsIsString) {
44
+ const options = { ...inlineOptions
45
+ };
46
+ delete options.plugins;
47
+ return {
48
+ options,
49
+ plugins: inlineOptions.plugins || []
50
+ };
51
+ } else {
52
+ try {
53
+ const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
54
+ const postCssConfig = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss-load-config')); })).default({}, searchPath);
55
+ return {
56
+ options: postCssConfig.options,
57
+ // @ts-expect-error - The postcssrc options don't agree with real postcss, but it should be ok
58
+ plugins: postCssConfig.plugins
59
+ };
60
+ } catch (e) {
61
+ if (!/No PostCSS Config found/.test(e.message)) {
62
+ throw e;
63
+ }
64
+
65
+ return null;
66
+ }
67
+ }
68
+ };
69
+
70
+ const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
71
+
72
+ const virtualExtCss = '.vanilla.css';
73
+ const virtualExtJs = '.vanilla.js';
74
+ function vanillaExtractPlugin({
75
+ identifiers,
76
+ esbuildOptions
77
+ } = {}) {
78
+ let config;
79
+ let server;
80
+ let postCssConfig;
81
+ const cssMap = new Map();
82
+ let forceEmitCssInSsrBuild = !!process.env.VITE_RSC_BUILD;
83
+ let packageName;
84
+
85
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
86
+
87
+ return {
88
+ name: 'vanilla-extract',
89
+ enforce: 'pre',
90
+
91
+ configureServer(_server) {
92
+ server = _server;
93
+ },
94
+
95
+ config(_userConfig, env) {
96
+ const include = env.command === 'serve' ? ['@vanilla-extract/css/injectStyles'] : [];
97
+ return {
98
+ optimizeDeps: {
99
+ include
100
+ },
101
+ ssr: {
102
+ external: ['@vanilla-extract/css', '@vanilla-extract/css/fileScope', '@vanilla-extract/css/adapter']
103
+ }
104
+ };
105
+ },
106
+
107
+ async configResolved(resolvedConfig) {
108
+ config = resolvedConfig;
109
+ packageName = integration.getPackageInfo(config.root).name;
110
+
111
+ if (config.command === 'serve') {
112
+ postCssConfig = await resolvePostcssConfig(config);
113
+ }
114
+
115
+ if (config.plugins.some(p => p.name === 'astro:build')) {
116
+ forceEmitCssInSsrBuild = true;
117
+ }
118
+ },
119
+
120
+ resolveId(source) {
121
+ const [validId, query] = source.split('?');
122
+
123
+ if (!validId.endsWith(virtualExtCss) && !validId.endsWith(virtualExtJs)) {
124
+ return;
125
+ } // Absolute paths seem to occur often in monorepos, where files are
126
+ // imported from outside the config root.
127
+
128
+
129
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId); // There should always be an entry in the `cssMap` here.
130
+ // The only valid scenario for a missing one is if someone had written
131
+ // a file in their app using the .vanilla.js/.vanilla.css extension
132
+
133
+ if (cssMap.has(absoluteId)) {
134
+ // Keep the original query string for HMR.
135
+ return absoluteId + (query ? `?${query}` : '');
136
+ }
137
+ },
138
+
139
+ load(id) {
140
+ const [validId] = id.split('?');
141
+
142
+ if (!cssMap.has(validId)) {
143
+ return;
144
+ }
145
+
146
+ const css = cssMap.get(validId);
147
+
148
+ if (typeof css !== 'string') {
149
+ return;
150
+ }
151
+
152
+ if (validId.endsWith(virtualExtCss)) {
153
+ return css;
154
+ }
155
+
156
+ return outdent__default["default"]`
157
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
158
+
159
+ const inject = (css) => injectStyles({
160
+ fileScope: ${JSON.stringify({
161
+ filePath: validId
162
+ })},
163
+ css
164
+ });
165
+
166
+ inject(${JSON.stringify(css)});
167
+
168
+ if (import.meta.hot) {
169
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
170
+ inject(css);
171
+ });
172
+ }
173
+ `;
174
+ },
175
+
176
+ async transform(code, id, ssrParam) {
177
+ const [validId] = id.split('?');
178
+
179
+ if (!integration.cssFileFilter.test(validId)) {
180
+ return null;
181
+ }
182
+
183
+ let ssr;
184
+
185
+ if (typeof ssrParam === 'boolean') {
186
+ ssr = ssrParam;
187
+ } else {
188
+ ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
189
+ }
190
+
191
+ if (ssr && !forceEmitCssInSsrBuild) {
192
+ return integration.addFileScope({
193
+ source: code,
194
+ filePath: vite.normalizePath(validId),
195
+ rootPath: config.root,
196
+ packageName: packageName
197
+ });
198
+ }
199
+
200
+ const {
201
+ source,
202
+ watchFiles
203
+ } = await integration.compile({
204
+ filePath: validId,
205
+ cwd: config.root,
206
+ esbuildOptions
207
+ });
208
+
209
+ for (const file of watchFiles) {
210
+ // In start mode, we need to prevent the file from rewatching itself.
211
+ // If it's a `build --watch`, it needs to watch everything.
212
+ if (config.command === 'build' || file !== validId) {
213
+ this.addWatchFile(file);
214
+ }
215
+ }
216
+
217
+ const output = await integration.processVanillaFile({
218
+ source,
219
+ filePath: validId,
220
+ identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
221
+ serializeVirtualCssPath: async ({
222
+ fileScope,
223
+ source
224
+ }) => {
225
+ const absoluteFilePath = url.fileURLToPath(fileScope.url);
226
+ const absoluteId = `${absoluteFilePath}${config.command === 'build' || ssr && forceEmitCssInSsrBuild ? virtualExtCss : virtualExtJs}`;
227
+ let cssSource = source;
228
+
229
+ if (postCssConfig) {
230
+ const postCssResult = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss')); })).default(postCssConfig.plugins).process(source, { ...postCssConfig.options,
231
+ from: undefined,
232
+ map: false
233
+ });
234
+ cssSource = postCssResult.css;
235
+ }
236
+
237
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
238
+ const {
239
+ moduleGraph
240
+ } = server;
241
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || []);
242
+
243
+ if (module) {
244
+ moduleGraph.invalidateModule(module); // Vite uses this timestamp to add `?t=` query string automatically for HMR.
245
+
246
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
247
+ }
248
+
249
+ server.ws.send({
250
+ type: 'custom',
251
+ event: styleUpdateEvent(absoluteId),
252
+ data: cssSource
253
+ });
254
+ }
255
+
256
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
257
+ // are consistent across build machines
258
+
259
+ return `import "${vite.normalizePath(path__default["default"].relative(config.root, absoluteId))}";`;
260
+ }
261
+ });
262
+ return {
263
+ code: output,
264
+ map: {
265
+ mappings: ''
266
+ }
267
+ };
268
+ }
269
+
270
+ };
271
+ }
272
+
273
+ exports.vanillaExtractPlugin = vanillaExtractPlugin;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./vanilla-extract-vite-plugin.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./vanilla-extract-vite-plugin.cjs.dev.js");
7
+ }
@@ -0,0 +1,273 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var path = require('path');
6
+ var url = require('url');
7
+ var vite = require('vite');
8
+ var outdent = require('outdent');
9
+ var integration = require('@vanilla-extract/integration');
10
+
11
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
12
+
13
+ function _interopNamespace(e) {
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
+ }
30
+
31
+ var path__default = /*#__PURE__*/_interopDefault(path);
32
+ var outdent__default = /*#__PURE__*/_interopDefault(outdent);
33
+
34
+ // Mostly copied from vite's implementation
35
+ // https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
36
+ const resolvePostcssConfig = async config => {
37
+ var _config$css;
38
+
39
+ // inline postcss config via vite config
40
+ const inlineOptions = (_config$css = config.css) === null || _config$css === void 0 ? void 0 : _config$css.postcss;
41
+ const inlineOptionsIsString = typeof inlineOptions === 'string';
42
+
43
+ if (inlineOptions && !inlineOptionsIsString) {
44
+ const options = { ...inlineOptions
45
+ };
46
+ delete options.plugins;
47
+ return {
48
+ options,
49
+ plugins: inlineOptions.plugins || []
50
+ };
51
+ } else {
52
+ try {
53
+ const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
54
+ const postCssConfig = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss-load-config')); })).default({}, searchPath);
55
+ return {
56
+ options: postCssConfig.options,
57
+ // @ts-expect-error - The postcssrc options don't agree with real postcss, but it should be ok
58
+ plugins: postCssConfig.plugins
59
+ };
60
+ } catch (e) {
61
+ if (!/No PostCSS Config found/.test(e.message)) {
62
+ throw e;
63
+ }
64
+
65
+ return null;
66
+ }
67
+ }
68
+ };
69
+
70
+ const styleUpdateEvent = fileId => `vanilla-extract-style-update:${fileId}`;
71
+
72
+ const virtualExtCss = '.vanilla.css';
73
+ const virtualExtJs = '.vanilla.js';
74
+ function vanillaExtractPlugin({
75
+ identifiers,
76
+ esbuildOptions
77
+ } = {}) {
78
+ let config;
79
+ let server;
80
+ let postCssConfig;
81
+ const cssMap = new Map();
82
+ let forceEmitCssInSsrBuild = !!process.env.VITE_RSC_BUILD;
83
+ let packageName;
84
+
85
+ const getAbsoluteVirtualFileId = source => vite.normalizePath(path__default["default"].join(config.root, source));
86
+
87
+ return {
88
+ name: 'vanilla-extract',
89
+ enforce: 'pre',
90
+
91
+ configureServer(_server) {
92
+ server = _server;
93
+ },
94
+
95
+ config(_userConfig, env) {
96
+ const include = env.command === 'serve' ? ['@vanilla-extract/css/injectStyles'] : [];
97
+ return {
98
+ optimizeDeps: {
99
+ include
100
+ },
101
+ ssr: {
102
+ external: ['@vanilla-extract/css', '@vanilla-extract/css/fileScope', '@vanilla-extract/css/adapter']
103
+ }
104
+ };
105
+ },
106
+
107
+ async configResolved(resolvedConfig) {
108
+ config = resolvedConfig;
109
+ packageName = integration.getPackageInfo(config.root).name;
110
+
111
+ if (config.command === 'serve') {
112
+ postCssConfig = await resolvePostcssConfig(config);
113
+ }
114
+
115
+ if (config.plugins.some(p => p.name === 'astro:build')) {
116
+ forceEmitCssInSsrBuild = true;
117
+ }
118
+ },
119
+
120
+ resolveId(source) {
121
+ const [validId, query] = source.split('?');
122
+
123
+ if (!validId.endsWith(virtualExtCss) && !validId.endsWith(virtualExtJs)) {
124
+ return;
125
+ } // Absolute paths seem to occur often in monorepos, where files are
126
+ // imported from outside the config root.
127
+
128
+
129
+ const absoluteId = source.startsWith(config.root) ? source : getAbsoluteVirtualFileId(validId); // There should always be an entry in the `cssMap` here.
130
+ // The only valid scenario for a missing one is if someone had written
131
+ // a file in their app using the .vanilla.js/.vanilla.css extension
132
+
133
+ if (cssMap.has(absoluteId)) {
134
+ // Keep the original query string for HMR.
135
+ return absoluteId + (query ? `?${query}` : '');
136
+ }
137
+ },
138
+
139
+ load(id) {
140
+ const [validId] = id.split('?');
141
+
142
+ if (!cssMap.has(validId)) {
143
+ return;
144
+ }
145
+
146
+ const css = cssMap.get(validId);
147
+
148
+ if (typeof css !== 'string') {
149
+ return;
150
+ }
151
+
152
+ if (validId.endsWith(virtualExtCss)) {
153
+ return css;
154
+ }
155
+
156
+ return outdent__default["default"]`
157
+ import { injectStyles } from '@vanilla-extract/css/injectStyles';
158
+
159
+ const inject = (css) => injectStyles({
160
+ fileScope: ${JSON.stringify({
161
+ filePath: validId
162
+ })},
163
+ css
164
+ });
165
+
166
+ inject(${JSON.stringify(css)});
167
+
168
+ if (import.meta.hot) {
169
+ import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
170
+ inject(css);
171
+ });
172
+ }
173
+ `;
174
+ },
175
+
176
+ async transform(code, id, ssrParam) {
177
+ const [validId] = id.split('?');
178
+
179
+ if (!integration.cssFileFilter.test(validId)) {
180
+ return null;
181
+ }
182
+
183
+ let ssr;
184
+
185
+ if (typeof ssrParam === 'boolean') {
186
+ ssr = ssrParam;
187
+ } else {
188
+ ssr = ssrParam === null || ssrParam === void 0 ? void 0 : ssrParam.ssr;
189
+ }
190
+
191
+ if (ssr && !forceEmitCssInSsrBuild) {
192
+ return integration.addFileScope({
193
+ source: code,
194
+ filePath: vite.normalizePath(validId),
195
+ rootPath: config.root,
196
+ packageName: packageName
197
+ });
198
+ }
199
+
200
+ const {
201
+ source,
202
+ watchFiles
203
+ } = await integration.compile({
204
+ filePath: validId,
205
+ cwd: config.root,
206
+ esbuildOptions
207
+ });
208
+
209
+ for (const file of watchFiles) {
210
+ // In start mode, we need to prevent the file from rewatching itself.
211
+ // If it's a `build --watch`, it needs to watch everything.
212
+ if (config.command === 'build' || file !== validId) {
213
+ this.addWatchFile(file);
214
+ }
215
+ }
216
+
217
+ const output = await integration.processVanillaFile({
218
+ source,
219
+ filePath: validId,
220
+ identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
221
+ serializeVirtualCssPath: async ({
222
+ fileScope,
223
+ source
224
+ }) => {
225
+ const absoluteFilePath = url.fileURLToPath(fileScope.url);
226
+ const absoluteId = `${absoluteFilePath}${config.command === 'build' || ssr && forceEmitCssInSsrBuild ? virtualExtCss : virtualExtJs}`;
227
+ let cssSource = source;
228
+
229
+ if (postCssConfig) {
230
+ const postCssResult = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('postcss')); })).default(postCssConfig.plugins).process(source, { ...postCssConfig.options,
231
+ from: undefined,
232
+ map: false
233
+ });
234
+ cssSource = postCssResult.css;
235
+ }
236
+
237
+ if (server && cssMap.has(absoluteId) && cssMap.get(absoluteId) !== source) {
238
+ const {
239
+ moduleGraph
240
+ } = server;
241
+ const [module] = Array.from(moduleGraph.getModulesByFile(absoluteId) || []);
242
+
243
+ if (module) {
244
+ moduleGraph.invalidateModule(module); // Vite uses this timestamp to add `?t=` query string automatically for HMR.
245
+
246
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
247
+ }
248
+
249
+ server.ws.send({
250
+ type: 'custom',
251
+ event: styleUpdateEvent(absoluteId),
252
+ data: cssSource
253
+ });
254
+ }
255
+
256
+ cssMap.set(absoluteId, cssSource); // We use the root relative id here to ensure file contents (content-hashes)
257
+ // are consistent across build machines
258
+
259
+ return `import "${vite.normalizePath(path__default["default"].relative(config.root, absoluteId))}";`;
260
+ }
261
+ });
262
+ return {
263
+ code: output,
264
+ map: {
265
+ mappings: ''
266
+ }
267
+ };
268
+ }
269
+
270
+ };
271
+ }
272
+
273
+ exports.vanillaExtractPlugin = vanillaExtractPlugin;