Skip to content

Commit 0b743e7

Browse files
Webpack: No longer require babel to be run on .css.ts files (#341)
Co-authored-by: Mark Dalgleish <[email protected]>
1 parent d425728 commit 0b743e7

File tree

13 files changed

+139
-82
lines changed

13 files changed

+139
-82
lines changed

.changeset/bright-onions-film.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': patch
3+
---
4+
5+
Refactor SSR file scoping to use centralised `addFileScope` implementation

.changeset/fuzzy-flies-camp.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@vanilla-extract/webpack-plugin': minor
3+
---
4+
5+
No longer require Babel to be run on .css.ts files
6+
7+
Previously, the `@vanilla-extract/webpack-plugin` required the `@vanilla-extract/babel-plugin` to be run over .css.ts files. In order to bring webpack inline with the other integrations, the `@vanilla-extract/webpack-plugin` can now be used without Babel.
8+
9+
Note: Automatic debug IDs still require the `@vanilla-extract/babel-plugin`.

.changeset/warm-walls-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/integration': minor
3+
---
4+
5+
Add `addFileScope` API

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,10 @@ There are currently a few integrations to choose from.
121121
1. Install the dependencies.
122122

123123
```bash
124-
npm install @vanilla-extract/css @vanilla-extract/babel-plugin @vanilla-extract/webpack-plugin
124+
npm install @vanilla-extract/css @vanilla-extract/webpack-plugin
125125
```
126126

127-
2. Add the [Babel](https://babeljs.io) plugin.
128-
129-
```json
130-
{
131-
"plugins": ["@vanilla-extract/babel-plugin"]
132-
}
133-
```
134-
135-
3. Add the [webpack](https://webpack.js.org) plugin.
127+
2. Add the [webpack](https://webpack.js.org) plugin.
136128

137129
> 💡 This plugin accepts an optional [configuration object](#configuration).
138130
@@ -179,6 +171,18 @@ module.exports = {
179171
```
180172
</details>
181173

174+
3. If you'd like automatic debuggable identifiers, you can add the [Babel](https://babeljs.io) plugin.
175+
176+
```bash
177+
$ npm install @vanilla-extract/babel-plugin
178+
```
179+
180+
```json
181+
{
182+
"plugins": ["@vanilla-extract/babel-plugin"]
183+
}
184+
```
185+
182186
### esbuild
183187

184188
1. Install the dependencies.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { posix, relative, sep } from 'path';
2+
import type { PackageInfo } from './packageInfo';
3+
4+
interface AddFileScopeParams {
5+
source: string;
6+
filePath: string;
7+
packageInfo: PackageInfo;
8+
}
9+
export function addFileScope({
10+
source,
11+
filePath,
12+
packageInfo,
13+
}: AddFileScopeParams) {
14+
if (source.indexOf('@vanilla-extract/css/fileScope') > -1) {
15+
return { source, updated: false };
16+
}
17+
18+
// Encode windows file paths as posix
19+
const normalizedPath = posix.join(
20+
...relative(packageInfo.dirname, filePath).split(sep),
21+
);
22+
23+
const packageName = packageInfo.name ? `"${packageInfo.name}"` : 'undefined';
24+
25+
const contents = `
26+
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
27+
setFileScope("${normalizedPath}", ${packageName});
28+
${source}
29+
endFileScope();
30+
`;
31+
32+
return { source: contents, updated: true };
33+
}

packages/integration/src/compile.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { dirname, relative, join, posix, sep } from 'path';
1+
import { dirname, join } from 'path';
22
import { promises as fs } from 'fs';
33

44
import { build as esbuild, Plugin } from 'esbuild';
55

66
import { cssFileFilter } from './filters';
77
import { getPackageInfo } from './packageInfo';
8+
import { addFileScope } from './addFileScope';
89

910
export const vanillaExtractFilescopePlugin = (): Plugin => ({
1011
name: 'vanilla-extract-filescope',
@@ -14,24 +15,15 @@ export const vanillaExtractFilescopePlugin = (): Plugin => ({
1415
build.onLoad({ filter: cssFileFilter }, async ({ path }) => {
1516
const originalSource = await fs.readFile(path, 'utf-8');
1617

17-
if (originalSource.indexOf('@vanilla-extract/css/fileScope') === -1) {
18-
// Encode windows file paths as posix
19-
const filePath = posix.join(
20-
...relative(packageInfo.dirname, path).split(sep),
21-
);
22-
23-
const contents = `
24-
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
25-
setFileScope("${filePath}", ${
26-
packageInfo.name ? `"${packageInfo.name}"` : 'undefined'
27-
});
28-
29-
${originalSource}
30-
endFileScope()
31-
`;
18+
const { source, updated } = addFileScope({
19+
source: originalSource,
20+
filePath: path,
21+
packageInfo,
22+
});
3223

24+
if (updated) {
3325
return {
34-
contents,
26+
contents: source,
3527
loader: path.match(/\.(ts|tsx)$/i) ? 'ts' : undefined,
3628
resolveDir: dirname(path),
3729
};

packages/integration/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export { getSourceFromVirtualCssFile } from './virtualFile';
33
export { getPackageInfo } from './packageInfo';
44
export { compile, vanillaExtractFilescopePlugin } from './compile';
55
export { hash } from './hash';
6+
export { addFileScope } from './addFileScope';
67
export * from './filters';
78

89
export type { IdentifierOption } from './processVanillaFile';
10+
export type { PackageInfo } from './packageInfo';

packages/integration/src/packageInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path';
22

33
import findUp from 'find-up';
44

5-
interface PackageInfo {
5+
export interface PackageInfo {
66
name: string;
77
path: string;
88
dirname: string;

packages/vite-plugin/src/index.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
hash,
1111
getPackageInfo,
1212
IdentifierOption,
13+
addFileScope,
1314
} from '@vanilla-extract/integration';
1415

1516
interface Options {
@@ -59,23 +60,11 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
5960
}
6061

6162
if (ssr) {
62-
// If file already has a scope (has already run through babel plugin)
63-
if (code.indexOf('@vanilla-extract/css/fileScope') > -1) {
64-
return code;
65-
}
66-
67-
const filePath = normalizePath(path.relative(packageInfo.dirname, id));
68-
69-
const packageName = packageInfo.name
70-
? `"${packageInfo.name}"`
71-
: 'undefined';
72-
73-
return `
74-
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
75-
setFileScope("${filePath}", ${packageName});
76-
${code}
77-
endFileScope();
78-
`;
63+
return addFileScope({
64+
source: code,
65+
filePath: normalizePath(path.relative(packageInfo.dirname, id)),
66+
packageInfo,
67+
}).source;
7968
}
8069

8170
const { source, watchFiles } = await compile({

packages/webpack-plugin/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { cssFileFilter, IdentifierOption } from '@vanilla-extract/integration';
1+
import {
2+
cssFileFilter,
3+
IdentifierOption,
4+
getPackageInfo,
5+
} from '@vanilla-extract/integration';
26
import path from 'path';
37
import type { Compiler, RuleSetRule } from 'webpack';
48
import chalk from 'chalk';
@@ -149,6 +153,7 @@ export class VanillaExtractPlugin {
149153
outputCss: this.outputCss,
150154
childCompiler: this.childCompiler,
151155
identifiers: this.identifiers,
156+
packageInfo: getPackageInfo(compiler.context),
152157
},
153158
},
154159
],

0 commit comments

Comments
 (0)