diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 92a63ee4f3..8455e60ee1 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -97,6 +97,7 @@ export async function initNitro (nuxt: Nuxt) { }, replace: { 'process.env.NUXT_NO_SSR': nuxt.options.ssr === false, + 'process.env.NUXT_NO_SCRIPTS': !!nuxt.options.experimental.noScripts, 'process.env.NUXT_INLINE_STYLES': !!nuxt.options.experimental.inlineSSRStyles, 'process.dev': nuxt.options.dev, __VUE_PROD_DEVTOOLS__: false diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 5c7a19c947..faefa9c92a 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -5,6 +5,8 @@ import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, // Temporary until finding better placement /* eslint-disable import/no-restricted-paths */ import escapeRE from 'escape-string-regexp' +import fse from 'fs-extra' +import { withoutLeadingSlash } from 'ufo' import pagesModule from '../pages/module' import metaModule from '../head/module' import componentsModule from '../components/module' @@ -80,6 +82,18 @@ async function initNuxt (nuxt: Nuxt) { addWebpackPlugin(TreeShakePlugin.webpack({ sourcemap: nuxt.options.sourcemap, treeShake: removeFromClient }), { server: false }) } + // TODO: [Experimental] Avoid emitting assets when flag is enabled + if (nuxt.options.experimental.noScripts) { + nuxt.hook('build:manifest', async (manifest) => { + for (const file in manifest) { + if (manifest[file].resourceType === 'script') { + await fse.rm(resolve(nuxt.options.buildDir, 'dist/client', withoutLeadingSlash(nuxt.options.app.buildAssetsDir), manifest[file].file), { force: true }) + manifest[file].file = '' + } + } + }) + } + // Transpile layers within node_modules nuxt.options.build.transpile.push( ...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string) diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index 658a65eaa5..d26d6f48e3 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -165,8 +165,8 @@ export default defineRenderHandler(async (event) => { _rendered.html ], bodyAppend: normalizeChunks([ - ``, - _rendered.renderScripts(), + process.env.NUXT_NO_SCRIPTS ? '' : ``, + process.env.NUXT_NO_SCRIPTS ? '' : _rendered.renderScripts(), // Note: bodyScripts may contain tags other than