From e0a4f5fbc05e8a0caeff064f4795ce94f85ec4e2 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Mon, 10 May 2021 09:00:05 -0700 Subject: [PATCH] Allow default import component to be renamed based on import statement default specifier (#193) * Allow renaming for default import components * Changeset --- .changeset/many-turtles-move.md | 5 +++++ packages/astro/src/build/page.ts | 21 ++++++++++++------- .../src/pages/default-rename.astro | 12 +++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 .changeset/many-turtles-move.md create mode 100644 packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro diff --git a/.changeset/many-turtles-move.md b/.changeset/many-turtles-move.md new file mode 100644 index 000000000..7d699882c --- /dev/null +++ b/.changeset/many-turtles-move.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allow renaming for default import components diff --git a/packages/astro/src/build/page.ts b/packages/astro/src/build/page.ts index ae472e071..1f0093271 100644 --- a/packages/astro/src/build/page.ts +++ b/packages/astro/src/build/page.ts @@ -222,14 +222,19 @@ async function gatherRuntimes({ astroConfig, buildState, filepath, logging, reso for (const componentImport of componentImports) { const importUrl = componentImport.source.value; const componentType = path.posix.extname(importUrl); - const componentName = path.posix.basename(importUrl, componentType); - const plugin = extensions[componentType] || defaultExtensions[componentType]; - plugins.add(plugin); - components[componentName] = { - plugin, - type: componentType, - specifier: importUrl, - }; + for (const specifier of componentImport.specifiers) { + if (specifier.type === 'ImportDefaultSpecifier') { + const componentName = specifier.local.name; + const plugin = extensions[componentType] || defaultExtensions[componentType]; + plugins.add(plugin); + components[componentName] = { + plugin, + type: componentType, + specifier: importUrl, + }; + break; + } + } } const dynamic = await acquireDynamicComponentImports(plugins, resolvePackageUrl); diff --git a/packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro b/packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro new file mode 100644 index 000000000..8d418fc15 --- /dev/null +++ b/packages/astro/test/fixtures/astro-dynamic/src/pages/default-rename.astro @@ -0,0 +1,12 @@ +--- +import CounterRenamed from '../components/Counter.jsx'; +import SvelteCounterRenamed from '../components/SvelteCounter.svelte'; +--- + +Dynamic pages + + + + + +