Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Css duplicates #432

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Add test for graph optimization of deduplicated CSS
  • Loading branch information
developit committed Feb 28, 2021
commit 6b8658c635f779788ecd602bdccb81519b6aca72
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/css-duplicates/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a { color: #f00; }
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/css-duplicates/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a { color: #f00; }
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/css-duplicates/c.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a { color: #00f; text-decoration: underline; }
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/css-duplicates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script type="module" src="/index.js"></script>
4 changes: 4 additions & 0 deletions packages/wmr/test/fixtures/css-duplicates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './a.css';
import './b.css';
import './c.css';
import('./lazy.js');
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/css-duplicates/lazy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './b.css';
31 changes: 31 additions & 0 deletions packages/wmr/test/production.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,37 @@ describe('production', () => {
expect.stringMatching(/^\/assets\/style\.\w+\.css$/)
]);
});

it('should merge duplicate CSS imports', async () => {
await loadFixture('css-duplicates', env);
instance = await runWmr(env.tmp.path, 'build');
const code = await instance.done;
console.info(instance.output.join('\n'));
expect(code).toBe(0);

const readdir = async f => (await fs.readdir(path.join(env.tmp.path, f))).filter(f => f[0] !== '.');
const readfile = async f => await fs.readFile(path.join(env.tmp.path, f), 'utf-8');

const dist = await readdir('dist');

const chunks = await readdir('dist/chunks');
expect(chunks).toEqual([expect.stringMatching(/^lazy\.\w+\.js$/)]);

const assets = await readdir('dist/assets');
// TODO: Rollup seems to randomly pick which asset ID to use for duplicated assets:
expect(assets).toEqual([expect.stringMatching(/^[ab]\.\w+\.css$/)]);

const css = await readfile('dist/assets/' + assets[0]);

// ensure all the CSS properties got merged into a single rule:
expect(css).toMatch(/a{[^{}]+}/);

const properties = css.slice(2, -1).split(';').sort();
expect(properties).toEqual(['color:#00f', 'color:red', 'text-decoration:underline']);

const index = await readfile('dist/' + dist.find(f => f.match(/^index\.\w+\.js$/)));
expect(index).toContain(`("/assets/${assets[0]}")`);
});
});

describe('config.publicPath', () => {
Expand Down