Skip to content

Commit

Permalink
fix module export and improve CSR handling for non-browser environments
Browse files Browse the repository at this point in the history
  • Loading branch information
kcargile committed Sep 12, 2024
1 parent 4474517 commit 26526d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
27 changes: 13 additions & 14 deletions src/textblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,25 @@ export interface TextblockTarget {
export const Textblock = (blocks: TextblockTarget[], options?: TextblockOptions) => {
console.log(`[TB] Textblock v${TB_VERSION || "🤔"}`);

const { debounce = 200, debug = false } = options || ({} as TextblockOptions);

if (typeof window !== "undefined") {
const cancelHandles = onDocumentReady(() => {
onLoad(blocks);
return onResize(debounceCallback(() => onLoad(blocks), debounce));
});
return () => {
if (typeof cancelHandles === "function") {
cancelHandles();
}
};
} else {
if (typeof window === "undefined" || typeof document === "undefined") {
console.error(
"[Textblock] A valid DOM is required. If you're using SSR, be sure to initialize Textblock on the client."
);

return null;
}

const { debounce = 200, debug = false } = options || ({} as TextblockOptions);
const cancelHandles = onDocumentReady(() => {
onLoad(blocks);
return onResize(debounceCallback(() => onLoad(blocks), debounce));
});

return () => {
if (typeof cancelHandles === "function") {
cancelHandles();
}
};

/**
* Executes a callback function once the document is fully loaded. If the
* document is already ready, the callback is executed immediately. Otherwise,
Expand Down
8 changes: 7 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
const TerserPlugin = require("terser-webpack-plugin");
const { version } = require("./package.json");
const webpack = require("webpack");
const { type } = require("os");

module.exports = (env, argv) => {
return {
entry: "./src/textblock.ts",
mode: argv.mode || "production",
target: "web",
module: {
rules: [
{
Expand All @@ -27,7 +29,11 @@ module.exports = (env, argv) => {
},
output: {
filename: "textblock.min.js",
path: __dirname + "/dist"
path: __dirname + "/dist",
library: {
type: "umd"
},
globalObject: 'typeof self !== "undefined" ? self : globalThis'
},
plugins: [
new webpack.DefinePlugin({
Expand Down

0 comments on commit 26526d5

Please sign in to comment.