vanilla-extract: Imports inside .css.ts files produce an error (Next Js)
Describe the bug
At least on Next 13, some imports inside .css.ts files produce the following error:
NonErrorEmittedError: (Emitted value instead of an instance of Error) ReferenceError: $RefreshReg$ is not defined
This is triggered when the file containing the thing you import IS or EXPORTS a .tsx file that exports a function which has a name in PascalCase.
Codesandbox instructions: go to the styles/test.css.ts file and comment / uncomment the imports in order.
Some examples available in the codesandbox:
// problem.tsx
export const PascalCaseFunction = () => {
console.log(":/");
};
// test.css.ts
import { PascalCaseFunction } from "./problem"
PascalCaseFunction() // error
// works.tsx
export const camelCaseFunction = () => {
console.log("WORKS");
};
// test.css.ts
import { camelCaseFunction } from "./works"
camelCaseFunction () // works
// multiple_exports.tsx
export * from "./problem";
export * from "./works";
// test.css.ts
import { camelCaseFunction } from "./multiple_exports"
camelCaseFunction () // error (because multiple_exports also exports the PascalCaseFunction)
Not lying, I had a pretty hard testing / debugging time on this one, that’s not obvious at all :p
Reproduction
https://codesandbox.io/p/sandbox/agitated-ride-g44g7r
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
CPU: (8) x64 12th Gen Intel(R) Core(TM) i7-12700K
Memory: 8.07 GB / 15.62 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 16.19.1 - /usr/local/bin/node
Yarn: 3.3.1 - /usr/local/bin/yarn
npm: 8.19.3 - /usr/local/bin/npm
Used Package Manager
npm
Logs
(in the terminal of course)
NonErrorEmittedError: (Emitted value instead of an instance of Error) ReferenceError: $RefreshReg$ is not defined
Import trace for requested module:
./styles/test.css.ts
### Validations
- [X] Check that there isn't [already an issue](https://github.com/vanilla-extract-css/vanilla-extract/issues) that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 15
- Comments: 21 (5 by maintainers)
This error also occurred to me in a monorepo. I was using barrel files to combine exports for multiple
.css.tsfiles.Importing directly from
.css.tshelpedUnfortunately I don’t have a sample repo available, I just have the codesandbox provided in the issue, but that should do it.
The solution (or should I say “workaround”) was to ensure the file you are importing into the
.css.tscomes from a file that doesn’t export anything that IS or MIGHT BE MISTAKEN FOR a React component.Take a look at the sandbox, you have multiple examples of working / not working cases.
The main idea is to not import anything that (is a function|is exported alongside other elements that are functions) starting with a capital letter.
I’ve just spent hours upgrading a repo from v6 to v7 replacing
createStyleswith vanilla extract and now come across this issue. Next.js 13 pages dir app. Followed the Mantine upgrade docs + vanilla extract docs. No idea how to resolve it.any solution yet?
I’ve come up with patching
@vanilla-extract/integrationto mitigate the$Refresh$issue by removing the problematic lines from sourceHey So all it export is something like this
@shelooks16 thanks for that, works like a charm
@toyi Thanks a lot, I think your investigation saved me from a ton of lost time doing it myself. I have the exact same scenario with Next13. Monorepo where one package is “ui” library implemented with vanilla extract and all of the react components and helper functions are exported via one index file. Then in my <component>.css.ts file in Next13 app inside the monorepo I was importing a helper function (which is just a normal function sitting in a .ts file in ui lib) like
import {helperFunctionName} from 'ui'and I was getting this weird error. Changing the import to the exact path likeimport {helperFunctionName} from 'ui/styles/helperFunctionFile'fixed the error -_-