Skip to content

Commit

Permalink
Fix dynamic/import
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Aug 5, 2024
1 parent bf6fb13 commit 6394a9c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/plugins/next-dynamic/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@ import type { Plugin } from "vite";
export const vitePluginNextDynamic = () =>
({
name: "vite-plugin-storybook-nextjs-dynamic",
transform(code) {
transform(code, id) {
// Regex to match the dynamic import pattern
const dynamicImportRegex =
/dynamic\(\s*async\s*\(\s*\)\s*=>\s*\{\s*typeof\s*require\.resolveWeak\s*!==\s*"undefined"\s*&&\s*require\.resolveWeak\(([^)]+)\);\s*\}/g;

// Check if the code matches the pattern
if (dynamicImportRegex.test(code)) {
const s = new MagicString(code);
dynamicImportRegex.lastIndex = 0;

let match: RegExpExecArray | null;
let match = dynamicImportRegex.exec(code);

// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
while ((match = dynamicImportRegex.exec(code)) !== null) {
while (match !== null) {
const [fullMatch, importPath] = match;

// Construct the new import statement
const newImport = `dynamic(() => import(${importPath})`;

// Replace the old code with the new import statement
s.overwrite(match.index, match.index + fullMatch.length, newImport);
match = dynamicImportRegex.exec(code);
}

return {
Expand Down

0 comments on commit 6394a9c

Please sign in to comment.