From 207f58d1715ac024cc7c81b76e26aa49fca5173f Mon Sep 17 00:00:00 2001 From: William Tetlow <9057181+williamtetlow@users.noreply.github.com> Date: Thu, 2 Jun 2022 18:54:35 +0100 Subject: [PATCH] fix: alias astro to @types/astro (#3503) * fix: alias astro to @types/astro * fix: handle resolve.alias being array * chore: add integrations patch to changeset * chore: remove empty file --- .changeset/spicy-turkeys-clean.md | 8 ++++++++ packages/astro/src/core/create-vite.ts | 18 +++++++++++++----- .../type-imports/src/pages/index.astro | 19 +++++++++++++++++++ packages/astro/test/type-imports.test.js | 16 ++++++++++++++++ packages/integrations/deno/src/index.ts | 13 +++++++++++-- .../netlify/src/integration-edge-functions.ts | 13 +++++++++++-- 6 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 .changeset/spicy-turkeys-clean.md create mode 100644 packages/astro/test/fixtures/type-imports/src/pages/index.astro create mode 100644 packages/astro/test/type-imports.test.js diff --git a/.changeset/spicy-turkeys-clean.md b/.changeset/spicy-turkeys-clean.md new file mode 100644 index 000000000..d417138b1 --- /dev/null +++ b/.changeset/spicy-turkeys-clean.md @@ -0,0 +1,8 @@ +--- +'astro': patch +'@astrojs/deno': patch +'@astrojs/netlify': patch +--- + +Alias `from 'astro'` imports to `'@astro/types'` +Update Deno and Netlify integrations to handle vite.resolves.alias as an array diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 8270028d4..a3feff5aa 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -96,11 +96,19 @@ export async function createVite( postcss: astroConfig.style.postcss || {}, }, resolve: { - alias: { - // This is needed for Deno compatibility, as the non-browser version - // of this module depends on Node `crypto` - randombytes: 'randombytes/browser', - }, + alias: [ + { + // This is needed for Deno compatibility, as the non-browser version + // of this module depends on Node `crypto` + find: 'randombytes', + replacement: 'randombytes/browser', + }, + { + // Typings are imported from 'astro' (e.g. import { Type } from 'astro') + find: /^astro$/, + replacement: fileURLToPath(new URL('../@types/astro', import.meta.url)), + }, + ], }, // Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html) ssr: { diff --git a/packages/astro/test/fixtures/type-imports/src/pages/index.astro b/packages/astro/test/fixtures/type-imports/src/pages/index.astro new file mode 100644 index 000000000..3577834fb --- /dev/null +++ b/packages/astro/test/fixtures/type-imports/src/pages/index.astro @@ -0,0 +1,19 @@ +--- +import type { MarkdownInstance } from 'astro' +--- + + + + + Astro + + + +

Astro

+ + + diff --git a/packages/astro/test/type-imports.test.js b/packages/astro/test/type-imports.test.js new file mode 100644 index 000000000..d420f4eac --- /dev/null +++ b/packages/astro/test/type-imports.test.js @@ -0,0 +1,16 @@ +import { expect } from 'chai'; +import { loadFixture } from './test-utils.js'; + +describe('Type Imports', async () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ root: './fixtures/type-imports' }); + await fixture.build(); + }); + + it('Allows importing types from "astro"', async () => { + // if the build passes then the test succeeds + expect(true).to.be.true; + }); +}); diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 3959e63a8..aef1f8383 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -25,8 +25,17 @@ export default function createIntegration(args?: Options): AstroIntegration { if (target === 'server') { vite.resolve = vite.resolve || {}; vite.resolve.alias = vite.resolve.alias || {}; - const alias = vite.resolve.alias as Record; - alias['react-dom/server'] = 'react-dom/server.browser'; + + const aliases = [{ find: 'react-dom/server', replacement: 'react-dom/server.browser' }]; + + if (Array.isArray(vite.resolve.alias)) { + vite.resolve.alias = [...vite.resolve.alias, ...aliases]; + } else { + for (const alias of aliases) { + (vite.resolve.alias as Record)[alias.find] = alias.replacement; + } + } + vite.ssr = { noExternal: true, }; diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 843caedc1..9b4503e2b 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -89,8 +89,17 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) if (target === 'server') { vite.resolve = vite.resolve || {}; vite.resolve.alias = vite.resolve.alias || {}; - const alias = vite.resolve.alias as Record; - alias['react-dom/server'] = 'react-dom/server.browser'; + + const aliases = [{ find: 'react-dom/server', replacement: 'react-dom/server.browser' }]; + + if (Array.isArray(vite.resolve.alias)) { + vite.resolve.alias = [...vite.resolve.alias, ...aliases]; + } else { + for (const alias of aliases) { + (vite.resolve.alias as Record)[alias.find] = alias.replacement; + } + } + vite.ssr = { noExternal: true, };