Skip to content

Commit

Permalink
Add fetch polyfill plugin #70
Browse files Browse the repository at this point in the history
  • Loading branch information
qrac committed Oct 27, 2022
1 parent ca74a1c commit 86a8c99
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/minista/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { InlineConfig } from "../config/index.js"
import { resolveConfig } from "../config/index.js"
import { pluginSvgr } from "../plugins/svgr.js"
import { pluginSpriteInit, pluginSprite } from "../plugins/sprite.js"
import { pluginFetch } from "../plugins/fetch.js"
import { pluginSsg } from "../plugins/ssg.js"
import { pluginPartial, pluginPartialImport } from "../plugins/partial.js"
import { pluginBundle } from "../plugins/bundle.js"
Expand Down Expand Up @@ -43,6 +44,7 @@ export async function build(inlineConfig: InlineConfig = {}) {
pluginSvgr(config),
pluginSpriteInit(config),
pluginSprite(config),
pluginFetch(config),
pluginSsg(),
pluginPartial(config),
],
Expand Down
2 changes: 2 additions & 0 deletions packages/minista/src/cli/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { InlineConfig } from "../config/index.js"
import { resolveConfig } from "../config/index.js"
import { pluginSvgr } from "../plugins/svgr.js"
import { pluginSpriteInit, pluginSprite } from "../plugins/sprite.js"
import { pluginFetch } from "../plugins/fetch.js"
import { pluginServe } from "../plugins/serve.js"
import { pluginPartial } from "../plugins/partial.js"

Expand All @@ -26,6 +27,7 @@ export async function develop(inlineConfig: InlineConfig = {}) {
pluginSvgr(config),
pluginSpriteInit(config),
pluginSprite(config),
pluginFetch(config),
pluginServe(config),
pluginPartial(config),
],
Expand Down
1 change: 0 additions & 1 deletion packages/minista/src/generate/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export async function generateHtml({
if (!data) {
return
}
data = `import { fetch } from "undici"\n` + data

ssgPath = path.join(config.sub.tempDir, fileName)

Expand Down
40 changes: 40 additions & 0 deletions packages/minista/src/plugins/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Plugin } from "vite"
import path from "node:path"
import fs from "fs-extra"
import picomatch from "picomatch"

import type { ResolvedConfig } from "../config/index.js"

export function pluginFetch(config: ResolvedConfig): Plugin {
return {
name: "minista-vite-plugin:fetch",
load(id) {
if (
picomatch.isMatch(id, [
path.join(config.sub.resolvedRoot, "src/pages/_global.{tsx,jsx}"),
path.join(config.sub.resolvedRoot, "src/_global.{tsx,jsx}"),
path.join(config.sub.resolvedRoot, "src/global.{tsx,jsx}"),
path.join(config.sub.resolvedRoot, "src/root.{tsx,jsx}"),
path.join(config.sub.resolvedRoot, "src/pages/**/*.{tsx,jsx,mdx,md}"),
])
) {
const code = fs.readFileSync(id, { encoding: "utf8" })
const hasGetStaticData = code.includes("getStaticData")
const useAwaitFetch = code.includes("await fetch")
const useUndiciFetch = code.match(
/import.*fetch.*from ("|')undici("|')/
)
const useNodeFetch = code.match(/import.*from ("|')node-fetch("|')/)

if (
hasGetStaticData &&
useAwaitFetch &&
!useUndiciFetch &&
!useNodeFetch
) {
return `import { fetch } from "undici"\n` + code
}
}
},
}
}

0 comments on commit 86a8c99

Please sign in to comment.