Skip to content

Commit bb48520

Browse files
mrm007mattcompiles
andauthored
Fix Rollup/Vite generated exports (#801)
Co-authored-by: mattcompiles <[email protected]>
1 parent 533a239 commit bb48520

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vanilla-extract/integration': patch
3+
---
4+
5+
Omit [`Symbol.toStringTag`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) when serializing module exports.
6+

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"ts-node": "^10.0.0",
5252
"typescript": "^4.5.0"
5353
},
54+
"packageManager": "[email protected]",
5455
"pnpm": {
5556
"patchedDependencies": {
5657
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// @ts-expect-error
2+
import evalCode from 'eval';
3+
4+
import { stringifyExports } from './processVanillaFile';
5+
6+
describe('stringifyExports', () => {
7+
test('with plain object exports', () => {
8+
const recipeImports = new Set<string>();
9+
const value = {
10+
string: 'value',
11+
boolean: true,
12+
number: 42,
13+
array: [420],
14+
object: { hello: 'world' },
15+
};
16+
17+
expect(stringifyExports(recipeImports, value, null)).toMatchInlineSnapshot(
18+
`"{string:'value','boolean':true,number:42,array:[420],object:{hello:'world'}}"`,
19+
);
20+
});
21+
22+
// https://rollupjs.org/guide/en/#outputgeneratedcode -- see output.generatedCode.symbols
23+
// https://github.com/vitejs/vite/pull/5018
24+
// https://github.com/vitejs/vite/blob/757a92f1c7c4fa961ed963edd245df77382dfde6/packages/vite/src/node/build.ts#L466-L469
25+
test('with exports generated by Rollup', () => {
26+
const recipeImports = new Set<string>();
27+
const value = Object.freeze(
28+
Object.defineProperty(
29+
{
30+
__proto__: null,
31+
some: 'export',
32+
},
33+
Symbol.toStringTag,
34+
{ value: 'Module' },
35+
),
36+
);
37+
38+
expect(stringifyExports(recipeImports, value, null)).toMatchInlineSnapshot(
39+
`"{some:'export'}"`,
40+
);
41+
});
42+
43+
test('with unusedCompositionRegex', () => {
44+
const recipeImports = new Set<string>();
45+
const value = {
46+
compositionOnly:
47+
'features_compositionOnly__1o6ek504 features_mergedStyle__1o6ek500 features_styleWithComposition__1o6ek501',
48+
mergedStyle: 'features_mergedStyle__1o6ek500',
49+
styleCompositionInSelector:
50+
'features_styleCompositionInSelector__1o6ek507 features__1o6ek505 features__1o6ek506',
51+
styleVariantsCompositionInSelector: {
52+
variant:
53+
'features_styleVariantsCompositionInSelector_variant__1o6ek50a features__1o6ek508 features__1o6ek509',
54+
},
55+
styleVariantsWithComposition: {
56+
variant:
57+
'features_styleVariantsWithComposition_variant__1o6ek502 features_mergedStyle__1o6ek500',
58+
},
59+
styleVariantsWithMappedComposition: {
60+
variant:
61+
'features_styleVariantsWithMappedComposition_variant__1o6ek503 features_mergedStyle__1o6ek500',
62+
},
63+
styleWithComposition:
64+
'features_styleWithComposition__1o6ek501 features_mergedStyle__1o6ek500',
65+
};
66+
67+
const exports = stringifyExports(
68+
recipeImports,
69+
value,
70+
/(features_compositionOnly__1o6ek504|features_styleCompositionInSelector__1o6ek507|features_styleVariantsCompositionInSelector_variant__1o6ek50a)\s/g,
71+
);
72+
const exportsObject = evalCode(`module.exports = ${exports}`, 'dummy.js');
73+
expect(exportsObject).toMatchInlineSnapshot(`
74+
Object {
75+
"compositionOnly": "features_mergedStyle__1o6ek500 features_styleWithComposition__1o6ek501",
76+
"mergedStyle": "features_mergedStyle__1o6ek500",
77+
"styleCompositionInSelector": "features__1o6ek505 features__1o6ek506",
78+
"styleVariantsCompositionInSelector": Object {
79+
"variant": "features__1o6ek508 features__1o6ek509",
80+
},
81+
"styleVariantsWithComposition": Object {
82+
"variant": "features_styleVariantsWithComposition_variant__1o6ek502 features_mergedStyle__1o6ek500",
83+
},
84+
"styleVariantsWithMappedComposition": Object {
85+
"variant": "features_styleVariantsWithMappedComposition_variant__1o6ek503 features_mergedStyle__1o6ek500",
86+
},
87+
"styleWithComposition": "features_styleWithComposition__1o6ek501 features_mergedStyle__1o6ek500",
88+
}
89+
`);
90+
});
91+
});

packages/integration/src/processVanillaFile.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export async function processVanillaFile({
161161
return serializeVanillaModule(cssImports, evalResult, unusedCompositionRegex);
162162
}
163163

164-
function stringifyExports(
164+
export function stringifyExports(
165165
recipeImports: Set<string>,
166166
value: any,
167167
unusedCompositionRegex: RegExp | null,
@@ -170,6 +170,7 @@ function stringifyExports(
170170
value,
171171
(value, _indent, next) => {
172172
const valueType = typeof value;
173+
173174
if (
174175
valueType === 'boolean' ||
175176
valueType === 'number' ||
@@ -181,6 +182,11 @@ function stringifyExports(
181182
return next(value);
182183
}
183184

185+
if (Symbol.toStringTag in Object(value)) {
186+
const { [Symbol.toStringTag]: _tag, ...valueWithoutTag } = value;
187+
return next(valueWithoutTag);
188+
}
189+
184190
if (valueType === 'string') {
185191
return next(
186192
unusedCompositionRegex

0 commit comments

Comments
 (0)