Skip to content
Open
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
9 changes: 7 additions & 2 deletions packages/@stylexjs/unplugin/__tests__/unplugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('@stylexjs/unplugin', () => {
},
};

plugin.generateBundle.call(ctx, {}, bundle);
plugin.generateBundle.handler.call(ctx, {}, bundle);

// generateBundle should have injected CSS into the bundle
const cssAssets = Object.values(bundle).filter(
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('@stylexjs/unplugin', () => {
return null;
},
};
plugin.generateBundle.call(ctx, {}, {});
plugin.generateBundle.handler.call(ctx, {}, {});

// writeBundle should still create fallback CSS
const tempDir = fs.mkdtempSync(
Expand All @@ -203,6 +203,11 @@ describe('@stylexjs/unplugin', () => {
}
});

test('runs generateBundle after Vite emits the CSS asset', () => {
const plugin = unplugin.vite({ dev: false });
expect(plugin.generateBundle.order).toBe('post');
});

test('marks StyleX deps as non-optimized in Vite', async () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'stylex-unplugin-vite-'),
Expand Down
29 changes: 16 additions & 13 deletions packages/@stylexjs/unplugin/src/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,22 @@ function attachViteHooks(plugin) {
});
} catch {}
},
generateBundle(_opts, bundle) {
const css = plugin.__stylexCollectCss?.();
if (!css) return;
const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
if (target) {
const current =
typeof target.source === 'string'
? target.source
: target.source?.toString() || '';
const nextSource = current ? current + '\n' + css : css;
replaceCssAssetWithHashedCopy(this, bundle, target, nextSource);
cssInjectedInGenerateBundle = true;
}
generateBundle: {
order: 'post',
handler(_opts, bundle) {
const css = plugin.__stylexCollectCss?.();
if (!css) return;
const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
if (target) {
const current =
typeof target.source === 'string'
? target.source
: target.source?.toString() || '';
const nextSource = current ? current + '\n' + css : css;
replaceCssAssetWithHashedCopy(this, bundle, target, nextSource);
cssInjectedInGenerateBundle = true;
}
},
},
async writeBundle(options, bundle) {
if (cssInjectedInGenerateBundle) return;
Expand Down