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