Skip to content

Commit 642669c

Browse files
committed
Initial version
1 parent 5d84ffe commit 642669c

File tree

4 files changed

+411
-0
lines changed

4 files changed

+411
-0
lines changed

dist/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4+
5+
var rollupPluginutils = require('rollup-pluginutils');
6+
require('postcss');
7+
var fs = _interopDefault(require('fs'));
8+
9+
var index = (opts) => {
10+
11+
let styles = {};
12+
let bundles = {};
13+
14+
const options = Object.assign({
15+
include: ['**/*.css']
16+
}, opts);
17+
18+
const filter = rollupPluginutils.createFilter(options.include, options.exclude);
19+
20+
return {
21+
name: 'cssbundle',
22+
23+
async load(id) {
24+
if (!filter(id)) return;
25+
return await fs.readFile(id, 'utf8');
26+
},
27+
28+
transform(code, id) {
29+
if (!filter(id)) return;
30+
styles[id] = code;
31+
return '';
32+
},
33+
34+
ongenerate(opts, bundle) {
35+
let css = Object.entries(styles)
36+
.sort((a, b) => bundle.modules.indexOf(a[0]) - bundle.modules.indexOf(b[0]))
37+
.map(entry => entry[1])
38+
.join('\n');
39+
bundles[opts.file] = css;
40+
},
41+
42+
onwrite(opts) {
43+
let dest = options.file || opts.file.replace(/\.js$/, '.css');
44+
fs.writeFile(dest, bundles[opts.file]);
45+
}
46+
}
47+
}
48+
49+
module.exports = index;

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"dependencies": {
3+
"postcss": "^6.0.21",
4+
"rollup-pluginutils": "^2.0.1"
5+
},
6+
"name": "rollup-plugin-css-bundle",
7+
"version": "1.0.0",
8+
"main": "dist/index.js",
9+
"module": "src/index.js",
10+
"repository": "git@github.com:danburzo/rollup-plugin-css-bundle.git",
11+
"author": "Dan Burzo <danburzo@gmail.com>",
12+
"license": "MIT",
13+
"files": [
14+
"src",
15+
"dist"
16+
],
17+
"devDependencies": {
18+
"rollup": "^0.58.1"
19+
},
20+
"scripts": {
21+
"build": "rollup -i src/index.js -o dist/index.js -f cjs",
22+
"watch": "rollup -w -i src/index.js -o dist/index.js -f cjs"
23+
}
24+
}

src/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createFilter } from 'rollup-pluginutils';
2+
import postcss from 'postcss';
3+
import fs from 'fs';
4+
5+
export default (opts) => {
6+
7+
let styles = {};
8+
let bundles = {};
9+
10+
const options = Object.assign({
11+
include: ['**/*.css']
12+
}, opts);
13+
14+
const filter = createFilter(options.include, options.exclude);
15+
16+
return {
17+
name: 'cssbundle',
18+
19+
async load(id) {
20+
if (!filter(id)) return;
21+
return await fs.readFile(id, 'utf8');
22+
},
23+
24+
transform(code, id) {
25+
if (!filter(id)) return;
26+
styles[id] = code;
27+
return '';
28+
},
29+
30+
ongenerate(opts, bundle) {
31+
let css = Object.entries(styles)
32+
.sort((a, b) => bundle.modules.indexOf(a[0]) - bundle.modules.indexOf(b[0]))
33+
.map(entry => entry[1])
34+
.join('\n');
35+
bundles[opts.file] = css;
36+
},
37+
38+
onwrite(opts) {
39+
let dest = options.file || opts.file.replace(/\.js$/, '.css');
40+
fs.writeFile(dest, bundles[opts.file]);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)