Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit 74759e6

Browse files
Gmin2derberg
andauthored
feat: making the transpiled version of react-template optional (#243)
Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent 61568f2 commit 74759e6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/renderer/template.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import fs from "fs";
23

34
import { render } from "./renderer";
45
import { isJsFile } from "../utils";
@@ -47,10 +48,24 @@ function importComponent(filepath: string): Promise<TemplateFunction | undefined
4748
try {
4849
// we should import component only in NodeJS
4950
if (require === undefined) resolve(undefined);
50-
// remove from cache imported file
51-
delete require.cache[require.resolve(filepath)];
5251

53-
const component = require(filepath);
52+
let componentPath = filepath;
53+
54+
// Check if the file exists
55+
if (!fs.existsSync(componentPath)) {
56+
// If transpiled version doesn't exist, try the original version
57+
const originalPath = componentPath.replace('__transpiled', 'template');
58+
if (fs.existsSync(originalPath)) {
59+
componentPath = originalPath;
60+
} else {
61+
throw new Error(`Neither transpiled nor original template file found: ${filepath}`);
62+
}
63+
}
64+
65+
// Remove from cache and require the file
66+
delete require.cache[require.resolve(componentPath)];
67+
const component = require(componentPath);
68+
5469
if (typeof component === "function") resolve(component);
5570
if (typeof component.default === "function") resolve(component.default);
5671
resolve(undefined);

0 commit comments

Comments
 (0)