Skip to content

Commit 41291f3

Browse files
committed
Provide a single default export that works for all targets
1 parent 4dffa26 commit 41291f3

File tree

9 files changed

+369
-418
lines changed

9 files changed

+369
-418
lines changed

index.browser.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
// allow synchronous import of WebAssembly from an entrypoint.
44
module.exports = import("./pkg.bundler/brotli_wasm.js");
55

6-
// We don't want to do this for _all_ usage, because dynamic import isn't
7-
// supported in older node versions.
6+
// In addition, we provide a default export with the same value, for compatibility
7+
// with the pure ESM web bundle:
8+
module.exports.default = module.exports;
9+
10+
// Without this, ts-loader gets annoyed by imports for the pure type. Clear ts-loader bug,
11+
// but this is a quick & easy fix on our end:
12+
module.exports.BrotliWasmType = undefined;

index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as BrotliWasm from './pkg.node/brotli_wasm';
2+
3+
// Re-export the core API - although this will only work OOTB for node usage.
4+
export * from './pkg.node/brotli_wasm';
5+
6+
declare const promisedValue: Promise<typeof BrotliWasm>;
7+
export default promisedValue;
8+
9+
export type BrotliWasmType = typeof BrotliWasm;

index.node.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// In Node, everything is conveniently available synchronously.
2+
const nodePkg = require('./pkg.node/brotli_wasm');
3+
module.exports = nodePkg;
4+
5+
// In addition though, we provide a default export, to match the pure ESM web bundle:
6+
module.exports.default = Promise.resolve(nodePkg);

index.web.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

index.web.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// The web version needs to be explicitly initiated, but we want the API to be the same
2-
// as the bundler version, so we write this small wrapper to make it work.
1+
// In pure ESM web bundles, you must call init() and wait for the promised result before you can
2+
// call any module methods. To make that as easy as possible, this module directly exposes the
3+
// init() promise result, and returns the methods at the end of the promise.
34
import init, * as brotliWasm from "./pkg.web/brotli_wasm";
4-
5-
export default initOpts => init(initOpts).then(() => brotliWasm);
5+
export default init().then(() => brotliWasm);

karma-esm.conf.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Karma-vite breaks Karma in Node <14, and browser tests don't depend on Node versions, so
2+
// we just skip the tests in those environments.
3+
const nodeMajorVersion = parseInt(process.versions.node.split('.'));
4+
if (nodeMajorVersion <= 14) {
5+
console.log('Skipping browser tests in old node version');
6+
process.exit(0);
7+
}
8+
9+
module.exports = function(config) {
10+
config.set({
11+
frameworks: ['mocha', 'chai', 'vite'],
12+
files: [
13+
{
14+
pattern: 'test/**/*.spec.ts',
15+
type: 'module',
16+
watched: false,
17+
served: false,
18+
}
19+
],
20+
mime: {
21+
'text/x-typescript': ['ts']
22+
},
23+
reporters: ['spec'],
24+
port: 9876,
25+
logLevel: config.LOG_INFO,
26+
27+
browsers: ['ChromeHeadless'],
28+
29+
autoWatch: false,
30+
singleRun: true,
31+
concurrency: Infinity
32+
});
33+
};

karma.conf.js renamed to karma-webpack.conf.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
// Karma-vite breaks Karma in Node <14, and browser tests don't depend on Node versions, so
2+
// we just skip the tests in those environments.
3+
const nodeMajorVersion = parseInt(process.versions.node.split('.'));
4+
if (nodeMajorVersion <= 14) {
5+
console.log('Skipping browser tests in old node version');
6+
process.exit(0);
7+
}
8+
19
const tmp = require('tmp');
210
tmp.setGracefulCleanup();
311

4-
const webpack = require('webpack');
5-
612
const outputDir = tmp.dirSync({ unsafeCleanup: true }).name;
713

