diff --git a/CHANGELOG.md b/CHANGELOG.md index b5d9e5e..327c350 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +#### 1.0.4 + +Uses the `generateBundle` hook rather than the deprecated `ongenerate` and `onwrite` hooks. ([#3](https://github.com/Evercoder/rollup-plugin-css-bundle/pull/3) by [@iamDecode](https://github.com/iamDecode)) + #### 1.0.3 Makes the plugin compatible with recent versions of Rollup, currently 0.6.66. ([#1](https://github.com/Evercoder/rollup-plugin-css-bundle/issues/1), [#2](https://github.com/Evercoder/rollup-plugin-css-bundle/pulls/2) — thanks [@iamDecode](https://github.com/iamDecode)!) diff --git a/package.json b/package.json index 813346d..c668593 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-css-bundle", - "version": "1.0.3", + "version": "1.0.4", "description": "A Rollup plugin to extract CSS into a single external file", "main": "dist/index.js", "module": "src/index.js", diff --git a/src/index.js b/src/index.js index bb4c8a6..963c048 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,6 @@ import utils from 'rollup-pluginutils'; export default opts => { let styles = {}; - let bundles = {}; const options = Object.assign( { @@ -30,30 +29,28 @@ export default opts => { return ''; }, - ongenerate(opts, bundle) { - // Depending on the Rollup version, - // the `modules` will be either an array - // or an object with key-value pairs. - let modules = Array.isArray(bundle.modules) - ? bundle.modules - : Object.getOwnPropertyNames(bundle.modules); - let css = Object.entries(styles) - .sort((a, b) => modules.indexOf(a[0]) - modules.indexOf(b[0])) - .map(entry => entry[1]) - .join('\n'); - bundles[opts.file] = css; - }, - - onwrite(opts) { - fs.outputFile( - options.output || + generateBundle(opts, bundle) { + for (const file in bundle) { + // Depending on the Rollup version, + // the `modules` will be either an array + // or an object with key-value pairs. + let modules = Array.isArray(bundle[file].modules) + ? bundle[file].modules + : Object.getOwnPropertyNames(bundle[file].modules); + let css = Object.entries(styles) + .sort((a, b) => modules.indexOf(a[0]) - modules.indexOf(b[0])) + .map(entry => entry[1]) + .join('\n'); + + fs.outputFile( + options.output || path.join( path.dirname(opts.file), - path.basename(opts.file, path.extname(opts.file)) + - '.css' + path.basename(file, path.extname(opts.file)) + '.css' ), - bundles[opts.file] - ); + css + ); + } } }; };