itty-packager 1.0.9 → 1.0.10
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/lib/builder.js +31 -3
- package/package.json +1 -1
package/lib/builder.js
CHANGED
|
@@ -192,9 +192,37 @@ async function injectSnippet(snippetName, outDir) {
|
|
|
192
192
|
return
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
const transformCode = code =>
|
|
196
|
-
|
|
197
|
-
.
|
|
195
|
+
const transformCode = code => {
|
|
196
|
+
// Find the exported variable name from the export statement
|
|
197
|
+
const exportMatch = code.match(/;export\s*{\s*(\w+)(?:\s+as\s+\w+)?\s*};?\s*$/)
|
|
198
|
+
if (!exportMatch) {
|
|
199
|
+
// Fallback to original logic if no export found
|
|
200
|
+
return code
|
|
201
|
+
.replace(/^let\s+(\w+)\s*=/, `let ${snippetName}=`)
|
|
202
|
+
.replace(/;export\s*{[^}]+};?\s*$/, ';')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const exportedVar = exportMatch[1]
|
|
206
|
+
|
|
207
|
+
// Find the final assignment to the exported variable (usually at module level)
|
|
208
|
+
// Look for patterns like "exportedVar=someFunction()" at the end before export
|
|
209
|
+
const finalAssignmentRegex = new RegExp(`(\\b${exportedVar}\\s*=\\s*[^;]+);export`)
|
|
210
|
+
const assignmentMatch = code.match(finalAssignmentRegex)
|
|
211
|
+
|
|
212
|
+
if (assignmentMatch) {
|
|
213
|
+
// Replace the final assignment
|
|
214
|
+
const oldAssignment = assignmentMatch[1]
|
|
215
|
+
const newAssignment = oldAssignment.replace(new RegExp(`\\b${exportedVar}\\s*=`), `${snippetName}=`)
|
|
216
|
+
return code
|
|
217
|
+
.replace(oldAssignment, newAssignment)
|
|
218
|
+
.replace(/;export\s*{[^}]+};?\s*$/, ';')
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// If no final assignment found, fall back to first declaration replacement
|
|
222
|
+
return code
|
|
223
|
+
.replace(/^let\s+(\w+)\s*=/, `let ${snippetName}=`)
|
|
224
|
+
.replace(/;export\s*{[^}]+};?\s*$/, ';')
|
|
225
|
+
}
|
|
198
226
|
|
|
199
227
|
const snippet = await fs.readFile(snippetPath, 'utf-8')
|
|
200
228
|
const transformed = transformCode(snippet).trim()
|