Skip to content
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
6 changes: 6 additions & 0 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
":automergeStableNonMajor",
":automergeTypes",
":maintainLockFilesWeekly",
"helpers:pinGitHubActionDigests",
],

"enabledManagers": [
Expand Down Expand Up @@ -105,6 +106,11 @@
],
"rangeStrategy": "widen",
},
{
"groupName": "github-actions",
"groupSlug": "github-actions",
"matchManagers": ["github-actions"],
},
],
"postUpdateOptions": [
"pnpmDedupe",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"lint-staged": "^15.5.2",
"sort-package-json": "^3.2.0",
"ts-patch": "^3.3.0",
"turbo": "^2.5.2",
"turbo": "^2.5.3",
"typescript": "^5.8.3",
"typescript-eslint": "^8.32.0",
"vitest": "^3.1.3"
Expand Down
60 changes: 60 additions & 0 deletions packages/background-only/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# background-only

This package serves as a mark package to enforce and mark modules that should only be used in the Background Thread context. It provides type definitions and runtime checks to ensure proper usage.

## Example Usages

Say we have a `Logger` module that has side effects calling into Native Modules or other APIs that are only available in the "Background" environment:

```tsx
// Logger.js
import "background-only";

export function log(msg) {
// Notice how we are calling into NativeModules here.
NativeModules.hybridMonitor.reportJSError(...);
}
```

By adding `import "background-only"` to poison this module, we are now declaring that this module is only safe to be bundled in a "Background" environment, protecting this module from being accidentally bundled into a "Main thread" environment by throwing an error at runtime.

For example, if we use `log` in a desirable position from a React component, such as in `useEffect` or an event handler, the `log` will work as expected:

```tsx
// App.jsx
import { log } from "./Logger";

function App() {
useEffect() {
log();
}
return <view />
}
```

However, if we use `log` in a undesirable position from a React component, such as in the body of the rendering function, it will throw an error at runtime time:

```tsx
// App.jsx
import { log } from './Logger';

function App() {
// throw!
log();
return <view />;
}
```

## Development Note

This package is already in its final form and does not require build or test steps. The files are simple enough to be used as-is.

The package consists of three files in their final form:

- `error.js` - Throws an error when imported in the wrong context
- `empty.js` - An empty module for the default export
- `index.d.ts` - TypeScript type definitions (empty)

## Credits

This is inspired by the [`server-only`](https://www.npmjs.com/package/server-only?activeTab=readme) package of React.
3 changes: 3 additions & 0 deletions packages/background-only/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
7 changes: 7 additions & 0 deletions packages/background-only/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
throw new Error(
'This module cannot be imported from a Main Thread module. '
+ 'It should only be used from a Background Thread.',
);
4 changes: 4 additions & 0 deletions packages/background-only/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
export {};
35 changes: 35 additions & 0 deletions packages/background-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "background-only",
"version": "0.0.1",
"description": "This is a marker package to indicate that a module can only be used in the Background Thread.",
"keywords": [
"react",
"lynx"
],
"repository": {
"type": "git",
"url": "https://github.com/lynx-family/lynx-stack.git",
"directory": "packages/background-only"
},
"license": "MIT",
"type": "module",
"exports": {
".": {
"lepus": {
"types": "./index.d.ts",
"default": "./error.js"
},
"default": {
"types": "./index.d.ts",
"default": "./empty.js"
}
}
},
"main": "empty.js",
"types": "index.d.ts",
"files": [
"error.js",
"empty.js",
"index.d.ts"
]
}
12 changes: 12 additions & 0 deletions packages/background-only/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": ".",
"composite": true,
},
"include": [
"*.ts",
"*.d.ts",
],
}
2 changes: 1 addition & 1 deletion packages/rspeedy/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"typia": "9.1.1",
"typia-rspack-plugin": "2.0.1",
"vitest": "^3.1.3",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"peerDependencies": {
"typescript": "5.1.6 - 5.8.x"
Expand Down
2 changes: 1 addition & 1 deletion packages/rspeedy/create-rspeedy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"test": "vitest"
},
"dependencies": {
"create-rstack": "1.4.0"
"create-rstack": "1.4.1"
},
"devDependencies": {
"@lynx-js/qrcode-rsbuild-plugin": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion packages/rspeedy/plugin-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@lynx-js/runtime-wrapper-webpack-plugin": "workspace:*",
"@lynx-js/template-webpack-plugin": "workspace:*",
"@lynx-js/web-webpack-plugin": "workspace:*",
"background-only": "^0.0.1"
"background-only": "workspace:^"
},
"devDependencies": {
"@lynx-js/react": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/vitest-setup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"path-serializer": "^0.4.0",
"vitest": "^3.1.3",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"engines": {
"node": ">=18"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/chunk-loading-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@rspack/core": "catalog:rspack",
"css-loader": "^7.1.2",
"mini-css-extract-plugin": "^2.9.2",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"peerDependencies": {
"@rspack/core": "^1.3.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/css-extract-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@rspack/core": "catalog:rspack",
"css-loader": "^7.1.2",
"sass-loader": "^16.0.5",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"peerDependencies": {
"@lynx-js/template-webpack-plugin": "^0.5.0 || ^0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/react-refresh-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@microsoft/api-extractor": "catalog:",
"@rspack/core": "catalog:rspack",
"swc-loader": "^0.2.6",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"peerDependencies": {
"@lynx-js/react-webpack-plugin": "^0.3.0 || ^0.4.0 || ^0.5.0 || ^0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/react-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@rspack/core": "catalog:rspack",
"css-loader": "^7.1.2",
"swc-loader": "^0.2.6",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"peerDependencies": {
"@lynx-js/react": "^0.103.0 || ^0.104.0 || ^0.105.0 || ^0.106.0 || ^0.107.0 || ^0.108.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"devDependencies": {
"@lynx-js/test-tools": "workspace:*",
"@microsoft/api-extractor": "catalog:",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"engines": {
"node": ">=18"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/template-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@microsoft/api-extractor": "catalog:",
"@types/css-tree": "^2.3.10",
"@types/object.groupby": "^1.0.4",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"engines": {
"node": ">=18"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/test-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"rimraf": "^6.0.1",
"source-map": "^0.7.4",
"tiny-invariant": "^1.3.3",
"webpack": "^5.99.7",
"webpack": "^5.99.8",
"webpack-merge": "^6.0.1"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/web-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@lynx-js/template-webpack-plugin": "workspace:*",
"@lynx-js/test-tools": "workspace:*",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"peerDependencies": {
"@lynx-js/template-webpack-plugin": "^0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/webpack-dev-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"devDependencies": {
"@microsoft/api-extractor": "catalog:",
"webpack": "^5.99.7"
"webpack": "^5.99.8"
},
"engines": {
"node": ">=18"
Expand Down
Loading