Skip to content

Commit

Permalink
fix: improved bundle: true debug and cache clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 15, 2024
1 parent 5411a75 commit d269066
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export default defineNuxtConfig({

devtools: { enabled: true },
compatibilityDate: '2024-07-14',

scripts: {
debug: true,
},
})
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.alias['#nuxt-scripts-validator'] = resolve(`./runtime/validation/${(nuxt.options.dev || nuxt.options._prepare) ? 'valibot' : 'mock'}`)
nuxt.options.alias['#nuxt-scripts'] = resolve('./runtime/types')
nuxt.options.alias['#nuxt-scripts-utils'] = resolve('./runtime/utils')
logger.level = (config.debug || nuxt.options.debug) ? 4 : 3
if (!config.enabled) {
// TODO fallback to useHead?
logger.debug('The module is disabled, skipping setup.')
Expand Down
18 changes: 13 additions & 5 deletions src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,22 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
// done after all transformation is done
// copy all scripts to build
nuxt.hooks.hook('build:done', async () => {
logger.log('[nuxt:scripts:bundler-transformer] Bundling scripts...')
await fsp.rm(cacheDir, { recursive: true, force: true })
await fsp.mkdir(cacheDir, { recursive: true })
await Promise.all([...renderedScript].map(async ([url, content]) => {
const scripts = [...renderedScript]
if (!scripts.length) {
logger.debug('[bundle-script-transformer] No scripts to bundle...')
return
}
logger.info('[bundle-script-transformer] Bundling scripts...')
// less aggressive cache clearing in dev
if (!nuxt.options.dev) {
await fsp.rm(cacheDir, { recursive: true, force: true })
await fsp.mkdir(cacheDir, { recursive: true })
}
await Promise.all(scripts.map(async ([url, content]) => {
if (content instanceof Error || !content.filename)
return
await fsp.writeFile(join(nuxt.options.buildDir, 'cache', 'scripts', content.filename), content.content)
logger.log(colors.gray(` ├─ ${url}${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`))
logger.info(colors.gray(` ├─ ${url}${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`))
}))
})

Expand Down

0 comments on commit d269066

Please sign in to comment.