Skip to content

fix: processVanillaFile fileScopes race #1585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"mlly": "^1.4.2"
},
"devDependencies": {
"@types/babel__core": "^7.20.5"
"@types/babel__core": "^7.20.5",
"@fixtures/sprinkles": "workspace:*"
}
}
49 changes: 48 additions & 1 deletion packages/integration/src/processVanillaFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { serializeVanillaModule } from './processVanillaFile';
import {
processVanillaFile,
serializeVanillaModule,
} from './processVanillaFile';

describe('serializeVanillaModule', () => {
test('with plain object exports', () => {
Expand Down Expand Up @@ -233,3 +236,47 @@ describe('serializeVanillaModule', () => {
`);
});
});

describe('processVanillaFile', () => {
jest.useFakeTimers();
test('should process vanilla file with correct promise order', async () => {
const serializeVirtualCssPath1 = jest.fn(
({ source }) =>
new Promise<string>((resolve) => {
setTimeout(() => resolve(source), 1);
}),
);
const serializeVirtualCssPath2 = jest.fn(({ source }) =>
Promise.resolve(source),
);

const [result] = await Promise.all([
processVanillaFile({
source: `
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
const { style, globalStyle } = require('@vanilla-extract/css');
__vanilla_filescope__.setFileScope("dependency.css.ts", "test");
exports.x = style({ color: 'blue' });
__vanilla_filescope__.endFileScope();
__vanilla_filescope__.setFileScope("style1.css.ts", "test");
exports.y = style([exports.x]);
globalStyle('body ' + exports.y, { color: 'red' });
__vanilla_filescope__.endFileScope();
`,
filePath: require.resolve('@fixtures/sprinkles'),
serializeVirtualCssPath: serializeVirtualCssPath1,
}),
processVanillaFile({
source: `
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
__vanilla_filescope__.setFileScope("style2.css.ts", "test");
__vanilla_filescope__.endFileScope();
`,
filePath: require.resolve('@fixtures/sprinkles'),
serializeVirtualCssPath: serializeVirtualCssPath2,
}),
jest.runAllTimersAsync(),
]);
expect(result).toMatch(/.*export var y = .*style1.*/);
});
});
24 changes: 9 additions & 15 deletions packages/integration/src/processVanillaFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FileScope, Adapter } from '@vanilla-extract/css';
import { transformCss } from '@vanilla-extract/css/transformCss';
import { removeAdapter, setAdapter } from '@vanilla-extract/css/adapter';
import evalCode from 'eval';
import { stringify } from 'javascript-stringify';
import dedent from 'dedent';
Expand Down Expand Up @@ -110,8 +111,13 @@ export async function processVanillaFile({
process.env.NODE_ENV = originalNodeEnv;

const adapterBoundSource = `
require('@vanilla-extract/css/adapter').setAdapter(__adapter__);
const { setAdapter, removeAdapter } = require('@vanilla-extract/css/adapter');
setAdapter(__adapter__);
${source}
// Backwards compat with older versions of @vanilla-extract/css
if (removeAdapter) {
removeAdapter();
}
`;

const evalResult = evalCode(
Expand All @@ -127,11 +133,13 @@ export async function processVanillaFile({

for (const [serialisedFileScope, fileScopeCss] of cssByFileScope) {
const fileScope = parseFileScope(serialisedFileScope);
setAdapter(cssAdapter);
const css = transformCss({
localClassNames: Array.from(localClassNames),
composedClassLists,
cssObjs: fileScopeCss,
}).join('\n');
removeAdapter();

const fileName = `${fileScope.filePath}.vanilla.css`;

Expand All @@ -158,20 +166,6 @@ export async function processVanillaFile({
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);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.