Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reimport file on each render #12

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/renderer/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ export async function renderTemplate(filepath: string, context: TemplateContext)
if (!isJsFile(filepath)) {
return undefined;
}
const data = await importComponent(filepath, context);

let data = undefined;
try {
data = await importComponent(filepath, context);
} catch(err) {
console.error(err);
magicmatatjahu marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}

// undefined, null etc. cases
if (!data) {
Expand All @@ -28,10 +35,22 @@ export async function renderTemplate(filepath: string, context: TemplateContext)
* @private
* @param filepath to import
*/
function importComponent(filepath: string, context: TemplateContext): Promise<React.ReactElement> {
return new Promise((resolve) => {
import(filepath).then(component => { resolve(component.default !== undefined ? component.default(context) : undefined) })
})
function importComponent(filepath: string, context: TemplateContext): Promise<React.ReactElement | undefined> {
return new Promise((resolve, reject) => {
try {
// we should import component only in NodeJS
if (require === undefined) resolve(undefined);
// remove from cache imported file
delete require.cache[require.resolve(filepath)];

const component = require(filepath);
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
if (typeof component === "function") resolve(component(context));
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
if (typeof component.default === "function") resolve(component.default(context));
resolve(undefined);
} catch(err) {
reject(err);
}
});
}

/**
Expand Down