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

more robust way of determining where a chunk belongs #64

Merged
merged 1 commit into from
Oct 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-humans-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@navita/vite-plugin': patch
---

Use options to determine if chunk belongs in server or client build
1 change: 1 addition & 0 deletions examples/with-remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev": "remix vite:dev --force",
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
"start": "remix-serve ./build/server/index.js",
"preview": "remix vite:build && remix-serve ./build/server/index.js",
"typecheck": "tsc"
},
"dependencies": {
Expand Down
15 changes: 10 additions & 5 deletions packages/vite-plugin/src/remix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let cssFileName: string;

export function navitaRemix(options?: Options): Plugin[] {
let isProduction = false;
let hasEmittedCss = false;

const { renderChunk, ...navitaVite } = navita(options);

Expand All @@ -25,8 +26,11 @@ export function navitaRemix(options?: Options): Plugin[] {

return `${code}\n${remixServerBuildExtension}`;
},
renderChunk(_, chunk) {
if (chunk.name === "root") {
renderChunk(_, chunk, options) {
const isServerChunk = options.dir.endsWith('/server');
const isClientChunk = options.dir.endsWith('/client');

if (isClientChunk && chunk.name === "root") {
// Generate a random name for the CSS file.
// Vite uses a file hash as the name, but since the client build will finish before
// the server build, we need to generate a random name for the CSS file.
Expand All @@ -39,12 +43,11 @@ export function navitaRemix(options?: Options): Plugin[] {

cssFileName = `assets/navita-${random}.css`;

// Attach the file to the root chunk so that it's included in the client build.
chunk.viteMetadata?.importedCss.add(cssFileName);
chunk.viteMetadata.importedCss.add(cssFileName);
return;
}

if (chunk.name === 'server-build') {
if (isServerChunk && !hasEmittedCss) {
// In the server-build, we'll generate the CSS and emit it as an asset.
// Remix will then move it to the client assets.
this.emitFile({
Expand All @@ -53,6 +56,8 @@ export function navitaRemix(options?: Options): Plugin[] {
type: 'asset',
source: getRenderer()?.engine.renderCssToString(),
});

hasEmittedCss = true;
}
},
}
Expand Down
Loading