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

feat: rollup plugin support generate file with pattern #815

Merged
merged 2 commits into from
Dec 23, 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
74 changes: 73 additions & 1 deletion packages/rollup-plugin/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('rollup-plugin-stylex', () => {
}
}

return { css, js };
return { css, js, output };
}

it('extracts CSS and removes stylex.inject calls', async () => {
Expand Down Expand Up @@ -322,4 +322,76 @@ describe('rollup-plugin-stylex', () => {
`);
});
});
it('output filename match pattern', async () => {
const { output } = await runStylex({ fileName: 'stylex.[hash].css' });
const css = output.find(
(chunkOrAsset) =>
chunkOrAsset.type === 'asset' &&
/^stylex.[0-9a-f]{8}\.css$/.test(chunkOrAsset.fileName),
);
expect(css.source).toMatchInlineSnapshot(`
"@layer priority1 {
@keyframes xgnty7z-B {
0% {
opacity: .25;
}

100% {
opacity: 1;
}
}
}

@layer priority2 {
.x1oz5o6v:hover {
background: red;
}
}

@layer priority3 {
.xeuoslp {
animation-name: xgnty7z-B;
}

.xu4yf9m {
border-start-start-radius: 7.5px;
}

.x1lliihq {
display: block;
}

.x78zum5 {
display: flex;
}

.xt0psk2 {
display: inline;
}

.x1hm9lzh {
margin-inline-start: 10px;
}
}

@layer priority4 {
.x1egiwwb {
height: 500px;
}

.xlrshdv {
margin-top: 99px;
}

.xh8yej3 {
width: 100%;
}

.x3hqpx7 {
width: 50%;
}
}
"
`);
});
});
17 changes: 15 additions & 2 deletions packages/rollup-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
} from 'rollup';
import browserslist from 'browserslist';
import { browserslistToTargets } from 'lightningcss';
import crypto from 'crypto';

const IS_DEV_ENV =
process.env.NODE_ENV === 'development' ||
Expand All @@ -44,6 +45,18 @@ export type PluginOptions = $ReadOnly<{
...
}>;

function replaceFileName(original: string, css: string) {
if (!original.includes('[hash]')) {
return original;
}
const hash = crypto
.createHash('sha256')
.update(css)
.digest('hex')
.slice(0, 8);
return original.replace(/\[hash\]/g, hash);
}

export default function stylexPlugin({
dev = IS_DEV_ENV,
unstable_moduleResolution = { type: 'commonJS', rootDir: process.cwd() },
Expand Down Expand Up @@ -72,7 +85,7 @@ export default function stylexPlugin({
const { code } = transform({
targets: browserslistToTargets(browserslist('>= 1%')),
...lightningcssOptions,
filename: fileName,
filename: 'stylex.css',
code: Buffer.from(collectedCSS),
});

Expand All @@ -82,7 +95,7 @@ export default function stylexPlugin({
// This is the intended API, but Flow doesn't support this pattern.
// $FlowExpectedError[object-this-reference]
this.emitFile({
fileName,
fileName: replaceFileName(fileName, processedCSS),
source: processedCSS,
type: 'asset',
});
Expand Down
Loading