Skip to content

Commit 0a0a1ca

Browse files
committed
current status
1 parent 0b3a99f commit 0a0a1ca

File tree

8 files changed

+330
-80
lines changed

8 files changed

+330
-80
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2

.eslintrc.yml

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
extends:
2+
- eslint:recommended
3+
- "plugin:json/recommended"
4+
- "plugin:@typescript-eslint/recommended"
5+
plugins:
6+
- json
7+
ignorePatterns:
8+
- node_modules
9+
root: true
10+
parser: "@typescript-eslint/parser"
11+
rules:
12+
no-await-in-loop: error
13+
no-extra-parens: warn
14+
no-template-curly-in-string: error
15+
class-methods-use-this: error
16+
complexity:
17+
- error
18+
- 5
19+
consistent-return: warn
20+
curly: error
21+
dot-notation: warn
22+
eqeqeq: error
23+
guard-for-in: error
24+
no-alert: error
25+
no-caller: error
26+
no-eval: error
27+
no-extend-native: error
28+
no-extra-bind: warn
29+
no-extra-label: warn
30+
no-invalid-this: error
31+
no-magic-numbers: warn
32+
no-multi-spaces: warn
33+
no-new-wrappers: error
34+
no-new: error
35+
no-console: warn
36+
no-proto: error
37+
no-return-await: error
38+
no-self-compare: error
39+
no-sequences: error
40+
no-throw-literal: error
41+
no-unused-expressions: warn
42+
no-useless-call: warn
43+
no-useless-return: warn
44+
radix: error
45+
require-await: error
46+
require-unicode-regexp: error
47+
wrap-iife:
48+
- error
49+
- inside
50+
no-shadow: error
51+
no-label-var: error
52+
no-undef-init: error
53+
no-undefined: error
54+
no-use-before-define: error
55+
func-call-spacing:
56+
- warn
57+
- never
58+
func-names:
59+
- error
60+
- never
61+
func-style:
62+
- warn
63+
- expression
64+
function-call-argument-newline:
65+
- error
66+
- consistent
67+
implicit-arrow-linebreak:
68+
- warn
69+
- beside
70+
indent:
71+
- warn
72+
- 2
73+
- SwitchCase: 1
74+
key-spacing: warn
75+
keyword-spacing: warn
76+
lines-between-class-members:
77+
- warn
78+
- always
79+
max-depth: warn
80+
max-len:
81+
- warn
82+
- code: 80
83+
ignorePattern: eslint
84+
max-params: warn
85+
newline-per-chained-call: warn
86+
new-parens: error
87+
no-array-constructor: error
88+
no-continue: error
89+
no-lonely-if: error
90+
no-multi-assign: error
91+
no-multiple-empty-lines:
92+
- warn
93+
- max: 1
94+
no-nested-ternary: error
95+
no-new-object: error
96+
no-tabs: warn
97+
no-trailing-spaces: warn
98+
no-unneeded-ternary: error
99+
no-whitespace-before-property: warn
100+
object-curly-newline:
101+
- warn
102+
- multiline: true
103+
minProperties: 1
104+
object-curly-spacing:
105+
- warn
106+
- always
107+
object-property-newline: warn
108+
one-var-declaration-per-line:
109+
- error
110+
- always
111+
quotes:
112+
- warn
113+
- single
114+
semi: error
115+
semi-spacing: warn
116+
semi-style:
117+
- error
118+
- last
119+
space-unary-ops:
120+
- warn
121+
- words: true
122+
nonwords: true
123+
space-before-function-paren:
124+
- warn
125+
- never
126+
space-before-blocks:
127+
- warn
128+
- always
129+
switch-colon-spacing:
130+
- warn
131+
- after: true
132+
before: false
133+
template-tag-spacing:
134+
- warn
135+
- always
136+
eol-last:
137+
- warn
138+
- always
139+
computed-property-spacing:
140+
- warn
141+
- never
142+
comma-style:
143+
- warn
144+
- last
145+
comma-spacing: warn
146+
comma-dangle:
147+
- warn
148+
- always
149+
block-spacing:
150+
- warn
151+
- always
152+
array-element-newline: warn
153+
brace-style: warn
154+
array-bracket-spacing:
155+
- warn
156+
- never
157+
- singleValue: true
158+
array-bracket-newline:
159+
- warn
160+
- minItems: 2
161+
prefer-rest-params: warn
162+
template-curly-spacing:
163+
- warn
164+
- always
165+
prefer-const: error
166+
no-var: error
167+
no-useless-constructor: warn
168+
no-useless-computed-key: warn
169+
no-confusing-arrow: warn
170+
arrow-spacing: warn
171+
arrow-body-style:
172+
- warn
173+
- as-needed
174+
parserOptions:
175+
ecmaVersion: 2018

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,51 @@ This is a small library used to teach the default react routers file based routi
44

55
## Defining a page
66

7-
A page is currently any ìndex.tsx within the src/pages folder. index.tsx in the root folder will not be processed correctly. The folder name will be taken as the page url, with two exceptions:
7+
A page is by default any ìndex.tsx within the src/pages folder. The folder name will be taken as the page url, with two exceptions:
88

99
- home will be the root
1010
- not-found will be the 404 page
1111

