Skip to content

Commit d856625

Browse files
authored
Use this.emitFile to generate css files (#72)
1 parent 9cc17db commit d856625

File tree

5 files changed

+33
-43
lines changed

5 files changed

+33
-43
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ export default {
6262
console.log(css.code); // the concatenated CSS
6363
console.log(css.map); // a sourcemap
6464

65-
// creates `main.css` and `main.css.map` — pass `false`
66-
// as the second argument if you don't want the sourcemap
67-
css.write('public/main.css');
65+
// creates `main.css` and `main.css.map`
66+
// using a falsy name will default to the bundle name
67+
// — pass `false` as the second argument if you don't want the sourcemap
68+
css.write('main.css');
6869
},
6970

7071
// Warnings are normally passed straight to Rollup. You can

index.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,10 @@ function exists(file) {
6969
}
7070
}
7171

72-
function mkdirp(dir) {
73-
const parent = path.dirname(dir);
74-
if (parent === dir) return;
75-
76-
mkdirp(parent);
77-
78-
try {
79-
fs.mkdirSync(dir);
80-
} catch (err) {
81-
if (err.code !== 'EEXIST') throw err;
82-
}
83-
}
84-
8572
class CssWriter {
86-
constructor (code, map, warn) {
73+
constructor (code, filename, map, warn, bundle) {
8774
this.code = code;
75+
this.filename = filename;
8876
this.map = {
8977
version: 3,
9078
file: null,
@@ -94,26 +82,24 @@ class CssWriter {
9482
mappings: map.mappings
9583
};
9684
this.warn = warn;
85+
this.bundle = bundle;
9786
}
9887

99-
write(dest, map) {
100-
dest = path.resolve(dest);
101-
mkdirp(path.dirname(dest));
102-
88+
write(dest = this.filename, map) {
10389
const basename = path.basename(dest);
10490

10591
if (map !== false) {
106-
fs.writeFileSync(dest, `${this.code}\n/*# sourceMappingURL=${basename}.map */`);
107-
fs.writeFileSync(`${dest}.map`, JSON.stringify({
92+
this.bundle.emitFile({type: 'asset', fileName: dest, source: `${this.code}\n/*# sourceMappingURL=${basename}.map */`});
93+
this.bundle.emitFile({type: 'asset', fileName: `${dest}.map`, source: JSON.stringify({
10894
version: 3,
10995
file: basename,
11096
sources: this.map.sources.map(source => path.relative(path.dirname(dest), source)),
11197
sourcesContent: this.map.sourcesContent,
11298
names: [],
11399
mappings: this.map.mappings
114-
}, null, ' '));
100+
}, null, ' ')});
115101
} else {
116-
fs.writeFileSync(dest, this.code);
102+
this.bundle.emitFile({type: 'asset', fileName: dest, source: this.code});
117103
}
118104
}
119105

@@ -297,7 +283,7 @@ module.exports = function svelte(options = {}) {
297283
return compiled.js;
298284
});
299285
},
300-
generateBundle() {
286+
generateBundle(options, bundle) {
301287
if (css) {
302288
// write out CSS file. TODO would be nice if there was a
303289
// a more idiomatic way to do this in Rollup
@@ -332,11 +318,13 @@ module.exports = function svelte(options = {}) {
332318
}
333319
}
334320

335-
const writer = new CssWriter(result, {
321+
const filename = Object.keys(bundle)[0].split('.').shift() + '.css';
322+
323+
const writer = new CssWriter(result, filename, {
336324
sources,
337325
sourcesContent,
338326
mappings: encode(mappings)
339-
}, this.warn);
327+
}, this.warn, this);
340328

341329
css(writer);
342330

package-lock.json

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"eslint": "^6.8.0",
3131
"locate-character": "^2.0.5",
3232
"mocha": "^7.1.1",
33-
"rollup": "^0.67.4",
33+
"rollup": "^1.32.1",
3434
"sander": "^0.6.0",
3535
"source-map": "^0.7.3",
3636
"svelte": "^3.0.0-beta.5"
@@ -42,6 +42,6 @@
4242
},
4343
"peerDependencies": {
4444
"svelte": "*",
45-
"rollup": ">=0.60.0"
45+
"rollup": ">=1.19.2"
4646
}
4747
}

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ describe('rollup-plugin-svelte', () => {
294294
plugin({
295295
css: value => {
296296
css = value;
297-
css.write('test/deterministic-css/dist/bundle.css');
297+
css.write('bundle.css');
298298
}
299299
})
300300
],
@@ -331,7 +331,7 @@ describe('rollup-plugin-svelte', () => {
331331
plugin({
332332
css: value => {
333333
css = value;
334-
css.write('test/filename-test/dist/bundle.css');
334+
css.write('bundle.css');
335335
}
336336
})
337337
],

0 commit comments

Comments
 (0)