dctc 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -3
- package/lib/complie_es.js +2 -3
- package/lib/complie_vite.js +5 -5
- package/lib/execute.js +1 -2
- package/package.json +16 -8
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
#
|
|
2
|
-
Dynamically compile TSX/TS
|
|
1
|
+
# dynamic-compile-tsx-commond(dctc)
|
|
2
|
+
TypeScript Execute (tsx): Dynamically compile TSX/TS file and execute it.
|
|
3
|
+
|
|
4
|
+
`dctc` may be similar tools in the fastest speed of execution!! <img src="https://emojis.slackmojis.com/emojis/images/1643514389/3643/cool-doge.gif?1643514389" width="30"/>
|
|
3
5
|
|
|
4
6
|
## Usage
|
|
5
7
|
```
|
|
@@ -18,10 +20,113 @@ npm install -g dctc
|
|
|
18
20
|
```
|
|
19
21
|
|
|
20
22
|
## Example
|
|
23
|
+
|
|
24
|
+
### Step 1: Create Source Files
|
|
25
|
+
|
|
26
|
+
Create a `src` directory and add the following files:
|
|
27
|
+
|
|
28
|
+
#### `src/index.tsx`
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import React from 'react';
|
|
32
|
+
import Header from './components/Header';
|
|
33
|
+
import Footer from './components/Footer';
|
|
34
|
+
|
|
35
|
+
const Page: React.FC<{ fontColor: string }> = (props) => {
|
|
36
|
+
return (
|
|
37
|
+
<div style={{ color: props?.fontColor || '#fff' }}>
|
|
38
|
+
<Header />
|
|
39
|
+
<main>
|
|
40
|
+
<h1>Hello, World!</h1>
|
|
41
|
+
<p>This is a dynamically compiled TSX file using dctc.</p>
|
|
42
|
+
</main>
|
|
43
|
+
<Footer />
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default Page;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
#### `src/components/Header.tsx`
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
import React from 'react';
|
|
55
|
+
|
|
56
|
+
const Header: React.FC = () => {
|
|
57
|
+
return (
|
|
58
|
+
<header>
|
|
59
|
+
<h2>Welcome to the DCTC Example</h2>
|
|
60
|
+
</header>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default Header;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### `src/components/Footer.tsx`
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import React from 'react';
|
|
71
|
+
|
|
72
|
+
const Footer: React.FC = () => {
|
|
73
|
+
return (
|
|
74
|
+
<footer>
|
|
75
|
+
<p>© 2025 DCTC Example</p>
|
|
76
|
+
</footer>
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export default Footer;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Step 2: Create a tsx script file
|
|
84
|
+
|
|
85
|
+
#### `/generate-html.tsx`
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { renderToString } from 'react-dom/server'
|
|
89
|
+
import React from 'react'
|
|
90
|
+
import Page from './src'
|
|
91
|
+
import fs from 'fs';
|
|
92
|
+
import path from 'path';
|
|
93
|
+
|
|
94
|
+
const work = () => {
|
|
95
|
+
const html = renderToString(<Page fontColor="pink" />)
|
|
96
|
+
fs.writeFileSync(path.join(__dirname, 'page.html'), html);
|
|
97
|
+
return html
|
|
98
|
+
}
|
|
99
|
+
work()
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Step 3: Enter the following command
|
|
103
|
+
|
|
21
104
|
```shell
|
|
22
|
-
dctc
|
|
105
|
+
dctc generate-html.tsx
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### The `page.html` file has been generated, and it looks like this
|
|
109
|
+
|
|
110
|
+
```html
|
|
111
|
+
<div style="color:pink">
|
|
112
|
+
<header>
|
|
113
|
+
<h2>Welcome to the DCTC Example</h2>
|
|
114
|
+
</header>
|
|
115
|
+
<main>
|
|
116
|
+
<h1>Hello, World!</h1>
|
|
117
|
+
<p>This is a dynamically compiled TSX file using dctc.</p>
|
|
118
|
+
</main>
|
|
119
|
+
<footer>
|
|
120
|
+
<p>© 2025 DCTC Example</p>
|
|
121
|
+
</footer>
|
|
122
|
+
</div>
|
|
23
123
|
```
|
|
24
124
|
|
|
125
|
+
#### Open the file `page.html` with your browser
|
|
126
|
+
|
|
127
|
+
<img width="441" alt="image" src="https://github.com/user-attachments/assets/67d82e20-81e8-4a98-8bb6-0175efd2bb30" />
|
|
128
|
+
|
|
129
|
+
|
|
25
130
|
## Contributing
|
|
26
131
|
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
|
27
132
|
Please make sure to update tests as appropriate.
|
package/lib/complie_es.js
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* @returns {Promise<string>} - The compiled code.
|
|
7
7
|
* @author pipi
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
9
|
const fs = require('fs'); // Import the file system module
|
|
11
10
|
const path = require('path'); // Import the path module
|
|
12
11
|
const chalk = require("chalk");
|
|
@@ -62,9 +61,9 @@ module.exports = async function (filePath) {
|
|
|
62
61
|
setup(build) {
|
|
63
62
|
build.onResolve({ filter: /\.tsx?$/ }, async (args) => {
|
|
64
63
|
const resolvedPath = path.resolve(args.resolveDir, args.path);
|
|
65
|
-
if (
|
|
64
|
+
if (fs.existsSync(resolvedPath + '.ts')) {
|
|
66
65
|
return { path: resolvedPath + '.ts' };
|
|
67
|
-
} else if (
|
|
66
|
+
} else if (fs.existsSync(resolvedPath + '.tsx')) {
|
|
68
67
|
return { path: resolvedPath + '.tsx' };
|
|
69
68
|
} else {
|
|
70
69
|
return null;
|
package/lib/complie_vite.js
CHANGED
|
@@ -27,13 +27,13 @@ module.exports = async function (filePath) {
|
|
|
27
27
|
// Configure Vite build options
|
|
28
28
|
const viteConfig = defineConfig({
|
|
29
29
|
overrides: {
|
|
30
|
-
fs: 'browserify-fs', //
|
|
31
|
-
path: 'path-browserify', //
|
|
32
|
-
//
|
|
30
|
+
fs: 'browserify-fs', // Use browserify-fs for file system access
|
|
31
|
+
path: 'path-browserify', // Use path-browserify for path manipulation
|
|
32
|
+
// Add other overrides as needed
|
|
33
33
|
},
|
|
34
34
|
build: {
|
|
35
|
-
target: 'node18',
|
|
36
|
-
format: 'cjs',
|
|
35
|
+
target: 'node18',
|
|
36
|
+
format: 'cjs',
|
|
37
37
|
lib: {
|
|
38
38
|
entry: absoluteFilePath, // Specify the entry file
|
|
39
39
|
formats: ['cjs'], // Specify the output format
|
package/lib/execute.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* This function takes a string of code and runs it in the current Node.js context.
|
|
2
|
+
* This function takes code as input and executes it in a new context,
|
|
4
3
|
* @param {string} code - The code to be executed.
|
|
5
4
|
* @author pipi
|
|
6
5
|
* @returns {void}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dctc",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Dynamically compile TSX/TS files and execute them.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -18,20 +18,28 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "git+https://github.com/SteamedBread2333/
|
|
21
|
+
"url": "git+https://github.com/SteamedBread2333/dynamic-compile-tsx-commond.git"
|
|
22
22
|
},
|
|
23
23
|
"keywords": [
|
|
24
|
-
"
|
|
25
|
-
"
|
|
24
|
+
"cli",
|
|
25
|
+
"runtime",
|
|
26
|
+
"node",
|
|
27
|
+
"cjs",
|
|
28
|
+
"commonjs",
|
|
29
|
+
"esm",
|
|
30
|
+
"typescript",
|
|
31
|
+
"typescript runner",
|
|
32
|
+
"typeScript execute",
|
|
26
33
|
"tsx",
|
|
27
|
-
"
|
|
34
|
+
"tsx runner",
|
|
35
|
+
"tsx execute"
|
|
28
36
|
],
|
|
29
37
|
"author": "pipi",
|
|
30
38
|
"license": "MIT",
|
|
31
39
|
"bugs": {
|
|
32
|
-
"url": "https://github.com/SteamedBread2333/
|
|
40
|
+
"url": "https://github.com/SteamedBread2333/dynamic-compile-tsx-commond/issues"
|
|
33
41
|
},
|
|
34
|
-
"homepage": "https://github.com/SteamedBread2333/
|
|
42
|
+
"homepage": "https://github.com/SteamedBread2333/dynamic-compile-tsx-commond#readme",
|
|
35
43
|
"dependencies": {
|
|
36
44
|
"chalk": "^4.1.2",
|
|
37
45
|
"commander": "^13.1.0",
|
|
@@ -40,4 +48,4 @@
|
|
|
40
48
|
"react-dom": "^18.0.0",
|
|
41
49
|
"typescript": "^5.7.3"
|
|
42
50
|
}
|
|
43
|
-
}
|
|
51
|
+
}
|