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(plugin-redirect): add redirectPlugin #323

Closed
wants to merge 1 commit into from
Closed
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
76 changes: 76 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@stackflow/plugin-basic-ui": "^0.20.2",
"@stackflow/plugin-history-sync": "^0.20.1",
"@stackflow/plugin-preload": "^0.20.1",
"@stackflow/plugin-redirect": "^0.19.0",
"@stackflow/plugin-renderer-basic": "^0.20.1",
"@stackflow/plugin-stack-depth-change": "^0.20.0",
"@stackflow/react": "^0.20.1",
Expand Down
6 changes: 6 additions & 0 deletions demo/src/stackflow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { vars } from "@seed-design/design-token";
import { basicUIPlugin } from "@stackflow/plugin-basic-ui";
import { historySyncPlugin } from "@stackflow/plugin-history-sync";
import { redirectPlugin } from "@stackflow/plugin-redirect";
import { basicRendererPlugin } from "@stackflow/plugin-renderer-basic";
import { stackflow } from "@stackflow/react";

Expand Down Expand Up @@ -38,6 +39,11 @@ export const { Stack, activities } = stackflow({
borderColor: vars.$semantic.color.divider3,
},
}),
redirectPlugin({
redirects: {
Article: ["Main"],
},
}),
historySyncPlugin({
routes: {
Main: "/",
Expand Down
4 changes: 4 additions & 0 deletions extensions/plugin-redirect/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["@stackflow/eslint-config/react"],
};
44 changes: 44 additions & 0 deletions extensions/plugin-redirect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# @stackflow/plugin-history-sync

Synchronizes the stack state with the current browser's history

- [Documentation](https://stackflow.so)

## Usage

```typescript
import { stackflow } from "@stackflow/react";
import { historySyncPlugin } from "@stackflow/plugin-history-sync";
import { MyHome } from "./MyHome";
import { MyArticle } from "./MyArticle";
import { NotFoundPage } from "./NotFoundPage";

const { Stack, useFlow } = stackflow({
activities: {
MyHome,
MyArticle,
NotFoundPage,
},
plugins: [
// ...
historySyncPlugin({
routes: {
/**
* You can link the registered activity with the URL template.
*/
MyHome: "/",
MyArticle: "/articles/:articleId",
NotFoundPage: "/404",
},
/**
* If a URL that does not correspond to the URL template is given, it moves to the `fallbackActivity`.
*/
fallbackActivity: ({ context }) => "NotFoundPage",
/**
* Uses the hash portion of the URL (i.e. window.location.hash)
*/
useHash: false,
}),
],
});
```
35 changes: 35 additions & 0 deletions extensions/plugin-redirect/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { build } = require("esbuild");
const config = require("@stackflow/esbuild-config");
const pkg = require("./package.json");

const watch = process.argv.includes("--watch");
const external = Object.keys({
...pkg.dependencies,
...pkg.peerDependencies,
});

Promise.all([
build({
...config({}),
format: "cjs",
external,
define: {
"process.env.PACKAGE_NAME": `"${pkg.name}"`,
"process.env.PACKAGE_VERSION": `"${pkg.version}"`,
},
watch,
}),
build({
...config({}),
format: "esm",
outExtension: {
".js": ".mjs",
},
external,
define: {
"process.env.PACKAGE_NAME": `"${pkg.name}"`,
"process.env.PACKAGE_VERSION": `"${pkg.version}"`,
},
watch,
}),
]).catch(() => process.exit(1));
67 changes: 67 additions & 0 deletions extensions/plugin-redirect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "@stackflow/plugin-redirect",
"version": "0.19.0",
"license": "MIT",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
}
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist",
"src",
"README.md"
],
"scripts": {
"build": "yarn build:js && yarn build:dts",
"build:dts": "tsc --emitDeclarationOnly",
"build:js": "node ./esbuild.config.js",
"clean": "rimraf dist",
"dev": "yarn build:js --watch && yarn build:dts --watch"
},
"dependencies": {
"url-pattern": "^1.0.3"
},
"devDependencies": {
"@stackflow/core": "^0.20.0",
"@stackflow/esbuild-config": "^0.20.0",
"@stackflow/eslint-config": "^0.20.0",
"@stackflow/react": "^0.20.1",
"@types/node": "^18.6.3",
"@types/react": "^18.0.10",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.20.0",
"esbuild": "^0.14.51",
"eslint": "^8.13.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-json-format": "^2.0.1",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.4.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"react": "^18.1.0",
"rimraf": "^3.0.2",
"typescript": "^4.7.4"
},
"peerDependencies": {
"@stackflow/core": "0",
"@stackflow/react": "0",
"@types/react": ">=16.8.0",
"react": ">=16.8.0"
},
"publishConfig": {
"access": "public"
},
"ultra": {
"concurrent": [
"dev",
"build"
]
}
}
1 change: 1 addition & 0 deletions extensions/plugin-redirect/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./redirectPlugin";
17 changes: 17 additions & 0 deletions extensions/plugin-redirect/src/redirectPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { StackflowReactPlugin } from "@stackflow/react";

