@arc-js/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +664 -0
- package/index.d.ts +10 -0
- package/index.jsx +33 -0
- package/index.min.jsx +1 -0
- package/index.min.jsx.map +1 -0
- package/minimal-config.d.ts +10 -0
- package/minimal-config.js +67 -0
- package/minimal-config.min.js +2 -0
- package/package.json +25 -0
- package/rooting.hooks.d.ts +87 -0
- package/rooting.hooks.jsx +205 -0
- package/rooting.hooks.min.jsx +1 -0
- package/rooting.hooks.min.jsx.map +1 -0
- package/routes-utils.d.ts +7 -0
- package/routes-utils.js +148 -0
- package/routes-utils.min.js +2 -0
- package/tsconfig.json +27 -0
- package/types.d.ts +31 -0
- package/types.js +1 -0
- package/types.min.js +2 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.min.jsx","sources":["index.jsx"],"names":["jsxRuntimeExports","routesUtils_js","routes","index","let","routes$1","map","path","route","element","layout","jsx","children","component","error","errorElement","homeRoute","find","a","notFoundRoute","filter","includes","indexOf","process","env","NODE_ENV"],"mappings":"OAAcA,sBAAiD,KAAxB,+BAC3BC,uBAAoB,2BACvBC,MAAiC,KAAnB,4CACgC,KAArB,sBAElC,IAAIC,MAAQ,KACRC,IAAIC,EAAWH,OAAOI,IAAI,IAAW,CACjCC,KAAMC,EAAMD,KACZE,QAASD,EAAME,OAAUV,kBAAkBW,IAAIH,EAAME,OAAQ,CAAEE,SAAUZ,kBAAkBW,IAAIH,EAAMK,UAAW,EAAE,CAAE,CAAE,EAAKb,kBAAkBW,IAAIH,EAAMK,UAAW,EAAG,EACrK,GAAML,EAAMM,MAAQ,CAChBC,aAAcf,kBAAkBW,IAAIH,EAAMM,MAAO,EAAE,CACvD,EAAI,EACP,EAAC,EACFV,IAEAY,EAAYX,EAASY,KAAK,GAAkB,MAAXC,EAAEX,IAAY,EAC/CY,EAAgBd,EAASY,KAAK,GAAkB,MAAXC,EAAEX,IAAY,EAanD,OAZAF,EAAW,CACP,GAAMW,EAAY,CAACA,GAAa,GAChC,GAAGX,EAASe,OAAO,GAAW,EAAIZ,EAAMD,MACpC,CAAC,IAAK,KAAKc,SAASb,EAAMD,IAAI,EAAE,EACpC,GAAMY,EAAgB,CAACA,GAAiB,IAC1CC,OAAO,GAAY,CAAC,CAACZ,EAAMD,MAAyC,CAAC,IAAjCC,EAAMD,KAAKe,QAAQ,OAAO,GACzB,CAAC,IAApCd,EAAMD,KAAKe,QAAQ,UAAU,CAAU,EACvCC,QAAQC,KAAKC,SAKVpB,CACX,SAESF,gBAAkB","sourceRoot":"../core"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ConfigDefinition, RouteDefinition, ConfigDatasDefinition } from './types.js';
|
|
2
|
+
import 'react';
|
|
3
|
+
|
|
4
|
+
declare const configModules: any;
|
|
5
|
+
declare function cleanPathConfig(filePath: string): string;
|
|
6
|
+
declare function filePathToConfigPath(filePath: string, isParent?: boolean): string;
|
|
7
|
+
declare function getAllConfig(): Promise<ConfigDefinition[]>;
|
|
8
|
+
declare function findConfigModuleRoute(route: RouteDefinition, configDatas: ConfigDefinition[]): ConfigDatasDefinition | undefined;
|
|
9
|
+
|
|
10
|
+
export { cleanPathConfig, configModules, filePathToConfigPath, findConfigModuleRoute, getAllConfig };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const configModules = import.meta.glob([
|
|
2
|
+
'/src/config.json',
|
|
3
|
+
'/src/modules/**/config.json'
|
|
4
|
+
]);
|
|
5
|
+
function cleanPathConfig(filePath) {
|
|
6
|
+
return filePath
|
|
7
|
+
.replace(/^\/src/, '')
|
|
8
|
+
.replace(/^\/src\/modules\//, '')
|
|
9
|
+
.replace(/^\/modules\//, '')
|
|
10
|
+
.replace(/config\.json$/, '');
|
|
11
|
+
}
|
|
12
|
+
function filePathToConfigPath(filePath, isParent = false) {
|
|
13
|
+
isParent = (typeof isParent === 'boolean') ? isParent : false;
|
|
14
|
+
let configPath = !!isParent ? filePath.replace(/config\.json$/, '') : cleanPathConfig(filePath);
|
|
15
|
+
configPath = configPath.split('/').filter((subPath) => (typeof subPath === 'string' &&
|
|
16
|
+
subPath.length > 0)).join('/');
|
|
17
|
+
return configPath.startsWith('/') ? configPath : `/${configPath}`.split('//').join('/');
|
|
18
|
+
}
|
|
19
|
+
async function getAllConfig() {
|
|
20
|
+
let configs = await Promise.all(Object.entries(configModules).map(async ([filePath, module]) => {
|
|
21
|
+
const parentTruePath = filePathToConfigPath(filePath, true).split('/').join('/');
|
|
22
|
+
const configPath = filePathToConfigPath(filePath);
|
|
23
|
+
let nameConfig = configPath.split('/').filter((subPath, indexSubPath, pathArr) => (indexSubPath > 0)).join('/');
|
|
24
|
+
nameConfig = (typeof nameConfig === 'string' &&
|
|
25
|
+
nameConfig.length > 0) ? nameConfig : undefined;
|
|
26
|
+
let config = (await module()).default;
|
|
27
|
+
config = (typeof config === 'object' &&
|
|
28
|
+
!Array.isArray(config) &&
|
|
29
|
+
Object.keys(config).length > 0) ? {
|
|
30
|
+
path: ((typeof config?.path === 'string' &&
|
|
31
|
+
config?.path.length > 0) ? config?.path : configPath),
|
|
32
|
+
name: ((typeof config?.name === 'string' &&
|
|
33
|
+
config?.name.length > 0) ? config?.name : nameConfig),
|
|
34
|
+
description: ((typeof config?.description === 'string' &&
|
|
35
|
+
config?.description.length > 0) ? config?.description : undefined),
|
|
36
|
+
author: ((typeof config?.author === 'string' &&
|
|
37
|
+
config?.author.length > 0) ? config?.author : undefined),
|
|
38
|
+
isEnabled: ((typeof config?.isEnabled === 'boolean') ? config?.isEnabled : true),
|
|
39
|
+
} : {
|
|
40
|
+
path: configPath,
|
|
41
|
+
name: nameConfig,
|
|
42
|
+
author: undefined,
|
|
43
|
+
isEnabled: true,
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
parentTruePath,
|
|
47
|
+
truePath: filePath,
|
|
48
|
+
path: configPath,
|
|
49
|
+
name: nameConfig,
|
|
50
|
+
description: undefined,
|
|
51
|
+
config,
|
|
52
|
+
};
|
|
53
|
+
}));
|
|
54
|
+
return configs;
|
|
55
|
+
}
|
|
56
|
+
function findConfigModuleRoute(route, configDatas) {
|
|
57
|
+
const res = configDatas.sort((a, b) => {
|
|
58
|
+
if (a.truePath.split('/').length > b.truePath.split('/').length)
|
|
59
|
+
return -1;
|
|
60
|
+
if (a.truePath.split('/').length < b.truePath.split('/').length)
|
|
61
|
+
return 1;
|
|
62
|
+
return 0;
|
|
63
|
+
}).find((config) => (route.truePath.indexOf(config.parentTruePath) === 0))?.config;
|
|
64
|
+
return res;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { cleanPathConfig, configModules, filePathToConfigPath, findConfigModuleRoute, getAllConfig };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
let configModules=import.meta.glob(["/src/config.json","/src/modules/**/config.json"]);function cleanPathConfig(t){return t.replace(/^\/src/,"").replace(/^\/src\/modules\//,"").replace(/^\/modules\//,"").replace(/config\.json$/,"")}function filePathToConfigPath(t,e=!1){let n=(e="boolean"==typeof e&&e)?t.replace(/config\.json$/,""):cleanPathConfig(t);return(n=n.split("/").filter(t=>"string"==typeof t&&0<t.length).join("/")).startsWith("/")?n:("/"+n).split("//").join("/")}async function getAllConfig(){return await Promise.all(Object.entries(configModules).map(async([t,e])=>{var n=filePathToConfigPath(t,!0).split("/").join("/"),o=filePathToConfigPath(t),i="string"==typeof(i=o.split("/").filter((t,e,n)=>0<e).join("/"))&&0<i.length?i:void 0,e=(await e()).default;return{parentTruePath:n,truePath:t,path:o,name:i,description:void 0,config:"object"==typeof e&&!Array.isArray(e)&&0<Object.keys(e).length?{path:"string"==typeof e?.path&&0<e?.path.length?e?.path:o,name:"string"==typeof e?.name&&0<e?.name.length?e?.name:i,description:"string"==typeof e?.description&&0<e?.description.length?e?.description:void 0,author:"string"==typeof e?.author&&0<e?.author.length?e?.author:void 0,isEnabled:"boolean"!=typeof e?.isEnabled||e?.isEnabled}:{path:o,name:i,author:void 0,isEnabled:!0}}}))}function findConfigModuleRoute(e,t){return t.sort((t,e)=>t.truePath.split("/").length>e.truePath.split("/").length?-1:t.truePath.split("/").length<e.truePath.split("/").length?1:0).find(t=>0===e.truePath.indexOf(t.parentTruePath))?.config}export{cleanPathConfig,configModules,filePathToConfigPath,findConfigModuleRoute,getAllConfig};
|
|
2
|
+
//# sourceMappingURL=minimal-config.min.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arc-js/core",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.0.1",
|
|
7
|
+
"description": "CORE est un module de routage intelligent et auto-configuré pour les applications React avec TypeScript/Javascript. Il fournit un système de routage basé sur la structure de fichiers, des hooks de navigation avancés et une configuration minimale pour les applications modulaires.",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "INICODE <contact.inicode@gmail.com>",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"init": "npm init --scope=@arc-js/core",
|
|
14
|
+
"login": "npm login"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"vite": "^7.3.0"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"react-router-dom": "^7.11.0",
|
|
21
|
+
"react": "^19.2.3",
|
|
22
|
+
"react-dom": "^19.2.3",
|
|
23
|
+
"@arc-js/qust": "^0.0.2"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Params, NavigateFunction } from 'react-router-dom';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface du paramètre de configuration de la fonction "goAndReloadRoute" .
|
|
5
|
+
*/
|
|
6
|
+
interface ConfigGoToSpecificUrl {
|
|
7
|
+
path?: string;
|
|
8
|
+
queries?: any;
|
|
9
|
+
refreshPage?: boolean;
|
|
10
|
+
replace?: boolean;
|
|
11
|
+
enableLoader?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Interface du paramètre de configuration de la fonction "goAndReloadRoute" .
|
|
15
|
+
*/
|
|
16
|
+
interface ConfigGoAndReloadRoute {
|
|
17
|
+
path?: string;
|
|
18
|
+
params?: any;
|
|
19
|
+
queries?: any;
|
|
20
|
+
enableLoader?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Interface du paramètre de configuration de la fonction "resolveRoute" .
|
|
24
|
+
*/
|
|
25
|
+
interface ConfigResolveRoute {
|
|
26
|
+
path?: string;
|
|
27
|
+
params?: any;
|
|
28
|
+
queries?: any;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Interface du paramètre de configuration de la fonction "goToRoute" .
|
|
32
|
+
*/
|
|
33
|
+
interface ConfigGoToRoute {
|
|
34
|
+
path?: string;
|
|
35
|
+
params?: any;
|
|
36
|
+
queries?: any;
|
|
37
|
+
refreshPage?: boolean;
|
|
38
|
+
replace?: boolean;
|
|
39
|
+
enableLoader?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Interface de retour du hooks "useRootingActions".
|
|
43
|
+
*/
|
|
44
|
+
interface RootingActionReturns {
|
|
45
|
+
params: Readonly<Params<string>>;
|
|
46
|
+
queries: any;
|
|
47
|
+
getParams: () => URLSearchParams;
|
|
48
|
+
navigate: NavigateFunction;
|
|
49
|
+
resolveRoute: (config: ConfigResolveRoute) => string;
|
|
50
|
+
goToRoute: (config: ConfigGoToRoute) => void;
|
|
51
|
+
goAndReloadRoute: (config: ConfigGoAndReloadRoute) => void;
|
|
52
|
+
pathName: string;
|
|
53
|
+
urlSearch: string;
|
|
54
|
+
getUrlData: (url: string | URL) => {
|
|
55
|
+
host: string;
|
|
56
|
+
hostname: string;
|
|
57
|
+
pathname: string;
|
|
58
|
+
search: string;
|
|
59
|
+
queries: any;
|
|
60
|
+
} | undefined;
|
|
61
|
+
goToUrl: (config: ConfigGoToSpecificUrl) => string;
|
|
62
|
+
checkIfIsCurrentRoute: (path?: string, exact?: boolean, strict?: boolean) => boolean;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Cette fonction permet de recuperer une route en fonction de son path, ses paramètres et ses query parameters.
|
|
66
|
+
* @param config - la donnée de la route à ressortir
|
|
67
|
+
* @param incorrectUrl - la route qui sera prise en compte si le path n'existe pas
|
|
68
|
+
* @returns string
|
|
69
|
+
*/
|
|
70
|
+
declare function nativeResolveRoute(config: ConfigResolveRoute, incorrectUrl?: string): string;
|
|
71
|
+
/**
|
|
72
|
+
* Ce hooks contient toutes les actions necessaires pour le routing.
|
|
73
|
+
* - recuperer les paramètres de la route
|
|
74
|
+
* - Recuperer les query parameters de la route
|
|
75
|
+
* - naviguer au travers des routes
|
|
76
|
+
* - recuperer une route en fonction de son path, ses paramètres et ses query parameters
|
|
77
|
+
* - naviguer vers une route en fonction de son path, ses paramètres et ses query parameters
|
|
78
|
+
* - naviguer vers une route en fonction de son path, ses paramètres et ses query parameters en rafraîchissant le navigateur
|
|
79
|
+
* - recuperer l'url de la route courante
|
|
80
|
+
* - recuperer la chaîne de caractères contenant les query parameters
|
|
81
|
+
* @type {() => RootingActionReturns}
|
|
82
|
+
* @returns {RootingActionReturns} Resultat du hooks.
|
|
83
|
+
*/
|
|
84
|
+
declare const useRootingActions: () => RootingActionReturns;
|
|
85
|
+
|
|
86
|
+
export { nativeResolveRoute, useRootingActions };
|
|
87
|
+
export type { ConfigGoAndReloadRoute, ConfigGoToRoute, ConfigGoToSpecificUrl, ConfigResolveRoute, RootingActionReturns };
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { r as reactExports } from './_virtual/index';
|
|
2
|
+
import { d as distExports } from './_virtual/index2';
|
|
3
|
+
import { getLang } from './src/utils';
|
|
4
|
+
import qust from './node_modules/@arc-js/qust/index';
|
|
5
|
+
|
|
6
|
+
function getParams() {
|
|
7
|
+
const queryString = window.location.search;
|
|
8
|
+
const urlParams = new URLSearchParams(queryString);
|
|
9
|
+
return urlParams;
|
|
10
|
+
}
|
|
11
|
+
function nativeResolveRoute(config, incorrectUrl = "") {
|
|
12
|
+
try {
|
|
13
|
+
const params = (typeof config?.params === 'object' &&
|
|
14
|
+
Array.isArray(config?.params) === false) ? config?.params : {};
|
|
15
|
+
const queries = (typeof config?.queries === 'object' &&
|
|
16
|
+
Array.isArray(config?.queries) === false) ? config?.queries : {};
|
|
17
|
+
const path = (typeof config?.path === 'string') ? config?.path : incorrectUrl;
|
|
18
|
+
if (!config?.queries) {
|
|
19
|
+
config.queries = {};
|
|
20
|
+
}
|
|
21
|
+
let lang = getLang(config?.queries?.lang);
|
|
22
|
+
if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
|
|
23
|
+
lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
|
|
24
|
+
}
|
|
25
|
+
config.queries = {
|
|
26
|
+
...config.queries,
|
|
27
|
+
lang,
|
|
28
|
+
};
|
|
29
|
+
let res = distExports.generatePath(path, params);
|
|
30
|
+
if (Object.keys(queries).length > 0) {
|
|
31
|
+
res = `${res}?${distExports.createSearchParams(queries)}`;
|
|
32
|
+
}
|
|
33
|
+
return res;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (process.env?.NODE_ENV === 'development') {
|
|
37
|
+
console.log(error);
|
|
38
|
+
}
|
|
39
|
+
return incorrectUrl;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const useRootingActions = () => {
|
|
43
|
+
const location = distExports.useLocation();
|
|
44
|
+
const pathname = location.pathname;
|
|
45
|
+
const params = distExports.useParams();
|
|
46
|
+
const pathName = distExports.useLocation().pathname;
|
|
47
|
+
const urlSearch = distExports.useLocation().search;
|
|
48
|
+
const paramsData = new URLSearchParams(distExports.useLocation().search);
|
|
49
|
+
let queries = {};
|
|
50
|
+
for (const key of paramsData.keys()) {
|
|
51
|
+
const valueParamsData = (paramsData.getAll(key).length === 1) ? paramsData.getAll(key)[0] : paramsData.getAll(key);
|
|
52
|
+
queries = {
|
|
53
|
+
...queries,
|
|
54
|
+
[key]: valueParamsData,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const navigate = distExports.useNavigate();
|
|
58
|
+
const resolveRoute = (config) => {
|
|
59
|
+
const incorrectUrl = pathName;
|
|
60
|
+
return nativeResolveRoute(config, incorrectUrl);
|
|
61
|
+
};
|
|
62
|
+
const goToRoute = (config, loaderHandler = () => { }) => {
|
|
63
|
+
loaderHandler = !!loaderHandler ? loaderHandler : () => { };
|
|
64
|
+
if (!config?.queries) {
|
|
65
|
+
config.queries = {};
|
|
66
|
+
}
|
|
67
|
+
let lang = getLang(config?.queries?.lang);
|
|
68
|
+
if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
|
|
69
|
+
lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
|
|
70
|
+
}
|
|
71
|
+
config.queries = {
|
|
72
|
+
...config.queries,
|
|
73
|
+
lang,
|
|
74
|
+
};
|
|
75
|
+
config.queries = Object.fromEntries(Object.entries(config.queries || {}).filter(([key, value]) => value !== undefined && value !== null));
|
|
76
|
+
config.params = Object.fromEntries(Object.entries(config.params || {}).filter(([key, value]) => value !== undefined && value !== null));
|
|
77
|
+
if (typeof !!config?.enableLoader === 'undefined' &&
|
|
78
|
+
(typeof !!config?.enableLoader === 'boolean' &&
|
|
79
|
+
!!config?.enableLoader)) {
|
|
80
|
+
loaderHandler();
|
|
81
|
+
}
|
|
82
|
+
const refreshPage = (typeof config?.refreshPage === 'boolean') ? config?.refreshPage : false;
|
|
83
|
+
const pathF = resolveRoute({
|
|
84
|
+
path: config?.path,
|
|
85
|
+
params: config?.params,
|
|
86
|
+
queries: config?.queries,
|
|
87
|
+
});
|
|
88
|
+
if (!!refreshPage) {
|
|
89
|
+
window.location = pathF;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
navigate(pathF, {
|
|
93
|
+
replace: config?.replace,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const goAndReloadRoute = (config, loaderHandler = () => { }) => {
|
|
98
|
+
loaderHandler = !!loaderHandler ? loaderHandler : () => { };
|
|
99
|
+
if (!config?.queries) {
|
|
100
|
+
config.queries = {};
|
|
101
|
+
}
|
|
102
|
+
let lang = getLang(config?.queries?.lang);
|
|
103
|
+
if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
|
|
104
|
+
lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
|
|
105
|
+
}
|
|
106
|
+
config.queries = {
|
|
107
|
+
...config.queries,
|
|
108
|
+
lang,
|
|
109
|
+
};
|
|
110
|
+
config.queries = Object.fromEntries(Object.entries(config.queries || {}).filter(([key, value]) => value !== undefined && value !== null));
|
|
111
|
+
config.params = Object.fromEntries(Object.entries(config.params || {}).filter(([key, value]) => value !== undefined && value !== null));
|
|
112
|
+
const url = resolveRoute(config);
|
|
113
|
+
window.location = url;
|
|
114
|
+
if (typeof !!config?.enableLoader === 'undefined' &&
|
|
115
|
+
(typeof !!config?.enableLoader === 'boolean' &&
|
|
116
|
+
!!config?.enableLoader)) {
|
|
117
|
+
loaderHandler();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const getUrlData = (url) => {
|
|
121
|
+
let urlF = undefined;
|
|
122
|
+
if (typeof url === 'string' &&
|
|
123
|
+
url.length > 0) {
|
|
124
|
+
urlF = new URL(url);
|
|
125
|
+
}
|
|
126
|
+
else if (url instanceof URL) {
|
|
127
|
+
urlF = url;
|
|
128
|
+
}
|
|
129
|
+
const paramsData = urlF?.search ? new URLSearchParams(urlF?.search) : undefined;
|
|
130
|
+
let queries = undefined;
|
|
131
|
+
if (!!paramsData) {
|
|
132
|
+
queries = {};
|
|
133
|
+
for (const key of paramsData.keys()) {
|
|
134
|
+
const valueParamsData = (paramsData.getAll(key).length === 1) ? paramsData.getAll(key)[0] : paramsData.getAll(key);
|
|
135
|
+
queries = {
|
|
136
|
+
...queries,
|
|
137
|
+
[key]: valueParamsData,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return urlF ? {
|
|
142
|
+
host: urlF?.host,
|
|
143
|
+
hostname: urlF?.hostname,
|
|
144
|
+
pathname: urlF?.pathname,
|
|
145
|
+
search: urlF?.search,
|
|
146
|
+
queries,
|
|
147
|
+
} : undefined;
|
|
148
|
+
};
|
|
149
|
+
const goToUrl = (config) => {
|
|
150
|
+
const incorrectUrl = "";
|
|
151
|
+
const queries = (typeof config?.queries === 'object' &&
|
|
152
|
+
Array.isArray(config?.queries) === false) ? config?.queries : {};
|
|
153
|
+
const path = (typeof config?.path === 'string') ? config?.path : incorrectUrl;
|
|
154
|
+
if (!config?.queries) {
|
|
155
|
+
config.queries = {};
|
|
156
|
+
}
|
|
157
|
+
let lang = getLang(config?.queries?.lang);
|
|
158
|
+
if (!!config?.queries?.lang && config?.queries?.lang != 'fr') {
|
|
159
|
+
lang = getLang(config?.queries?.lang || getParams().get('lang') || 'fr');
|
|
160
|
+
}
|
|
161
|
+
config.queries = {
|
|
162
|
+
...config.queries,
|
|
163
|
+
lang,
|
|
164
|
+
};
|
|
165
|
+
const uncleanUrl = qust.stringify(queries);
|
|
166
|
+
const qsValue = decodeURI(uncleanUrl);
|
|
167
|
+
const url = `${path}?${qsValue}`;
|
|
168
|
+
if (!!config?.refreshPage) {
|
|
169
|
+
window.location = url;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
window.location.assign(url);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const checkIfIsCurrentRoute = (path, exact = true, strict = true) => {
|
|
176
|
+
const pathF = (typeof path === 'string' &&
|
|
177
|
+
path.length > 0) ? path : '';
|
|
178
|
+
const exactF = (typeof exact === 'boolean') ? exact : true;
|
|
179
|
+
const strictF = (typeof strict === 'boolean') ? strict : true;
|
|
180
|
+
const matchAbout = distExports.matchPath({
|
|
181
|
+
path: pathF,
|
|
182
|
+
end: exactF,
|
|
183
|
+
caseSensitive: strictF,
|
|
184
|
+
}, pathname);
|
|
185
|
+
return !!matchAbout;
|
|
186
|
+
};
|
|
187
|
+
reactExports.useEffect(() => {
|
|
188
|
+
}, []);
|
|
189
|
+
return {
|
|
190
|
+
params,
|
|
191
|
+
queries,
|
|
192
|
+
navigate,
|
|
193
|
+
resolveRoute,
|
|
194
|
+
goToRoute,
|
|
195
|
+
goAndReloadRoute,
|
|
196
|
+
pathName,
|
|
197
|
+
urlSearch,
|
|
198
|
+
getUrlData,
|
|
199
|
+
goToUrl,
|
|
200
|
+
getParams,
|
|
201
|
+
checkIfIsCurrentRoute,
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export { nativeResolveRoute, useRootingActions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{r as reactExports}from"./_virtual/index";import{d as distExports}from"./_virtual/index2";import{getLang}from"./src/utils";import qust from"./node_modules/@arc-js/qust/index";function getParams(){var e=window.location.search;return new URLSearchParams(e)}function nativeResolveRoute(a,t=""){try{var s="object"==typeof a?.params&&!1===Array.isArray(a?.params)?a?.params:{},i="object"==typeof a?.queries&&!1===Array.isArray(a?.queries)?a?.queries:{},o="string"==typeof a?.path?a?.path:t;a?.queries||(a.queries={});let e=getLang(a?.queries?.lang),r=(a?.queries?.lang&&"fr"!=a?.queries?.lang&&(e=getLang(a?.queries?.lang||getParams().get("lang")||"fr")),a.queries={...a.queries,lang:e},distExports.generatePath(o,s));return r=0<Object.keys(i).length?r+"?"+distExports.createSearchParams(i):r}catch(e){return process.env?.NODE_ENV,t}}let useRootingActions=()=>{let t=distExports.useLocation().pathname;var e=distExports.useParams();let r=distExports.useLocation().pathname;var a,s=distExports.useLocation().search,i=new URLSearchParams(distExports.useLocation().search);let o={};for(a of i.keys()){var n=1===i.getAll(a).length?i.getAll(a)[0]:i.getAll(a);o={...o,[a]:n}}let l=distExports.useNavigate(),u=e=>nativeResolveRoute(e,r);return reactExports.useEffect(()=>{},[]),{params:e,queries:o,navigate:l,resolveRoute:u,goToRoute:(e,r=()=>{})=>{r=r||(()=>{}),e?.queries||(e.queries={});let a=getLang(e?.queries?.lang);e?.queries?.lang&&"fr"!=e?.queries?.lang&&(a=getLang(e?.queries?.lang||getParams().get("lang")||"fr")),e.queries={...e.queries,lang:a},e.queries=Object.fromEntries(Object.entries(e.queries||{}).filter(([,e])=>null!=e)),e.params=Object.fromEntries(Object.entries(e.params||{}).filter(([,e])=>null!=e)),void 0===!!e?.enableLoader&&"boolean"==typeof!!e?.enableLoader&&e?.enableLoader&&r();var r="boolean"==typeof e?.refreshPage&&e?.refreshPage,t=u({path:e?.path,params:e?.params,queries:e?.queries});r?window.location=t:l(t,{replace:e?.replace})},goAndReloadRoute:(e,r=()=>{})=>{r=r||(()=>{}),e?.queries||(e.queries={});let a=getLang(e?.queries?.lang);e?.queries?.lang&&"fr"!=e?.queries?.lang&&(a=getLang(e?.queries?.lang||getParams().get("lang")||"fr")),e.queries={...e.queries,lang:a},e.queries=Object.fromEntries(Object.entries(e.queries||{}).filter(([,e])=>null!=e)),e.params=Object.fromEntries(Object.entries(e.params||{}).filter(([,e])=>null!=e));var t=u(e);window.location=t,void 0===!!e?.enableLoader&&"boolean"==typeof!!e?.enableLoader&&e?.enableLoader&&r()},pathName:r,urlSearch:s,getUrlData:e=>{let r=void 0;"string"==typeof e&&0<e.length?r=new URL(e):e instanceof URL&&(r=e);var a=r?.search?new URLSearchParams(r?.search):void 0;let t=void 0;if(a){t={};for(var s of a.keys()){var i=1===a.getAll(s).length?a.getAll(s)[0]:a.getAll(s);t={...t,[s]:i}}}return r?{host:r?.host,hostname:r?.hostname,pathname:r?.pathname,search:r?.search,queries:t}:void 0},goToUrl:e=>{var r="object"==typeof e?.queries&&!1===Array.isArray(e?.queries)?e?.queries:{},a="string"==typeof e?.path?e?.path:"";e?.queries||(e.queries={});let t=getLang(e?.queries?.lang);e?.queries?.lang&&"fr"!=e?.queries?.lang&&(t=getLang(e?.queries?.lang||getParams().get("lang")||"fr")),e.queries={...e.queries,lang:t};r=qust.stringify(r),a=a+"?"+decodeURI(r);e?.refreshPage?window.location=a:window.location.assign(a)},getParams:getParams,checkIfIsCurrentRoute:(e,r=!0,a=!0)=>{e="string"==typeof e&&0<e.length?e:"";return!!distExports.matchPath({path:e,end:"boolean"!=typeof r||r,caseSensitive:"boolean"!=typeof a||a},t)}}};export{nativeResolveRoute,useRootingActions};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rooting.hooks.min.jsx","sources":["rooting.hooks.jsx"],"names":["reactExports","distExports","getLang","qust","getParams","queryString","window","location","search","URLSearchParams","nativeResolveRoute","config","incorrectUrl","params","Array","isArray","queries","path","let","lang","res","get","generatePath","Object","keys","length","createSearchParams","error","process","env","NODE_ENV","useRootingActions","pathname","useLocation","useParams","pathName","key","urlSearch","paramsData","valueParamsData","getAll","navigate","useNavigate","resolveRoute","useEffect","goToRoute","loaderHandler","fromEntries","entries","filter","value","enableLoader","refreshPage","pathF","replace","goAndReloadRoute","url","getUrlData","urlF","undefined","URL","host","hostname","goToUrl","uncleanUrl","stringify","decodeURI","assign","checkIfIsCurrentRoute","exact","strict","matchPath","end","caseSensitive"],"mappings":"OAAcA,iBAAsC,KAAlB,0BACpBC,gBAAsC,KAAnB,2BACxBC,OAA4B,KAAb,qBACjBC,SAAU,oCAEjB,SAASC,YACL,IAAMC,EAAcC,OAAOC,SAASC,OAEpC,OADkB,IAAIC,gBAAgBJ,CAAW,CAErD,CACA,SAASK,mBAAmBC,EAAQC,EAAe,IAC/C,IACI,IAAMC,EAAoC,UAA1B,OAAOF,GAAQE,QACO,CAAA,IAAlCC,MAAMC,QAAQJ,GAAQE,MAAM,EAAeF,GAAQE,OAAS,GAC1DG,EAAsC,UAA3B,OAAOL,GAAQK,SACO,CAAA,IAAnCF,MAAMC,QAAQJ,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,QAAAA,CACJ,EAAI2C,KAAAA,CACR,EAmDII,QAlDY,IACZ,IACM/C,EAAsC,UAA3B,OAAOL,GAAQK,SACO,CAAA,IAAnCF,MAAMC,QAAQJ,GAAQK,OAAO,EAAeL,GAAQK,QAAU,GAC5DC,EAAgC,UAAxB,OAAON,GAAQM,KAAqBN,GAAQM,KAHrC,GAIhBN,GAAQK,UACTL,EAAOK,QAAU,IAErBE,IAAIC,EAAOjB,QAAQS,GAAQK,SAASG,IAAI,EAClCR,GAAQK,SAASG,MAAiC,MAAzBR,GAAQK,SAASG,OAC5CA,EAAOjB,QAAQS,GAAQK,SAASG,MAAQf,UAAU,EAAEiB,IAAI,MAAM,GAAK,IAAI,GAE3EV,EAAOK,QAAU,CACb,GAAGL,EAAOK,QACVG,KAAAA,CACJ,EACM6C,EAAa7D,KAAK8D,UAAUjD,CAAO,EAEnCwC,EAASvC,EAAH,IADIiD,UAAUF,CAAU,EAE9BrD,GAAQyC,YACV9C,OAAOC,SAAWiD,EAGlBlD,OAAOC,SAAS4D,OAAOX,CAAG,CAElC,EA0BIpD,UAAAA,UACAgE,sBA1B0B,CAACnD,EAAMoD,EAAQ,CAAA,EAAMC,EAAS,CAAA,KAClDjB,EAAyB,UAAhB,OAAOpC,GACJ,EAAdA,EAAKQ,OAAcR,EAAO,GAQ9B,MAAO,CAAC,CALWhB,YAAYsE,UAAU,CACrCtD,KAAMoC,EACNmB,IAJ6B,WAAjB,OAAOH,GAAuBA,EAK1CI,cAJ+B,WAAlB,OAAOH,GAAwBA,CAKhD,EAAGtC,CAAQ,CAEf,CAgBA,CACJ,SAEStB,mBAAoBqB,iBAAmB","sourceRoot":"../core"}
|
package/routes-utils.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { getAllConfig, findConfigModuleRoute } from './minimal-config.js';
|
|
2
|
+
|
|
3
|
+
const errorPageName = '_error';
|
|
4
|
+
const defaultErrorPath = '/src/pages/_error';
|
|
5
|
+
const notFoundPageName = '_404';
|
|
6
|
+
const notFoundPagePath = '*';
|
|
7
|
+
const defaultLayoutPath = '/src/pages/_layout';
|
|
8
|
+
const layoutName = '_layout';
|
|
9
|
+
const indexPageName = 'index';
|
|
10
|
+
const indexPageRule = /\/index$/;
|
|
11
|
+
const indexPagePath = '/';
|
|
12
|
+
const pageModules = import.meta.glob([
|
|
13
|
+
'/src/pages/**/*.tsx',
|
|
14
|
+
'/src/modules/**/pages/**/*.tsx'
|
|
15
|
+
]);
|
|
16
|
+
function cleanPathPage(filePath) {
|
|
17
|
+
return filePath
|
|
18
|
+
.replace(/^\/src\/pages\//, '')
|
|
19
|
+
.replace(/^\/src\/modules\//, '')
|
|
20
|
+
.replace(/\/pages\//, '/')
|
|
21
|
+
.replace(/\.tsx$/, '')
|
|
22
|
+
.replace(/\{([^\]]+)\}/g, ':$1');
|
|
23
|
+
}
|
|
24
|
+
function filePathToRoutePath(filePath) {
|
|
25
|
+
let routePath = cleanPathPage(filePath);
|
|
26
|
+
if (routePath.endsWith(`/${indexPageName}`)) {
|
|
27
|
+
routePath = routePath.replace(indexPageRule, '') || indexPagePath;
|
|
28
|
+
}
|
|
29
|
+
else if (routePath === `${indexPageName}`) {
|
|
30
|
+
routePath = indexPagePath;
|
|
31
|
+
}
|
|
32
|
+
else if (routePath === notFoundPageName) {
|
|
33
|
+
routePath = notFoundPagePath;
|
|
34
|
+
return routePath;
|
|
35
|
+
}
|
|
36
|
+
return routePath.startsWith(indexPagePath) ? routePath : `/${routePath}`;
|
|
37
|
+
}
|
|
38
|
+
async function findLayoutForPage(filePath) {
|
|
39
|
+
const layoutPath = filePath.replace(/\/pages\/.*\.tsx$/, `/pages/${layoutName}.tsx`);
|
|
40
|
+
const layoutModules = import.meta.glob('/src/modules/**/_layout.tsx', { eager: true });
|
|
41
|
+
const defaultLayoutModules = import.meta.glob('/src/pages/_layout.tsx', { eager: true });
|
|
42
|
+
return layoutModules[layoutPath]?.default || defaultLayoutModules[`${defaultLayoutPath}.tsx`]?.default;
|
|
43
|
+
}
|
|
44
|
+
async function findErrorForPage(filePath) {
|
|
45
|
+
const errorPath = filePath.replace(/\/pages\/.*\.tsx$/, `/pages/${errorPageName}.tsx`);
|
|
46
|
+
const errorModules = import.meta.glob("/src/modules/**/_error.tsx", { eager: true });
|
|
47
|
+
const defaultErrorModules = import.meta.glob("/src/pages/_error.tsx", { eager: true });
|
|
48
|
+
if (process.env?.NODE_ENV === 'debug') {
|
|
49
|
+
console.log(`[src -> @arc -> core -> rooter -> routes-utils] findErrorForPage | datas:: `, JSON.stringify({
|
|
50
|
+
filePath,
|
|
51
|
+
errorPath,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
return errorModules[errorPath]?.default || defaultErrorModules[`${defaultErrorPath}.tsx`]?.default;
|
|
55
|
+
}
|
|
56
|
+
async function findErrorForPageLink(filePath) {
|
|
57
|
+
const errorPath = filePath.replace(/\/pages\/.*\.tsx$/, `/pages/${errorPageName}.tsx`);
|
|
58
|
+
return cleanPathPage(errorPath).split('/src').join('');
|
|
59
|
+
}
|
|
60
|
+
function nearestRoute(targetRoute, routes) {
|
|
61
|
+
const possibleLayouts = routes.map((route) => ({
|
|
62
|
+
...route,
|
|
63
|
+
path: (route.path || '').split('/').filter((partRoute, indexPartRoute, arrayPartRoute) => ((arrayPartRoute.length >= 2 &&
|
|
64
|
+
typeof arrayPartRoute?.[arrayPartRoute.length - 2] === 'string' &&
|
|
65
|
+
arrayPartRoute?.[arrayPartRoute.length - 2].length <= 0 &&
|
|
66
|
+
indexPartRoute < (arrayPartRoute.length - 2)) ||
|
|
67
|
+
(!(arrayPartRoute.length >= 2 &&
|
|
68
|
+
typeof arrayPartRoute?.[arrayPartRoute.length - 2] === 'string' &&
|
|
69
|
+
arrayPartRoute?.[arrayPartRoute.length - 2].length <= 0) &&
|
|
70
|
+
indexPartRoute < (arrayPartRoute.length - 1)))).join('/'),
|
|
71
|
+
})).filter((route) => targetRoute.path.indexOf(route.path) === 0).sort((a, b) => {
|
|
72
|
+
if (a.path.indexOf(targetRoute.path) > b.path.indexOf(targetRoute.path))
|
|
73
|
+
return -1;
|
|
74
|
+
if (a.path.indexOf(targetRoute.path) < b.path.indexOf(targetRoute.path))
|
|
75
|
+
return 1;
|
|
76
|
+
if (a.path.split('/').length > b.path.split('/').length)
|
|
77
|
+
return -1;
|
|
78
|
+
if (a.path.split('/').length < b.path.split('/').length)
|
|
79
|
+
return 1;
|
|
80
|
+
return 0;
|
|
81
|
+
});
|
|
82
|
+
return possibleLayouts?.[0];
|
|
83
|
+
}
|
|
84
|
+
async function getRoutes() {
|
|
85
|
+
const configs = await getAllConfig();
|
|
86
|
+
if (process.env?.NODE_ENV === 'development') {
|
|
87
|
+
console.log(`[src -> @arc -> core -> rooter -> routes-utils] getRoutes | configs:: `, configs);
|
|
88
|
+
}
|
|
89
|
+
let routes = await Promise.all(Object.entries(pageModules).map(async ([filePath, module]) => {
|
|
90
|
+
let layout = (!([
|
|
91
|
+
notFoundPageName
|
|
92
|
+
].map((path) => (cleanPathPage(path))).includes(cleanPathPage(filePath)))) ? await findLayoutForPage(filePath) : undefined;
|
|
93
|
+
let erroPage = await findErrorForPage(filePath);
|
|
94
|
+
await findErrorForPageLink(filePath);
|
|
95
|
+
const routePath = filePathToRoutePath(filePath);
|
|
96
|
+
const component = (await module()).default;
|
|
97
|
+
const routePathParent = routePath.split('/').filter((val, index, arr) => (typeof val === 'string' &&
|
|
98
|
+
val.length > 0 &&
|
|
99
|
+
index < (arr.length - 1))).join('/');
|
|
100
|
+
return {
|
|
101
|
+
truePath: filePath,
|
|
102
|
+
path: routePath,
|
|
103
|
+
component,
|
|
104
|
+
layout,
|
|
105
|
+
error: erroPage,
|
|
106
|
+
pathParent: routePathParent,
|
|
107
|
+
};
|
|
108
|
+
}));
|
|
109
|
+
const notFoundRoutes = routes.filter((route) => (!!route.truePath &&
|
|
110
|
+
route.truePath.split('/').filter((partRoute) => partRoute.indexOf('_404.tsx') !== -1).length > 0)).map((route) => ({
|
|
111
|
+
...route,
|
|
112
|
+
path: route.path.split('/').map((partRoute) => partRoute.split('_404').join(notFoundPagePath)).join('/'),
|
|
113
|
+
}));
|
|
114
|
+
routes = [
|
|
115
|
+
...routes.filter((route) => !(!!route.truePath &&
|
|
116
|
+
route.truePath.split('/').filter((partRoute) => partRoute.indexOf('_404.tsx') !== -1).length > 0)),
|
|
117
|
+
...notFoundRoutes,
|
|
118
|
+
];
|
|
119
|
+
const layoutRoutes = routes.filter((route) => (!!route.path && (route.path.indexOf('/_layout') !== -1)));
|
|
120
|
+
const errorRoutes = routes.filter((route) => (!!route.path && (route.path.indexOf('/_error') !== -1)));
|
|
121
|
+
if (process.env?.NODE_ENV === 'development') {
|
|
122
|
+
console.log(`[src -> @arc -> core -> rooter -> routes-utils] getRoutes | notFoundRoutes:: `, notFoundRoutes);
|
|
123
|
+
console.log(`[src -> @arc -> core -> rooter -> routes-utils] getRoutes | routes:: `, routes);
|
|
124
|
+
console.log(`[src -> @arc -> core -> rooter -> routes-utils] getRoutes | layoutRoutes:: `, layoutRoutes);
|
|
125
|
+
}
|
|
126
|
+
routes = routes.map((route) => ({
|
|
127
|
+
...route,
|
|
128
|
+
layout: nearestRoute(route, layoutRoutes)?.component || route.layout,
|
|
129
|
+
error: nearestRoute(route, errorRoutes)?.component || route.error,
|
|
130
|
+
}));
|
|
131
|
+
const routesF = routes.map((route, indexRoute) => {
|
|
132
|
+
const routeConfig = findConfigModuleRoute(route, configs);
|
|
133
|
+
if (typeof routeConfig?.path === 'string' &&
|
|
134
|
+
routeConfig?.path.length > 0 &&
|
|
135
|
+
routeConfig?.path != '/') {
|
|
136
|
+
route.path = route.path.split('/').filter((subPath, indexSubPath, path) => (indexSubPath > 1)).join('/');
|
|
137
|
+
route.path = `${routeConfig?.path}/${route.path}`.split('//').join('/');
|
|
138
|
+
}
|
|
139
|
+
return route;
|
|
140
|
+
});
|
|
141
|
+
if (process.env?.NODE_ENV === 'development') {
|
|
142
|
+
console.log(`[src -> @arc -> core -> rooter -> routes-utils] getRoutes | routesF:: `, routesF);
|
|
143
|
+
}
|
|
144
|
+
return routesF;
|
|
145
|
+
}
|
|
146
|
+
const routes = await getRoutes();
|
|
147
|
+
|
|
148
|
+
export { getRoutes, routes };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{getAllConfig,findConfigModuleRoute}from"./minimal-config.js";let errorPageName="_error",defaultErrorPath="/src/pages/_error",notFoundPageName="_404",notFoundPagePath="*",defaultLayoutPath="/src/pages/_layout",layoutName="_layout",indexPageName="index",indexPageRule=/\/index$/,indexPagePath="/",pageModules=import.meta.glob(["/src/pages/**/*.tsx","/src/modules/**/pages/**/*.tsx"]);function cleanPathPage(e){return e.replace(/^\/src\/pages\//,"").replace(/^\/src\/modules\//,"").replace(/\/pages\//,"/").replace(/\.tsx$/,"").replace(/\{([^\]]+)\}/g,":$1")}function filePathToRoutePath(e){let t=cleanPathPage(e);if(t.endsWith("/"+indexPageName))t=t.replace(indexPageRule,"")||indexPagePath;else if(t===""+indexPageName)t=indexPagePath;else if(t===notFoundPageName)return t=notFoundPagePath;return t.startsWith(indexPagePath)?t:"/"+t}async function findLayoutForPage(e){var e=e.replace(/\/pages\/.*\.tsx$/,`/pages/${layoutName}.tsx`),t=import.meta.glob("/src/modules/**/_layout.tsx",{eager:!0}),a=import.meta.glob("/src/pages/_layout.tsx",{eager:!0});return t[e]?.default||a[defaultLayoutPath+".tsx"]?.default}async function findErrorForPage(e){var e=e.replace(/\/pages\/.*\.tsx$/,`/pages/${errorPageName}.tsx`),t=import.meta.glob("/src/modules/**/_error.tsx",{eager:!0}),a=import.meta.glob("/src/pages/_error.tsx",{eager:!0});return process.env?.NODE_ENV,t[e]?.default||a[defaultErrorPath+".tsx"]?.default}async function findErrorForPageLink(e){return cleanPathPage(e.replace(/\/pages\/.*\.tsx$/,`/pages/${errorPageName}.tsx`)).split("/src").join("")}function nearestRoute(a,e){return e.map(e=>({...e,path:(e.path||"").split("/").filter((e,t,a)=>2<=a.length&&"string"==typeof a?.[a.length-2]&&a?.[a.length-2].length<=0&&t<a.length-2||!(2<=a.length&&"string"==typeof a?.[a.length-2]&&a?.[a.length-2].length<=0)&&t<a.length-1).join("/")})).filter(e=>0===a.path.indexOf(e.path)).sort((e,t)=>e.path.indexOf(a.path)>t.path.indexOf(a.path)?-1:e.path.indexOf(a.path)<t.path.indexOf(a.path)?1:e.path.split("/").length>t.path.split("/").length?-1:e.path.split("/").length<t.path.split("/").length?1:0)?.[0]}async function getRoutes(){let r=await getAllConfig(),e=(process.env?.NODE_ENV,await Promise.all(Object.entries(pageModules).map(async([e,t])=>{var a=[notFoundPageName].map(e=>cleanPathPage(e)).includes(cleanPathPage(e))?void 0:await findLayoutForPage(e),r=await findErrorForPage(e),n=(await findErrorForPageLink(e),filePathToRoutePath(e)),t=(await t()).default,o=n.split("/").filter((e,t,a)=>"string"==typeof e&&0<e.length&&t<a.length-1).join("/");return{truePath:e,path:n,component:t,layout:a,error:r,pathParent:o}})));var t=e.filter(e=>!!e.truePath&&0<e.truePath.split("/").filter(e=>-1!==e.indexOf("_404.tsx")).length).map(e=>({...e,path:e.path.split("/").map(e=>e.split("_404").join(notFoundPagePath)).join("/")}));let a=(e=[...e.filter(e=>!(e.truePath&&0<e.truePath.split("/").filter(e=>-1!==e.indexOf("_404.tsx")).length)),...t]).filter(e=>!!e.path&&-1!==e.path.indexOf("/_layout")),n=e.filter(e=>!!e.path&&-1!==e.path.indexOf("/_error"));process.env?.NODE_ENV;t=(e=e.map(e=>({...e,layout:nearestRoute(e,a)?.component||e.layout,error:nearestRoute(e,n)?.component||e.error}))).map((e,t)=>{var a=findConfigModuleRoute(e,r);return"string"==typeof a?.path&&0<a?.path.length&&"/"!=a?.path&&(e.path=e.path.split("/").filter((e,t,a)=>1<t).join("/"),e.path=(a?.path+"/"+e.path).split("//").join("/")),e});return process.env?.NODE_ENV,t}let routes=await getRoutes();export{getRoutes,routes};
|
|
2
|
+
//# sourceMappingURL=routes-utils.min.js.map
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable", "ESNext"],
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
|
+
"allowImportingTsExtensions": true,
|
|
11
|
+
"isolatedModules": true,
|
|
12
|
+
"moduleDetection": "force",
|
|
13
|
+
"noEmit": true,
|
|
14
|
+
"jsx": "react-jsx",
|
|
15
|
+
|
|
16
|
+
"strict": true,
|
|
17
|
+
"noUnusedLocals": true,
|
|
18
|
+
"noUnusedParameters": true,
|
|
19
|
+
"noFallthroughCasesInSwitch": true,
|
|
20
|
+
"noUncheckedSideEffectImports": true
|
|
21
|
+
},
|
|
22
|
+
"include": ["src/**/*"],
|
|
23
|
+
"exclude": [
|
|
24
|
+
"node_modules",
|
|
25
|
+
"**/*.d.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|