refactor: remove isSSR util

This commit is contained in:
Nate Moore 2021-12-07 09:52:22 -06:00
parent ce2b2848c6
commit aa6109daca
2 changed files with 17 additions and 46 deletions

View file

@ -3,20 +3,6 @@ import type { BaseNode, Identifier } from 'estree';
import MagicString from 'magic-string'; import MagicString from 'magic-string';
import { walk } from 'estree-walker'; 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) // 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)$/;
@ -28,23 +14,23 @@ function isIdentifier(node: BaseNode): node is Identifier {
} }
export default function pluginFetch(): Plugin { export default function pluginFetch(): Plugin {
return { return {
name: '@astrojs/vite-plugin-fetch', name: '@astrojs/vite-plugin-fetch',
enforce: 'post', enforce: 'post',
async transform(code, id, opts) { async transform(code, id, opts) {
const ssr = isSSR(opts); const ssr = Boolean(opts?.ssr);
// 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) {
return null; return null;
} }
// Only transform JS-like files // Only transform JS-like files
if (!id.match(SUPPORTED_FILES)) { if (!id.match(SUPPORTED_FILES)) {
return null; return null;
} }
// Optimization: only run on probable matches // Optimization: only run on probable matches
if (!code.includes('fetch')) { if (!code.includes('fetch')) {
return null; return null;
} }
const ast = this.parse(code); const ast = this.parse(code);
let fetchDeclared = false; let fetchDeclared = false;

View file

@ -24,21 +24,6 @@ const IMPORT_STATEMENTS: Record<string, string> = {
// be careful about esbuild not treating h, React, Fragment, etc. as unused. // be careful about esbuild not treating h, React, Fragment, etc. as unused.
const PREVENT_UNUSED_IMPORTS = ';;(React,Fragment,h);'; 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 { function getEsbuildLoader(fileExt: string): string {
return fileExt.substr(1); return fileExt.substr(1);
} }