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

Add devStyleRuntime option #373

Merged
merged 2 commits into from Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fluffy-kings-watch.md
@@ -0,0 +1,7 @@
---
'@vanilla-extract/vite-plugin': minor
---

Add `devStyleRuntime` option to allow switching between dev style runtimes

The built-in Vite dev style runtime (what adds styles to the page when running `vite serve`) doesn't seem to clean up old styles as expected. Passing `devStyleRuntime: 'vanilla-extract'` will instead use vanilla-extract's browser runtime. This pushes all style creation in development to the browser, but may give a better HMR experience.
16 changes: 14 additions & 2 deletions packages/vite-plugin/src/index.ts
Expand Up @@ -15,17 +15,29 @@ import {

interface Options {
identifiers?: IdentifierOption;

/**
* Which CSS runtime to use when running `vite serve`.
* @default 'vite'
*/
devStyleRuntime?: 'vite' | 'vanilla-extract';
}
export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
export function vanillaExtractPlugin({
identifiers,
devStyleRuntime = 'vite',
}: Options = {}): Plugin {
let config: ResolvedConfig;
let packageInfo: ReturnType<typeof getPackageInfo>;
let useRuntime = false;
const cssMap = new Map<string, string>();

return {
name: 'vanilla-extract',
enforce: 'pre',
configResolved(resolvedConfig) {
config = resolvedConfig;
useRuntime =
devStyleRuntime === 'vanilla-extract' && config.command === 'serve';

packageInfo = getPackageInfo(config.root);
},
Expand Down Expand Up @@ -59,7 +71,7 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
return null;
}

if (ssr) {
if (ssr || useRuntime) {
return addFileScope({
source: code,
filePath: normalizePath(path.relative(packageInfo.dirname, id)),
Expand Down
2 changes: 1 addition & 1 deletion test-helpers/src/startFixture/vite.ts
Expand Up @@ -42,7 +42,7 @@ export const startViteFixture = async (
configFile: false,
root,
logLevel: 'error',
plugins: [vanillaExtractPlugin()],
plugins: [vanillaExtractPlugin({ devStyleRuntime: 'vanilla-extract' })],
server: {
port,
},
Expand Down