Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate Vite resolve id to url #4239

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-socks-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Astro client scripts sourcemap 404
7 changes: 4 additions & 3 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fileURLToPath } from 'url';
import path from 'path';
import type { ViteDevServer } from 'vite';
import type {
AstroConfig,
Expand All @@ -12,7 +13,7 @@ import type {
import { prependForwardSlash } from '../../../core/path.js';
import { PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
import { LogOptions } from '../../logger/core.js';
import { isPage } from '../../util.js';
import { isPage, resolveIdToUrl } from '../../util.js';
import { render as coreRender } from '../core.js';
import { RouteCache } from '../route-cache.js';
import { collectMdMetadata } from '../util.js';
Expand Down Expand Up @@ -116,7 +117,7 @@ export async function render(
scripts.add({
props: {
type: 'module',
src: '/@id/astro/runtime/client/hmr.js',
src: await resolveIdToUrl(viteServer, 'astro/runtime/client/hmr.js'),
},
children: '',
});
Expand Down Expand Up @@ -186,7 +187,7 @@ export async function render(
if (s.startsWith('/@fs')) {
return resolveClientDevPath(s);
}
return '/@id' + prependForwardSlash(s);
return await resolveIdToUrl(viteServer, s);
},
renderers,
request,
Expand Down
20 changes: 18 additions & 2 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import path from 'path';
import resolve from 'resolve';
import slash from 'slash';
import { fileURLToPath, pathToFileURL } from 'url';
import type { ErrorPayload } from 'vite';
import type { ErrorPayload, ViteDevServer } from 'vite';
import type { AstroConfig } from '../@types/astro';
import { removeTrailingForwardSlash } from './path.js';
import { prependForwardSlash, removeTrailingForwardSlash } from './path.js';

// process.env.PACKAGE_VERSION is injected when we build and publish the astro package.
export const ASTRO_VERSION = process.env.PACKAGE_VERSION ?? 'development';
Expand Down Expand Up @@ -207,3 +207,19 @@ export function getLocalAddress(serverAddress: string, host: string | boolean):
return serverAddress;
}
}

/**
* Simulate Vite's resolve and import analysis so we can import the id as an URL
* through a script tag or a dynamic import as-is.
*/
// NOTE: `/@id/` should only be used when the id is fully resolved
export async function resolveIdToUrl(viteServer: ViteDevServer, id: string) {
const result = await viteServer.pluginContainer.resolveId(id);
if (!result) {
return VALID_ID_PREFIX + id;
}
if (path.isAbsolute(result.id)) {
return '/@fs' + prependForwardSlash(result.id);
}
return VALID_ID_PREFIX + result.id;
}