Created
June 4, 2023 18:06
-
-
Save mabry1985/13b951630f05eebc35c66d8e706dee70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Static to Dynamic | |
// Description: Convert static import to dynamic import | |
// e.g. import { Foo } from "bar"; | |
// to let { Foo } = await import("bar"); | |
// Author: Josh Mabry | |
// Twitter: @AI_Citizen | |
import "@johnlindquist/kit"; | |
const text = await getSelectedText(); | |
function convertImportString(input) { | |
const importRegex = /import\s+({[^}]+})\s+from\s+"([^"]+)";/; | |
if (!importRegex.test(input)) { | |
throw new Error("Invalid import string format"); | |
} | |
const [_, importList, modulePath] = input.match(importRegex); | |
const output = `let ${importList} = await import("${modulePath}");`; | |
return output; | |
} | |
const output = convertImportString(text); | |
await setSelectedText(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment