Skip to content

Commit a34581d

Browse files
committed
publish 1.2.3
1 parent 59d739a commit a34581d

File tree

8 files changed

+80
-27
lines changed

8 files changed

+80
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface Configuration {
4141
minifyPages?: boolean,
4242
appendPageChunk?: boolean,
4343
preLoadCSS?: boolean,
44+
preloadStartDelay?: number,
4445
},
4546
fileFinder?: {
4647
fileName?: string,

package-lock.json

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@idrinth/react-file-based-routes",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"repository": "https://github.com/idrinth/react-file-based-routes",
55
"description": "A simple file based routing library, that does not force itself on you.",
66
"license": "MIT",
@@ -25,6 +25,7 @@
2525
"typescript": "^5.3.3"
2626
},
2727
"dependencies": {
28+
"@putout/minify": "^3.8.5",
2829
"astring": "^1.8.6",
2930
"estree-walker": "^3.0.3",
3031
"fast-glob": "^3.3.2",

src/configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface FileBuilder {
2727
appendPageChunk: boolean,
2828
preLoadCSS: boolean,
2929
preLoadJS: boolean,
30+
preloadStartDelay: number,
3031
}
3132
interface FileFinder {
3233
fileName: string,
@@ -80,6 +81,7 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
8081
appendPageChunk: true,
8182
preLoadCSS: true,
8283
preLoadJS: true,
84+
preloadStartDelay: 5000,
8385
},
8486
};
8587
const configFile = cwd + '/.idrinth-react-file-based-routes.json';

src/generate-folders.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
import minifier from 'html-minifier';
66
import {Configuration} from './configuration.js';
77
import writeIndexHtml from "./write-index-html.js";
8+
import {
9+
minify,
10+
} from '@putout/minify';
811

912
export default (cwd: string, configuration: Configuration) => {
1013
const template = (() => {
@@ -33,21 +36,62 @@ export default (cwd: string, configuration: Configuration) => {
3336
}
3437
}
3538
}
39+
const preload = (() => {
40+
const func = `const header = document.getElementsByTagName('header')[0];
41+
const append = (type, as, file) => {
42+
const prefetch = document.createElement('link');
43+
prefetch.setAttribute('href', '/assets/' + file);
44+
prefetch.setAttribute('type', type);
45+
prefetch.setAttribute('rel', 'preload');
46+
prefetch.setAttribute('as', as);
47+
header.appendChild(prefetch);
48+
};`;
49+
if (jsFiles.length > 0 && cssFiles.length > 0) {
50+
const script = `window.setTimeout(() => {
51+
${ func }
52+
for (const css of ${JSON.stringify(cssFiles)}) {
53+
append('text/css', 'style', css);
54+
}
55+
for (const js of ${JSON.stringify(jsFiles)}) {
56+
append('application/javascript', 'script', js);
57+
}
58+
}, ${ configuration.fileBuilder.preloadStartDelay });`;
59+
return `<script type="text/javascript">${ minify(script) }</script>`;
60+
}
61+
if (cssFiles.length > 0) {
62+
const script = `window.setTimeout(() => {
63+
${ func }
64+
for (const css of ${JSON.stringify(cssFiles)}) {
65+
append('text/css', 'style', css);
66+
}
67+
}, ${ configuration.fileBuilder.preloadStartDelay });`;
68+
return `<script type="text/javascript">${ minify(script) }</script>`;
69+
}
70+
if (jsFiles.length > 0) {
71+
const script = `window.setTimeout(() => {
72+
${ func }
73+
for (const js of ${JSON.stringify(jsFiles)}) {
74+
append('application/javascript', 'script', js);
75+
}
76+
}, ${ configuration.fileBuilder.preloadStartDelay });`;
77+
return `<script type="text/javascript">${ minify(script) }</script>`;
78+
}
79+
return '';
80+
})();
3681
for (const file of files) {
3782
if (file.endsWith('.js') && file.startsWith(fileName)) {
3883
const content = readFileSync(cwd + '/' + configuration.fileFinder.distJSRoot + '/' + file, 'utf8',);
3984
const res = matcher.exec(content);
4085
if (res && res[1]) {
4186
const pageName = res[1].replace(/\/$/u, '');
42-
console.log(pageName);
4387
if (typeof configuration.routes.overridePathMappings[pageName] === 'string') {
4488
if (configuration.routes.overridePathMappings[pageName] !== '*') {
45-
writeIndexHtml(cwd, file, configuration.routes.overridePathMappings[pageName], template, configuration, cssFiles, jsFiles);
89+
writeIndexHtml(cwd, file, configuration.routes.overridePathMappings[pageName], template, configuration, preload);
4690
} else {
47-
writeIndexHtml(cwd, file, '', template, configuration, cssFiles, jsFiles);
91+
writeIndexHtml(cwd, file, '', template, configuration, preload);
4892
}
4993
} else {
50-
writeIndexHtml(cwd, file, `/${ pageName }/`, template, configuration, cssFiles, jsFiles);
94+
writeIndexHtml(cwd, file, `/${ pageName }/`, template, configuration, preload);
5195
}
5296
}
5397
}

src/write-index-html.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import {Configuration} from './configuration.js';
22
import {mkdirSync, writeFileSync} from 'fs';
33

4-
export default (cwd: string, jsFile: string, path: string, template: string, configuration: Configuration, cssPreload: string[], jsPreload: string[]) => {
4+
export default (cwd: string, jsFile: string, path: string, template: string, configuration: Configuration, preload: string) => {
55
if (!configuration.fileBuilder.buildIndex) {
66
return;
77
}
8-
let preload = '';
9-
for (const css of cssPreload) {
10-
preload += `<link rel="preload" href="/assets/${ css }" as="style" type="text/css"/>`
11-
}
12-
for (const js of jsPreload) {
13-
if (! configuration.fileBuilder.appendPageChunk || js !== jsFile) {
14-
preload += `<link rel="preload" href="/assets/${js}" as="script" type="text/javascript"/>`;
15-
}
16-
}
8+
179
const target = cwd + '/dist' + path + 'index.html';
1810
mkdirSync('./dist' + path, {recursive: true},);
1911
if (! configuration.fileBuilder.appendPageChunk) {
20-
if (preload.length > 0) {
21-
writeFileSync(
22-
target,
23-
template.replace(/<\/head>/, `${ preload }</head>`),
24-
);
25-
return;
26-
}
27-
writeFileSync(target, template,);
12+
writeFileSync(
13+
target,
14+
template.replace(/<\/head>/, `${ preload }</head>`),
15+
);
2816
return;
2917
}
3018
writeFileSync(

tsconfig.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
"noUnusedParameters": true,
1010
"noFallthroughCasesInSwitch": true,
1111
"allowSyntheticDefaultImports": true,
12-
"moduleResolution": "NodeNext"
12+
"moduleResolution": "NodeNext",
13+
"typeRoots": [
14+
"typings",
15+
"node_modules/@types"
16+
],
1317
},
1418
"include": [
15-
"src/**/*.ts"
19+
"src/**/*.ts",
20+
"typings/*.d.ts"
1621
]
1722
}

typings/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module '@putout/minify' {
2+
export function minify(code: string): string;
3+
}

0 commit comments

Comments
 (0)