Skip to content

order css chunks by filename #101

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

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ module.exports = function svelte(options = {}) {
const sources = [];
const sourcesContent = [];

for (let chunk of cssLookup.values()) {
const chunks = Array.from(cssLookup.keys()).sort().map(key => cssLookup.get(key));

for (let chunk of chunks) {
if (!chunk.code) continue;
result += chunk.code + '\n';

Expand Down
5 changes: 5 additions & 0 deletions test/deterministic-css/expected/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/deterministic-css/src/A.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p class="a">red</p>

<style>
.a {
color: red;
}
</style>
9 changes: 9 additions & 0 deletions test/deterministic-css/src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import A from './A.svelte';
import B from './B.svelte';
import C from './C.svelte';
</script>

<A/>
<B/>
<C/>
7 changes: 7 additions & 0 deletions test/deterministic-css/src/B.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p class="b">green</p>

<style>
.b {
color: green;
}
</style>
7 changes: 7 additions & 0 deletions test/deterministic-css/src/C.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p class="c">blue</p>

<style>
.c {
color: blue;
}
</style>
5 changes: 5 additions & 0 deletions test/deterministic-css/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import App from './App.svelte';

new App({
target: document.body
});
37 changes: 37 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const path = require('path');
const sander = require('sander');
const assert = require('assert');
Expand Down Expand Up @@ -255,4 +256,40 @@ describe('rollup-plugin-svelte', () => {
assert.deepEqual(warnings.map(w => w.code), ['a11y-hidden', 'a11y-distracting-elements']);
assert.deepEqual(handled.map(w => w.code), ['a11y-hidden']);
});

it('bundles CSS deterministically', async () => {
sander.rimrafSync('test/deterministic-css/dist');
sander.mkdirSync('test/deterministic-css/dist');

let css;

const bundle = await rollup.rollup({
input: 'test/deterministic-css/src/main.js',
plugins: [
{
resolveId: async (id) => {
if (/A\.svelte/.test(id)) {
await new Promise(f => setTimeout(f, 50));
}
}
},
plugin({
css: value => {
css = value;
css.write('test/deterministic-css/dist/bundle.css');
}
})
]
});

await bundle.write({
format: 'iife',
file: 'test/deterministic-css/dist/bundle.js'
});

assert.equal(
fs.readFileSync('test/deterministic-css/dist/bundle.css', 'utf-8'),
fs.readFileSync('test/deterministic-css/expected/bundle.css', 'utf-8')
);
});
});