Skip to content

Commit

Permalink
When removing duplicate CSS, also remove from metadata (#4643)
Browse files Browse the repository at this point in the history
* When removing duplicate CSS, also remove from metadata

* Adding a changeset
  • Loading branch information
matthewp committed Sep 6, 2022
1 parent eb1862b commit 307b7b9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-zoos-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Remove regression when there is duplicate client/server CSS
1 change: 1 addition & 0 deletions packages/astro/src/core/build/vite-plugin-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
if (Object.keys(c.modules).every((id) => internals.cssChunkModuleIds.has(id))) {
for (const importedCssImport of meta.importedCss) {
delete bundle[importedCssImport];
meta.importedCss.delete(importedCssImport);
}
return;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/0-css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

/** @type {import('./test-utils').Fixture} */
let fixture;

describe('CSS', function () {
Expand Down Expand Up @@ -360,6 +361,16 @@ describe('CSS', function () {
// SvelteDynamic styles is already included in the main page css asset
const unusedCssAsset = bundledAssets.find((asset) => /SvelteDynamic\..*\.css/.test(asset));
expect(unusedCssAsset, 'Found unused style ' + unusedCssAsset).to.be.undefined;

let foundVitePreloadCSS = false;
const bundledJS = await fixture.glob('**/*.?(m)js');
for(const filename of bundledJS) {
const content = await fixture.readFile(filename);
if(content.match(/ReactDynamic\..*\.css/)) {
foundVitePreloadCSS = filename;
}
}
expect(foundVitePreloadCSS).to.equal(false, 'Should not have found a preload for the dynamic CSS');
});
});
});
12 changes: 10 additions & 2 deletions packages/astro/test/fixtures/0-css/src/components/ReactDynamic.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import React from 'react';

const ReactLazy = React.lazy(() => import('./ReactLazy'));

export default function() {
return <div></div>;
}
return (
<div>
<React.Suspense>
<ReactLazy />
</React.Suspense>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import mod from './ReactLazy.module.css';

export default function() {
return <div className={mod.lazy}></div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.lazy {
background: yellow;
}
5 changes: 5 additions & 0 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import preview from '../dist/core/preview/index.js';
import { nodeLogDestination } from '../dist/core/logger/node.js';
import os from 'os';
import stripAnsi from 'strip-ansi';
import fastGlob from 'fast-glob';

// polyfill WebAPIs to globalThis for Node v12, Node v14, and Node v16
polyfill(globalThis, {
Expand All @@ -30,6 +31,7 @@ polyfill(globalThis, {
* @property {(path: string) => Promise<string>} readFile
* @property {(path: string, updater: (content: string) => string) => Promise<void>} writeFile
* @property {(path: string) => Promise<string[]>} readdir
* @property {(pattern: string) => Promise<string[]>} glob
* @property {() => Promise<DevServer>} startDevServer
* @property {() => Promise<PreviewServer>} preview
* @property {() => Promise<void>} clean
Expand Down Expand Up @@ -147,6 +149,9 @@ export async function loadFixture(inlineConfig) {
readFile: (filePath) =>
fs.promises.readFile(new URL(filePath.replace(/^\//, ''), config.outDir), 'utf8'),
readdir: (fp) => fs.promises.readdir(new URL(fp.replace(/^\//, ''), config.outDir)),
glob: (p) => fastGlob(p, {
cwd: fileURLToPath(config.outDir)
}),
clean: async () => {
await fs.promises.rm(config.outDir, { maxRetries: 10, recursive: true, force: true });
},
Expand Down

0 comments on commit 307b7b9

Please sign in to comment.