Skip to content

Commit 1163a44

Browse files
committed
Use this.emitFile to generate css files
1 parent e18e8ae commit 1163a44

File tree

4 files changed

+31
-41
lines changed

4 files changed

+31
-41
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ export default {
5959
console.log(css.code); // the concatenated CSS
6060
console.log(css.map); // a sourcemap
6161

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

6768
// 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

@@ -292,7 +278,7 @@ module.exports = function svelte(options = {}) {
292278
return compiled.js;
293279
});
294280
},
295-
generateBundle() {
281+
generateBundle(options, bundle) {
296282
if (css) {
297283
// write out CSS file. TODO would be nice if there was a
298284
// a more idiomatic way to do this in Rollup
@@ -327,11 +313,13 @@ module.exports = function svelte(options = {}) {
327313
}
328314
}
329315

330-
const writer = new CssWriter(result, {
316+
const filename = Object.keys(bundle)[0].split('.').shift() + '.css';
317+
318+
const writer = new CssWriter(result, filename, {
331319
sources,
332320
sourcesContent,
333321
mappings: encode(mappings)
334-
}, this.warn);
322+
}, this.warn, this);
335323

336324
css(writer);
337325
}

package-lock.json

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

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"eslint": "^6.8.0",
3030
"locate-character": "^2.0.5",
3131
"mocha": "^7.1.1",
32-
"rollup": "^0.67.4",
32+
"rollup": "^1.32.1",
3333
"sander": "^0.6.0",
3434
"source-map": "^0.7.3",
3535
"svelte": "^3.0.0-beta.5"
@@ -41,6 +41,6 @@
4141
},
4242
"peerDependencies": {
4343
"svelte": "*",
44-
"rollup": ">=0.60.0"
44+
"rollup": ">=1.32.1"
4545
}
4646
}

0 commit comments

Comments
 (0)