Skip to content

Commit 4414929

Browse files
committed
chore(build): build for browser corretly
1 parent 9f1cba6 commit 4414929

6 files changed

Lines changed: 152 additions & 106 deletions

File tree

index.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
import { isBrowser } from "./src/utils/browser";
2-
3-
// defined by webpack plugin
4-
declare let BUILT: any;
5-
6-
console.log("isBrowser", isBrowser());
7-
console.log(typeof BUILT);
8-
9-
if (isBrowser() && typeof BUILT === "undefined") {
10-
throw new Error(
11-
"It looks like you are using the Nodejs version of Kuzzle SDK " +
12-
"in a browser. " +
13-
"It is strongly recommended to use the browser-specific build instead. " +
14-
"Learn more at https://github.com/kuzzleio/sdk-javascript/tree/master#browser",
15-
);
16-
}
17-
181
export * from "./src/Kuzzle";
192
export * from "./src/protocols";
203
export * from "./src/protocols/abstract/Base";

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,19 @@
2727
"dev": "node -r ts-node/register ",
2828
"doc-testing": "bash .ci/test-docs.sh"
2929
},
30-
"browser": "dist/kuzzle.js",
31-
"main": "out/index.js",
30+
"browser": "./dist/kuzzle.js",
31+
"main": "./out/index.js",
32+
"types": "index.d.ts",
33+
"exports": {
34+
".": {
35+
"browser": "./dist/kuzzle.js",
36+
"import": "./out/index.js",
37+
"require": "./out/index.js",
38+
"default": "./out/index.js",
39+
"types": "./index.d.ts"
40+
},
41+
"./package.json": "./package.json"
42+
},
3243
"license": "Apache-2.0",
3344
"dependencies": {
3445
"ws": "8.18.3"

src/core/Room.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,3 @@ class Room {
148148
}
149149

150150
export default Room;
151-
module.exports = Room;

src/protocols/Http.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -459,45 +459,49 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
459459
headers: JSONObject;
460460
body: string;
461461
}> {
462-
// eslint-disable-next-line @typescript-eslint/no-var-requires
463-
const httpModule = this.ssl ? require("https") : require("http");
464-
465-
return new Promise((resolve, reject) => {
466-
const req = httpModule.request(
467-
url,
468-
{
469-
method,
470-
headers: options.headers,
471-
},
472-
(res) => {
473-
const chunks: Buffer[] = [];
474-
475-
res.on("data", (chunk) => chunks.push(chunk));
476-
res.on("end", () => {
477-
resolve({
478-
statusCode: res.statusCode,
479-
headers: res.headers as JSONObject,
480-
body: Buffer.concat(chunks).toString(),
481-
});
482-
});
483-
},
484-
);
462+
const httpModulePromise: Promise<
463+
typeof import("node:http") | typeof import("node:https")
464+
> = this.ssl ? import("node:https") : import("node:http");
465+
466+
return httpModulePromise.then(
467+
(httpModule) =>
468+
new Promise((resolve, reject) => {
469+
const req = httpModule.request(
470+
url,
471+
{
472+
method,
473+
headers: options.headers,
474+
},
475+
(res) => {
476+
const chunks: Buffer[] = [];
477+
478+
res.on("data", (chunk) => chunks.push(chunk));
479+
res.on("end", () => {
480+
resolve({
481+
statusCode: res.statusCode,
482+
headers: res.headers as JSONObject,
483+
body: Buffer.concat(chunks).toString(),
484+
});
485+
});
486+
},
487+
);
485488

486-
req.on("error", reject);
489+
req.on("error", reject);
487490

488-
const timeout = options.timeout || 0;
489-
if (timeout > 0) {
490-
req.setTimeout(timeout, () => {
491-
req.destroy(new Error("Request timed out"));
492-
});
493-
}
491+
const timeout = options.timeout || 0;
492+
if (timeout > 0) {
493+
req.setTimeout(timeout, () => {
494+
req.destroy(new Error("Request timed out"));
495+
});
496+
}
494497

495-
if (options.body) {
496-
req.write(options.body);
497-
}
498+
if (options.body) {
499+
req.write(options.body);
500+
}
498501

499-
req.end();
500-
});
502+
req.end();
503+
}),
504+
);
501505
}
502506

