shama / webpack-stream

:tropical_drink: Run webpack through a stream interface
MIT License
1.4k stars 123 forks source link

Errors in `webpack` break the pipe / task #34

Open ArnaudRinquin opened 9 years ago

ArnaudRinquin commented 9 years ago

Hello,

I am using webpack and gulp to build a coffee-script app using webpack watch option.

Any coffee-loader error, such as a syntax error, will just crash webpack along with gulp.

Using plumber or anything to catch the error will prevent gulp from crashing but won't keep webpack alive.

How can we fix that?

shama commented 9 years ago

I'm not sure. I don't use plumber or the watch option with webpack.

shama commented 9 years ago

Closing as I don't think this is an issue with this task. If it is, let me know when an example that reproduces. Thanks!

jeffijoe commented 9 years ago

I have the same problem.

shama commented 9 years ago

If it is, let me know when an example that reproduces. Thanks!

jeffijoe commented 9 years ago

I found that I had bail:true, setting it to false fixed it. Atleast for syntax errors.

aldendaniels commented 8 years ago

@ArnaudRinquin - Ran into this same issue. It's a gulp issue I think. There's a good explanation in https://github.com/gulpjs/gulp/issues/259. The following worked for me:

  return gulp.src('webclient/index.js')
    .pipe(gulpWebpack(webpackConfig, webpack))
    .on('error', function handleError() {
      this.emit('end'); // Recover from errors
    })
    .pipe(gulp.dest('webclient-dist/static'));
aldendaniels commented 8 years ago

@shama - You might want to consider re-opening this as a documentation issue. Anyone using the watch option will need an .on('error', ...) handler.

nidu commented 8 years ago

@aldendaniels is everything ok after recovering? For me gulp is recovering well, watches and compiles files (judging by logs) but doesn't overwrite result files.

aldendaniels commented 8 years ago

@nidu - sorry for not getting back. I'm no longer using webpack-stream, but as I recall everything worked well so long as an .on('error', ...) handler was specified.

gkiely commented 8 years ago

For anyone looking at patching this, the issue seems to be on line:117 in index.js

Replacing self.emit('error', new gutil.PluginError('webpack-stream', errorMessage)); with console.error(errorMessage, "\x07");

Seems to fix it but i'm sure there is a better approach.

krzysieki commented 8 years ago

Really annoying issue. I knew that some time ago there was no such a problem and after some research found that in webpack-stream@2.1.1 it is working correctly.

So for me downgrading resolved issue and webpack-stream@2.1.1 compiles typescript and does not crash on errors, but it is sad that newer versions cant handle errors correctly

kmck commented 8 years ago

I'm having this problem as well. Working around it with .on(error, function() { ... }) for now, but out-of-the-box compatibility with plumber would be way nicer.

CezaryDanielNowak commented 8 years ago

I fixed it by changing:

  gulp.src('app.js')
  .pipe(webpack(webpackConfig, webpackSync, webpackCallback))
  .on('error', (err) => {
    gutil.log('WEBPACK ERROR', err);
  })
  .pipe(gulp.dest('dest/'));

into:

  gulp.src('app.js')
  .pipe(
    webpack(webpackConfig, webpackSync, webpackCallback)
    .on('error', (err) => {
      gutil.log('WEBPACK ERROR', err);
    })
  )
  .pipe(gulp.dest('dest/'));
gregorskii commented 8 years ago

+1 to @CezaryDanielNowak using .on right after the webpack-stream will allow the error to trigger but resume the watch process. Just make sure you emit and end from the catch:

gulp.task('webpack', () => {

    return gulp.src(config.webpack.paths)
        .pipe(plugins.vinylNamed())
        .pipe(plugins.webpackStream(webpackConfig))
        .on('error', function(error) {
            plugins.util.log(plugins.util.colors.red(error.message));
            this.emit('end');
        })       
        .pipe(gulp.dest(config.dist + '/scripts'))
    ;
});

note the use of a normal function there, its to keep scope on this when running this.emit('end') using an arrow function in the .on('error') callback will break the scope of this due to lexical scoping.

