Skip to content

Commit

Permalink
fix: use new nitropack public assets dir & app.head
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 15, 2022
1 parent 4771c58 commit bf4cfca
Show file tree
Hide file tree
Showing 3 changed files with 905 additions and 762 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@builder.io/partytown": "^0.5.2",
"@nuxt/kit": "npm:@nuxt/kit-edge@latest",
"knitwork": "^0.1.1",
"serve-static": "^1.15.0",
"ufo": "^0.8.3"
},
"devDependencies": {
Expand Down
23 changes: 8 additions & 15 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { join } from 'path'
import { promises as fsp } from 'fs'
import { genObjectFromRawEntries } from 'knitwork'
import serveStatic from 'serve-static'
import { defineNuxtModule, isNuxt2 } from '@nuxt/kit'
import type { PartytownConfig } from '@builder.io/partytown/integration'
import { copyLibFiles, libDirPath } from '@builder.io/partytown/utils'
Expand Down Expand Up @@ -82,24 +81,18 @@ export default defineNuxtModule<ModuleOptions>({
)
} else {
// Use @vueuse/head syntax to inject scripts
nuxt.options.meta.script = nuxt.options.meta.script || []
nuxt.options.meta.script.unshift(
nuxt.options.app.head.script = nuxt.options.app.head.script || []
nuxt.options.app.head.script.unshift(
{ children: `partytown = ${renderedConfig}` },
{ children: partytownSnippet }
)
}

if (nuxt.options.dev) {
// Serve the partytown library directly from node_modules in development
nuxt.options.serverMiddleware.push({
path: options.lib,
handler: serveStatic(libDirPath()),
})
} else {
// Copy partytown directory into .output/public in production build
nuxt.hook('nitro:generate', async ctx => {
await copyLibFiles(join(ctx.output.publicDir, options.lib), { debugDir: options.debug })
})
}
// Add the partytown library directly from node_modules
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || []
nuxt.options.nitro.publicAssets.push({
baseURL: options.lib,
dir: libDirPath(),
})
},
})
Loading

0 comments on commit bf4cfca

Please sign in to comment.