Skip to content

Use this.emitFile to generate css files #72

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 11 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export default {
console.log(css.code); // the concatenated CSS
console.log(css.map); // a sourcemap

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

// Warnings are normally passed straight to Rollup. You can
Expand Down
38 changes: 13 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,10 @@ function exists(file) {
}
}

function mkdirp(dir) {
const parent = path.dirname(dir);
if (parent === dir) return;

mkdirp(parent);

try {
fs.mkdirSync(dir);
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
}

class CssWriter {
constructor (code, map, warn) {
constructor (code, filename, map, warn, bundle) {
this.code = code;
this.filename = filename;
this.map = {
version: 3,
file: null,
Expand All @@ -89,26 +77,24 @@ class CssWriter {
mappings: map.mappings
};
this.warn = warn;
this.bundle = bundle;
}

write(dest, map) {
dest = path.resolve(dest);
mkdirp(path.dirname(dest));

write(dest = this.filename, map) {
const basename = path.basename(dest);

if (map !== false) {
fs.writeFileSync(dest, `${this.code}\n/*# sourceMappingURL=${basename}.map */`);
fs.writeFileSync(`${dest}.map`, JSON.stringify({
this.bundle.emitFile({type: 'asset', fileName: dest, source: `${this.code}\n/*# sourceMappingURL=${basename}.map */`});
this.bundle.emitFile({type: 'asset', fileName: `${dest}.map`, source: JSON.stringify({
version: 3,
file: basename,
sources: this.map.sources.map(source => path.relative(path.dirname(dest), source)),
sourcesContent: this.map.sourcesContent,
names: [],
mappings: this.map.mappings
}, null, ' '));
}, null, ' ')});
} else {
fs.writeFileSync(dest, this.code);
this.bundle.emitFile({type: 'asset', fileName: dest, source: this.code});
}
}

Expand Down Expand Up @@ -292,7 +278,7 @@ module.exports = function svelte(options = {}) {
return compiled.js;
});
},
generateBundle() {
generateBundle(options, bundle) {
if (css) {
// write out CSS file. TODO would be nice if there was a
// a more idiomatic way to do this in Rollup
Expand Down Expand Up @@ -327,11 +313,13 @@ module.exports = function svelte(options = {}) {
}
}

const writer = new CssWriter(result, {
const filename = Object.keys(bundle)[0].split('.').shift() + '.css';

const writer = new CssWriter(result, filename, {
sources,
sourcesContent,
mappings: encode(mappings)
}, this.warn);
}, this.warn, this);

css(writer);
}
Expand Down
23 changes: 12 additions & 11 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"eslint": "^6.8.0",
"locate-character": "^2.0.5",
"mocha": "^7.1.1",
"rollup": "^0.67.4",
"rollup": "^1.32.1",
"sander": "^0.6.0",
"source-map": "^0.7.3",
"svelte": "^3.0.0-beta.5"
Expand All @@ -41,6 +41,6 @@
},
"peerDependencies": {
"svelte": "*",
"rollup": ">=0.60.0"
"rollup": ">=1.32.1"
}
}
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('rollup-plugin-svelte', () => {
plugin({
css: value => {
css = value;
css.write('test/deterministic-css/dist/bundle.css');
css.write('bundle.css');
}
})
],
Expand Down Expand Up @@ -315,7 +315,7 @@ describe('rollup-plugin-svelte', () => {
plugin({
css: value => {
css = value;
css.write('test/filename-test/dist/bundle.css');
css.write('bundle.css');
}
})
],
Expand Down