以下のコミットで、CSSのストリームをbundle.on()
ではなく、browserifyのイベントと同じb.on
で監視するようになってました。
https://github.com/css-modules/css-modulesify/commit/0759bb0ade779b81d9384007ca2abbeccd61f137
これで前回の記事(PostCSSプラグインを使うやつ)を、watchifyでも使えるようになったので以下に使用例を書きます。
var fs = require('fs');
var through2 = require('through2');
var browserify = require('browserify');
var watchify = require('watchify');
var postcss = require('postcss');
var gutil = require('gulp-util');
var cssProcessors = [
require('postcss-nested'),
require('cssnano')
];
var b = browserify('src/index.js');
b.transform("babelify", {presets: ["es2015", "react"]});
b.plugin(watchify);
b.plugin(require('css-modulesify'), {
rootDir: __dirname
});
b.on('update', bundleFunc);
b.on('log', gutil.log);
b.on('css stream', function (css) {
var postcssStream = through2(
function transform(chunk, enc, next){
postcss(cssProcessors)
.process(chunk)
.then(function (result) {
next(null, result.css);
});
}
);
css
.pipe(postcssStream)
.pipe(fs.createWriteStream('dist/modules.css'));
});
bundleFunc();
function bundleFunc () {
return b.bundle()
.pipe(fs.createWriteStream('dist/index.js'));
}