Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,21 @@ export const pluginTailwindCSS = (
path: resourcePath,
});

return `\
import "${pathToFileURL(VIRTUAL_UTILITIES_ID)}?${params.toString()}";
${code}`;
const importStr = `import "${pathToFileURL(VIRTUAL_UTILITIES_ID)}?${params.toString()}";\n`;

const match = code.match(
/^(?:[\s]*|(?:\/\*[\s\S]*?\*\/)|(?:\/\/[^\n]*\n))*(?:(?:"[^"]*"|'[^']*')[ \t]*;?[\s]*)+/,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Handle inline comments after semicolonless directives

The new directive-matching regex does not include trailing inline comments, so input like 'use client' // note matches only 'use client' and injects the import before the comment, producing invalid code such as 'use client' import "...";. This is a build-breaking regression for valid JS/TS files that use semicolonless directive lines with same-line // or /* */ comments.

Useful? React with 👍 / 👎.

);

if (match) {
return (
code.slice(0, match[0].length) +
importStr +
code.slice(match[0].length)
);
}

return importStr + code;
},
);

Expand Down
2 changes: 2 additions & 0 deletions test/rsc-test-react/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use client';
console.log('hello');
33 changes: 33 additions & 0 deletions test/rsc-test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, test } from '@playwright/test';
import { createRsbuild } from '@rsbuild/core';
import { pluginTailwindCSS } from '../../src';

const __dirname = dirname(fileURLToPath(import.meta.url));

test('should preserve "use client" directive at the top of the file', async () => {
const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
plugins: [pluginTailwindCSS()],
output: {
minify: false,
},
},
});

await rsbuild.build();

const distPath = resolve(__dirname, './dist/static/js');
const files = fs.readdirSync(distPath);
const mainFile = files.find((f) => f.endsWith('.js'));
if (!mainFile) {
throw new Error('No JS file found');
}
const content = fs.readFileSync(resolve(distPath, mainFile), 'utf-8');

// "use client" should be present in the output
expect(content).toMatch(/['"]use client['"]/);
});
2 changes: 2 additions & 0 deletions test/rsc-test/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use client';
console.log('hello');
Loading