Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(global-scope): Add @alwatr/global-scope package #7

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/global-scope/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Global Scope

Cross-platform substitute for globalThis that operates in both Node.js and the browser, providing accurate type definitions and additional global variables to improve debugging.

## Installation

```bash
yarn add @alwatr/global-scope
```

## Usage

### globalScope

Alternative to `globalThis` that works cross-platform.

```typescript
import {globalScope} from '@alwatr/global-scope';

globalScope.alwatr = {
...globalScope.alwatr,
version: '1.0.0',
};

globalScope.setTimeout(() => {
console.log(globalScope.alwatr.version); // 1.0.0
}, 1_000);
```

### sharedScope_

A global variable that can be used to share state across modules without accessible publicly in the global scope.

```typescript
// module1.ts
import {sharedScope_} from '@alwatr/global-scope';
sharedScope_.foo = 'bar';
```

```typescript
// module2.ts
import {sharedScope_} from '@alwatr/global-scope';
console.log(sharedScope_.foo); // 'bar'
```
78 changes: 78 additions & 0 deletions packages/global-scope/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"name": "@alwatr/global-scope",
"version": "1.0.0",
"description": "Cross-platform substitute for globalThis that operates in both Node.js and the browser, providing accurate type definitions and additional global variables to improve debugging.",
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
"keywords": [
"global",
"globalThis",
"global-polyfill",
"global-shim",
"globalThis-polyfill",
"globalThis-shim",
"cross-platform",
"ECMAScript",
"typescript",
"javascript",
"node",
"nodejs",
"browser",
"esm",
"module",
"utility",
"util",
"utils",
"nanolib",
"alwatr"
],
"type": "module",
"main": "./dist/main.cjs",
"module": "./dist/main.mjs",
"types": "./dist/main.d.ts",
"exports": {
".": {
"import": "./dist/main.mjs",
"require": "./dist/main.cjs",
"types": "./dist/main.d.ts"
}
},
"license": "MIT",
"files": [
"**/*.{js,mjs,cjs,map,d.ts,html,md}",
"!demo/**/*"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/Alwatr/nanolib",
"directory": "packages/global-scope"
},
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/global-scope#readme",
"bugs": {
"url": "https://github.com/Alwatr/nanolib/issues"
},
"prettier": "@alwatr/prettier-config",
"scripts": {
"b": "yarn run build",
"w": "yarn run watch",
"c": "yarn run clean",
"cb": "yarn run clean && yarn run build",
"d": "yarn run build:es && ALWATR_DEBUG=1 yarn node",
"build": "yarn run build:ts & yarn run build:es",
"build:es": "nano-build",
"build:ts": "tsc --build",
"watch": "yarn run watch:ts & yarn run watch:es",
"watch:es": "yarn run build:es --watch",
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
"clean": "rm -rfv dist .tsbuildinfo"
},
"devDependencies": {
"@alwatr/nano-build": "workspace:^",
"@alwatr/prettier-config": "workspace:^",
"@alwatr/tsconfig-base": "workspace:^",
"@types/node": "^20.10.5",
"typescript": "^5.3.3"
}
}
40 changes: 40 additions & 0 deletions packages/global-scope/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Alternative to `globalThis` that works cross-platform.
*
* @example
* ```typescript
* globalScope.alwatr = {
* ...globalScope.alwatr,
* foo: 'bar',
* }
* ```
*/
export const globalScope: typeof globalThis =
(typeof globalThis === 'object' && globalThis) ||
(typeof window === 'object' && window) ||
(typeof global === 'object' && global) ||
self;

/**
* A global variable that can be used to share state across modules without accessible publicly in the global scope.
*
* @example
* ```typescript
* // module1.ts
* shareScope_.foo = 'bar';
*
* // module2.ts
* console.log(shareScope_.foo); // 'bar'
* ```
*/
export const sharedScope_: Record<string, unknown> = {};

declare global {
// eslint-disable-next-line no-var
var __shared_scope_defined__: boolean;
}

if (globalScope.__shared_scope_defined__ !== undefined) {
throw new Error('global_scope_module_duplicated');
}
globalScope.__shared_scope_defined__ = true;
Empty file.
11 changes: 11 additions & 0 deletions packages/global-scope/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@alwatr/tsconfig-base/tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"emitDeclarationOnly": true,
"composite": true,
"tsBuildInfoFile": ".tsbuildinfo"
},
"include": ["src/**/*.ts"]
}
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ __metadata:
languageName: unknown
linkType: soft

"@alwatr/global-scope@workspace:packages/global-scope":
version: 0.0.0-use.local
resolution: "@alwatr/global-scope@workspace:packages/global-scope"
dependencies:
"@alwatr/nano-build": "workspace:^"
"@alwatr/prettier-config": "workspace:^"
"@alwatr/tsconfig-base": "workspace:^"
"@types/node": "npm:^20.10.5"
typescript: "npm:^5.3.3"
languageName: unknown
linkType: soft

"@alwatr/nano-build@workspace:^, @alwatr/nano-build@workspace:packages/nano-build":
version: 0.0.0-use.local
resolution: "@alwatr/nano-build@workspace:packages/nano-build"
Expand Down Expand Up @@ -1047,6 +1059,15 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^20.10.5":
version: 20.10.5
resolution: "@types/node@npm:20.10.5"
dependencies:
undici-types: "npm:~5.26.4"
checksum: be30609aae0bfe492097815f166ccc07f465220cb604647fa4e5ec05a1d16c012a41b82b5f11ecfe2485cbb479d4d20384b95b809ca0bcff6d94d5bbafa645bb
languageName: node
linkType: hard

"@types/normalize-package-data@npm:^2.4.1":
version: 2.4.3
resolution: "@types/normalize-package-data@npm:2.4.3"
Expand Down Expand Up @@ -5683,6 +5704,13 @@ __metadata:
languageName: node
linkType: hard

"undici-types@npm:~5.26.4":
version: 5.26.5
resolution: "undici-types@npm:5.26.5"
checksum: bb673d7876c2d411b6eb6c560e0c571eef4a01c1c19925175d16e3a30c4c428181fb8d7ae802a261f283e4166a0ac435e2f505743aa9e45d893f9a3df017b501
languageName: node
linkType: hard

"unicorn-magic@npm:^0.1.0":
version: 0.1.0
resolution: "unicorn-magic@npm:0.1.0"
Expand Down