12-
This is not yet configurable, but will be soon.
12+
## Configuration
13+
14+
All rules can be configured in an .idrinth-react-file-based-routes.json file, options are currently:
15+
16+
```ts
17+
interface Configuration {
18+
sitemap?: {
19+
domain?: string,
20+
build?: boolean,
21+
},
22+
routes?: {
23+
build?: true,
24+
},
25+
htmlMinify?: {
26+
collapseBooleanAttributes?: boolean,
27+
conservativeCollapse?: boolean,
28+
collapseWhitespace?: boolean,
29+
removeAttributeQuotes?: boolean,
30+
removeComments?: boolean,
31+
},
32+
fileBuilder?: {
33+
buildIndex?: boolean,
34+
minifyPages?: boolean,
35+
appendPageChunk?: boolean,
36+
},
37+
fileFinder?: {
38+
fileName?: string,
39+
overridePathMappings?: {
40+
[filesystemPath: string]: string,
41+
},
42+
pagesRoot?: string,
43+
distJSRoot?: string,
44+
},
45+
}
46+
```
1347

1448
# Setup
1549

1650
You will need to do three things for the complete package:
1751

1852
- Define a call to generate-routes in you package.json as afterInstall and early in your build process to generate the routes.tsx and the sitemap
19-
- Define a call to generate-folders in your package.json as a late build step to optimise the index.html files
53+
- Optionally define a call to generate-folders in your package.json as a late build step to optimise the index.html files
2054
- Import the generated src/routes.tsx, so that you can use createBrowserRouter or similar functions to create your router

bin/generate-folders.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
22
import generate from '../src/generate-folders.js';
3+
import configuration from '../src/configuration';
34

4-
generate(process.cwd());
5+
generate(process.cwd(), configuration(process.cwd()));

bin/generate-routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
22
import generate from '../src/generate-routes.js';
3+
import configuration from '../src/configuration';
34

4-
generate(process.cwd(), process.argv[2]);
5+
generate(process.cwd(), configuration(process.cwd()));

src/configuration.ts

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,83 @@
11
import {readFileSync, existsSync} from 'fs';
22

3-
interface Configuration {
4-
sitemap?: {
5-
domain?: string,
6-
build?: boolean,
7-
},
8-
htmlMinify?: {
9-
collapseBooleanAttributes?: boolean,
10-
conservativeCollapse?: boolean,
11-
collapseWhitespace?: boolean,
12-
removeAttributeQuotes?: boolean,
13-
removeComments?: boolean,
14-
},
15-
fileBuilder?: {
16-
buildIndex?: boolean,
17-
minifyPages?: boolean,
18-
appendPageChunk?: boolean,
19-
},
20-
fileFinder?: {
21-
fileName: 'index.tsx'|'+Page.tsx'|'*.tsx'|'page.tsx',
22-
overridePathMappings: {
23-
[filesystemPath: string]: string,
24-
},
25-
pagesRoot?: string,
3+
export interface Configuration {
4+
sitemap: {
5+
domain: string,
6+
build: boolean,
7+
},
8+
routes: {
9+
build: boolean
10+
},
11+
htmlMinify: {
12+
collapseBooleanAttributes: boolean,
13+
conservativeCollapse: boolean,
14+
collapseWhitespace: boolean,
15+
removeAttributeQuotes: boolean,
16+
removeComments: boolean,
17+
},
18+
fileBuilder: {
19+
buildIndex: boolean,
20+
minifyPages: boolean,
21+
appendPageChunk: boolean,
22+
},
23+
fileFinder: {
24+
fileName: string,
25+
overridePathMappings: {
26+
[filesystemPath: string]: string,
2627
},
28+
pagesRoot: string,
29+
distJSRoot: string,
30+
},
2731
}
2832

2933
export default (cwd: string): Configuration => {
30-
const config = {};
31-
if (existsSync(cwd + '/.idrinth-react-file-based-routes.json')) {
32-
const userconfig: Configuration = JSON.parse(readFileSync(cwd + '/.idrinth-react-file-based-routes.json', 'utf-8'));
33-
if (typeof userconfig.htmlMinify === 'object') {
34-
35-
}
36-
if (typeof userconfig.htmlMinify === 'object') {
37-
34+
const config = {
35+
sitemap: {
36+
build: true,
37+
domain: '',
38+
},
39+
routes: {
40+
build: true,
41+
},
42+
htmlMinify: {
43+
collapseBooleanAttributes: true,
44+
conservativeCollapse: true,
45+
collapseWhitespace: true,
46+
removeAttributeQuotes: true,
47+
removeComments: true,
48+
},
49+
fileFinder: {
50+
fileName: 'index.tsx',
51+
overridePathMappings: {
52+
home: '/',
53+
'not-found': '*',
54+
},
55+
pagesRoot: 'src/pages',
56+
distJSRoot: 'dist/assets',
57+
},
58+
fileBuilder: {
59+
buildIndex: true,
60+
minifyPages: true,
61+
appendPageChunk: true,
62+
},
63+
};
64+
const configFile = cwd + '/.idrinth-react-file-based-routes.json';
65+
if (existsSync(configFile)) {
66+
const userconfig = JSON.parse(readFileSync(configFile, 'utf-8'));
67+
if (typeof userconfig === 'object') {
68+
for (const prop of Object.keys(config)) {
69+
if (typeof userconfig[prop] === 'object') {
70+
for (const setting of Object.keys(config[prop])) {
71+
if (typeof config[prop][setting] === typeof userconfig[prop][setting]) {
72+
config[prop][setting] = userconfig[prop][setting];
73+
}
74+
}
3875
}
76+
}
3977
}
40-
return config;
78+
}
79+
if (config.sitemap.build && !config.sitemap.domain) {
80+
throw new Error('sitemap.domain must be set if a sitemap should be build.');
81+
}
82+
return config;
4183
}

0 commit comments

Comments
 (0)