Skip to content

Commit d269066

Browse files
committed
fix: improved bundle: true debug and cache clearing
1 parent 5411a75 commit d269066

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

playground/nuxt.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ export default defineNuxtConfig({
66

77
devtools: { enabled: true },
88
compatibilityDate: '2024-07-14',
9+
10+
scripts: {
11+
debug: true,
12+
},
913
})

src/module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export default defineNuxtModule<ModuleOptions>({
101101
nuxt.options.alias['#nuxt-scripts-validator'] = resolve(`./runtime/validation/${(nuxt.options.dev || nuxt.options._prepare) ? 'valibot' : 'mock'}`)
102102
nuxt.options.alias['#nuxt-scripts'] = resolve('./runtime/types')
103103
nuxt.options.alias['#nuxt-scripts-utils'] = resolve('./runtime/utils')
104+
logger.level = (config.debug || nuxt.options.debug) ? 4 : 3
104105
if (!config.enabled) {
105106
// TODO fallback to useHead?
106107
logger.debug('The module is disabled, skipping setup.')

src/plugins/transform.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,22 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
104104
// done after all transformation is done
105105
// copy all scripts to build
106106
nuxt.hooks.hook('build:done', async () => {
107-
logger.log('[nuxt:scripts:bundler-transformer] Bundling scripts...')
108-
await fsp.rm(cacheDir, { recursive: true, force: true })
109-
await fsp.mkdir(cacheDir, { recursive: true })
110-
await Promise.all([...renderedScript].map(async ([url, content]) => {
107+
const scripts = [...renderedScript]
108+
if (!scripts.length) {
109+
logger.debug('[bundle-script-transformer] No scripts to bundle...')
110+
return
111+
}
112+
logger.info('[bundle-script-transformer] Bundling scripts...')
113+
// less aggressive cache clearing in dev
114+
if (!nuxt.options.dev) {
115+
await fsp.rm(cacheDir, { recursive: true, force: true })
116+
await fsp.mkdir(cacheDir, { recursive: true })
117+
}
118+
await Promise.all(scripts.map(async ([url, content]) => {
111119
if (content instanceof Error || !content.filename)
112120
return
113121
await fsp.writeFile(join(nuxt.options.buildDir, 'cache', 'scripts', content.filename), content.content)
114-
logger.log(colors.gray(` ├─ ${url}${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`))
122+
logger.info(colors.gray(` ├─ ${url}${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`))
115123
}))
116124
})
117125

0 commit comments

Comments
 (0)