814
module.exports = function(config) {
@@ -38,21 +44,12 @@ module.exports = function(config) {
3844
resolve: {
3945
extensions: ['.ts', '.js'],
4046
fallback: {
41-
buffer: require.resolve('buffer/'),
42-
fs: require.resolve('graceful-fs/graceful-fs'),
43-
path: require.resolve('path-browserify/'),
44-
constants: require.resolve('constants-browserify/'),
45-
stream: require.resolve('stream-browserify/'),
47+
crypto: false
4648
}
4749
},
4850
experiments: {
4951
asyncWebAssembly: true
5052
},
51-
plugins: [
52-
new webpack.ProvidePlugin({
53-
Buffer: ['buffer', 'Buffer'],process: 'process/browser',
54-
})
55-
],
5653
output: {
5754
path: outputDir
5855
}

package.json

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,36 @@
22
"name": "brotli-wasm",
33
"version": "1.2.0",
44
"description": "A reliable compressor and decompressor for Brotli, supporting node & browsers via wasm",
5-
"main": "./pkg.node/brotli_wasm.js",
6-
"types": "./pkg.node/brotli_wasm.d.ts",
5+
"types": "./index.d.ts",
6+
"main": "./index.node.js",
77
"browser": "./index.browser.js",
8+
"exports": {
9+
".": {
10+
"import": "./index.web.js",
11+
"browser": "./index.browser.js",
12+
"require": "./index.node.js",
13+
"default": "./index.web.js"
14+
}
15+
},
816
"sideEffects": false,
917
"files": [
1018
"pkg.node",
1119
"pkg.bundler",
1220
"pkg.web",
21+
"index.node.js",
1322
"index.browser.js",
14-
"index.web.d.ts",
15-
"index.web.js"
23+
"index.web.js",
24+
"index.d.ts"
1625
],
1726
"scripts": {
1827
"build": "node ./build.js",
1928
"pretest": "npm run build",
20-
"test": "npm run test:node && npm run test:browser",
21-
"test:node": "ts-mocha -r esm -p test/tsconfig.json 'test/**/*.spec.ts'",
22-
"test:browser": "karma start",
23-
"test:browser:debug": "npm run test:browser -- --single-run=false --browsers Chrome"
29+
"test": "npm run test:node && npm run test:esm && npm run test:webpack",
30+
"test:node": "ts-mocha -p test/tsconfig.json 'test/**/*.spec.ts'",
31+
"test:webpack": "karma start ./karma-webpack.conf.js",
32+
"test:esm": "karma start ./karma-esm.conf.js",
33+
"test:webpack:debug": "npm run test:webpack -- --single-run=false --browsers Chrome",
34+
"test:esm:debug": "npm run test:esm -- --single-run=false --browsers Chrome"
2435
},
2536
"repository": {
2637
"type": "git",
@@ -39,10 +50,15 @@
3950
},
4051
"homepage": "https://github.com/httptoolkit/brotli-wasm#readme",
4152
"devDependencies": {
53+
"@peculiar/webcrypto": "^1.4.0",
54+
"@types/atob": "^2.1.2",
55+
"@types/btoa": "^1.2.3",
4256
"@types/chai": "^4.2.18",
4357
"@types/mocha": "^8.2.2",
4458
"@types/node": "^15.6.0",
4559
"@types/text-encoding": "0.0.36",
60+
"atob": "^2.1.2",
61+
"btoa": "^1.2.1",
4662
"buffer": "^6.0.3",
4763
"chai": "^4.3.4",
4864
"karma": "^6.3.2",
@@ -52,16 +68,16 @@
5268
"karma-sourcemap-loader": "^0.3.8",
5369
"karma-spec-reporter": "0.0.32",
5470
"karma-typescript": "^5.5.1",
71+
"karma-vite": "^1.0.1",
5572
"karma-webpack": "^5.0.0",
5673
"mocha": "^8.4.0",
5774
"shelljs": "^0.8.4",
75+
"text-encoding": "^0.7.0",
5876
"ts-loader": "^9.2.1",
77+
"ts-mocha": "^10.0.0",
5978
"ts-node": "^9.1.1",
6079
"typescript": "^4.2.4",
6180
"wasm-pack": "^0.10.3",
62-
"webpack": "^5.37.1",
63-
"esm": "^3.2.25",
64-
"text-encoding": "^0.7.0",
65-
"ts-mocha": "^10.0.0"
81+
"webpack": "^5.37.1"
6682
}
6783
}

0 commit comments

Comments
 (0)