Skip to content

Commit

Permalink
Compress CSS resources to keep the virtual size down (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayaabkhan committed Mar 17, 2022
1 parent 51574f9 commit bec1cd8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changeset/friendly-turkeys-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@vanilla-extract/integration': patch
'@vanilla-extract/webpack-plugin': patch
---

Improve build performance when creating large CSS files
5 changes: 4 additions & 1 deletion packages/integration/src/processVanillaFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { stringify } from 'javascript-stringify';
import isPlainObject from 'lodash/isPlainObject';
import outdent from 'outdent';
import { hash } from './hash';
import zlib from 'zlib';

const originalNodeEnv = process.env.NODE_ENV;

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

const base64Source = Buffer.from(css, 'utf-8').toString('base64');
const compressedCSS = zlib.gzipSync(css);
const base64Source = compressedCSS.toString('base64');

const fileName = `${
fileScope.packageName
? `${fileScope.packageName}/${fileScope.filePath}`
Expand Down
8 changes: 7 additions & 1 deletion packages/integration/src/virtualFile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import zlib from 'zlib';

export function getSourceFromVirtualCssFile(id: string) {
const match = id.match(/^(?<fileName>.*)\?source=(?<source>.*)$/) ?? [];

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

const decompressedSource = zlib.gunzipSync(
Buffer.from(match.groups.source, 'base64'),
);

return {
fileName: match.groups.fileName,
source: Buffer.from(match.groups.source, 'base64').toString('utf-8'),
source: decompressedSource.toString('utf-8'),
};
}
13 changes: 9 additions & 4 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@
"./extracted": {
"module": "./extracted/dist/vanilla-extract-webpack-plugin-extracted.esm.js",
"default": "./extracted/dist/vanilla-extract-webpack-plugin-extracted.cjs.js"
},
"./virtual": {
"module": "./virtual/dist/vanilla-extract-webpack-plugin-virtual.esm.js",
"default": "./virtual/dist/vanilla-extract-webpack-plugin-virtual.cjs.js"
}
},
"preconstruct": {
"entrypoints": [
"index.ts",
"loader.ts",
"extracted.js"
"extracted.js",
"virtual.ts"
]
},
"files": [
"/dist",
"/loader",
"/extracted"
"/extracted",
"/virtual"
],
"repository": {
"type": "git",
Expand All @@ -45,8 +51,7 @@
"@vanilla-extract/integration": "^2.0.1",
"chalk": "^4.1.1",
"debug": "^4.3.1",
"loader-utils": "^2.0.0",
"virtual-resource-loader": "^1.0.0"
"loader-utils": "^2.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.5",
Expand Down
10 changes: 7 additions & 3 deletions packages/webpack-plugin/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import type { LoaderContext } from './types';
import { debug, formatResourcePath } from './logger';
import { ChildCompiler } from './compiler';

const virtualLoader = require.resolve(
path.join(path.dirname(require.resolve('../../package.json')), 'virtual'),
);

const emptyCssExtractionFile = require.resolve(
path.join(path.dirname(require.resolve('../../package.json')), 'extracted'),
);
Expand Down Expand Up @@ -73,9 +77,9 @@ export function pitch(this: LoaderContext) {
identOption:
identifiers ?? (this.mode === 'production' ? 'short' : 'debug'),
serializeVirtualCssPath: ({ fileName, base64Source }) => {
const virtualResourceLoader = `${require.resolve(
'virtual-resource-loader',
)}?${JSON.stringify({ source: base64Source })}`;
const virtualResourceLoader = `${virtualLoader}?${JSON.stringify({
source: base64Source,
})}`;

const request = loaderUtils.stringifyRequest(
this,
Expand Down
11 changes: 11 additions & 0 deletions packages/webpack-plugin/src/virtual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-expect-error
import { getOptions } from 'loader-utils';
import zlib from 'zlib';

export default function (this: any) {
const { source } = getOptions(this);

const decompressedSource = zlib.gunzipSync(Buffer.from(source, 'base64'));

return decompressedSource.toString('utf-8');
}
4 changes: 4 additions & 0 deletions packages/webpack-plugin/virtual/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/vanilla-extract-webpack-plugin-virtual.cjs.js",
"module": "dist/vanilla-extract-webpack-plugin-virtual.esm.js"
}
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bec1cd8

Please sign in to comment.