Skip to content

Commit

Permalink
Dual-build for ESM and CJS
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Jun 6, 2024
1 parent c4cb837 commit 39f8139
Show file tree
Hide file tree
Showing 45 changed files with 2,315 additions and 1,160 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
5 changes: 3 additions & 2 deletions build-browser-bundle.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env -S npx tsx --
#!/usr/bin/env zx

import { build } from "esbuild"
import babelPlugin from "esbuild-plugin-babel"

await build({
entryPoints: ["./dist/index.browser.cjs"],
entryPoints: ["./dist/esm/index.browser.mjs"],
outfile: "./dist/index.browser-bundle.js",
bundle: true,
format: "iife",
globalName: "RPCWebsocket",
minify: true,
plugins: [babelPlugin()],
resolveExtensions: [".js", ".mjs"],
treeShaking: true,
})
21 changes: 21 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env zx

import { readdir, rename } from "node:fs/promises"
import { $, path } from "zx"

await $`tsc -p tsconfig.cjs.json`
await $`tsc -p tsconfig.esm.json`

const files = (
await readdir("./dist", { withFileTypes: true, recursive: true })
).filter(({ isFile }) => isFile)
await Promise.all(
files.map(async (file) =>
{
const extension = file.path.includes("/cjs") ? ".cjs" : ".mjs"
return await rename(
path.join(file.path, file.name),
path.join(file.path, file.name.replace(".js", extension))
)
})
)
12 changes: 6 additions & 6 deletions dist/index.browser.cjs → dist/cjs/index.browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const websocket_browser_cjs_1 = __importDefault(require("./lib/client/websocket.browser.cjs"));
const client_cjs_1 = __importDefault(require("./lib/client.cjs"));
class Client extends client_cjs_1.default {
constructor(address = "ws://localhost:8080", { autoconnect = true, reconnect = true, reconnect_interval = 1000, max_reconnects = 5 } = {}, generate_request_id) {
super(websocket_browser_cjs_1.default, address, {
const websocket_browser_1 = __importDefault(require("./lib/client/websocket.browser"));
const client_1 = __importDefault(require("./lib/client"));
class Client extends client_1.default {
constructor(address = "ws://localhost:8080", { autoconnect = true, reconnect = true, reconnect_interval = 1000, max_reconnects = 5, } = {}, generate_request_id) {
super(websocket_browser_1.default, address, {
autoconnect,
reconnect,
reconnect_interval,
max_reconnects
max_reconnects,
}, generate_request_id);
}
}
Expand Down
14 changes: 7 additions & 7 deletions dist/index.cjs → dist/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Server = exports.Client = void 0;
const websocket_cjs_1 = __importDefault(require("./lib/client/websocket.cjs"));
const client_cjs_1 = __importDefault(require("./lib/client.cjs"));
class Client extends client_cjs_1.default {
const websocket_1 = __importDefault(require("./lib/client/websocket"));
const client_1 = __importDefault(require("./lib/client"));
class Client extends client_1.default {
constructor(address = "ws://localhost:8080", { autoconnect = true, reconnect = true, reconnect_interval = 1000, max_reconnects = 5, ...rest_options } = {}, generate_request_id) {
super(websocket_cjs_1.default, address, {
super(websocket_1.default, address, {
autoconnect,
reconnect,
reconnect_interval,
max_reconnects,
...rest_options
...rest_options,
}, generate_request_id);
}
}
exports.Client = Client;
var server_cjs_1 = require("./lib/server.cjs");
Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return __importDefault(server_cjs_1).default; } });
var server_1 = require("./lib/server");
Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return __importDefault(server_1).default; } });
Loading

0 comments on commit 39f8139

Please sign in to comment.