refactor: remove isSSR util
This commit is contained in:
parent
ce2b2848c6
commit
aa6109daca
2 changed files with 17 additions and 46 deletions
|
@ -3,20 +3,6 @@ import type { BaseNode, Identifier } from 'estree';
|
|||
import MagicString from 'magic-string';
|
||||
import { walk } from 'estree-walker';
|
||||
|
||||
// https://github.com/vitejs/vite/discussions/5109#discussioncomment-1450726
|
||||
function isSSR(options: undefined | boolean | { ssr?: boolean }): boolean {
|
||||
if (options === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (typeof options === 'boolean') {
|
||||
return options;
|
||||
}
|
||||
if (typeof options == 'object') {
|
||||
return !!options.ssr;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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)$/;
|
||||
|
@ -28,23 +14,23 @@ function isIdentifier(node: BaseNode): node is Identifier {
|
|||
}
|
||||
|
||||
export default function pluginFetch(): Plugin {
|
||||
return {
|
||||
name: '@astrojs/vite-plugin-fetch',
|
||||
enforce: 'post',
|
||||
async transform(code, id, opts) {
|
||||
const ssr = isSSR(opts);
|
||||
// If this isn't an SSR pass, `fetch` will already be available!
|
||||
if (!ssr) {
|
||||
return null;
|
||||
}
|
||||
// Only transform JS-like files
|
||||
if (!id.match(SUPPORTED_FILES)) {
|
||||
return null;
|
||||
}
|
||||
// Optimization: only run on probable matches
|
||||
if (!code.includes('fetch')) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name: '@astrojs/vite-plugin-fetch',
|
||||
enforce: 'post',
|
||||
async transform(code, id, opts) {
|
||||
const ssr = Boolean(opts?.ssr);
|
||||
// If this isn't an SSR pass, `fetch` will already be available!
|
||||
if (!ssr) {
|
||||
return null;
|
||||
}
|
||||
// Only transform JS-like files
|
||||
if (!id.match(SUPPORTED_FILES)) {
|
||||
return null;
|
||||
}
|
||||
// Optimization: only run on probable matches
|
||||
if (!code.includes('fetch')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const ast = this.parse(code);
|
||||
let fetchDeclared = false;
|
||||
|
|
|
@ -24,21 +24,6 @@ const IMPORT_STATEMENTS: Record<string, string> = {
|
|||
// be careful about esbuild not treating h, React, Fragment, etc. as unused.
|
||||
const PREVENT_UNUSED_IMPORTS = ';;(React,Fragment,h);';
|
||||
|
||||
// This check on a flexible "options" object is needed because Vite uses this flexible argument for ssr.
|
||||
// More context: https://github.com/vitejs/vite/discussions/5109#discussioncomment-1450726
|
||||
function isSSR(options: undefined | boolean | { ssr: boolean }): boolean {
|
||||
if (options === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (typeof options === 'boolean') {
|
||||
return options;
|
||||
}
|
||||
if (typeof options == 'object') {
|
||||
return !!options.ssr;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getEsbuildLoader(fileExt: string): string {
|
||||
return fileExt.substr(1);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue