From 180f60d06010c2a83be10929fac85f9f166a4f03 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Tue, 24 Dec 2024 14:23:17 -0500 Subject: [PATCH] align none optimization option with output filename and contents --- packages/cli/src/config/rollup.config.js | 7 +- packages/cli/src/lifecycles/bundle.js | 105 +++++++++--------- .../plugins/resource/plugin-standard-css.js | 4 +- .../plugins/resource/plugin-standard-html.js | 2 +- .../build.config-optimization-none.spec.js | 26 ++++- 5 files changed, 77 insertions(+), 67 deletions(-) diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index a4d07608b..585f7de53 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -120,8 +120,7 @@ function greenwoodSyncPageResourceBundlesPlugin(compilation) { if (resourceKey === facadeModuleId) { const { fileName } = bundles[bundle]; - const { rawAttributes, contents } = resource; - const noop = rawAttributes && rawAttributes.indexOf('data-gwd-opt="none"') >= 0 || compilation.config.optimization === 'none'; + const { contents } = resource; const outputPath = new URL(`./${fileName}`, outputDir); compilation.resources.set(resource.sourcePathURL.pathname, { @@ -130,10 +129,6 @@ function greenwoodSyncPageResourceBundlesPlugin(compilation) { optimizedFileContents: await fs.promises.readFile(outputPath, 'utf-8'), contents }); - - if (noop) { - await fs.promises.writeFile(outputPath, contents); - } } } } diff --git a/packages/cli/src/lifecycles/bundle.js b/packages/cli/src/lifecycles/bundle.js index d06de1f2e..551b3f89f 100644 --- a/packages/cli/src/lifecycles/bundle.js +++ b/packages/cli/src/lifecycles/bundle.js @@ -116,7 +116,7 @@ async function bundleStyleResources(compilation, resourcePlugins) { const { outputDir } = compilation.context; for (const resource of compilation.resources.values()) { - const { contents, optimizationAttr, src = '', type } = resource; + const { contents, src = '', type } = resource; if (['style', 'link'].includes(type)) { const resourceKey = resource.sourcePathURL.pathname; @@ -150,75 +150,70 @@ async function bundleStyleResources(compilation, resourcePlugins) { }); } - // keep the original contents, but still hash the filename based on content - if (compilation.config.optimization === 'none' || optimizationAttr === 'none') { - optimizedFileContents = contents; - } else { - const url = resource.sourcePathURL; - const contentType = 'text/css'; - const headers = new Headers({ 'Content-Type': contentType, 'Accept': contentType }); - const request = new Request(url, { headers }); - const initResponse = new Response(contents, { headers }); - - let response = await resourcePlugins.reduce(async (responsePromise, plugin) => { - const intermediateResponse = await responsePromise; - const shouldServe = plugin.shouldServe && await plugin.shouldServe(url, request); - - if (shouldServe) { - const currentResponse = await plugin.serve(url, request); - const mergedResponse = mergeResponse(intermediateResponse.clone(), currentResponse.clone()); - - if (mergedResponse.headers.get('Content-Type').indexOf(contentType) >= 0) { - return Promise.resolve(mergedResponse.clone()); - } + const url = resource.sourcePathURL; + const contentType = 'text/css'; + const headers = new Headers({ 'Content-Type': contentType, 'Accept': contentType }); + const request = new Request(url, { headers }); + const initResponse = new Response(contents, { headers }); + + let response = await resourcePlugins.reduce(async (responsePromise, plugin) => { + const intermediateResponse = await responsePromise; + const shouldServe = plugin.shouldServe && await plugin.shouldServe(url, request); + + if (shouldServe) { + const currentResponse = await plugin.serve(url, request); + const mergedResponse = mergeResponse(intermediateResponse.clone(), currentResponse.clone()); + + if (mergedResponse.headers.get('Content-Type').indexOf(contentType) >= 0) { + return Promise.resolve(mergedResponse.clone()); } + } - return Promise.resolve(responsePromise); - }, Promise.resolve(initResponse)); + return Promise.resolve(responsePromise); + }, Promise.resolve(initResponse)); - response = await resourcePlugins.reduce(async (responsePromise, plugin) => { - const intermediateResponse = await responsePromise; - const shouldPreIntercept = plugin.shouldPreIntercept && await plugin.shouldPreIntercept(url, request, intermediateResponse.clone()); + response = await resourcePlugins.reduce(async (responsePromise, plugin) => { + const intermediateResponse = await responsePromise; + const shouldPreIntercept = plugin.shouldPreIntercept && await plugin.shouldPreIntercept(url, request, intermediateResponse.clone()); - if (shouldPreIntercept) { - const currentResponse = await plugin.preIntercept(url, request, intermediateResponse.clone()); - const mergedResponse = mergeResponse(intermediateResponse.clone(), currentResponse.clone()); + if (shouldPreIntercept) { + const currentResponse = await plugin.preIntercept(url, request, intermediateResponse.clone()); + const mergedResponse = mergeResponse(intermediateResponse.clone(), currentResponse.clone()); - if (mergedResponse.headers.get('Content-Type').indexOf(contentType) >= 0) { - return Promise.resolve(mergedResponse.clone()); - } + if (mergedResponse.headers.get('Content-Type').indexOf(contentType) >= 0) { + return Promise.resolve(mergedResponse.clone()); } + } - return Promise.resolve(responsePromise); - }, Promise.resolve(response.clone())); + return Promise.resolve(responsePromise); + }, Promise.resolve(response.clone())); - response = await resourcePlugins.reduce(async (responsePromise, plugin) => { - const intermediateResponse = await responsePromise; - const shouldIntercept = plugin.shouldIntercept && await plugin.shouldIntercept(url, request, intermediateResponse.clone()); + response = await resourcePlugins.reduce(async (responsePromise, plugin) => { + const intermediateResponse = await responsePromise; + const shouldIntercept = plugin.shouldIntercept && await plugin.shouldIntercept(url, request, intermediateResponse.clone()); - if (shouldIntercept) { - const currentResponse = await plugin.intercept(url, request, intermediateResponse.clone()); - const mergedResponse = mergeResponse(intermediateResponse.clone(), currentResponse.clone()); + if (shouldIntercept) { + const currentResponse = await plugin.intercept(url, request, intermediateResponse.clone()); + const mergedResponse = mergeResponse(intermediateResponse.clone(), currentResponse.clone()); - if (mergedResponse.headers.get('Content-Type').indexOf(contentType) >= 0) { - return Promise.resolve(mergedResponse.clone()); - } + if (mergedResponse.headers.get('Content-Type').indexOf(contentType) >= 0) { + return Promise.resolve(mergedResponse.clone()); } + } - return Promise.resolve(responsePromise); - }, Promise.resolve(response.clone())); + return Promise.resolve(responsePromise); + }, Promise.resolve(response.clone())); - response = await resourcePlugins.reduce(async (responsePromise, plugin) => { - const intermediateResponse = await responsePromise; - const shouldOptimize = plugin.shouldOptimize && await plugin.shouldOptimize(url, intermediateResponse.clone()); + response = await resourcePlugins.reduce(async (responsePromise, plugin) => { + const intermediateResponse = await responsePromise; + const shouldOptimize = plugin.shouldOptimize && await plugin.shouldOptimize(url, intermediateResponse.clone()); - return shouldOptimize - ? Promise.resolve(await plugin.optimize(url, intermediateResponse.clone())) - : Promise.resolve(responsePromise); - }, Promise.resolve(response.clone())); + return shouldOptimize + ? Promise.resolve(await plugin.optimize(url, intermediateResponse.clone())) + : Promise.resolve(responsePromise); + }, Promise.resolve(response.clone())); - optimizedFileContents = await response.text(); - } + optimizedFileContents = await response.text(); compilation.resources.set(resourceKey, { ...compilation.resources.get(resourceKey), diff --git a/packages/cli/src/plugins/resource/plugin-standard-css.js b/packages/cli/src/plugins/resource/plugin-standard-css.js index f5cf8d09b..3fea80433 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-css.js +++ b/packages/cli/src/plugins/resource/plugin-standard-css.js @@ -343,7 +343,9 @@ class StandardCssResource extends ResourceInterface { } async intercept(url, request, response) { - let body = bundleCss(await response.text(), url, this.compilation); + let body = this.compilation.config.optimization !== 'none' + ? bundleCss(await response.text(), url, this.compilation) + : await response.text(); let headers = {}; if ((request.headers.get('Accept')?.indexOf('text/javascript') >= 0 || url.searchParams?.get('polyfill') === 'type-css') && !url.searchParams.has('type')) { diff --git a/packages/cli/src/plugins/resource/plugin-standard-html.js b/packages/cli/src/plugins/resource/plugin-standard-html.js index 827cc70b6..1639ba971 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-html.js +++ b/packages/cli/src/plugins/resource/plugin-standard-html.js @@ -184,7 +184,7 @@ class StandardHtmlResource extends ResourceInterface { if (type === 'script') { const tag = root.querySelectorAll('script').find(script => script.getAttribute('src') === src); - if (!optimizationAttr && optimization === 'default') { + if (!optimizationAttr && (optimization === 'default' || optimization === 'none')) { const optimizedFilePath = `${basePath}/${optimizedFileName}`; body = body.replace(src, optimizedFilePath); diff --git a/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js b/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js index def114b6b..29ab01eba 100644 --- a/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js +++ b/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js @@ -77,11 +77,20 @@ describe('Build Greenwood With: ', function() { }); it('should have the expected