diff --git a/packages/md/src/extract-script.ts b/packages/md/src/extract-script.ts index 19e9ffd5a..a31c9d5e0 100644 --- a/packages/md/src/extract-script.ts +++ b/packages/md/src/extract-script.ts @@ -1,8 +1,8 @@ const SCRIPT_RE = /^]*>([\s\S]*?)<\/script>\s*\n?/; const ISLAND_IMPORT_RE = - /import\s+(\w+)\s+from\s+("[^"]+"|'[^']+')\s+with\s*\{\s*island\s*:\s*("[^"]+"|'[^']+')\s*\}/g; + /import\s+([$_A-Za-z][$_\w]*)\s+from\s+("[^"]+"|'[^']+')\s+with\s*\{\s*island\s*:\s*("[^"]+"|'[^']+')\s*,?\s*\}/g; const ISLAND_IMPORT_LINE_RE = - /^[ \t]*import\s+\w+\s+from\s+(?:"[^"]+"|'[^']+')\s+with\s*\{\s*island\s*:\s*(?:"[^"]+"|'[^']+')\s*\}\s*;?\s*$/gm; + /^[ \t]*import\s+[$_A-Za-z][$_\w]*\s+from\s+(?:"[^"]+"|'[^']+')\s+with\s*\{\s*island\s*:\s*(?:"[^"]+"|'[^']+')\s*,?\s*\}\s*;?\s*$/gm; export type IslandImport = { name: string; diff --git a/packages/md/test/unit/extract-script.test.ts b/packages/md/test/unit/extract-script.test.ts index fc233d685..667df4ac7 100644 --- a/packages/md/test/unit/extract-script.test.ts +++ b/packages/md/test/unit/extract-script.test.ts @@ -133,4 +133,33 @@ const count = ref(0) const result = extractScript(source); expect(result.clientCode).toBe(`import { ref } from "vue"\n\nconst count = ref(0)`); }); + + it('extracts island imports with a trailing comma in the attributes', () => { + const source = ` + +# Hello`; + const result = extractScript(source); + expect(result.islandImports).toHaveLength(1); + expect(result.islandImports[0]).toEqual({ + name: 'Counter', + specifier: './Counter.vue', + strategy: 'load', + }); + // the island import line is stripped from client code (not leaked as an orphan import) + expect(result.clientCode).toBeNull(); + }); + + it('extracts island imports whose component name starts with $ or _', () => { + const source = ` + +# Hello`; + const result = extractScript(source); + expect(result.islandImports).toHaveLength(1); + expect(result.islandImports[0].name).toBe('$Counter'); + expect(result.clientCode).toBeNull(); + }); });