Skip to content

Commit 5ed4b4c

Browse files
committed
Test the bundle size using esbuild instead of webpack.
1 parent beb60ac commit 5ed4b4c

4 files changed

Lines changed: 22 additions & 37 deletions

File tree

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- Updated GitHub Actions CI config to run tests with Node.js v12, v14, v16.
1515
- Simplified JSDoc related package scripts now that [`jsdoc-md`](https://npm.im/jsdoc-md) v10+ automatically generates a Prettier formatted readme.
1616
- Added a package `test:jsdoc` script that checks the readme API docs are up to date with the source JSDoc.
17+
- Test the bundle size using [`esbuild`](https://npm.im/esbuild) instead of [`webpack`](https://npm.im/webpack) and [`disposable-directory`](https://npm.im/disposable-directory).
18+
- Increased the documented bundle size to “< 4 kB” to match that of [`esbuild`](https://npm.im/esbuild) instead of [`webpack`](https://npm.im/webpack).
1719
- Use the correct `kB` symbol instead of `KB` wherever bundle size is mentioned in the package description and readme.
1820
- Updated the [example Next.js app](https://graphql-react.vercel.app) URL in the readme.
1921

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "graphql-react",
33
"version": "13.0.0",
4-
"description": "A GraphQL client for React using modern context and hooks APIs that’s lightweight (< 3.5 kB) but powerful; the first Relay and Apollo alternative with server side rendering.",
4+
"description": "A GraphQL client for React using modern context and hooks APIs that’s lightweight (< 4 kB) but powerful; the first Relay and Apollo alternative with server side rendering.",
55
"license": "MIT",
66
"author": {
77
"name": "Jayden Seric",
@@ -58,7 +58,7 @@
5858
"@testing-library/react-hooks": "^6.0.0",
5959
"abort-controller": "^3.0.0",
6060
"coverage-node": "^5.0.1",
61-
"disposable-directory": "^4.0.0",
61+
"esbuild": "^0.12.0",
6262
"eslint": "^7.26.0",
6363
"eslint-config-env": "^20.0.0",
6464
"eslint-config-prettier": "^8.3.0",
@@ -80,8 +80,7 @@
8080
"react-dom": "^17.0.2",
8181
"react-test-renderer": "^17.0.2",
8282
"revertable-globals": "^2.0.0",
83-
"test-director": "^6.0.0",
84-
"webpack": "^5.37.0"
83+
"test-director": "^6.0.0"
8584
},
8685
"scripts": {
8786
"jsdoc": "jsdoc-md",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[![npm version](https://badgen.net/npm/v/graphql-react)](https://npm.im/graphql-react) [![CI status](https://github.com/jaydenseric/graphql-react/workflows/CI/badge.svg)](https://github.com/jaydenseric/graphql-react/actions)
66

7-
A [GraphQL](https://graphql.org) client for [React](https://reactjs.org) using modern [context](https://reactjs.org/docs/context) and [hooks](https://reactjs.org/docs/hooks-intro) APIs that’s lightweight (< 3.5 kB) but powerful; the first [Relay](https://relay.dev) and [Apollo](https://apollographql.com/apollo-client) alternative with server side rendering.
7+
A [GraphQL](https://graphql.org) client for [React](https://reactjs.org) using modern [context](https://reactjs.org/docs/context) and [hooks](https://reactjs.org/docs/hooks-intro) APIs that’s lightweight (< 4 kB) but powerful; the first [Relay](https://relay.dev) and [Apollo](https://apollographql.com/apollo-client) alternative with server side rendering.
88

99
The [API](#api) can also be used to custom load, cache and server side render any data, even from non GraphQL sources.
1010

test/bundle.test.mjs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,27 @@
11
import { strictEqual } from 'assert';
2-
import fs from 'fs';
3-
import { join } from 'path';
42
import { fileURLToPath } from 'url';
5-
import { promisify } from 'util';
6-
import disposableDirectory from 'disposable-directory';
3+
import esbuild from 'esbuild';
74
import gzipSize from 'gzip-size';
8-
import webpack from 'webpack';
95

106
export default (tests) => {
117
tests.add('Bundle.', async () => {
12-
await disposableDirectory(async (tempDirPath) => {
13-
const filename = 'bundle.cjs';
14-
const compiler = webpack({
15-
context: fileURLToPath(new URL('./', import.meta.url)),
16-
entry: '../public/index.js',
17-
output: {
18-
path: tempDirPath,
19-
filename,
20-
libraryTarget: 'commonjs2',
21-
},
22-
target: 'node',
23-
mode: 'production',
24-
externals: /^(?:object-assign|react|react-dom)(?:\/|\\|$)/u,
25-
});
26-
const compile = promisify(compiler.run).bind(compiler);
27-
const stats = await compile();
28-
29-
if (stats.hasWarnings() || stats.hasErrors())
30-
throw new Error(stats.toString('errors-warnings'));
8+
const {
9+
outputFiles: [bundle],
10+
} = await esbuild.build({
11+
entryPoints: [
12+
fileURLToPath(new URL('../public/index.js', import.meta.url)),
13+
],
14+
external: ['react'],
15+
write: false,
16+
bundle: true,
17+
minify: true,
18+
legalComments: 'none',
19+
});
3120

32-
const bundleCode = await fs.promises.readFile(
33-
join(tempDirPath, filename),
34-
'utf8'
35-
);
36-
const kB = (await gzipSize(bundleCode)) / 1000;
21+
const kB = (await gzipSize(bundle.contents)) / 1000;
3722

38-
console.info(`${kB} kB minified and gzipped bundle.`);
23+
console.info(`${kB} kB minified and gzipped bundle.`);
3924

40-
strictEqual(kB < 3.5, true);
41-
});
25+
strictEqual(kB < 4, true);
4226
});
4327
};

0 commit comments

Comments
 (0)