krzysieki commented 8 years ago

After some debugging it seems that, in my setup, problem is not caused by #115 but it occurs because webpack-stream calls:

self.emit('error', new gutil.PluginError ...

two times.

After changing second "self.emit('error', new ..... " to simple log(errorMessage) everything works ok.

I suppose that error should be emitted once.

TomaszScisloGS commented 8 years ago

I'm facing the same issue. Is there any other solution than downgrade to 2.1.1?

zaqqaz commented 8 years ago

Also had problem with this. (https://github.com/shama/webpack-stream/issues/18#issuecomment-220851827)

Think, not necessary emit error in watch mode.

Code below NOT working in my case, watch stiil work ok, webpack also, but pipes below(like uglify and dest) not.

.pipe(plugins.webpackStream(webpackConfig))
 .on('error', function(error) {
            plugins.util.log(plugins.util.colors.red(error.message));
            this.emit('end');
        })       
headfire94 commented 8 years ago

I'm facing the same issue

girishla commented 8 years ago

Same issue here

nicholasrq commented 8 years ago

@gregorskii works like a charm for me

krzysieki commented 8 years ago

On my setup @gregorskii 's code is still not working.

@nicholasrq:

So webpack-stream didnt crash but it didnt refresh code too

DmitryMasley commented 8 years ago

@krzysieki the same problem for me.

Munsen commented 8 years ago

I have same problem I solved this problem like this:

webpack-stream/index.js:131

    var compiler = webpack(config, function (err, stats) {
      if (err) {
        if (this.watcher) this.watcher.close();
        self.emit('error', new gutil.PluginError('webpack-stream', err));
      }
      var jsonStats = stats.toJson() || {};
      var errors = jsonStats.errors || [];

gulpfiles.js


function webpackError() {
  this.emit('end');
}

var stream = webpackStream(webpackConfig);
gulp.task('webpack', (f) => {
  return gulp.src(assets_path)
    .pipe(named((f) => {
      var match = /(.+)\.(.+)$/.exec(f.relative);
      return match[1];
    }))
    .pipe(stream)
    .on('error', webpackError)
    .pipe(gulp.dest(assets_output));
});

gulp.watch(assets_path, ['webpack']);

It just stop watch and rerun pipe if it broken.

I think webpack-stream should be not break pipe in watch mode. so above code is temporary solution.

alexbassy commented 8 years ago

I have the same problem as @krzysieki

-> Save, webpack compiles fine -> Save with a syntax error, webpack outputs error, "Cannot find module" error in browser -> Fix error, save, webpack says it's compiled fine, but file is not updated and missing module error persists

So basically the watch works fine as long as I never save with an error. If I do, I have to relaunch the gulp script.

ennovum commented 8 years ago

+1

jeroennoten commented 8 years ago

With a little help of @gkiely's comment, I found the root of the problem:

To solve this issue, webpack-stream should not emit an error event on compilation error. At least not when watching. PR #126 should solve this.

kmck commented 8 years ago

I just tested the patch in https://github.com/shama/webpack-stream/pull/126 and it takes care of the issue while running the watcher nicely!

@shama could you please merge it and update the npm package?

TSMMark commented 8 years ago

126 did not help me. Exact same problem: files are never written after an error.

justinmchase commented 6 years ago

@TSMMark How could the files be written if there was an error? You have to have a successful parse to be able to emit a file.

What you want is for the watch to not be broken when you have a parse error, but to instead see the error in console output. Then you fix the error, and the following compilation will be successful and the file will be written.

TSMMark commented 6 years ago

I meant subsequent builds in my watch task. If it errored one time I would have to terminate the watch and start it over.

To fix use "webpack-stream": "github:jeroennoten/webpack-stream#patch-1"

B3none commented 5 years ago
return gulp.src(paths.scripts.src, {sourcemaps: true})
    .pipe(
      webpack(webpackConfig)
        .on('error', (err) => {
          console.log(err.message);
          this.emit('end'); // Recover from errors
        })
    );