Skip to content

Commit 50bae14

Browse files
authored
Cleanup adapter after processing styles (#343)
1 parent 0d8efe2 commit 50bae14

25 files changed

+1835
-488
lines changed

.changeset/few-lions-deny.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
'@vanilla-extract/integration': patch
4+
---
5+
6+
Cleanup adapter after processing styles

.github/workflows/screenshots.yml

Lines changed: 0 additions & 36 deletions
This file was deleted.

.github/workflows/validate.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,34 @@ jobs:
2828

2929
- name: Test
3030
run: yarn test:jest
31+
playwright:
32+
name: Playwright tests
33+
runs-on: macos-latest
34+
env:
35+
CI: true
36+
steps:
37+
- name: Checkout Repo
38+
uses: actions/checkout@main
39+
40+
- name: Setup Node.js 14.x
41+
uses: actions/setup-node@main
42+
with:
43+
node-version: 14.x
44+
45+
- name: Install Dependencies
46+
run: yarn --immutable
47+
48+
- name: Install Browsers
49+
run: yarn playwright install chromium
50+
51+
- name: Build packages
52+
run: yarn build
53+
54+
- name: Screenshot tests
55+
run: yarn test:playwright
56+
57+
- uses: actions/upload-artifact@v2
58+
if: failure()
59+
with:
60+
name: test-results
61+
path: test-results/

packages/css/src/adapter.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ export const mockAdapter: Adapter = {
99
getIdentOption: () => 'debug',
1010
};
1111

12-
let adapter: Adapter = mockAdapter;
12+
const adapterStack: Array<Adapter> = [mockAdapter];
13+
14+
const currentAdapter = () => {
15+
if (adapterStack.length < 1) {
16+
throw new Error('No adapter configured');
17+
}
18+
19+
return adapterStack[adapterStack.length - 1];
20+
};
1321

1422
let hasConfiguredAdapter = false;
1523

@@ -21,34 +29,41 @@ export const setAdapterIfNotSet = (newAdapter: Adapter) => {
2129

2230
export const setAdapter = (newAdapter: Adapter) => {
2331
hasConfiguredAdapter = true;
24-
adapter = newAdapter;
32+
33+
adapterStack.push(newAdapter);
34+
};
35+
36+
export const removeAdapter = () => {
37+
adapterStack.pop();
2538
};
2639

2740
export const appendCss: Adapter['appendCss'] = (...props) => {
28-
return adapter.appendCss(...props);
41+
return currentAdapter().appendCss(...props);
2942
};
3043

3144
export const registerClassName: Adapter['registerClassName'] = (...props) => {
32-
return adapter.registerClassName(...props);
45+
return currentAdapter().registerClassName(...props);
3346
};
3447

3548
export const registerComposition: Adapter['registerComposition'] = (
3649
...props
3750
) => {
38-
return adapter.registerComposition(...props);
51+
return currentAdapter().registerComposition(...props);
3952
};
4053

4154
export const markCompositionUsed: Adapter['markCompositionUsed'] = (
4255
...props
4356
) => {
44-
return adapter.markCompositionUsed(...props);
57+
return currentAdapter().markCompositionUsed(...props);
4558
};
4659

4760
export const onEndFileScope: Adapter['onEndFileScope'] = (...props) => {
48-
return adapter.onEndFileScope(...props);
61+
return currentAdapter().onEndFileScope(...props);
4962
};
5063

5164
export const getIdentOption: Adapter['getIdentOption'] = (...props) => {
65+
const adapter = currentAdapter();
66+
5267
// Backwards compatibility with old versions of the integration package
5368
if (!('getIdentOption' in adapter)) {
5469
return process.env.NODE_ENV === 'production' ? 'short' : 'debug';

packages/integration/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "1.2.0",
44
"description": "Zero-runtime Stylesheets-in-TypeScript",
55
"main": "dist/vanilla-extract-integration.cjs.js",
6-
"module": "dist/vanilla-extract-integration.esm.js",
76
"files": [
87
"/dist"
98
],

packages/integration/src/processVanillaFile.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { FileScope, Adapter } from '@vanilla-extract/css';
2-
import { setAdapter } from '@vanilla-extract/css/adapter';
32
import { transformCss } from '@vanilla-extract/css/transformCss';
43
// @ts-expect-error
54
import evalCode from 'eval';
@@ -75,21 +74,22 @@ export function processVanillaFile({
7574
getIdentOption: () => identOption,
7675
};
7776

78-
setAdapter(cssAdapter);
79-
8077
const currentNodeEnv = process.env.NODE_ENV;
8178

82-
const sourceWithBoundLoaderInstance = `require('@vanilla-extract/css/adapter').setAdapter(__adapter__);${source};`;
83-
8479
// Vite sometimes modifies NODE_ENV which causes different versions (e.g. dev/prod) of vanilla packages to be loaded
8580
// This can cause CSS to be bound to the wrong instance, resulting in no CSS output
8681
// To get around this we set the NODE_ENV back to the original value ONLY during eval
8782
process.env.NODE_ENV = originalNodeEnv;
8883

84+
const adapterBoundSource = `
85+
require('@vanilla-extract/css/adapter').setAdapter(__adapter__);
86+
${source}
87+
`;
88+
8989
const evalResult = evalCode(
90-
sourceWithBoundLoaderInstance,
90+
adapterBoundSource,
9191
filePath,
92-
{ console, __adapter__: cssAdapter, process },
92+
{ console, process, __adapter__: cssAdapter },
9393
true,
9494
);
9595

@@ -119,6 +119,20 @@ export function processVanillaFile({
119119
cssImports.push(virtualCssFilePath);
120120
}
121121

122+
// We run this code inside eval as jest seems to create a difrerent instance of the adapter file
123+
// for requires executed within the eval and all CSS can be lost.
124+
evalCode(
125+
`const { removeAdapter } = require('@vanilla-extract/css/adapter');
126+
// Backwards compat with older versions of @vanilla-extract/css
127+
if (removeAdapter) {
128+
removeAdapter();
129+
}
130+
`,
131+
filePath,
132+
{ console, process },
133+
true,
134+
);
135+
122136
const unusedCompositions = composedClassLists
123137
.filter(({ identifier }) => !usedCompositions.has(identifier))
124138
.map(({ identifier }) => identifier);

test-helpers/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
"@vanilla-extract/webpack-plugin": "*",
1616
"babel-loader": "^8.2.2",
1717
"css-loader": "^5.2.4",
18+
"cssnano": "^5.0.8",
1819
"got": "^11.8.2",
1920
"html-webpack-plugin": "^5.3.1",
2021
"mini-css-extract-plugin": "^1.5.1",
2122
"minimist": "^1.2.5",
2223
"path-browserify": "^1.0.1",
2324
"portfinder": "^1.0.28",
25+
"postcss": "^8.3.6",
26+
"prettier": "^2.3.2",
2427
"serve-handler": "^6.1.3",
2528
"snowpack": "^3.5.1",
2629
"style-loader": "^2.0.0",
@@ -31,6 +34,7 @@
3134
},
3235
"devDependencies": {
3336
"@types/minimist": "^1",
37+
"@types/prettier": "^2",
3438
"@types/serve-handler": "^6"
3539
}
3640
}

test-helpers/src/getStylesheet.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import postcss from 'postcss';
2+
import prettier from 'prettier';
3+
// @ts-expect-error
4+
import cssnano from 'cssnano';
15
import got from 'got';
26

37
export const stylesheetName = 'main.css';
48

59
export async function getStylesheet(url: string, stylesheetName = 'main.css') {
610
const response = await got(`${url}/${stylesheetName}`);
11+
const { css } = await postcss([cssnano({ preset: 'default' })]).process(
12+
response.body,
13+
{
14+
from: undefined,
15+
},
16+
);
717

8-
return response.body;
18+
return prettier.format(css, { parser: 'css' });
919
}

test-helpers/src/startFixture/esbuild.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const startEsbuildFixture = async (
6565
return {
6666
type: 'esbuild',
6767
url: `http://localhost:${port}`,
68+
stylesheet: 'index.css',
6869
close: () => {
6970
server.stop();
7071

test-helpers/src/startFixture/webpack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export const startWebpackFixture = (
110110
}),
111111
),
112112
type,
113+
stylesheet: 'main.css',
113114
});
114115
},
115116
});

0 commit comments

Comments
 (0)