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

Commit b2b0eb2

Browse files
feat: add support for TS in templates
1 parent 86df05b commit b2b0eb2

File tree

5 files changed

+245
-52
lines changed

5 files changed

+245
-52
lines changed

package-lock.json

Lines changed: 193 additions & 6 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@babel/core": "7.12.9",
3535
"@babel/preset-env": "^7.12.7",
3636
"@babel/preset-react": "^7.12.7",
37+
"@babel/preset-typescript": "^7.13.0",
3738
"@rollup/plugin-babel": "^5.2.1",
3839
"babel-plugin-source-map-support": "^2.1.3",
3940
"prop-types": "^15.7.2",
@@ -58,8 +59,7 @@
5859
"jsdoc-to-markdown": "^6.0.1",
5960
"markdown-toc": "^1.2.0",
6061
"semantic-release": "^17.3.0",
61-
"ts-jest": "^26.4.4",
62-
"typescript": "^4.1.2"
62+
"ts-jest": "^26.4.4"
6363
},
6464
"scripts": {
6565
"start": "tsc --watch",

src/renderer/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isJsFile } from "../utils";
55
import { TemplateContext, TemplateRenderResult } from "../types";
66

77
/**
8-
* render a file with react. This function automatically transforms jsx to js before importing the component.
8+
* Render a file with React.
99
*
1010
* @param filepath the path to file to render
1111
*/

src/transpiler/transpiler.ts

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,59 @@ import { TranspileFilesOptions } from '../types';
99
const ROOT_DIR = Path.resolve(__dirname, '../..');
1010

1111
/**
12-
* Transpile files in a given directory (and sub directory if recursive option are passed) and write it to an output directory, if no errors are thrown it completed successfully.
12+
* Transpile files in a given directory (and sub directory if recursive option are passed) and write it to an output directory,
13+
* if no errors are thrown it completed successfully.
1314
*
1415
* @param directory to transpile.
1516
* @param outputDir to write the transpiled files to.
1617
* @param options any extra options that should be passed.
1718
*/
1819
export async function transpileFiles(directory: string, outputDir: string, options?: TranspileFilesOptions) {
19-
const { files, dirs } = await getStatsInDir(directory);
20-
if (files.length) {
21-
/**
22-
* WHEN ADDING PLUGINS to transform the input keep in mind that
23-
* IF any of these changes the actual original content in some way
24-
* the output is not able to produce accurate source map to the original file.
25-
*
26-
* An example of this is using the `sourceMaps: 'inline'` configuration for the babel plugin.
27-
*/
28-
const bundles = await rollup({
29-
input: files,
30-
onwarn: ()=>{},
31-
plugins: [
32-
babel({
33-
cwd: ROOT_DIR,
34-
babelHelpers: "bundled",
35-
plugins: [
36-
"source-map-support"
37-
],
38-
presets: [
39-
"@babel/preset-env",
40-
["@babel/preset-react", {
41-
runtime: "automatic",
42-
}],
43-
],
44-
})
45-
]
20+
const { files, dirs } = await getStatsInDir(directory);
21+
22+
if (files.length) {
23+
/**
24+
* WHEN ADDING PLUGINS to transform the input keep in mind that
25+
* IF any of these changes the actual original content in some way
26+
* the output is not able to produce accurate source map to the original file.
27+
*
28+
* An example of this is using the `sourceMaps: 'inline'` configuration for the babel plugin.
29+
*/
30+
const bundles = await rollup({
31+
input: files,
32+
onwarn: ()=>{},
33+
plugins: [
34+
babel({
35+
cwd: ROOT_DIR,
36+
babelHelpers: "bundled",
37+
extensions: ['.js', '.jsx', '.cjs', '.tsx'],
38+
presets: [
39+
"@babel/preset-env",
40+
"@babel/preset-typescript",
41+
["@babel/preset-react", {
42+
runtime: "automatic",
43+
}],
44+
],
45+
plugins: [
46+
"source-map-support"
47+
],
4648
})
47-
await bundles.write({
48-
format: "commonjs",
49-
sourcemap: true,
50-
dir: outputDir,
51-
exports: "auto"
52-
})
53-
}
49+
]
50+
});
51+
52+
await bundles.write({
53+
format: "commonjs",
54+
sourcemap: true,
55+
dir: outputDir,
56+
exports: "auto"
57+
});
58+
}
5459

55-
// Check if we should transpile all subdirs
56-
if (options?.recursive === true && dirs.length > 0) {
57-
for (const subdir of dirs) {
58-
const subdirPath = Path.parse(subdir);
59-
await transpileFiles(subdir, Path.resolve(outputDir, subdirPath.base), options);
60-
}
60+
// Check if we should transpile all subdirs
61+
if (options?.recursive === true && dirs.length > 0) {
62+
for (const subdir of dirs) {
63+
const subdirPath = Path.parse(subdir);
64+
await transpileFiles(subdir, Path.resolve(outputDir, subdirPath.base), options);
6165
}
66+
}
6267
}

src/utils/isJsFile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const ALLOWED_EXTS = ['js', 'jsx', 'cjs'];
1+
const ALLOWED_EXTS = ['js', 'jsx', 'cjs', 'tsx'];
22

33
/**
4-
* Function which checks if file is JS file
4+
* Function which checks if file is JS file that supports JSX syntax
5+
*
56
* @private
67
* @param {string} filename
78
* @returns {boolean}

0 commit comments

Comments
 (0)