Skip to content

Commit

Permalink
align none optimization option with output filename and contents
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Dec 24, 2024
1 parent f77b894 commit 180f60d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 67 deletions.
7 changes: 1 addition & 6 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -130,10 +129,6 @@ function greenwoodSyncPageResourceBundlesPlugin(compilation) {
optimizedFileContents: await fs.promises.readFile(outputPath, 'utf-8'),
contents
});

if (noop) {
await fs.promises.writeFile(outputPath, contents);
}
}
}
}
Expand Down
105 changes: 50 additions & 55 deletions packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/plugins/resource/plugin-standard-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/plugins/resource/plugin-standard-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,20 @@ describe('Build Greenwood With: ', function() {
});

it('should have the expected <script> tag in the <head>', function() {
const scriptTags = Array.from(dom.window.document.querySelectorAll('head script[type="module"]')).filter(tag => !tag.getAttribute('data-gwd'));
const src = jsFiles[0].replace(this.context.publicDir, '');
const scriptTags = Array.from(dom.window.document.querySelectorAll('head script[type="module"]')).filter(tag => tag.getAttribute('src') === src);

expect(scriptTags.length).to.be.equal(1);
});

it('should have the expected preload <script> tag in the <head>', function() {
const src = jsFiles[0].replace(this.context.publicDir, '');
const scriptPreloadTags = Array.from(dom.window.document.querySelectorAll('head link[as="script"]'));

expect(scriptPreloadTags.length).to.be.equal(1);
expect(scriptPreloadTags[0].getAttribute('href')).to.be.equal(src);
});

it('should contain the expected <app-header> in the <body>', function() {
const header = dom.window.document.querySelectorAll('body app-header');

Expand All @@ -100,10 +109,19 @@ describe('Build Greenwood With: ', function() {
expect(css).to.contain('{\n margin: 0;\n padding: 0;\n font-family: \'Comic Sans\', sans-serif;\n}');
});

it('should have two <link> tag in the <head>', function() {
const linkTags = dom.window.document.querySelectorAll('head link');
it('should have the expected preload <link> tag in the <head>', function() {
const href = cssFiles[0].replace(this.context.publicDir, '');
const linkPreloadTags = Array.from(dom.window.document.querySelectorAll('head link[as="style"]'));

expect(linkPreloadTags.length).to.be.equal(1);
expect(linkPreloadTags[0].getAttribute('href')).to.be.equal(href);
});

it('should have the expected <link> tag href for theme.css', function() {
const href = cssFiles[0].replace(this.context.publicDir, '');
const linkTags = Array.from(dom.window.document.querySelectorAll('head link[rel="stylesheet"]')).filter(tag => tag.getAttribute('href') === href);

expect(linkTags.length).to.be.equal(2);
expect(linkTags.length).to.be.equal(1);
});
});
});
Expand Down

0 comments on commit 180f60d

Please sign in to comment.