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
This commit is contained in:
William Tetlow 2022-06-02 18:54:35 +01:00 committed by GitHub
parent 67ad33debf
commit 207f58d171
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 9 deletions

View file

@ -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

View file

@ -96,11 +96,19 @@ export async function createVite(
postcss: astroConfig.style.postcss || {}, postcss: astroConfig.style.postcss || {},
}, },
resolve: { resolve: {
alias: { alias: [
// This is needed for Deno compatibility, as the non-browser version {
// of this module depends on Node `crypto` // This is needed for Deno compatibility, as the non-browser version
randombytes: 'randombytes/browser', // 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) // Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html)
ssr: { ssr: {

View file

@ -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>

View file

@ -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;
});
});

View file

@ -25,8 +25,17 @@ export default function createIntegration(args?: Options): AstroIntegration {
if (target === 'server') { if (target === 'server') {
vite.resolve = vite.resolve || {}; vite.resolve = vite.resolve || {};
vite.resolve.alias = vite.resolve.alias || {}; 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 = { vite.ssr = {
noExternal: true, noExternal: true,
}; };

View file

@ -89,8 +89,17 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {})
if (target === 'server') { if (target === 'server') {
vite.resolve = vite.resolve || {}; vite.resolve = vite.resolve || {};
vite.resolve.alias = vite.resolve.alias || {}; 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 = { vite.ssr = {
noExternal: true, noExternal: true,
}; };