Skip to content

Commit 48782c1

Browse files
committed
Working browser + node build with tests and everything
Webpack + Karma has been a massive pain, but I think this setup is enough to get it all happy!
1 parent 95b8870 commit 48782c1

File tree

5 files changed

+153
-11
lines changed

5 files changed

+153
-11
lines changed

build.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ shell.rm('-rf', 'pkg');
99
shell.rm('-rf', 'dist');
1010
shell.mkdir('dist');
1111

12-
// Copy the core web files into dist
12+
// Create the bundler output
1313
shell.exec('wasm-pack build --target bundler');
14-
shell.cp('pkg/brotli_wasm*', 'dist');
15-
shell.rm('-rf', 'pkg');
16-
shell.mv('dist/brotli_wasm.js', 'dist/browser-bundle.js');
14+
shell.mv('pkg', 'pkg.bundler');
1715

18-
// Copy the node wrapper into dist too
1916
shell.exec('wasm-pack build --target nodejs');
20-
shell.cp('pkg/brotli_wasm.js', 'dist/node.js');
21-
shell.rm('-rf', 'pkg');
17+
shell.mv('pkg', 'pkg.node');

karma.conf.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const tmp = require('tmp');
2+
tmp.setGracefulCleanup();
3+
4+
const webpack = require('webpack');
5+
6+
const outputDir = tmp.dirSync({ unsafeCleanup: true }).name;
7+
8+
module.exports = function(config) {
9+
config.set({
10+
frameworks: ['mocha', 'chai', 'webpack'],
11+
files: [
12+
'test/**/*.spec.ts',
13+
// Required due to https://github.com/ryanclark/karma-webpack/issues/498. Results in an
14+
// annoying warning before the webpack build, but then it works fine.
15+
{ pattern: `${outputDir}/*.wasm`, included: false, served: true }
16+
],
17+
mime: {
18+
'text/x-typescript': ['ts']
19+
},
20+
webpack: {
21+
mode: 'development',
22+
devtool: 'source-map',
23+
module: {
24+
rules: [
25+
{
26+
test: /\.ts$/,
27+
loader: 'ts-loader',
28+
options: {
29+
configFile: 'test/tsconfig.json',
30+
compilerOptions: {
31+
outDir: tmp.dirSync({ unsafeCleanup: true }).name
32+
}
33+
},
34+
exclude: /node_modules/
35+
}
36+
]
37+
},
38+
resolve: {
39+
extensions: ['.ts', '.js'],
40+
fallback: {
41+
buffer: require.resolve('buffer/')
42+
}
43+
},
44+
experiments: {
45+
asyncWebAssembly: true
46+
},
47+
plugins: [
48+
new webpack.ProvidePlugin({
49+
Buffer: ['buffer', 'Buffer'],
50+
})
51+
],
52+
output: {
53+
path: outputDir
54+
}
55+
},
56+
webpackMiddleware: {
57+
stats: 'error-only'
58+
},
59+
preprocessors: {
60+
'test/**/*.ts': ['webpack', 'sourcemap']
61+
},
62+
reporters: ['spec'],
63+
port: 9876,
64+
logLevel: config.LOG_INFO,
65+
66+
browsers: ['ChromeHeadless'],
67+
68+
autoWatch: false,
69+
singleRun: true,
70+
concurrency: Infinity
71+
});
72+
};

package.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
"name": "brotli-wasm",
33
"version": "1.0.0",
44
"description": "A compressor and decompressor for Brotli that actually works, for node & browsers",
5-
"main": "dist/node.js",
6-
"browser": "dist/browser.js",
5+
"main": "./pkg.node/brotli_wasm.js",
6+
"types": "./pkg.node/brotli_wasm.d.ts",
7+
"browser": "./pkg.bundler/brotli_wasm.js",
8+
"sideEffects": false,
79
"scripts": {
810
"build": "node ./build.js",
9-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"pretest": "npm run build",
12+
"test": "npm run test:node && npm run test:browser",
13+
"test:node": "TS_NODE_FILES=true mocha -r ts-node/register 'test/**/*.spec.ts'",
14+
"test:browser": "karma start",
15+
"test:browser:debug": "npm run test:browser -- --single-run=false --browsers Chrome"
1016
},
1117
"repository": {
1218
"type": "git",
@@ -25,7 +31,25 @@
2531
},
2632
"homepage": "https://github.com/httptoolkit/brotli-wasm#readme",
2733
"devDependencies": {
34+
"@types/chai": "^4.2.18",
35+
"@types/mocha": "^8.2.2",
36+
"@types/node": "^15.6.0",
37+
"buffer": "^6.0.3",
38+
"chai": "^4.3.4",
39+
"karma": "^6.3.2",
40+
"karma-chai": "^0.1.0",
41+
"karma-chrome-launcher": "^3.1.0",
42+
"karma-mocha": "^2.0.1",
43+
"karma-sourcemap-loader": "^0.3.8",
44+
"karma-spec-reporter": "0.0.32",
45+
"karma-typescript": "^5.5.1",
46+
"karma-webpack": "^5.0.0",
47+
"mocha": "^8.4.0",
2848
"shelljs": "^0.8.4",
29-
"wasm-pack": "^0.9.1"
49+
"ts-loader": "^9.2.1",
50+
"ts-node": "^9.1.1",
51+
"typescript": "^4.2.4",
52+
"wasm-pack": "^0.9.1",
53+
"webpack": "^5.37.1"
3054
}
3155
}

test/brotli.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from 'chai';
2+
import * as brotliPromise from '..';
3+
4+
describe("Brotli-wasm", () => {
5+
6+
let brotli: typeof import('..');
7+
beforeEach(async () => {
8+
brotli = await brotliPromise;
9+
});
10+
11+
it("can compress data", () => {
12+
const input = Buffer.from("Test input data");
13+
const result = brotli.compress(input);
14+
expect(Buffer.from(result).toString('base64')).to.equal('Gw4A+KWpyubolCCjVAjmxJ4D');
15+
});
16+
17+
it("can decompress data", () => {
18+
// Generated with: echo -n '$CONTENT' | brotli --stdout - | base64
19+
const input = Buffer.from('GxoAABypU587dC0k9ianQOgqjS32iUTcCA==', 'base64');
20+
const result = brotli.decompress(input);
21+
expect(Buffer.from(result).toString('utf8')).to.equal('Brotli brotli brotli brotli');
22+
});
23+
24+
it("cleanly fails when decompressing garbage", () => {
25+
const input = Buffer.from("This is not brotli data, it's just a string");
26+
expect(() =>
27+
brotli.decompress(input)
28+
).to.throw('Brotli decompress failed');
29+
});
30+
31+
it("can compress & decompress back to the original result", () => {
32+
const input = "Some thrilling text I urgently need to compress";
33+
const result = Buffer.from(
34+
brotli.decompress(brotli.compress(Buffer.from(input)))
35+
).toString('utf8');
36+
expect(result).to.equal(input);
37+
});
38+
});

test/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"sourceMap": true,
7+
"strict": true
8+
},
9+
"include": [
10+
"**/*.ts"
11+
]
12+
}

0 commit comments

Comments
 (0)