type HistorySyncPluginOptions<K extends string> = {
redirects: {
[key in K]?: Exclude<K, key> | Exclude<K, key>[];
};
};

export function redirectPlugin<T extends { [activityName: string]: unknown }>(
options: HistorySyncPluginOptions<Extract<keyof T, string>>,
): StackflowReactPlugin<T> {
type K = Extract<keyof T, string>;

return () => ({
key: "@stackflow/plugin-redirect",
});
}
8 changes: 8 additions & 0 deletions extensions/plugin-redirect/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist"
},
"exclude": ["./dist"]
}
35 changes: 35 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,7 @@ __metadata:
"@stackflow/plugin-basic-ui": ^0.20.2
"@stackflow/plugin-history-sync": ^0.20.1
"@stackflow/plugin-preload": ^0.20.1
"@stackflow/plugin-redirect": ^0.19.0
"@stackflow/plugin-renderer-basic": ^0.20.1
"@stackflow/plugin-stack-depth-change": ^0.20.0
"@stackflow/react": ^0.20.1
Expand Down Expand Up @@ -2489,6 +2490,40 @@ __metadata:
languageName: unknown
linkType: soft

"@stackflow/plugin-redirect@^0.19.0, @stackflow/plugin-redirect@workspace:extensions/plugin-redirect":
version: 0.0.0-use.local
resolution: "@stackflow/plugin-redirect@workspace:extensions/plugin-redirect"
dependencies:
"@stackflow/core": ^0.20.0
"@stackflow/esbuild-config": ^0.20.0
"@stackflow/eslint-config": ^0.20.0
"@stackflow/react": ^0.20.1
"@types/node": ^18.6.3
"@types/react": ^18.0.10
"@typescript-eslint/eslint-plugin": ^5.32.0
"@typescript-eslint/parser": ^5.20.0
esbuild: ^0.14.51
eslint: ^8.13.0
eslint-config-airbnb: ^19.0.4
eslint-config-prettier: ^8.5.0
eslint-plugin-import: ^2.26.0
eslint-plugin-json-format: ^2.0.1
eslint-plugin-jsx-a11y: ^6.5.1
eslint-plugin-react: ^7.29.4
eslint-plugin-react-hooks: ^4.4.0
eslint-plugin-simple-import-sort: ^7.0.0
react: ^18.1.0
rimraf: ^3.0.2
typescript: ^4.7.4
url-pattern: ^1.0.3
peerDependencies:
"@stackflow/core": 0
"@stackflow/react": 0
"@types/react": ">=16.8.0"
react: ">=16.8.0"
languageName: unknown
linkType: soft

"@stackflow/plugin-renderer-basic@^0.20.1, @stackflow/plugin-renderer-basic@workspace:extensions/plugin-renderer-basic":
version: 0.0.0-use.local
resolution: "@stackflow/plugin-renderer-basic@workspace:extensions/plugin-renderer-basic"
Expand Down