Skip to content
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

test(remix-dev/vite): add Vanilla Extract to CSS tests #8000

Merged

Conversation

hi-ogawa
Copy link
Contributor

Follow up of #7990

  • Tests

Copy link

changeset-bot bot commented Nov 14, 2023

⚠️ No Changeset found

Latest commit: ae5f63a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Comment on lines +24 to +31
// set sideEffects for global vanilla extract css
let packageJson = JSON.parse(
await fs.readFile(
path.resolve(__dirname, "helpers", "node-template", "package.json"),
"utf-8"
)
);
packageJson.sideEffects = ["*.css.ts"];
Copy link
Contributor Author

@hi-ogawa hi-ogawa Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that currently remix's template has sideEffects: false by default:

"sideEffects": false,

So, vanilla extract global style won't work out-of-the-box.
However, I think this is a general behavior of vite build and not specific to remix, so probably any caveat should be mentioned on their documentation side, such as https://vanilla-extract.style/documentation/integrations/vite/

@@ -60,7 +60,7 @@ test("builds deterministically under different paths", async () => {
},
}),

"postcss.config.js": js`
"postcss.config.mjs": js`
Copy link
Contributor Author

@hi-ogawa hi-ogawa Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(EDIT: this diff shows postcss.config.mjs, but I put it back to js. Also I'm not sure postcss is even actually loading .mjs file...)

Something puzzling is happening in this seemingly unrelated test.
After installing @vanilla-extract/vite-plugin in integration/package.json, this test fails (not only on CI but it also fails locally on my PC).

✘ [ERROR] require() of ES Module /home/runner/work/remix/remix/.tmp/integration/remix-node-template-s04nobi5qlo/postcss.config.js from /home/runner/work/remix/remix/node_modules/lilconfig/dist/index.js not supported.

My hunch is that, since @vanilla-extract/vite-plugin has dependency on postcss-load-config, the existence of this in the visible node_modules might be affecting some postcss's internal config resolution. But postcss-load-config v3 seems to have issue with esm postcss/postcss-cli#387 (comment).

Copy link
Contributor Author

@hi-ogawa hi-ogawa Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it back to postcss.config.js and it seems adding postcss-load-config v4 to root package.json fixes this issue 4222ed9 though I'm not sure if I'm doing the right thing...
Please let me know if it should be handled in a different way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find. I opened a PR to fix this at the source vanilla-extract-css/vanilla-extract#1231

Copy link
Contributor Author

@hi-ogawa hi-ogawa Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I don't have much experience with postcss, so I was quite at lost.
I would be happy to test if the fix is released. Please feel free to let me know!

EDIT: I verified it works now without fiddling with postcss-load-config 08bb2ed. Thanks for the fix!

@markdalgleish markdalgleish changed the title test(remix-dev/vite): add vanilla extract css test test(remix-dev/vite): add Vanilla Extract to CSS tests Nov 20, 2023
plugins: [remix()],
plugins: [
vanillaExtractPlugin({
emitCssInSsr: true,
Copy link
Member

@markdalgleish markdalgleish Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It surprised me that this is needed. After digging into it a bit, it makes sense that it's needed for our current approach to inlining CSS in dev though, however it's not clear to me why this would be needed for the production build.

I'm happy for this PR to go through as-is for now since it's still capturing the current state and proving that it works. It'd be a good idea to follow this up separately though.

Copy link
Contributor Author

@hi-ogawa hi-ogawa Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out.
My intuition was also emitCssInSsr should not be necessarily for production build, but I didn't investigate this in detail (actually I haven't even tested turning this off yet...)

On vanilla extract plugin side, I saw it's setting some default behavior for known ssr frameworks, so maybe similar thing could be done for remix on their side.
I'll follow up and will see if there's any appropriate change on either side.

https://github.com/vanilla-extract-css/vanilla-extract/blob/5c9216ca47769d8b042fe46f9662da6a3dc665b1/packages/vite-plugin/src/index.ts#L76-L88

        config.plugins.some((plugin) =>
          [
            'astro:build',
            'solid-start-server',
            'vite-plugin-qwik',
            'vite-plugin-svelte',
          ].includes(plugin.name),
        )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards enabling emitCssInSsr by default in VE. It feels closer to the way CSS typically works in Vite where it's part of both the server and client graphs. The fact that CSS is missing in the server graph ends up breaking other tools and integrations because this is unexpected. I'm chatting with the rest of the VE team about it.

Adding Remix to this list would definitely be an easy non-breaking fix though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking in with VE!

I also experimented with toggling emitCssInSsr and it seems my intuition was wrong about this.
Probably you know this better, but let me share what I found.

When emitCssInSsr: false, the server build vite build --ssr will preserve vanilla javascript runtime code like below:

// in build/index.js

// originally from app/styles-vanilla-local.css.ts
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
import { style } from "@vanilla-extract/css";

setFileScope("app/styles-vanilla-local.css.ts", "remix-template-remix");
const index = style({
  background: "lightblue",
  padding: "20px"
});
endFileScope();

// originally from app/routes/_index.tsx
jsx("div", { "data-css-vanilla-local": true, className: index ... })

I was thinking emitCssInSsr: false means that it still compiles styles-vanilla-local.css.ts into hashed class name string (in js) and only skips emitting css code, but that doesn't seem to be the case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair misunderstanding. If we revisit the options for the plugin, your comment is worth considering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests have been updated to use the latest Vanilla Extract Vite plugin where emitCssInSsr is no longer needed: #8063.

@markdalgleish markdalgleish merged commit 1ee1f21 into remix-run:dev Nov 20, 2023
5 checks passed
@hi-ogawa hi-ogawa deleted the test-vite-vanilla-extract-plugin branch November 20, 2023 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants