Skip to content

Instantly share code, notes, and snippets.

@mabry1985
Created June 4, 2023 18:06
Show Gist options
  • Save mabry1985/13b951630f05eebc35c66d8e706dee70 to your computer and use it in GitHub Desktop.
Save mabry1985/13b951630f05eebc35c66d8e706dee70 to your computer and use it in GitHub Desktop.
// 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