codeceptjs 4.0.0-beta.15 → 4.0.0-beta.16

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.
@@ -74,97 +74,114 @@ const __dirname = __dirname_fn(__filename);
74
74
  const transpiledFiles = new Map()
75
75
  const baseDir = path.dirname(mainFilePath)
76
76
 
77
- // Transpile main file
78
- let jsContent = transpileTS(mainFilePath)
79
-
80
- // Find and transpile all relative TypeScript imports
81
- // Match: import ... from './file' or '../file' or './file.ts'
82
- const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
83
- let match
84
- const imports = []
85
-
86
- while ((match = importRegex.exec(jsContent)) !== null) {
87
- imports.push(match[1])
88
- }
89
-
90
- // Transpile each imported TypeScript file
91
- for (const relativeImport of imports) {
92
- let importedPath = path.resolve(baseDir, relativeImport)
93
-
94
- // Handle .js extensions that might actually be .ts files
95
- if (importedPath.endsWith('.js')) {
96
- const tsVersion = importedPath.replace(/\.js$/, '.ts')
97
- if (fs.existsSync(tsVersion)) {
98
- importedPath = tsVersion
99
- }
77
+ // Recursive function to transpile a file and all its TypeScript dependencies
78
+ const transpileFileAndDeps = (filePath) => {
79
+ // Already transpiled, skip
80
+ if (transpiledFiles.has(filePath)) {
81
+ return
100
82
  }
101
83
 
102
- // Try adding .ts extension if file doesn't exist and no extension provided
103
- if (!path.extname(importedPath)) {
104
- const tsPath = importedPath + '.ts'
105
- if (fs.existsSync(tsPath)) {
106
- importedPath = tsPath
107
- } else {
108
- // Try .js extension as well
109
- const jsPath = importedPath + '.js'
110
- if (fs.existsSync(jsPath)) {
111
- // Skip .js files, they don't need transpilation
112
- continue
113
- }
114
- }
115
- }
84
+ // Transpile this file
85
+ let jsContent = transpileTS(filePath)
86
+
87
+ // Find all relative TypeScript imports in this file
88
+ const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
89
+ let match
90
+ const imports = []
116
91
 
117
- // If it's a TypeScript file, transpile it
118
- if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
119
- const transpiledImportContent = transpileTS(importedPath)
120
- const tempImportFile = importedPath.replace(/\.ts$/, '.temp.mjs')
121
- fs.writeFileSync(tempImportFile, transpiledImportContent)
122
- transpiledFiles.set(importedPath, tempImportFile)
92
+ while ((match = importRegex.exec(jsContent)) !== null) {
93
+ imports.push(match[1])
123
94
  }
124
- }
125
-
126
- // Replace imports in the main file to point to temp .mjs files
127
- jsContent = jsContent.replace(
128
- /from\s+['"](\..+?)(?:\.ts)?['"]/g,
129
- (match, importPath) => {
130
- let resolvedPath = path.resolve(baseDir, importPath)
95
+
96
+ // Get the base directory for this file
97
+ const fileBaseDir = path.dirname(filePath)
98
+
99
+ // Recursively transpile each imported TypeScript file
100
+ for (const relativeImport of imports) {
101
+ let importedPath = path.resolve(fileBaseDir, relativeImport)
131
102
 
132
- // Handle .js extension that might be .ts
133
- if (resolvedPath.endsWith('.js')) {
134
- const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
135
- if (transpiledFiles.has(tsVersion)) {
136
- const tempFile = transpiledFiles.get(tsVersion)
137
- const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
138
- return `from './${relPath}'`
103
+ // Handle .js extensions that might actually be .ts files
104
+ if (importedPath.endsWith('.js')) {
105
+ const tsVersion = importedPath.replace(/\.js$/, '.ts')
106
+ if (fs.existsSync(tsVersion)) {
107
+ importedPath = tsVersion
139
108
  }
140
109
  }
141
110
 
142
- // Try with .ts extension
143
- const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
144
-
145
- // If we transpiled this file, use the temp file
146
- if (transpiledFiles.has(tsPath)) {
147
- const tempFile = transpiledFiles.get(tsPath)
148
- // Get relative path from main temp file to this temp file
149
- const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
150
- // Ensure the path starts with ./
151
- if (!relPath.startsWith('.')) {
152
- return `from './${relPath}'`
111
+ // Try adding .ts extension if file doesn't exist and no extension provided
112
+ if (!path.extname(importedPath)) {
113
+ const tsPath = importedPath + '.ts'
114
+ if (fs.existsSync(tsPath)) {
115
+ importedPath = tsPath
116
+ } else {
117
+ // Try .js extension as well
118
+ const jsPath = importedPath + '.js'
119
+ if (fs.existsSync(jsPath)) {
120
+ // Skip .js files, they don't need transpilation
121
+ continue
122
+ }
153
123
  }
154
- return `from '${relPath}'`
155
124
  }
156
125
 
157
- // Otherwise, keep the import as-is
158
- return match
126
+ // If it's a TypeScript file, recursively transpile it and its dependencies
127
+ if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
128
+ transpileFileAndDeps(importedPath)
129
+ }
159
130
  }
160
- )
161
-
162
- // Create a temporary JS file with .mjs extension for the main file
163
- const tempJsFile = mainFilePath.replace(/\.ts$/, '.temp.mjs')
164
- fs.writeFileSync(tempJsFile, jsContent)
131
+
132
+ // After all dependencies are transpiled, rewrite imports in this file
133
+ jsContent = jsContent.replace(
134
+ /from\s+['"](\..+?)(?:\.ts)?['"]/g,
135
+ (match, importPath) => {
136
+ let resolvedPath = path.resolve(fileBaseDir, importPath)
137
+
138
+ // Handle .js extension that might be .ts
139
+ if (resolvedPath.endsWith('.js')) {
140
+ const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
141
+ if (transpiledFiles.has(tsVersion)) {
142
+ const tempFile = transpiledFiles.get(tsVersion)
143
+ const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
144
+ // Ensure the path starts with ./
145
+ if (!relPath.startsWith('.')) {
146
+ return `from './${relPath}'`
147
+ }
148
+ return `from '${relPath}'`
149
+ }
150
+ }
151
+
152
+ // Try with .ts extension
153
+ const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
154
+
155
+ // If we transpiled this file, use the temp file
156
+ if (transpiledFiles.has(tsPath)) {
157
+ const tempFile = transpiledFiles.get(tsPath)
158
+ const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
159
+ // Ensure the path starts with ./
160
+ if (!relPath.startsWith('.')) {
161
+ return `from './${relPath}'`
162
+ }
163
+ return `from '${relPath}'`
164
+ }
165
+
166
+ // Otherwise, keep the import as-is
167
+ return match
168
+ }
169
+ )
170
+
171
+ // Write the transpiled file with updated imports
172
+ const tempFile = filePath.replace(/\.ts$/, '.temp.mjs')
173
+ fs.writeFileSync(tempFile, jsContent)
174
+ transpiledFiles.set(filePath, tempFile)
175
+ }
176
+
177
+ // Start recursive transpilation from the main file
178
+ transpileFileAndDeps(mainFilePath)
179
+
180
+ // Get the main transpiled file
181
+ const tempJsFile = transpiledFiles.get(mainFilePath)
165
182
 
166
183
  // Store all temp files for cleanup
167
- const allTempFiles = [tempJsFile, ...Array.from(transpiledFiles.values())]
184
+ const allTempFiles = Array.from(transpiledFiles.values())
168
185
 
169
186
  return { tempFile: tempJsFile, allTempFiles }
170
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codeceptjs",
3
- "version": "4.0.0-beta.15",
3
+ "version": "4.0.0-beta.16",
4
4
  "type": "module",
5
5
  "description": "Supercharged End 2 End Testing Framework for NodeJS",
6
6
  "keywords": [