503507
_constructRoutes(publicApi) {

test/core/room.test.js

Lines changed: 97 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const should = require("should");
66
const Room = require("../../src/core/Room");
77
const { KuzzleEventEmitter } = require("../../src/core/KuzzleEventEmitter");
88

9+
console.log(Room);
10+
911
describe("Room", () => {
1012
const eventEmitter = new KuzzleEventEmitter();
1113
const options = { opt: "in" };
@@ -39,7 +41,7 @@ describe("Room", () => {
3941

4042
controller.kuzzle.autoResubscribe = "default";
4143

42-
const room = new Room(
44+
const room = new Room.default(
4345
controller,
4446
"index",
4547
"collection",
@@ -83,7 +85,14 @@ describe("Room", () => {
8385
const body = { foo: "bar" };
8486
const cb = sinon.stub();
8587

86-
const room = new Room(controller, "index", "collection", body, cb, opts);
88+
const room = new Room.default(
89+
controller,
90+
"index",
91+
"collection",
92+
body,
93+
cb,
94+
opts,
95+
);
8796

8897
should(room.request.scope).be.equal("scope");
8998
should(room.request.state).be.equal("state");
@@ -97,15 +106,36 @@ describe("Room", () => {
97106

98107
controller.kuzzle.autoResubscribe = "default";
99108

100-
const room1 = new Room(controller, "index", "collection", body, cb, {
101-
autoResubscribe: true,
102-
});
103-
const room2 = new Room(controller, "index", "collection", body, cb, {
104-
autoResubscribe: false,
105-
});
106-
const room3 = new Room(controller, "index", "collection", body, cb, {
107-
autoResubscribe: "foobar",
108-
});
109+
const room1 = new Room.default(
110+
controller,
111+
"index",
112+
"collection",
113+
body,
114+
cb,
115+
{
116+
autoResubscribe: true,
117+
},
118+
);
119+
const room2 = new Room.default(
120+
controller,
121+
"index",
122+
"collection",
123+
body,
124+
cb,
125+
{
126+
autoResubscribe: false,
127+
},
128+
);
129+
const room3 = new Room.default(
130+
controller,
131+
"index",
132+
"collection",
133+
body,
134+
cb,
135+
{
136+
autoResubscribe: "foobar",
137+
},
138+
);
109139

110140
should(room1.autoResubscribe).be.a.Boolean().and.be.True();
111141
should(room2.autoResubscribe).be.a.Boolean().and.be.False();
@@ -116,15 +146,36 @@ describe("Room", () => {
116146
const body = { foo: "bar" };
117147
const cb = sinon.stub();
118148

119-
const room1 = new Room(controller, "index", "collection", body, cb, {
120-
subscribeToSelf: true,
121-
});
122-
const room2 = new Room(controller, "index", "collection", body, cb, {
123-
subscribeToSelf: false,
124-
});
125-
const room3 = new Room(controller, "index", "collection", body, cb, {
126-
subscribeToSelf: "foobar",
127-
});
149+
const room1 = new Room.default(
150+
controller,
151+
"index",
152+
"collection",
153+
body,
154+
cb,
155+
{
156+
subscribeToSelf: true,
157+
},
158+
);
159+
const room2 = new Room.default(
160+
controller,
161+
"index",
162+
"collection",
163+
body,
164+
cb,
165+
{
166+
subscribeToSelf: false,
167+
},
168+
);
169+
const room3 = new Room.default(
170+
controller,
171+
"index",
172+
"collection",
173+
body,
174+
cb,
175+
{
176+
subscribeToSelf: "foobar",
177+
},
178+
);
128179

129180
should(room1.subscribeToSelf).be.a.Boolean().and.be.True();
130181
should(room2.subscribeToSelf).be.a.Boolean().and.be.False();
@@ -154,7 +205,14 @@ describe("Room", () => {
154205
};
155206
const body = { foo: "bar" };
156207
const cb = sinon.stub();
157-
const room = new Room(controller, "index", "collection", body, cb, opts);
208+
const room = new Room.default(
209+
controller,
210+
"index",
211+
"collection",
212+
body,
213+
cb,
214+
opts,
215+
);
158216

159217
return room.subscribe().then((res) => {
160218
should(controller.kuzzle.query)
@@ -188,7 +246,14 @@ describe("Room", () => {
188246
};
189247
const body = { foo: "bar" };
190248
const cb = sinon.stub();
191-
const room = new Room(controller, "index", "collection", body, cb, opts);
249+
const room = new Room.default(
250+
controller,
251+
"index",
252+
"collection",
253+
body,
254+
cb,
255+
opts,
256+
);
192257

193258
return room.subscribe().then(() => {
194259
should(room.id).be.equal("my-room-id");
@@ -206,7 +271,14 @@ describe("Room", () => {
206271
};
207272
const body = { foo: "bar" };
208273
const cb = sinon.stub();
209-
const room = new Room(controller, "index", "collection", body, cb, opts);
274+
const room = new Room.default(
275+
controller,
276+
"index",
277+
"collection",
278+
body,
279+
cb,
280+
opts,
281+
);
210282

211283
room._channelListener = sinon.stub();
212284

@@ -225,7 +297,7 @@ describe("Room", () => {
225297
let room;
226298

227299
beforeEach(() => {
228-
room = new Room(
300+
room = new Room.default(
229301
controller,
230302
"index",
231303
"collection",
@@ -261,7 +333,7 @@ describe("Room", () => {
261333

262334
beforeEach(() => {
263335
cb = sinon.stub();
264-
room = new Room(
336+
room = new Room.default(
265337
controller,
266338
"index",
267339
"collection",

vite.config.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
11
import path from "path";
2-
import { defineConfig, Plugin } from "vite";
2+
import { defineConfig } from "vite";
33
import { version } from "./package.json";
44

5-
const ignoredModules = new Set<string>([
6-
"http",
7-
"package",
8-
"ws",
9-
"https",
10-
"url",
11-
]);
12-
13-
const ignoreOptionalModules: Plugin = {
14-
load(id: string) {
15-
if (ignoredModules.has(id)) {
16-
return "export default {}";
17-
}
18-
return null;
19-
},
20-
name: "ignore-optional-modules",
21-
resolveId(source: any) {
22-
return ignoredModules.has(source) ? source : null;
23-
},
24-
};
25-
265
export default defineConfig({
276
build: {
287
lib: {
298
// Use the TypeScript entry so Rollup sees ES modules and can generate a proper UMD bundle
309
entry: path.resolve(__dirname, "index.ts"),
31-
fileName: () => "kuzzle",
10+
fileName: () => "kuzzle.js",
3211
name: "KuzzleSDK",
3312
// Match the legacy Webpack output: single UMD bundle
3413
formats: ["umd"],
@@ -38,15 +17,13 @@ export default defineConfig({
3817
output: {
3918
banner: `// Kuzzle Javascript SDK version ${version}`,
4019
// Expose build flags on the global scope for browser usage (mirrors old webpack define)
41-
intro: `var BUILT = true; var SDKVERSION = "${version}";`,
4220
},
4321
},
4422
sourcemap: true,
4523
target: "es2015",
4624
},
4725
define: {
48-
BUILT: true,
4926
SDKVERSION: JSON.stringify(version),
5027
},
51-
plugins: [ignoreOptionalModules],
28+
plugins: [],
5229
});

0 commit comments

Comments
 (0)