Skip to content

Commit 94e719b

Browse files
committed
开源迁移
1 parent c4727fe commit 94e719b

File tree

8 files changed

+270
-2
lines changed

8 files changed

+270
-2
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Local
2+
.DS_Store
3+
*.local
4+
*.log*
5+
6+
# Dist
7+
node_modules
8+
dist/
9+
10+
# IDE
11+
.vscode/*
12+
!.vscode/extensions.json
13+
.idea
14+
15+
pnpm-lock.yaml
16+
package-lock.json
17+
yarn.lock

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# react-security-nprogress
2-
React 安全路由器的 NProgress 行为插件(外接)
1+
# React Security NProgress
2+
3+
React 安全路由器 NProgress 插件

biome.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"vcs": {
7+
"enabled": true,
8+
"defaultBranch": "main",
9+
"clientKind": "git",
10+
"useIgnoreFile": true
11+
},
12+
"formatter": {
13+
"enabled": true,
14+
"indentStyle": "space"
15+
},
16+
"css": {
17+
"parser": {
18+
"cssModules": true
19+
}
20+
},
21+
"javascript": {
22+
"formatter": {
23+
"quoteStyle": "single",
24+
"arrowParentheses": "asNeeded",
25+
"jsxQuoteStyle": "double",
26+
"lineWidth": 160
27+
}
28+
},
29+
"linter": {
30+
"enabled": true,
31+
"rules": {
32+
"recommended": true,
33+
"suspicious": {
34+
"noDuplicateFontNames": "off",
35+
"noExplicitAny": "off"
36+
},
37+
"style": {
38+
"noUselessElse": "off"
39+
},
40+
"complexity": {
41+
"noUselessSwitchCase": "off",
42+
"noForEach": "off"
43+
}
44+
}
45+
},
46+
"files": {
47+
"ignoreUnknown": true,
48+
"ignore": [".vscode/**/*", "node_modules/**/*", "dist/**/*"]
49+
}
50+
}

package.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "@lanaqi/rsr-nprogress",
3+
"version": "0.0.1-rc.0",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"types": "./dist/index.d.ts",
8+
"import": "./dist/index.js"
9+
}
10+
},
11+
"module": "./dist/index.js",
12+
"types": "./dist/index.d.ts",
13+
"files": ["dist"],
14+
"keywords": ["react-security-router", "rsr", "nprogress"],
15+
"description": "React 安全路由器 NProgress 插件",
16+
"scripts": {
17+
"build": "rslib build",
18+
"lint": "biome lint",
19+
"check": "biome check",
20+
"format": "biome format --write",
21+
"release": "npm run build && npm publish"
22+
},
23+
"devDependencies": {
24+
"@biomejs/biome": "^1.9.4",
25+
"@lanaqi/rsr": "^0.0.1-rc.0",
26+
"@rsbuild/plugin-react": "^1.1.1",
27+
"@rslib/core": "^0.5.1",
28+
"@types/nprogress": "^0.2.3",
29+
"@types/react": "^18.3.18",
30+
"@types/react-dom": "^18.3.5",
31+
"nprogress": "^0.2.0",
32+
"react": "^18.3.1",
33+
"react-dom": "^18.3.1",
34+
"typescript": "^5.7.3"
35+
},
36+
"peerDependencies": {
37+
"@lanaqi/rsr": ">=0.0.1-rc.0",
38+
"nprogress": ">=0.2.0",
39+
"react": ">=17.0.0",
40+
"react-dom": ">=17.0.0"
41+
},
42+
"private": false,
43+
"publishConfig": {
44+
"access": "public",
45+
"registry": "https://registry.npmjs.org"
46+
},
47+
"repository": {
48+
"type": "git",
49+
"url": "https://github.com/lanaqi-opensource/react-security-nprogress.git"
50+
}
51+
}

