Skip to content

Commit 5e7d34d

Browse files
committed
add preload for js and css files
1 parent 9179dfe commit 5e7d34d

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface Configuration {
4040
buildIndex?: boolean,
4141
minifyPages?: boolean,
4242
appendPageChunk?: boolean,
43+
preLoadCSS?: boolean,
4344
},
4445
fileFinder?: {
4546
fileName?: string,

package-lock.json

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

src/configuration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface FileBuilder {
2525
buildIndex: boolean,
2626
minifyPages: boolean,
2727
appendPageChunk: boolean,
28+
preLoadCSS: boolean,
29+
preLoadJS: boolean,
2830
}
2931
interface FileFinder {
3032
fileName: string,
@@ -76,6 +78,8 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
7678
buildIndex: true,
7779
minifyPages: true,
7880
appendPageChunk: true,
81+
preLoadCSS: true,
82+
preLoadJS: true,
7983
},
8084
};
8185
const configFile = cwd + '/.idrinth-react-file-based-routes.json';

src/generate-folders.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,35 @@ export default (cwd: string, configuration: Configuration) => {
1616
})();
1717
const matcher = new RegExp(`${configuration.fileFinder.distJSRoot}/(.*?)${configuration.fileFinder.fileName}`);
1818
const fileName = configuration.fileFinder.fileName.replace(/\.tsx$/, '-');
19+
const cssFiles = [];
20+
const jsFiles = [];
21+
const files = readdirSync(cwd + '/' + configuration.fileFinder.distJSRoot, {encoding: 'utf8', recursive: true});
22+
if (configuration.fileBuilder.preLoadCSS) {
23+
for (const file of files) {
24+
if (file.endsWith('.css',)) {
25+
cssFiles.push(file,);
26+
}
27+
}
28+
}
29+
if (configuration.fileBuilder.preLoadJS) {
30+
for (const file of files) {
31+
if (file.endsWith('.js',)) {
32+
jsFiles.push(file,);
33+
}
34+
}
35+
}
1936

20-
for (const file of readdirSync(cwd + '/' + configuration.fileFinder.distJSRoot, {encoding: 'utf8', recursive: true})) {
37+
for (const file of files) {
2138
if (file.endsWith('.js') && file.startsWith(fileName)) {
2239
const content = readFileSync(cwd + '/' + configuration.fileFinder.distJSRoot + '/' + file, 'utf8',);
2340
const res = matcher.exec(content);
2441
if (res && res[1]) {
2542
if (typeof configuration.routes.overridePathMappings[res[1]] === 'string') {
2643
if (configuration.routes.overridePathMappings[res[1]] !== '*') {
27-
writeIndexHtml(cwd, file, configuration.routes.overridePathMappings[res[1]], template, configuration);
44+
writeIndexHtml(cwd, file, configuration.routes.overridePathMappings[res[1]], template, configuration, cssFiles, jsFiles);
2845
}
2946
} else {
30-
writeIndexHtml(cwd, file, `/${ res[1] }/`, template, configuration);
47+
writeIndexHtml(cwd, file, `/${ res[1] }/`, template, configuration, cssFiles, jsFiles);
3148
}
3249
}
3350
}

src/write-index-html.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
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) => {
4+
export default (cwd: string, jsFile: string, path: string, template: string, configuration: Configuration, cssPreload: string[], jsPreload: 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+
}
817
const target = cwd + '/dist' + path + 'index.html';
918
mkdirSync('./dist' + path, {recursive: true},);
1019
if (! configuration.fileBuilder.appendPageChunk) {
20+
if (preload.length > 0) {
21+
writeFileSync(
22+
target,
23+
template.replace(/<\/head>/, `${ preload }</head>`),
24+
);
25+
return;
26+
}
1127
writeFileSync(target, template,);
1228
return;
1329
}
1430
writeFileSync(
1531
target,
16-
template.replace(/<\/head>/, `<script src="/assets/${jsFile}" type="module"></script></head>`),
32+
template.replace(
33+
/<\/head>/,
34+
`<script src="/assets/${jsFile}" type="module"></script>${ preload }</head>`,
35+
),
1736
);
1837
}

0 commit comments

Comments
 (0)