dctc 1.0.5 → 1.1.1

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 CHANGED
@@ -19,8 +19,9 @@ dctc [options] <file>
19
19
 
20
20
  ## Options
21
21
  ```
22
- -h, --help display help for command
23
- -v, --version output the version number
22
+ -h, --help display help for command
23
+ -v, --version output the version number
24
+ -c, --compiler <name> specify compiler: es, rollup, rolldown (default: es)
24
25
  ```
25
26
 
26
27
  ## Installation
@@ -114,6 +115,13 @@ work()
114
115
  dctc generate-html.tsx
115
116
  ```
116
117
 
118
+ Or specify a compiler:
119
+
120
+ ```shell
121
+ dctc --compiler rollup generate-html.tsx
122
+ dctc -c rolldown generate-html.tsx
123
+ ```
124
+
117
125
  #### The `page.html` file has been generated, and it looks like this
118
126
 
119
127
  ```html
package/bin/index.js CHANGED
@@ -14,6 +14,11 @@ function isHelp(param) {
14
14
  return lowerParam === "--help" || lowerParam === "-h"
15
15
  }
16
16
 
17
+ function isCompilerOption(param) {
18
+ const lowerParam = param.toLowerCase();
19
+ return lowerParam === "--compiler" || lowerParam === "-c"
20
+ }
21
+
17
22
  function isFilePath(param) {
18
23
  try {
19
24
  if (!fs.existsSync(param)) {
@@ -26,20 +31,43 @@ function isFilePath(param) {
26
31
  }
27
32
  }
28
33
 
29
- function work(param) {
30
- if (isVersion(param)) {
31
- applyVersion();
32
- } else if (isHelp(param)) {
33
- applyHelp()
34
- } else if (isFilePath(param)) {
35
- applyDctc(param);
34
+ function work() {
35
+ const args = process.argv.slice(2);
36
+ let compiler = 'es'; // default compiler
37
+ let filePath = null;
38
+
39
+ // Parse arguments
40
+ for (let i = 0; i < args.length; i++) {
41
+ const arg = args[i];
42
+
43
+ if (isVersion(arg)) {
44
+ applyVersion();
45
+ return;
46
+ } else if (isHelp(arg)) {
47
+ applyHelp();
48
+ return;
49
+ } else if (isCompilerOption(arg)) {
50
+ // Get compiler value from next argument
51
+ if (i + 1 < args.length) {
52
+ compiler = args[i + 1].toLowerCase();
53
+ i++; // Skip next argument as it's the compiler value
54
+ } else {
55
+ logErr('Please provide a compiler name after --compiler/-c');
56
+ applyHelp();
57
+ process.exit(1);
58
+ }
59
+ } else if (isFilePath(arg)) {
60
+ filePath = arg;
61
+ }
62
+ }
63
+
64
+ if (filePath) {
65
+ applyDctc(filePath, compiler);
36
66
  } else {
37
67
  logErr('Please provide a file path as an argument');
38
- applyHelp()
68
+ applyHelp();
39
69
  process.exit(1);
40
70
  }
41
71
  }
42
72
 
43
- const param = process.argv[2];
44
-
45
- work(param)
73
+ work()
@@ -1,5 +1,6 @@
1
- // const complie_vite = require("../complie_vite");
2
1
  const complie_es = require("../complie_es");
2
+ const complie_rollup = require("../complie_rollup");
3
+ const complie_rolldown = require("../complie_rolldown");
3
4
  const execute = require("../execute");
4
5
  const chalk = require("chalk");
5
6
  const log = content => console.log(chalk.green(content));
@@ -13,16 +14,36 @@ function applyHelp() {
13
14
  logInfo("dctc is a tool for running TypeScript and JSX files in the browser.");
14
15
  logInfo("Usage: dctc [options] <file>");
15
16
  logInfo("Options:");
16
- logInfo(` -v, --version Print the version number`);
17
- logInfo(` -h, --help Print this help message`);
17
+ logInfo(` -v, --version Print the version number`);
18
+ logInfo(` -h, --help Print this help message`);
19
+ logInfo(` -c, --compiler <name> Specify compiler: es, rollup, rolldown (default: es)`);
18
20
  logInfo("Examples:");
19
21
  logInfo(` dctc src/index.tsx`);
20
22
  logInfo(` dctc src/index.ts`);
23
+ logInfo(` dctc --compiler rollup src/index.tsx`);
24
+ logInfo(` dctc -c rolldown src/index.ts`);
21
25
  }
22
26
 
23
- async function applyDctc(inputFile) {
24
- // const code = await complie_vite(inputFile);
25
- const code = await complie_es(inputFile);
27
+ async function applyDctc(inputFile, compiler = 'es') {
28
+ let code;
29
+
30
+ switch (compiler.toLowerCase()) {
31
+ case 'es':
32
+ case 'esbuild':
33
+ code = await complie_es(inputFile);
34
+ break;
35
+ case 'rollup':
36
+ code = await complie_rollup(inputFile);
37
+ break;
38
+ case 'rolldown':
39
+ code = await complie_rolldown(inputFile);
40
+ break;
41
+ default:
42
+ log(`Unknown compiler: ${compiler}, using default 'es'`);
43
+ code = await complie_es(inputFile);
44
+ break;
45
+ }
46
+
26
47
  execute(code, inputFile);
27
48
  process.exit(0);
28
49
  }
package/lib/complie_es.js CHANGED
@@ -80,6 +80,8 @@ module.exports = async function (filePath) {
80
80
  const code = result.outputFiles[0].text;
81
81
  return code;
82
82
  } catch (error) {
83
- logErr('Build failed:', error); // Log build failure
83
+ logErr('Build failed:');
84
+ console.error(error);
85
+ throw error;
84
86
  }
85
87
  }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Compile a given file to CommonJS format using Rolldown.
3
+ * This function takes a file path, compiles it to CommonJS format,
4
+ * and returns the compiled code.
5
+ * @param {string} filePath - The path to the file to be compiled.
6
+ * @returns {Promise<string>} - The compiled code.
7
+ * @author pipi
8
+ */
9
+ const fs = require('fs'); // Import the file system module
10
+ const path = require('path'); // Import the path module
11
+ const chalk = require("chalk");
12
+ const { rolldown } = require('rolldown');
13
+ const logErr = content => console.log(chalk.red(content)); // Function to log error messages in red color
14
+
15
+ module.exports = async function (filePath) {
16
+ // Ensure the file exists
17
+ if (!fs.existsSync(filePath)) {
18
+ console.error(`File does not exist: ${filePath}`);
19
+ process.exit(1);
20
+ }
21
+
22
+ // Get the absolute path of the file
23
+ const absoluteFilePath = path.resolve(filePath);
24
+
25
+ try {
26
+ // Create a rolldown build
27
+ const build = await rolldown({
28
+ input: absoluteFilePath,
29
+ external: ['react', 'react-dom'],
30
+ resolve: {
31
+ extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
32
+ },
33
+ platform: 'node',
34
+ });
35
+
36
+ // Generate output without writing to file
37
+ const output = await build.generate({
38
+ format: 'cjs',
39
+ });
40
+
41
+ // Get the compiled code from output
42
+ const code = output.output[0].code;
43
+
44
+ return code;
45
+ } catch (error) {
46
+ logErr('Build failed:');
47
+ console.error(error);
48
+ throw error;
49
+ }
50
+ }
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Compile a given file to CommonJS format using Rollup.
3
+ * This function takes a file path, compiles it to CommonJS format,
4
+ * and returns the compiled code.
5
+ * @param {string} filePath - The path to the file to be compiled.
6
+ * @returns {Promise<string>} - The compiled code.
7
+ * @author pipi
8
+ */
9
+ const fs = require('fs'); // Import the file system module
10
+ const path = require('path'); // Import the path module
11
+ const chalk = require("chalk");
12
+ const rollup = require('rollup');
13
+ const { nodeResolve } = require('@rollup/plugin-node-resolve');
14
+ const commonjs = require('@rollup/plugin-commonjs');
15
+ const typescript = require('@rollup/plugin-typescript');
16
+ const logErr = content => console.log(chalk.red(content)); // Function to log error messages in red color
17
+
18
+ // Custom plugin to ensure correct file resolution
19
+ function resolveTsFiles() {
20
+ return {
21
+ name: 'resolve-ts-files',
22
+ resolveId(source, importer) {
23
+ // Only handle relative imports
24
+ if (!importer || !source.startsWith('.')) {
25
+ return null;
26
+ }
27
+
28
+ const resolveDir = importer ? path.dirname(importer) : process.cwd();
29
+ const resolvedPath = path.resolve(resolveDir, source);
30
+
31
+ // Check for .tsx first, then .ts
32
+ if (fs.existsSync(resolvedPath + '.tsx')) {
33
+ return resolvedPath + '.tsx';
34
+ } else if (fs.existsSync(resolvedPath + '.ts')) {
35
+ return resolvedPath + '.ts';
36
+ }
37
+
38
+ return null;
39
+ },
40
+ };
41
+ }
42
+
43
+ // Custom plugin to ensure entry file is correctly resolved
44
+ function ensureEntryFile(entryFile) {
45
+ const normalizedEntryFile = path.resolve(entryFile);
46
+
47
+ return {
48
+ name: 'ensure-entry-file',
49
+ buildStart(options) {
50
+ // Verify the entry file exists and is correct
51
+ if (!fs.existsSync(entryFile)) {
52
+ throw new Error(`Entry file does not exist: ${entryFile}`);
53
+ }
54
+ },
55
+ resolveId(id, importer) {
56
+ // Handle entry file resolution with highest priority
57
+ // This runs before other plugins to ensure correct file is used
58
+ const normalizedId = path.resolve(id);
59
+
60
+ // If this is the entry file (exact match), return it
61
+ if (normalizedId === normalizedEntryFile) {
62
+ return entryFile;
63
+ }
64
+
65
+ // If id matches entry file without extension, return the actual entry file
66
+ // This prevents rollup from trying .ts when we want .tsx
67
+ const idWithoutExt = normalizedId.replace(/\.(tsx?|jsx?)$/, '');
68
+ const entryWithoutExt = normalizedEntryFile.replace(/\.(tsx?|jsx?)$/, '');
69
+ if (idWithoutExt === entryWithoutExt && idWithoutExt !== normalizedId) {
70
+ // Only return entry file if id doesn't have extension but entry does
71
+ return entryFile;
72
+ }
73
+
74
+ return null;
75
+ },
76
+ };
77
+ }
78
+
79
+ module.exports = async function (filePath) {
80
+ // Ensure the file exists
81
+ if (!fs.existsSync(filePath)) {
82
+ console.error(`File does not exist: ${filePath}`);
83
+ process.exit(1);
84
+ }
85
+
86
+ // Get the absolute path of the file
87
+ const absoluteFilePath = path.resolve(filePath);
88
+
89
+ try {
90
+ // Create a rollup bundle
91
+ const bundle = await rollup.rollup({
92
+ input: absoluteFilePath,
93
+ plugins: [
94
+ // Custom plugin to intercept and fix entry file resolution
95
+ {
96
+ name: 'fix-entry-file',
97
+ resolveId(id) {
98
+ // If this is the entry file, ensure it's returned as-is
99
+ const normalizedId = path.resolve(id);
100
+ const normalizedEntry = path.resolve(absoluteFilePath);
101
+
102
+ // Exact match
103
+ if (normalizedId === normalizedEntry) {
104
+ return absoluteFilePath;
105
+ }
106
+
107
+ // Match without extension (rollup might try this)
108
+ const idWithoutExt = normalizedId.replace(/\.(tsx?|jsx?)$/, '');
109
+ const entryWithoutExt = normalizedEntry.replace(/\.(tsx?|jsx?)$/, '');
110
+ if (idWithoutExt === entryWithoutExt && idWithoutExt !== normalizedEntry) {
111
+ // If rollup is trying to resolve without extension, return the actual entry file
112
+ return absoluteFilePath;
113
+ }
114
+
115
+ return null;
116
+ },
117
+ },
118
+ // Ensure entry file is resolved correctly before TypeScript plugin
119
+ ensureEntryFile(absoluteFilePath),
120
+ // TypeScript plugin must come before nodeResolve to handle TS/TSX files
121
+ typescript({
122
+ tsconfig: false,
123
+ compilerOptions: {
124
+ target: 'es2015',
125
+ module: 'esnext',
126
+ moduleResolution: 'node',
127
+ strict: true,
128
+ esModuleInterop: true,
129
+ skipLibCheck: true,
130
+ allowSyntheticDefaultImports: true,
131
+ jsx: 'react',
132
+ lib: ['ESNext', 'DOM'],
133
+ resolveJsonModule: true,
134
+ },
135
+ include: [absoluteFilePath, '**/*.ts', '**/*.tsx'],
136
+ // Explicitly exclude files that might be incorrectly resolved
137
+ exclude: [],
138
+ }),
139
+ resolveTsFiles(),
140
+ nodeResolve({
141
+ preferBuiltins: false,
142
+ extensions: ['.tsx', '.ts', '.jsx', '.js', '.json'],
143
+ }),
144
+ commonjs(),
145
+ ],
146
+ external: ['react', 'react-dom'],
147
+ });
148
+
149
+ // Generate the output
150
+ const { output } = await bundle.generate({
151
+ format: 'cjs',
152
+ exports: 'auto',
153
+ });
154
+
155
+ // Get the compiled code
156
+ const code = output[0].code;
157
+
158
+ // Close the bundle
159
+ await bundle.close();
160
+
161
+ return code;
162
+ } catch (error) {
163
+ logErr('Build failed:');
164
+ console.error(error);
165
+ throw error;
166
+ }
167
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dctc",
3
- "version": "1.0.5",
3
+ "version": "1.1.1",
4
4
  "description": "Dynamically compile TSX/TS files and execute them.",
5
5
  "main": "./lib/index.js",
6
6
  "bin": {
@@ -14,7 +14,7 @@
14
14
  "LICENSE"
15
15
  ],
16
16
  "scripts": {
17
- "test": "echo \"Error: no test specified\" && exit 1"
17
+ "test": "node bin/index.js test/test.ts"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
@@ -41,11 +41,17 @@
41
41
  },
42
42
  "homepage": "https://github.com/SteamedBread2333/dynamic-compile-tsx-commond#readme",
43
43
  "dependencies": {
44
+ "@rollup/plugin-commonjs": "^28.0.1",
45
+ "@rollup/plugin-node-resolve": "^15.3.0",
46
+ "@rollup/plugin-typescript": "^12.1.0",
44
47
  "chalk": "^4.1.2",
45
48
  "commander": "^13.1.0",
46
49
  "esbuild": "^0.24.2",
47
50
  "react": "^18.0.0",
48
51
  "react-dom": "^18.0.0",
52
+ "rolldown": "^0.12.0",
53
+ "rollup": "^4.21.0",
54
+ "tslib": "^2.8.1",
49
55
  "typescript": "^5.7.3"
50
56
  }
51
57
  }
@@ -1,66 +0,0 @@
1
- /**
2
- * Compile a given file to CommonJS format using Vite.
3
- * This function takes a file path, compiles it to CommonJS format,
4
- * and runs the compiled code in the current Node.js context.
5
- * @param {string} filePath - The path to the file to be compiled.
6
- * @returns {string} code - The compiled code.
7
- * @author pipi
8
- */
9
- const { build } = require('vite'); // Import the build function from Vite
10
- const fs = require('fs'); // Import the file system module
11
- const path = require('path'); // Import the path module
12
- const chalk = require("chalk");
13
- const logErr = content => console.log(chalk.red(content));
14
-
15
- const { defineConfig } = require('vite');
16
-
17
- module.exports = async function (filePath) {
18
- // Ensure the file exists
19
- if (!fs.existsSync(filePath)) {
20
- console.error(`File does not exist: ${filePath}`);
21
- process.exit(1);
22
- }
23
-
24
- // Get the absolute path of the file
25
- const absoluteFilePath = path.resolve(filePath);
26
-
27
- // Configure Vite build options
28
- const viteConfig = defineConfig({
29
- overrides: {
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
- },
34
- build: {
35
- target: 'node18',
36
- format: 'cjs',
37
- lib: {
38
- entry: absoluteFilePath, // Specify the entry file
39
- formats: ['cjs'], // Specify the output format
40
- },
41
- outDir: 'dist-dctc',
42
- rollupOptions: {
43
- output: {
44
- entryFileNames: 'bundle.js',
45
- },
46
- },
47
- },
48
- });
49
-
50
- try {
51
- // Use Vite to build the file
52
- const bundle = await build(viteConfig);
53
-
54
- // Get the compiled file content
55
- const output = bundle[0].output[0];
56
- if (!output) {
57
- logErr('No output file generated');
58
- process.exit(1);
59
- }
60
-
61
- return output.code;
62
- } catch (error) {
63
- logErr('Compilation failed:', error);
64
- process.exit(1);
65
- }
66
- }