Skip to content

Commit bec1cd8

Browse files
authored
Compress CSS resources to keep the virtual size down (#621)
1 parent 51574f9 commit bec1cd8

File tree

8 files changed

+48
-17
lines changed

8 files changed

+48
-17
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vanilla-extract/integration': patch
3+
'@vanilla-extract/webpack-plugin': patch
4+
---
5+
6+
Improve build performance when creating large CSS files

packages/integration/src/processVanillaFile.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { stringify } from 'javascript-stringify';
66
import isPlainObject from 'lodash/isPlainObject';
77
import outdent from 'outdent';
88
import { hash } from './hash';
9+
import zlib from 'zlib';
910

1011
const originalNodeEnv = process.env.NODE_ENV;
1112

@@ -109,7 +110,9 @@ export async function processVanillaFile({
109110
cssObjs: fileScopeCss,
110111
}).join('\n');
111112

112-
const base64Source = Buffer.from(css, 'utf-8').toString('base64');
113+
const compressedCSS = zlib.gzipSync(css);
114+
const base64Source = compressedCSS.toString('base64');
115+
113116
const fileName = `${
114117
fileScope.packageName
115118
? `${fileScope.packageName}/${fileScope.filePath}`
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
import zlib from 'zlib';
2+
13
export function getSourceFromVirtualCssFile(id: string) {
24
const match = id.match(/^(?<fileName>.*)\?source=(?<source>.*)$/) ?? [];
35

46
if (!match || !match.groups) {
57
throw new Error('No source in vanilla CSS file');
68
}
79

10+
const decompressedSource = zlib.gunzipSync(
11+
Buffer.from(match.groups.source, 'base64'),
12+
);
13+
814
return {
915
fileName: match.groups.fileName,
10-
source: Buffer.from(match.groups.source, 'base64').toString('utf-8'),
16+
source: decompressedSource.toString('utf-8'),
1117
};
1218
}

packages/webpack-plugin/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@
1717
"./extracted": {
1818
"module": "./extracted/dist/vanilla-extract-webpack-plugin-extracted.esm.js",
1919
"default": "./extracted/dist/vanilla-extract-webpack-plugin-extracted.cjs.js"
20+
},
21+
"./virtual": {
22+
"module": "./virtual/dist/vanilla-extract-webpack-plugin-virtual.esm.js",
23+
"default": "./virtual/dist/vanilla-extract-webpack-plugin-virtual.cjs.js"
2024
}
2125
},
2226
"preconstruct": {
2327
"entrypoints": [
2428
"index.ts",
2529
"loader.ts",
26-
"extracted.js"
30+
"extracted.js",
31+
"virtual.ts"
2732
]
2833
},
2934
"files": [
3035
"/dist",
3136
"/loader",
32-
"/extracted"
37+
"/extracted",
38+
"/virtual"
3339
],
3440
"repository": {
3541
"type": "git",
@@ -45,8 +51,7 @@
4551
"@vanilla-extract/integration": "^2.0.1",
4652
"chalk": "^4.1.1",
4753
"debug": "^4.3.1",
48-
"loader-utils": "^2.0.0",
49-
"virtual-resource-loader": "^1.0.0"
54+
"loader-utils": "^2.0.0"
5055
},
5156
"devDependencies": {
5257
"@types/debug": "^4.1.5",

packages/webpack-plugin/src/loader.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import type { LoaderContext } from './types';
1212
import { debug, formatResourcePath } from './logger';
1313
import { ChildCompiler } from './compiler';
1414

15+
const virtualLoader = require.resolve(
16+
path.join(path.dirname(require.resolve('../../package.json')), 'virtual'),
17+
);
18+
1519
const emptyCssExtractionFile = require.resolve(
1620
path.join(path.dirname(require.resolve('../../package.json')), 'extracted'),
1721
);
@@ -73,9 +77,9 @@ export function pitch(this: LoaderContext) {
7377
identOption:
7478
identifiers ?? (this.mode === 'production' ? 'short' : 'debug'),
7579
serializeVirtualCssPath: ({ fileName, base64Source }) => {
76-
const virtualResourceLoader = `${require.resolve(
77-
'virtual-resource-loader',
78-
)}?${JSON.stringify({ source: base64Source })}`;
80+
const virtualResourceLoader = `${virtualLoader}?${JSON.stringify({
81+
source: base64Source,
82+
})}`;
7983

8084
const request = loaderUtils.stringifyRequest(
8185
this,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-expect-error
2+
import { getOptions } from 'loader-utils';
3+
import zlib from 'zlib';
4+
5+
export default function (this: any) {
6+
const { source } = getOptions(this);
7+
8+
const decompressedSource = zlib.gunzipSync(Buffer.from(source, 'base64'));
9+
10+
return decompressedSource.toString('utf-8');
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"main": "dist/vanilla-extract-webpack-plugin-virtual.cjs.js",
3+
"module": "dist/vanilla-extract-webpack-plugin-virtual.esm.js"
4+
}

pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)