Skip to content

Commit b4591d5

Browse files
authored
Fix hash context information not being applied (#30)
1 parent 5bf3b62 commit b4591d5

File tree

7 files changed

+121
-47
lines changed

7 files changed

+121
-47
lines changed

.changeset/chilled-cars-smell.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@vanilla-extract/babel-plugin': patch
3+
'@vanilla-extract/esbuild-plugin': patch
4+
---
5+
6+
Fix hash context information not being applied
7+
8+
This change makes it so all files with a valid CSS file extension (e.g. `.css.ts`) get hash context information (internally referred to as `filescope`) applied. This fixes some situations where the "New styles cannot be registered dynamically after initial boot" error would occur incorrectly.

.changeset/warm-trains-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/babel-plugin': patch
3+
---
4+
5+
Ignore compiling files that have already been compiled

packages/babel-plugin/src/index.test.ts

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('babel plugin', () => {
3232
`;
3333

3434
expect(transform(source)).toMatchInlineSnapshot(`
35-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
35+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
3636
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
3737
import { style } from '@vanilla-extract/css';
3838
const one = style({
@@ -52,7 +52,7 @@ describe('babel plugin', () => {
5252
`;
5353

5454
expect(transform(source)).toMatchInlineSnapshot(`
55-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
55+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
5656
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
5757
import { mapToStyles } from '@vanilla-extract/css';
5858
const colors = mapToStyles({
@@ -74,7 +74,7 @@ describe('babel plugin', () => {
7474
`;
7575

7676
expect(transform(source)).toMatchInlineSnapshot(`
77-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
77+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
7878
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
7979
import { mapToStyles } from '@vanilla-extract/css';
8080
const colors = mapToStyles({
@@ -96,7 +96,7 @@ describe('babel plugin', () => {
9696
`;
9797

9898
expect(transform(source)).toMatchInlineSnapshot(`
99-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
99+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
100100
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
101101
import { style } from '@vanilla-extract/css';
102102
export default style({
@@ -120,7 +120,7 @@ describe('babel plugin', () => {
120120
`;
121121

122122
expect(transform(source)).toMatchInlineSnapshot(`
123-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
123+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
124124
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
125125
import { style } from '@vanilla-extract/css';
126126
const test = {
@@ -146,7 +146,7 @@ describe('babel plugin', () => {
146146
`;
147147

148148
expect(transform(source)).toMatchInlineSnapshot(`
149-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
149+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
150150
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
151151
import { style } from '@vanilla-extract/css';
152152
@@ -170,7 +170,7 @@ describe('babel plugin', () => {
170170
`;
171171

172172
expect(transform(source)).toMatchInlineSnapshot(`
173-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
173+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
174174
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
175175
import { style } from '@vanilla-extract/css';
176176
@@ -194,7 +194,7 @@ describe('babel plugin', () => {
194194
`;
195195

196196
expect(transform(source)).toMatchInlineSnapshot(`
197-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
197+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
198198
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
199199
import { style } from '@vanilla-extract/css';
200200
@@ -216,7 +216,7 @@ describe('babel plugin', () => {
216216
`;
217217

218218
expect(transform(source)).toMatchInlineSnapshot(`
219-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
219+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
220220
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
221221
import { globalStyle } from '@vanilla-extract/css';
222222
globalStyle('html, body', {
@@ -234,7 +234,7 @@ describe('babel plugin', () => {
234234
`;
235235

236236
expect(transform(source)).toMatchInlineSnapshot(`
237-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
237+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
238238
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
239239
import { createVar } from '@vanilla-extract/css';
240240
const myVar = createVar(\\"myVar\\");
@@ -252,7 +252,7 @@ describe('babel plugin', () => {
252252
`;
253253

254254
expect(transform(source)).toMatchInlineSnapshot(`
255-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
255+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
256256
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
257257
import { fontFace } from '@vanilla-extract/css';
258258
const myFont = fontFace({
@@ -272,7 +272,7 @@ describe('babel plugin', () => {
272272
`;
273273

274274
expect(transform(source)).toMatchInlineSnapshot(`
275-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
275+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
276276
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
277277
import { globalFontFace } from '@vanilla-extract/css';
278278
globalFontFace('myFont', {
@@ -293,7 +293,7 @@ describe('babel plugin', () => {
293293
`;
294294

295295
expect(transform(source)).toMatchInlineSnapshot(`
296-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
296+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
297297
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
298298
import { keyframes } from '@vanilla-extract/css';
299299
const myAnimation = keyframes({
@@ -319,7 +319,7 @@ describe('babel plugin', () => {
319319
`;
320320

321321
expect(transform(source)).toMatchInlineSnapshot(`
322-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
322+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
323323
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
324324
import { globalKeyframes } from '@vanilla-extract/css';
325325
globalKeyframes('myKeyframes', {
@@ -342,7 +342,7 @@ describe('babel plugin', () => {
342342
`;
343343

344344
expect(transform(source)).toMatchInlineSnapshot(`
345-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
345+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
346346
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
347347
import { createTheme } from '@vanilla-extract/css';
348348
const darkTheme = createTheme({}, {}, \\"darkTheme\\");
@@ -358,7 +358,7 @@ describe('babel plugin', () => {
358358
`;
359359

360360
expect(transform(source)).toMatchInlineSnapshot(`
361-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
361+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
362362
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
363363
import { createTheme } from '@vanilla-extract/css';
364364
const [theme, vars] = createTheme({}, {}, \\"theme\\");
@@ -374,7 +374,7 @@ describe('babel plugin', () => {
374374
`;
375375

376376
expect(transform(source)).toMatchInlineSnapshot(`
377-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
377+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
378378
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
379379
import { createGlobalTheme } from '@vanilla-extract/css';
380380
const themeVars = createGlobalTheme(':root', {
@@ -394,7 +394,7 @@ describe('babel plugin', () => {
394394
`;
395395

396396
expect(transform(source)).toMatchInlineSnapshot(`
397-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
397+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
398398
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
399399
import { createThemeVars } from '@vanilla-extract/css';
400400
const themeVars = createThemeVars({
@@ -420,7 +420,7 @@ describe('babel plugin', () => {
420420
`;
421421

422422
expect(transform(source)).toMatchInlineSnapshot(`
423-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
423+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
424424
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
425425
import { style, mapToStyles } from '@vanilla-extract/css';
426426
const three = style({
@@ -437,7 +437,7 @@ describe('babel plugin', () => {
437437
`);
438438
});
439439

440-
it('should only apply to functions imported from the relevant package', () => {
440+
it('should only apply debug ids to functions imported from the relevant package', () => {
441441
const source = `
442442
import { style } from 'some-other-package';
443443
@@ -447,13 +447,57 @@ describe('babel plugin', () => {
447447
`;
448448

449449
expect(transform(source)).toMatchInlineSnapshot(`
450-
"import { style } from 'some-other-package';
450+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
451+
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
452+
import { style } from 'some-other-package';
453+
const three = style({
454+
zIndex: 2
455+
});
456+
endFileScope()"
457+
`);
458+
});
459+
460+
it('should only apply to .css.ts files', () => {
461+
const source = `
462+
import { style } from '@vanilla-extract/css';
463+
464+
const three = style({
465+
zIndex: 2,
466+
});
467+
`;
468+
469+
expect(transform(source, {}, './dir/mockFilename.ts'))
470+
.toMatchInlineSnapshot(`
471+
"import { style } from '@vanilla-extract/css';
451472
const three = style({
452473
zIndex: 2
453474
});"
454475
`);
455476
});
456477

478+
it('should ignore files that already have filescope information', () => {
479+
const source = `
480+
import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
481+
setFileScope('src/dir/someFileName.css.ts', 'some-package');
482+
import { style } from '@vanilla-extract/css';
483+
484+
const three = style({
485+
zIndex: 2,
486+
});
487+
endFileScope();
488+
`;
489+
490+
expect(transform(source)).toMatchInlineSnapshot(`
491+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
492+
setFileScope('src/dir/someFileName.css.ts', 'some-package');
493+
import { style } from '@vanilla-extract/css';
494+
const three = style({
495+
zIndex: 2
496+
});
497+
endFileScope();"
498+
`);
499+
});
500+
457501
it('should handle renaming imports', () => {
458502
const source = `
459503
import { style as specialStyle } from '@vanilla-extract/css';
@@ -464,7 +508,7 @@ describe('babel plugin', () => {
464508
`;
465509

466510
expect(transform(source)).toMatchInlineSnapshot(`
467-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
511+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
468512
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
469513
import { style as specialStyle } from '@vanilla-extract/css';
470514
const four = specialStyle({
@@ -486,7 +530,7 @@ describe('babel plugin', () => {
486530
`;
487531

488532
expect(transform(source)).toMatchInlineSnapshot(`
489-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
533+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
490534
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
491535
import { style } from '@vanilla-extract/css';
492536
export const height = [style({
@@ -508,7 +552,7 @@ describe('babel plugin', () => {
508552
`;
509553

510554
expect(transform(source)).toMatchInlineSnapshot(`
511-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
555+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
512556
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
513557
import { style } from '@vanilla-extract/css';
514558
export const height = {
@@ -530,7 +574,7 @@ describe('babel plugin', () => {
530574
`;
531575

532576
expect(transform(source)).toMatchInlineSnapshot(`
533-
"import { setFileScope, endFileScope } from \\"@vanilla-extract/css/fileScope\\";
577+
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
534578
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
535579
import * as css from '@vanilla-extract/css';
536580
const one = css.style({

packages/babel-plugin/src/index.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import template from '@babel/template';
44
import findUp from 'find-up';
55

66
const packageIdentifier = '@vanilla-extract/css';
7+
const filescopePackageIdentifier = '@vanilla-extract/css/fileScope';
78

89
const buildSetFileScope = template(`
9-
import { setFileScope, endFileScope } from %%packageIdentifier%%
10+
import { setFileScope, endFileScope } from '${filescopePackageIdentifier}'
1011
setFileScope(%%filePath%%, %%packageName%%)
1112
`);
1213

@@ -122,6 +123,8 @@ type Context = PluginPass & {
122123
packageIdentifier: string;
123124
filePath: string;
124125
packageName: string;
126+
isCssFile: boolean;
127+
alreadyCompiled: boolean;
125128
};
126129

127130
export default function (): PluginObj<Context> {
@@ -150,14 +153,17 @@ export default function (): PluginObj<Context> {
150153

151154
return {
152155
pre({ opts }) {
153-
this.importIdentifiers = new Map();
154-
this.namespaceImport = '';
155-
156156
if (!opts.filename) {
157157
// TODO Make error better
158158
throw new Error('Filename must be available');
159159
}
160160

161+
this.isCssFile = /\.css\.(js|ts|jsx|tsx)$/.test(opts.filename);
162+
this.alreadyCompiled = false;
163+
164+
this.importIdentifiers = new Map();
165+
this.namespaceImport = '';
166+
161167
const packageInfo = getPackageInfo(opts.cwd);
162168

163169
if (!packageInfo) {
@@ -177,14 +183,11 @@ export default function (): PluginObj<Context> {
177183
visitor: {
178184
Program: {
179185
exit(path) {
180-
if (this.importIdentifiers.size > 0 || this.namespaceImport) {
186+
if (this.isCssFile && !this.alreadyCompiled) {
181187
// Wrap module with file scope calls
182188
path.unshiftContainer(
183189
'body',
184190
buildSetFileScope({
185-
packageIdentifier: t.stringLiteral(
186-
`${packageIdentifier}/fileScope`,
187-
),
188191
filePath: t.stringLiteral(this.filePath),
189192
packageName: t.stringLiteral(this.packageName),
190193
}),
@@ -198,7 +201,16 @@ export default function (): PluginObj<Context> {
198201
},
199202
},
200203
ImportDeclaration(path) {
201-
if (path.node.source.value === packageIdentifier) {
204+
if (!this.isCssFile || this.alreadyCompiled) {
205+
// Bail early if file isn't a .css.ts file or the file has already been compiled
206+
return path.stop();
207+
}
208+
209+
if (path.node.source.value === filescopePackageIdentifier) {
210+
// If file scope import is found it means the file has already been compiled
211+
this.alreadyCompiled = true;
212+
return path.stop();
213+
} else if (path.node.source.value === packageIdentifier) {
202214
path.node.specifiers.forEach((specifier) => {
203215
if (t.isImportNamespaceSpecifier(specifier)) {
204216
this.namespaceImport = specifier.local.name;
@@ -217,6 +229,11 @@ export default function (): PluginObj<Context> {
217229
}
218230
},
219231
CallExpression(path) {
232+
if (!this.isCssFile || this.alreadyCompiled) {
233+
// Bail early if file isn't a .css.ts file or the file has already been compiled
234+
return path.stop();
235+
}
236+
220237
const { node } = path;
221238

222239
const usedExport = getRelevantCall(

0 commit comments

Comments
 (0)