Skip to content

Commit

Permalink
Merge pull request #20 from marcusirgens/single-quotes
Browse files Browse the repository at this point in the history
Support single-quoted import statements
  • Loading branch information
haruaki07 authored Dec 30, 2023
2 parents 1fd16e8 + 540a735 commit 54fb8c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 3 additions & 3 deletions preprocessor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function phosphorSvelteOptimize() {
if (!filename || /node_modules/.test(filename)) return

const re = new RegExp(
/import\s*{([\s\S]*?)\s*}\s*from\s*"phosphor-svelte"/g
/import\s*{([\s\S]*?)\s*}\s*from\s*["']phosphor-svelte["']([\s\n]*;)?/g
)

let output = new MagicString(content, { filename })
Expand Down Expand Up @@ -44,8 +44,8 @@ export function phosphorSvelteOptimize() {

output.update(
match.index,
match.index + match[0].length + 1,
newImports.join("\n")
match.index + match[0].length,
newImports.join("\n")
)
}

Expand Down
42 changes: 42 additions & 0 deletions tests/preprocessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,47 @@ import IconGear from "phosphor-svelte/lib/Gear";

expect(result.code).toBe(output)
})

describe("should parse", () => {
it("single quotes", async () => {
const processor = phosphorSvelteOptimize()
const content = `<script>
import { Airplane } from 'phosphor-svelte';
</script>\n`;

const result = await preprocess(content, processor, {
filename: "App.svelte",
})
const output = `<script>
import Airplane from "phosphor-svelte/lib/Airplane";
</script>\n`

expect(result.code).toBe(output)
})

it("multiline imports", async () => {
const processor = phosphorSvelteOptimize()
const content = `<script lang="ts">
import {
Airplane,
type Hourglass as HourglassAlias,
FolderNotch,
} from "phosphor-svelte";
import { Ghost } from 'phosphor-svelte';
</script>\n`;

const result = await preprocess(content, processor, {
filename: "App.svelte",
})
const output = `<script lang="ts">
import Airplane from "phosphor-svelte/lib/Airplane";
import FolderNotch from "phosphor-svelte/lib/FolderNotch";
import type { Hourglass as HourglassAlias } from "phosphor-svelte";
import Ghost from "phosphor-svelte/lib/Ghost";
</script>\n`

expect(result.code).toBe(output)
})
})
})
})

0 comments on commit 54fb8c5

Please sign in to comment.