Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/md/src/extract-script.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const SCRIPT_RE = /^<script\b[^>]*>([\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;
Expand Down
29 changes: 29 additions & 0 deletions packages/md/test/unit/extract-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<script>
import Counter from "./Counter.vue" with { island: "load", }
</script>

# 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 = `<script>
import $Counter from "./Counter.vue" with { island: "load" }
</script>

# Hello`;
const result = extractScript(source);
expect(result.islandImports).toHaveLength(1);
expect(result.islandImports[0].name).toBe('$Counter');
expect(result.clientCode).toBeNull();
});
});