Skip to content

Commit

Permalink
fix: alias astro to @types/astro (#3503)
Browse files Browse the repository at this point in the history
* fix: alias astro to @types/astro

* fix: handle resolve.alias being array

* chore: add integrations patch to changeset

* chore: remove empty file
  • Loading branch information
williamtetlow committed Jun 2, 2022
1 parent 67ad33d commit 207f58d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .changeset/spicy-turkeys-clean.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 13 additions & 5 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
19 changes: 19 additions & 0 deletions packages/astro/test/fixtures/type-imports/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import type { MarkdownInstance } from 'astro'
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Astro</title>
<style is:global>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>Astro</h1>
<img src="/puppy.png"/>
</body>
</html>
16 changes: 16 additions & 0 deletions packages/astro/test/type-imports.test.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
13 changes: 11 additions & 2 deletions packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
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<string, string>)[alias.find] = alias.replacement;
}
}

vite.ssr = {
noExternal: true,
};
Expand Down
13 changes: 11 additions & 2 deletions packages/integrations/netlify/src/integration-edge-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
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<string, string>)[alias.find] = alias.replacement;
}
}

vite.ssr = {
noExternal: true,
};
Expand Down

0 comments on commit 207f58d

Please sign in to comment.