Skip to content

Commit fbc2c31

Browse files
authored
rollup: Add unstable_injectFilescopes flag (#1654)
1 parent 11a7c66 commit fbc2c31

File tree

6 files changed

+678
-9
lines changed

6 files changed

+678
-9
lines changed

.changeset/quiet-islands-shake.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@vanilla-extract/rollup-plugin': minor
3+
---
4+
5+
Add optional `unstable_injectFilescopes` flag
6+
7+
The `unstable_injectFilescopes` flag injects filescopes into Vanilla Extract modules instead of generating CSS. This is useful for utility or component libraries that prefer their consumers to process Vanilla Extract files instead of bundling CSS.
8+
9+
Note that this flag only works with `preserveModules: true`.

packages/rollup-plugin/src/index.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import {
66
type IdentifierOption,
77
getSourceFromVirtualCssFile,
88
virtualCssFileFilter,
9+
transform,
910
type CompileOptions,
1011
} from '@vanilla-extract/integration';
1112
import { posix } from 'path';
12-
import { generateCssBundle, stripSideEffectImportsMatching } from './lib';
13+
import {
14+
generateCssBundle,
15+
stripSideEffectImportsMatching,
16+
tryGetPackageName,
17+
} from './lib';
1318

1419
const { relative, normalize, dirname } = posix;
1520

@@ -51,13 +56,25 @@ export interface Options {
5156
sourcemap?: boolean;
5257
}
5358
| false;
59+
60+
/**
61+
* Inject filescopes into Vanilla Extract modules instead of generating CSS.
62+
* Useful for utility or component libraries that prefer their consumers to
63+
* process Vanilla Extract files instead of bundling CSS.
64+
*
65+
* Only works with `preserveModules: true`.
66+
*
67+
* @default false
68+
*/
69+
unstable_injectFilescopes?: boolean;
5470
}
5571

5672
export function vanillaExtractPlugin({
5773
identifiers,
5874
cwd = process.cwd(),
5975
esbuildOptions,
6076
extract = false,
77+
unstable_injectFilescopes = false,
6178
}: Options = {}): Plugin {
6279
const isProduction = process.env.NODE_ENV === 'production';
6380

@@ -71,14 +88,29 @@ export function vanillaExtractPlugin({
7188
},
7289

7390
// Transform .css.js to .js
74-
async transform(_code, id) {
91+
async transform(code, id) {
7592
if (!cssFileFilter.test(id)) {
7693
return null;
7794
}
7895

96+
const identOption = identifiers ?? (isProduction ? 'short' : 'debug');
7997
const [filePath] = id.split('?');
8098

81-
const identOption = identifiers ?? (isProduction ? 'short' : 'debug');
99+
if (unstable_injectFilescopes) {
100+
const packageName = await tryGetPackageName(cwd);
101+
const transformedCode = await transform({
102+
source: code,
103+
filePath: id,
104+
rootPath: cwd,
105+
packageName: packageName ?? '',
106+
identOption,
107+
});
108+
109+
return {
110+
code: transformedCode,
111+
map: { mappings: '' },
112+
};
113+
}
82114

83115
const { source, watchFiles } = await compile({
84116
filePath,

packages/rollup-plugin/src/lib.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cssFileFilter } from '@vanilla-extract/integration';
22
import MagicString, { Bundle as MagicStringBundle } from 'magic-string';
33
import type { ModuleInfo, PluginContext } from 'rollup';
4+
import { posix } from 'path';
45

56
/** Generate a CSS bundle from Rollup context */
67
export function generateCssBundle(
@@ -121,3 +122,13 @@ export function stripSideEffectImportsMatching(
121122
}
122123
return output;
123124
}
125+
126+
export async function tryGetPackageName(cwd: string): Promise<string | null> {
127+
try {
128+
const packageJson = require(posix.join(cwd, 'package.json'));
129+
130+
return packageJson?.name || null;
131+
} catch {
132+
return null;
133+
}
134+
}

0 commit comments

Comments
 (0)