Bugfix: plugin-astro-fetch tries to append node-fetch to node-fetch (#1671)
This commit is contained in:
parent
bd2ac13753
commit
62684dbe4b
2 changed files with 17 additions and 8 deletions
|
@ -12,7 +12,16 @@ import fetchVitePlugin from '../vite-plugin-fetch/index.js';
|
||||||
import { getPackageJSON, resolveDependency } from './util.js';
|
import { getPackageJSON, resolveDependency } from './util.js';
|
||||||
|
|
||||||
// Some packages are just external, and that’s the way it goes.
|
// Some packages are just external, and that’s 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([
|
const ALWAYS_NOEXTERNAL = new Set([
|
||||||
'astro', // This is only because Vite's native ESM doesn't resolve "exports" correctly.
|
'astro', // This is only because Vite's native ESM doesn't resolve "exports" correctly.
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -18,7 +18,7 @@ function isSSR(options: undefined | boolean | { ssr: boolean }): boolean {
|
||||||
// This matches any JS-like file (that we know of)
|
// This matches any JS-like file (that we know of)
|
||||||
// See https://regex101.com/r/Cgofir/1
|
// See https://regex101.com/r/Cgofir/1
|
||||||
const SUPPORTED_FILES = /\.(astro|svelte|vue|[cm]?js|jsx|[cm]?ts|tsx)$/;
|
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`;
|
const DEFINE_FETCH = `import fetch from 'node-fetch';\n`;
|
||||||
|
|
||||||
export default function pluginFetch(): Plugin {
|
export default function pluginFetch(): Plugin {
|
||||||
|
@ -26,11 +26,6 @@ export default function pluginFetch(): Plugin {
|
||||||
name: '@astrojs/vite-plugin-fetch',
|
name: '@astrojs/vite-plugin-fetch',
|
||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
async transform(code, id, opts) {
|
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);
|
const ssr = isSSR(opts);
|
||||||
// If this isn't an SSR pass, `fetch` will already be available!
|
// If this isn't an SSR pass, `fetch` will already be available!
|
||||||
if (!ssr) {
|
if (!ssr) {
|
||||||
|
@ -44,7 +39,12 @@ export default function pluginFetch(): Plugin {
|
||||||
if (!code.includes('fetch')) {
|
if (!code.includes('fetch')) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// Ignore specific modules
|
||||||
|
for (const ignored of IGNORED_MODULES) {
|
||||||
|
if (id.match(ignored)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
const s = new MagicString(code);
|
const s = new MagicString(code);
|
||||||
s.prepend(DEFINE_FETCH);
|
s.prepend(DEFINE_FETCH);
|
||||||
const result = s.toString();
|
const result = s.toString();
|
||||||
|
|
Loading…
Reference in a new issue