0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Bugfix: plugin-astro-fetch tries to append node-fetch to node-fetch (#1671)

This commit is contained in:
Drew Powers 2021-10-27 09:37:29 -06:00 committed by GitHub
parent bd2ac13753
commit 62684dbe4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View file

@ -12,7 +12,16 @@ import fetchVitePlugin from '../vite-plugin-fetch/index.js';
import { getPackageJSON, resolveDependency } from './util.js';
// Some packages are just external, and thats the way it goes.
const ALWAYS_EXTERNAL = new Set(['@sveltejs/vite-plugin-svelte', 'estree-util-value-to-estree', 'micromark-util-events-to-acorn', 'prismjs', 'shorthash', 'unified']);
const ALWAYS_EXTERNAL = new Set([
'@sveltejs/vite-plugin-svelte',
'estree-util-value-to-estree',
'micromark-util-events-to-acorn',
'node-fetch',
'prismjs',
'shorthash',
'unified',
'whatwg-url',
]);
const ALWAYS_NOEXTERNAL = new Set([
'astro', // This is only because Vite's native ESM doesn't resolve "exports" correctly.
]);

View file

@ -18,7 +18,7 @@ function isSSR(options: undefined | boolean | { ssr: boolean }): boolean {
// This matches any JS-like file (that we know of)
// See https://regex101.com/r/Cgofir/1
const SUPPORTED_FILES = /\.(astro|svelte|vue|[cm]?js|jsx|[cm]?ts|tsx)$/;
const IGNORED_FILES = new Set(['astro/dist/runtime/server/index.js']);
const IGNORED_MODULES = [/astro\/dist\/runtime\/server/, /\/node-fetch\//];
const DEFINE_FETCH = `import fetch from 'node-fetch';\n`;
export default function pluginFetch(): Plugin {
@ -26,11 +26,6 @@ export default function pluginFetch(): Plugin {
name: '@astrojs/vite-plugin-fetch',
enforce: 'post',
async transform(code, id, opts) {
// Ignore internal files, etc.
for (const ignored of IGNORED_FILES) {
if (id.endsWith(ignored)) return null;
}
const ssr = isSSR(opts);
// If this isn't an SSR pass, `fetch` will already be available!
if (!ssr) {
@ -44,7 +39,12 @@ export default function pluginFetch(): Plugin {
if (!code.includes('fetch')) {
return null;
}
// Ignore specific modules
for (const ignored of IGNORED_MODULES) {
if (id.match(ignored)) {
return null;
}
}
const s = new MagicString(code);
s.prepend(DEFINE_FETCH);
const result = s.toString();