diff --git a/README.md b/README.md index 5755824..126f306 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 0d52cfb..682854a 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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}); } } @@ -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 @@ -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); } diff --git a/package-lock.json b/package-lock.json index 45295bb..0bbd624 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,15 +37,15 @@ "dev": true }, "@types/estree": { - "version": "0.0.39", - "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", - "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", + "version": "0.0.44", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.44.tgz", + "integrity": "sha512-iaIVzr+w2ZJ5HkidlZ3EJM8VTZb2MJLCjw3V+505yVts0gRC4UMvjw0d1HPtGqI/HQC/KdsYtayfzl+AXY2R8g==", "dev": true }, "@types/node": { - "version": "10.12.12", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.12.tgz", - "integrity": "sha512-Pr+6JRiKkfsFvmU/LK68oBRCQeEg36TyAbPhc2xpez24OOZZCuoIhWGTd39VZy6nGafSbxzGouFPTFD/rR1A0A==", + "version": "14.0.11", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.11.tgz", + "integrity": "sha512-lCvvI24L21ZVeIiyIUHZ5Oflv1hhHQ5E1S25IRlKIXaRkVgmXpJMI3wUJkmym2bTbCe+WoIibQnMVAU3FguaOg==", "dev": true }, "acorn": { @@ -1403,13 +1403,14 @@ } }, "rollup": { - "version": "0.67.4", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.67.4.tgz", - "integrity": "sha512-AVuP73mkb4BBMUmksQ3Jw0jTrBTU1i7rLiUYjFxLZGb3xiFmtVEg40oByphkZAsiL0bJC3hRAJUQos/e5EBd+w==", + "version": "1.32.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.32.1.tgz", + "integrity": "sha512-/2HA0Ec70TvQnXdzynFffkjA6XN+1e2pEv/uKS5Ulca40g2L7KuOE3riasHoNVHOsFD5KKZgDsMk1CP3Tw9s+A==", "dev": true, "requires": { - "@types/estree": "0.0.39", - "@types/node": "*" + "@types/estree": "*", + "@types/node": "*", + "acorn": "^7.1.0" } }, "rollup-pluginutils": { diff --git a/package.json b/package.json index 2a71b7e..4d7b800 100644 --- a/package.json +++ b/package.json @@ -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" @@ -41,6 +41,6 @@ }, "peerDependencies": { "svelte": "*", - "rollup": ">=0.60.0" + "rollup": ">=1.19.2" } } diff --git a/test/test.js b/test/test.js index c48e0e0..16c6b67 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); } }) ], @@ -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'); } }) ],