LoginSignup
0
0

More than 5 years have passed since last update.

css-modulesify + watchify + PostCSS

Posted at

以下のコミットで、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'));
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0