rslib.config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { pluginReact } from '@rsbuild/plugin-react';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
source: {
6+
entry: {
7+
index: ['./src/index.ts'],
8+
},
9+
},
10+
lib: [
11+
{
12+
bundle: true,
13+
dts: true,
14+
format: 'esm',
15+
},
16+
],
17+
output: {
18+
target: 'web',
19+
},
20+
plugins: [pluginReact()],
21+
});

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* NProgress
3+
*/
4+
export * from './nprogress';

src/nprogress.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import type { AccessDecision, AccessContext, AccessManager, AccessPath, AccessResource } from '@lanaqi/rsr';
2+
import { AbstractAddon } from '@lanaqi/rsr';
3+
4+
import NProgress, { type NProgressOptions } from 'nprogress';
5+
6+
import 'nprogress/nprogress.css';
7+
8+
/**
9+
* NProgress 插件实现
10+
*/
11+
export class NProgressAddon extends AbstractAddon {
12+
/**
13+
* NProgress 实例
14+
* @private
15+
*/
16+
private readonly progress: NProgress.NProgress;
17+
18+
/**
19+
* 超时时间
20+
* @private
21+
*/
22+
private readonly timeout: number;
23+
24+
/**
25+
* 超时清理
26+
* @private
27+
*/
28+
private clear?: number;
29+
30+
/**
31+
* 构造函数
32+
* @param progress NProgress 实例
33+
* @param timeout 超时时间
34+
*/
35+
public constructor(progress: NProgress.NProgress, timeout: number) {
36+
super();
37+
this.progress = progress;
38+
this.timeout = timeout;
39+
this.clear = undefined;
40+
}
41+
42+
/**
43+
* 守护之前
44+
* @param context 上下文
45+
* @param manager 管理器
46+
* @param currentPath 当前路径
47+
* @param currentResource 当前资源
48+
* @override
49+
*/
50+
public guardBefore(context: AccessContext, manager: AccessManager, currentPath: AccessPath, currentResource: AccessResource | null): void {
51+
if (!this.progress.isStarted()) {
52+
this.progress.start();
53+
if (this.clear) {
54+
clearTimeout(this.clear);
55+
}
56+
this.clear = setTimeout(() => {
57+
if (this.progress.isStarted()) {
58+
this.progress.done();
59+
}
60+
}, this.timeout);
61+
}
62+
}
63+
64+
/**
65+
* 守护之后
66+
* @param context 上下文
67+
* @param manager 管理器
68+
* @param currentPath 当前路径
69+
* @param currentResource 当前资源
70+
* @param currentDecision 当前决策
71+
* @override
72+
*/
73+
public guardAfter(
74+
context: AccessContext,
75+
manager: AccessManager,
76+
currentPath: AccessPath,
77+
currentResource: AccessResource | null,
78+
currentDecision: AccessDecision,
79+
): void {
80+
if (this.progress.isStarted()) {
81+
this.progress.done();
82+
}
83+
if (this.clear) {
84+
clearTimeout(this.clear);
85+
this.clear = undefined;
86+
}
87+
}
88+
}
89+
90+
/**
91+
* NProgress 插件函数
92+
* @param config NProgress 可选配置
93+
* @param timeout 超时时间,默认 15 * 1000 毫秒
94+
*/
95+
export const nProgressAddon = (config?: Partial<NProgressOptions>, timeout = 15 * 1000) => {
96+
let options: Partial<NProgressOptions>;
97+
if (!config) {
98+
options = {
99+
showSpinner: false,
100+
easing: 'ease',
101+
speed: 200,
102+
trickleSpeed: 100,
103+
};
104+
} else {
105+
options = config;
106+
}
107+
const progress = NProgress.configure(options);
108+
return new NProgressAddon(progress, timeout);
109+
};

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["DOM", "ES2021"],
4+
"module": "ESNext",
5+
"jsx": "react-jsx",
6+
"strict": true,
7+
"skipLibCheck": true,
8+
"isolatedModules": true,
9+
"resolveJsonModule": true,
10+
"moduleResolution": "bundler",
11+
"useDefineForClassFields": true,
12+
"target": "ESNext"
13+
},
14+
"include": ["src"]
15+
}

0 commit comments

Comments
 (0)