Skip to content

Commit

Permalink
add preload for js and css files
Browse files Browse the repository at this point in the history
  • Loading branch information
Idrinth committed Mar 28, 2024
1 parent 9179dfe commit 5e7d34d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface Configuration {
buildIndex?: boolean,
minifyPages?: boolean,
appendPageChunk?: boolean,
preLoadCSS?: boolean,
},
fileFinder?: {
fileName?: string,
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface FileBuilder {
buildIndex: boolean,
minifyPages: boolean,
appendPageChunk: boolean,
preLoadCSS: boolean,
preLoadJS: boolean,
}
interface FileFinder {
fileName: string,
Expand Down Expand Up @@ -76,6 +78,8 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
buildIndex: true,
minifyPages: true,
appendPageChunk: true,
preLoadCSS: true,
preLoadJS: true,
},
};
const configFile = cwd + '/.idrinth-react-file-based-routes.json';
Expand Down
23 changes: 20 additions & 3 deletions src/generate-folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,35 @@ export default (cwd: string, configuration: Configuration) => {
})();
const matcher = new RegExp(`${configuration.fileFinder.distJSRoot}/(.*?)${configuration.fileFinder.fileName}`);
const fileName = configuration.fileFinder.fileName.replace(/\.tsx$/, '-');
const cssFiles = [];
const jsFiles = [];
const files = readdirSync(cwd + '/' + configuration.fileFinder.distJSRoot, {encoding: 'utf8', recursive: true});
if (configuration.fileBuilder.preLoadCSS) {
for (const file of files) {
if (file.endsWith('.css',)) {
cssFiles.push(file,);
}
}
}
if (configuration.fileBuilder.preLoadJS) {
for (const file of files) {
if (file.endsWith('.js',)) {
jsFiles.push(file,);
}
}
}

for (const file of readdirSync(cwd + '/' + configuration.fileFinder.distJSRoot, {encoding: 'utf8', recursive: true})) {
for (const file of files) {
if (file.endsWith('.js') && file.startsWith(fileName)) {
const content = readFileSync(cwd + '/' + configuration.fileFinder.distJSRoot + '/' + file, 'utf8',);
const res = matcher.exec(content);
if (res && res[1]) {
if (typeof configuration.routes.overridePathMappings[res[1]] === 'string') {
if (configuration.routes.overridePathMappings[res[1]] !== '*') {
writeIndexHtml(cwd, file, configuration.routes.overridePathMappings[res[1]], template, configuration);
writeIndexHtml(cwd, file, configuration.routes.overridePathMappings[res[1]], template, configuration, cssFiles, jsFiles);
}
} else {
writeIndexHtml(cwd, file, `/${ res[1] }/`, template, configuration);
writeIndexHtml(cwd, file, `/${ res[1] }/`, template, configuration, cssFiles, jsFiles);
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/write-index-html.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import {Configuration} from './configuration.js';
import {mkdirSync, writeFileSync} from 'fs';

export default (cwd: string, jsFile: string, path: string, template: string, configuration: Configuration) => {
export default (cwd: string, jsFile: string, path: string, template: string, configuration: Configuration, cssPreload: string[], jsPreload: string[]) => {
if (!configuration.fileBuilder.buildIndex) {
return;
}
let preload = '';
for (const css of cssPreload) {
preload += `<link rel="preload" href="/assets/${ css }" as="style" type="text/css"/>`
}
for (const js of jsPreload) {
if (! configuration.fileBuilder.appendPageChunk || js !== jsFile) {
preload += `<link rel="preload" href="/assets/${js}" as="script" type="text/javascript"/>`;
}
}
const target = cwd + '/dist' + path + 'index.html';
mkdirSync('./dist' + path, {recursive: true},);
if (! configuration.fileBuilder.appendPageChunk) {
if (preload.length > 0) {
writeFileSync(
target,
template.replace(/<\/head>/, `${ preload }</head>`),
);
return;
}
writeFileSync(target, template,);
return;
}
writeFileSync(
target,
template.replace(/<\/head>/, `<script src="/assets/${jsFile}" type="module"></script></head>`),
template.replace(
/<\/head>/,
`<script src="/assets/${jsFile}" type="module"></script>${ preload }</head>`,
),
);
}

0 comments on commit 5e7d34d

Please sign in to comment.