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
40 changes: 40 additions & 0 deletions packages/@stylexjs/unplugin/__tests__/unplugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ describe('@stylexjs/unplugin', () => {
expect(result).toBeNull();
});

test('includes .astro files', () => {
const plugin = unplugin.raw({});
expect(plugin.transformInclude('/virtual/page.astro')).toBe(true);
expect(
plugin.transformInclude('/virtual/page.astro?astro&type=script'),
).toBe(true);
});

test('compiles StyleX in .astro modules', async () => {
const plugin = unplugin.rollup({
runtimeInjection: false,
devPersistToDisk: false,
dev: false,
});
if (typeof plugin.buildStart === 'function') {
plugin.buildStart();
}
// Astro hands bundler plugins the compiled module, not the `---` source
const source = `
import * as stylex from '@stylexjs/stylex';
const styles = stylex.create({ hero: { color: 'red' } });
export default styles;
`;
const result = await plugin.transform(source, '/virtual/page.astro');
expect(result).not.toBeNull();
expect(result.code).not.toContain('stylex.create');

const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'stylex-unplugin-test-'),
);
try {
await plugin.writeBundle({ dir: tempDir }, {});
const cssPath = path.join(tempDir, 'assets', 'stylex.css');
expect(fs.existsSync(cssPath)).toBe(true);
expect(fs.readFileSync(cssPath, 'utf8')).toContain('color: red;');
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});

test('still transforms built-in imports when a custom importSource is set', async () => {
const plugin = unplugin.raw({ importSources: ['custom-stylex'] });
if (typeof plugin.buildStart === 'function') {
Expand Down
11 changes: 9 additions & 2 deletions packages/@stylexjs/unplugin/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ function discoverStylexPackages({

const JS_LIKE_RE = /\.[cm]?[jt]sx?(\?|$)/;
const SVELTE_LIKE_RE = /\.svelte(\?|$)/;
const ASTRO_LIKE_RE = /\.astro(\?|$)/;

function isTransformableId(id) {
return (
JS_LIKE_RE.test(id) || SVELTE_LIKE_RE.test(id) || ASTRO_LIKE_RE.test(id)
);
}

export const unpluginFactory = (userOptions = {}, metaOptions) => {
// framework :: 'rollup' | 'vite' | 'rolldown' | 'farm' | 'unloader'
Expand Down Expand Up @@ -423,13 +430,13 @@ export const unpluginFactory = (userOptions = {}, metaOptions) => {
},

transformInclude(id) {
return JS_LIKE_RE.test(id) || SVELTE_LIKE_RE.test(id);
return isTransformableId(id);
},

// Core code transform
async transform(code, id) {
// Only handle JS-like files; avoid parsing CSS/JSON/etc
if (!JS_LIKE_RE.test(id) && !SVELTE_LIKE_RE.test(id)) return null;
if (!isTransformableId(id)) return null;
if (!shouldHandle(code)) return null;

// Extract the pure filename by removing everything after '?' (e.g., handling Vite's '?v=' cache busting).
Expand Down