Skip to content

Commit cddfa79

Browse files
committed
Use this.emitFile to generate css files
1 parent f8e8fa1 commit cddfa79

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ export default {
5555
console.log(css.code); // the concatenated CSS
5656
console.log(css.map); // a sourcemap
5757

58-
// creates `main.css` and `main.css.map` — pass `false`
59-
// as the second argument if you don't want the sourcemap
60-
css.write('public/main.css');
58+
// creates `main.css` and `main.css.map`
59+
// using a falsy name will default to the bundle name
60+
// — pass `false` as the second argument if you don't want the sourcemap
61+
css.write('main.css');
6162
},
6263

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

index.js

+13-25
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,10 @@ function exists(file) {
6464
}
6565
}
6666

67-
function mkdirp(dir) {
68-
const parent = path.dirname(dir);
69-
if (parent === dir) return;
70-
71-
mkdirp(parent);
72-
73-
try {
74-
fs.mkdirSync(dir);
75-
} catch (err) {
76-
if (err.code !== 'EEXIST') throw err;
77-
}
78-
}
79-
8067
class CssWriter {
81-
constructor (code, map, warn) {
68+
constructor (code, filename, map, warn, bundle) {
8269
this.code = code;
70+
this.filename = filename;
8371
this.map = {
8472
version: 3,
8573
file: null,
@@ -89,26 +77,24 @@ class CssWriter {
8977
mappings: map.mappings
9078
};
9179
this.warn = warn;
80+
this.bundle = bundle;
9281
}
9382

94-
write(dest, map) {
95-
dest = path.resolve(dest);
96-
mkdirp(path.dirname(dest));
97-
83+
write(dest = this.filename, map) {
9884
const basename = path.basename(dest);
9985

10086
if (map !== false) {
101-
fs.writeFileSync(dest, `${this.code}\n/*# sourceMappingURL=${basename}.map */`);
102-
fs.writeFileSync(`${dest}.map`, JSON.stringify({
87+
this.bundle.emitFile({type: 'asset', fileName: dest, source: `${this.code}\n/*# sourceMappingURL=${basename}.map */`});
88+
this.bundle.emitFile({type: 'asset', fileName: `${dest}.map`, source: JSON.stringify({
10389
version: 3,
10490
file: basename,
10591
sources: this.map.sources.map(source => path.relative(path.dirname(dest), source)),
10692
sourcesContent: this.map.sourcesContent,
10793
names: [],
10894
mappings: this.map.mappings
109-
}, null, ' '));
95+
}, null, ' ')});
11096
} else {
111-
fs.writeFileSync(dest, this.code);
97+
this.bundle.emitFile({type: 'asset', fileName: dest, source: this.code});
11298
}
11399
}
114100

@@ -291,7 +277,7 @@ module.exports = function svelte(options = {}) {
291277
return compiled.js;
292278
});
293279
},
294-
generateBundle() {
280+
generateBundle(options, bundle) {
295281
if (css) {
296282
// write out CSS file. TODO would be nice if there was a
297283
// a more idiomatic way to do this in Rollup
@@ -324,11 +310,13 @@ module.exports = function svelte(options = {}) {
324310
}
325311
}
326312

327-
const writer = new CssWriter(result, {
313+
const filename = Object.keys(bundle)[0].split('.').slice(0, -1) + '.css';
314+
315+
const writer = new CssWriter(result, filename, {
328316
sources,
329317
sourcesContent,
330318
mappings: encode(mappings)
331-
}, this.warn);
319+
}, this.warn, this);
332320

333321
css(writer);
334322
}

package-lock.json

+17-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"eslint": "^5.10.0",
2929
"locate-character": "^2.0.5",
3030
"mocha": "^5.2.0",
31-
"rollup": "^0.67.4",
31+
"rollup": "^1.25.1",
3232
"sander": "^0.6.0",
3333
"source-map": "^0.6.1",
3434
"svelte": "^3.0.0-beta.5"
@@ -39,7 +39,7 @@
3939
"sourcemap-codec": "^1.4.4"
4040
},
4141
"peerDependencies": {
42-
"svelte": "*",
43-
"rollup": ">=0.60.0"
42+
"rollup": ">=1.0.0",
43+
"svelte": "*"
4444
}
4545
}

0 commit comments

Comments
 (0)