Skip to content

Commit

Permalink
feat: use nitro to load polyfill at the earliest
Browse files Browse the repository at this point in the history
  • Loading branch information
trijpstra-fourlights committed Jun 18, 2023
1 parent f5a6be1 commit f15b519
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
defineNuxtModule,
addPlugin,
addServerHandler,
createResolver,
extendViteConfig,
addServerPlugin,
} from '@nuxt/kit'
import { withQuery } from 'ufo'
import polyfillist from 'polyfillist'
Expand All @@ -27,7 +27,8 @@ export default defineNuxtModule<ModuleOptions>({
host: 'https://polyfill.io/v3/polyfill.min.js',
},
async setup (options, nuxt) {
const resolver = createResolver(import.meta.url)
const resolver = createResolver(import.meta.url)

const features = await polyfillist(options.target)
const isSelfHost = options.host === 'selfhost'
const src = options.host && !isSelfHost
Expand All @@ -37,8 +38,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.runtimeConfig.nupolyon = { features }
nuxt.options.runtimeConfig.public.nupolyon = { src, isSelfHost }

// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
addPlugin(resolver.resolve('./runtime/plugin'))
addServerPlugin(resolver.resolve('./runtime/server/plugins/polyfill'))

if (isSelfHost) {
addServerHandler({
Expand Down
34 changes: 0 additions & 34 deletions src/runtime/plugin.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/runtime/server/plugins/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { joinURL, cleanDoubleSlashes } from 'ufo'
import { useRuntimeConfig } from "#imports";

/**
* Due to an upstream bug in Nuxt 3 we need to stub the `defineNitroPlugin`.
* See https://github.com/nuxt/nuxt/issues/18556
*
* Once this issue is resolved, then this file can be updated to the following:
*
* ```ts
* import { defineNitroPlugin } from '#imports'
* ```
*/
import { NitroApp } from 'nitropack'

export type NitroAppPlugin = (nitro: NitroApp) => void
export function defineNitroPlugin (def: NitroAppPlugin): NitroAppPlugin {
return def
}
/**
* End of workaround
*/

export default defineNitroPlugin((nitroApp) => {
const config = useRuntimeConfig()

const { src, isSelfHost } = config.public.nupolyon
// Prepend the current runtime value of app.baseURL if configured for self-hosting
const host = !isSelfHost ? src : cleanDoubleSlashes(joinURL(config.app.baseURL, src))

if (host) {
// NOTE: By setting `nomodule` we make sure it executed directly upon loading, and also works in browsers which don't support ESM.
const polyfill = `<script src="${host}" crossorigin="anonymous" nomodule></script>`
nitroApp.hooks.hook('render:html', (html) => {
html.head.unshift(polyfill) // insert at the beginning of the array
})
}
})

0 comments on commit f15b519

Please sign in to comment.