-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathprocessVanillaFile.ts
394 lines (329 loc) · 10.4 KB
/
processVanillaFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { FileScope, Adapter } from '@vanilla-extract/css';
import { transformCss } from '@vanilla-extract/css/transformCss';
import evalCode from 'eval';
import { stringify } from 'javascript-stringify';
import dedent from 'dedent';
import { hash } from './hash';
import { serializeCss } from './serialize';
import type { IdentifierOption } from './types';
const originalNodeEnv = process.env.NODE_ENV;
// Copied from https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore/blob/51f83bd3db728fd7ee177de1ffc253fdb99c537f/README.md#_isplainobject
function isPlainObject(value: unknown) {
if (typeof value !== 'object' || value === null) {
return false;
}
if (Object.prototype.toString.call(value) !== '[object Object]') {
return false;
}
const prototype = Object.getPrototypeOf(value);
if (prototype === null) {
return true;
}
const constructor =
Object.prototype.hasOwnProperty.call(prototype, 'constructor') &&
prototype.constructor;
return (
typeof constructor === 'function' &&
constructor instanceof constructor &&
Function.prototype.call(constructor) === Function.prototype.call(value)
);
}
export function stringifyFileScope({
packageName,
filePath,
}: FileScope): string {
return packageName ? `${filePath}$$$${packageName}` : filePath;
}
export function parseFileScope(serialisedFileScope: string): FileScope {
const [filePath, packageName] = serialisedFileScope.split('$$$');
return {
filePath,
packageName,
};
}
interface ProcessVanillaFileOptions {
source: string;
filePath: string;
outputCss?: boolean;
identOption?: IdentifierOption;
serializeVirtualCssPath?: (file: {
fileName: string;
fileScope: FileScope;
source: string;
}) => string | Promise<string>;
}
export async function processVanillaFile({
source,
filePath,
outputCss = true,
identOption = process.env.NODE_ENV === 'production' ? 'short' : 'debug',
serializeVirtualCssPath,
}: ProcessVanillaFileOptions) {
type Css = Parameters<Adapter['appendCss']>[0];
type Composition = Parameters<Adapter['registerComposition']>[0];
const cssByFileScope = new Map<string, Array<Css>>();
const localClassNames = new Set<string>();
const composedClassLists: Array<Composition> = [];
const usedCompositions = new Set<string>();
const cssAdapter: Adapter = {
appendCss: (css, fileScope) => {
if (outputCss) {
const serialisedFileScope = stringifyFileScope(fileScope);
const fileScopeCss = cssByFileScope.get(serialisedFileScope) ?? [];
fileScopeCss.push(css);
cssByFileScope.set(serialisedFileScope, fileScopeCss);
}
},
registerClassName: (className) => {
localClassNames.add(className);
},
registerComposition: (composedClassList) => {
composedClassLists.push(composedClassList);
},
markCompositionUsed: (identifier) => {
usedCompositions.add(identifier);
},
onEndFileScope: () => {},
getIdentOption: () => identOption,
};
const currentNodeEnv = process.env.NODE_ENV;
// Vite sometimes modifies NODE_ENV which causes different versions (e.g. dev/prod) of vanilla packages to be loaded
// This can cause CSS to be bound to the wrong instance, resulting in no CSS output
// To get around this we set the NODE_ENV back to the original value ONLY during eval
process.env.NODE_ENV = originalNodeEnv;
const adapterBoundSource = `
require('@vanilla-extract/css/adapter').setAdapter(__adapter__);
${source}
`;
const evalResult = evalCode(
adapterBoundSource,
filePath,
{ console, process, __adapter__: cssAdapter },
true,
) as Record<string, unknown>;
process.env.NODE_ENV = currentNodeEnv;
const cssImports = [];
for (const [serialisedFileScope, fileScopeCss] of cssByFileScope) {
const fileScope = parseFileScope(serialisedFileScope);
const css = transformCss({
localClassNames: Array.from(localClassNames),
composedClassLists,
cssObjs: fileScopeCss,
}).join('\n');
const fileName = `${fileScope.filePath}.vanilla.css`;
let virtualCssFilePath: string;
if (serializeVirtualCssPath) {
const serializedResult = serializeVirtualCssPath({
fileName,
fileScope,
source: css,
});
if (typeof serializedResult === 'string') {
virtualCssFilePath = serializedResult;
} else {
virtualCssFilePath = await serializedResult;
}
} else {
const serializedCss = await serializeCss(css);
virtualCssFilePath = `import '${fileName}?source=${serializedCss}';`;
}
cssImports.push(virtualCssFilePath);
}
// We run this code inside eval as jest seems to create a difrerent instance of the adapter file
// for requires executed within the eval and all CSS can be lost.
evalCode(
`const { removeAdapter } = require('@vanilla-extract/css/adapter');
// Backwards compat with older versions of @vanilla-extract/css
if (removeAdapter) {
removeAdapter();
}
`,
filePath,
{ console, process },
true,
);
const unusedCompositions = composedClassLists
.filter(({ identifier }) => !usedCompositions.has(identifier))
.map(({ identifier }) => identifier);
const unusedCompositionRegex =
unusedCompositions.length > 0
? RegExp(`(${unusedCompositions.join('|')})\\s`, 'g')
: null;
return serializeVanillaModule(cssImports, evalResult, unusedCompositionRegex);
}
function stringifyExports(
functionSerializationImports: Set<string>,
value: any,
unusedCompositionRegex: RegExp | null,
key: string,
exportLookup: Map<any, string>,
exportDependencyGraph: DependencyGraph,
): any {
return stringify(
value,
(value, _indent, next) => {
const valueType = typeof value;
if (
valueType === 'boolean' ||
valueType === 'number' ||
valueType === 'undefined' ||
value === null
) {
return next(value);
}
if (Array.isArray(value) || isPlainObject(value)) {
const reusedExport = exportLookup.get(value);
if (reusedExport && reusedExport !== key) {
exportDependencyGraph.addDependency(key, reusedExport);
return reusedExport;
}
return next(value);
}
if (Symbol.toStringTag in Object(value)) {
const { [Symbol.toStringTag]: _tag, ...valueWithoutTag } = value;
return next(valueWithoutTag);
}
if (valueType === 'string') {
return next(
unusedCompositionRegex
? value.replace(unusedCompositionRegex, '')
: value,
);
}
if (
valueType === 'function' &&
(value.__function_serializer__ || value.__recipe__)
) {
const { importPath, importName, args } =
value.__function_serializer__ || value.__recipe__;
if (
typeof importPath !== 'string' ||
typeof importName !== 'string' ||
!Array.isArray(args)
) {
throw new Error('Invalid function serialization params');
}
try {
const hashedImportName = `_${hash(`${importName}${importPath}`).slice(
0,
5,
)}`;
functionSerializationImports.add(
`import { ${importName} as ${hashedImportName} } from '${importPath}';`,
);
return `${hashedImportName}(${args
.map((arg) =>
stringifyExports(
functionSerializationImports,
arg,
unusedCompositionRegex,
key,
exportLookup,
exportDependencyGraph,
),
)
.join(',')})`;
} catch (err) {
console.error(err);
throw new Error('Invalid function serialization params');
}
}
throw new Error(dedent`
Invalid exports.
You can only export plain objects, arrays, strings, numbers and null/undefined.
`);
},
0,
{
references: true, // Allow circular references
maxDepth: Infinity,
maxValues: Infinity,
},
);
}
const defaultExportName = '__default__';
class DependencyGraph {
graph: Map<string, Set<string>>;
public constructor() {
this.graph = new Map();
}
/**
* Creates a "depends on" relationship between `key` and `dependency`
*/
public addDependency(key: string, dependency: string) {
const dependencies = this.graph.get(key);
if (dependencies) {
dependencies.add(dependency);
} else {
this.graph.set(key, new Set([dependency]));
}
}
/**
* Whether or not `key` depends on `dependency`
*/
public dependsOn(key: string, dependency: string): boolean {
const dependencies = this.graph.get(key);
if (dependencies) {
if (dependencies?.has(dependency)) {
return true;
}
for (const [dep] of dependencies.entries()) {
if (this.dependsOn(dep, dependency)) {
return true;
}
}
}
return false;
}
}
export function serializeVanillaModule(
cssImports: Array<string>,
exports: Record<string, unknown>,
unusedCompositionRegex: RegExp | null,
) {
const functionSerializationImports = new Set<string>();
const exportLookup = new Map(
Object.entries(exports).map(([key, value]) => [
value,
key === 'default' ? defaultExportName : key,
]),
);
const exportDependencyGraph = new DependencyGraph();
const moduleExports = Object.entries(exports).map(([key, value]) => {
const serializedExport = stringifyExports(
functionSerializationImports,
value,
unusedCompositionRegex,
key === 'default' ? defaultExportName : key,
exportLookup,
exportDependencyGraph,
);
if (key === 'default') {
return [
defaultExportName,
[
`var ${defaultExportName} = ${serializedExport};`,
`export default ${defaultExportName};`,
].join('\n'),
];
}
return [key, `export var ${key} = ${serializedExport};`];
});
const sortedModuleExports = moduleExports
.sort(([key1], [key2]) => {
if (exportDependencyGraph.dependsOn(key1, key2)) {
return 1;
}
if (exportDependencyGraph.dependsOn(key2, key1)) {
return -1;
}
return 0;
})
.map(([, s]) => s);
const outputCode = [
...cssImports,
...functionSerializationImports,
...sortedModuleExports,
];
return outputCode.join('\n');
}