Skip to content

Commit 0759bb0

Browse files
committed
Merge pull request #90 from joeybaker/master
Fix: correctly emit 'css stream'
2 parents 3720a70 + 545c6fd commit 0759bb0

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ b.plugin(require('css-modulesify'), {
5656
});
5757

5858
var bundle = b.bundle()
59-
bundle.on('css stream', function (css) {
59+
b.on('css stream', function (css) {
6060
css.pipe(fs.createWriteStream('mycss.css'));
6161
});
6262
```
@@ -66,11 +66,12 @@ bundle.on('css stream', function (css) {
6666
- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames. css-modulesify will try to use the browserify `basedir` if `rootDir` is not specified, if both are not specified it will use the location from which the command was executed.
6767
- `output`: path to write the generated css. If not provided, you'll need to listen to the `'css stream'` event on the bundle to get the output.
6868
- `jsonOutput`: optional path to write a json manifest of classnames.
69-
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins).
69+
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins). NOTE: it's safer to use `after`
70+
- `after`: optional array of postcss plugins to run after the required css-modules core plugins are run.
7071
- `generateScopedName`: (API only) a function to override the default behaviour of creating locally scoped classnames.
7172

7273
### Events
73-
- `b.bundle().on('css stream', callback)` The callback is called with a readable stream containing the compiled CSS. You can write this to a file.
74+
- `b.on('css stream', callback)` The callback is called with a readable stream containing the compiled CSS. You can write this to a file.
7475

7576
## Using CSS Modules on the backend
7677

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ module.exports = function (browserify, options) {
204204
var compiledCssStream = new ReadableStream();
205205
compiledCssStream._read = function () {};
206206

207-
bundle.emit('css stream', compiledCssStream);
207+
browserify.emit('css stream', compiledCssStream);
208208

209209
bundle.on('end', function () {
210210
// Combine the collected sources for a single bundle into a single CSS file

tests/stream-output.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,42 @@ tape('stream output', function (t) {
2828
t.plan(cssFilesTotal * 2 + 1);
2929

3030
var cssFilesCount = 0;
31-
browserify(path.join(simpleCaseDir, 'main.js'))
31+
var b = browserify(path.join(simpleCaseDir, 'main.js'));
32+
33+
b
3234
.plugin(cssModulesify, {
3335
rootDir: path.join(simpleCaseDir)
3436
})
3537
.on('error', t.error)
36-
.bundle(function noop () {})
37-
.on('css stream', function (stream) {
38-
stream
39-
.on('data', function onData (css) {
40-
var cssString = css.toString();
41-
// just get the first class name, use that as an id
42-
var cssId = cssString.split('\n')[0].split(' ')[0];
43-
44-
t.ok(
45-
++cssFilesCount <= cssFilesTotal
46-
, 'emits data for ' + cssId
47-
);
48-
49-
t.ok(
50-
cssString.indexOf('._styles') === 0
51-
, 'emits compiled css for ' + cssId
52-
);
53-
})
54-
.on('end', function onEnd () {
55-
t.pass('ends the stream');
56-
})
57-
.on('error', t.error);
58-
});
38+
.bundle(function noop () {});
39+
40+
b
41+
.once('css stream', function (stream) {
42+
stream
43+
.on('data', function onData (css) {
44+
var cssString = css.toString();
45+
// just get the first class name, use that as an id
46+
var cssId = cssString.split('\n')[0].split(' ')[0];
47+
48+
t.ok(
49+
++cssFilesCount <= cssFilesTotal
50+
, 'emits data for ' + cssId
51+
);
52+
53+
t.ok(
54+
cssString.indexOf('._styles') === 0
55+
, 'emits compiled css for ' + cssId
56+
);
57+
})
58+
.on('end', function onEnd () {
59+
t.pass('ends the stream');
60+
61+
b.bundle(function noop () {});
62+
63+
b.once('css stream', function (stream2) {
64+
t.ok(stream2, 'registers a second event for a CSS stream');
65+
});
66+
})
67+
.on('error', t.error);
68+
});
5969
});

0 commit comments

Comments
 (0)