Skip to content

Commit

Permalink
Merge pull request #8 from josh/throw-errors
Browse files Browse the repository at this point in the history
Report relay-compiler errors as webpack errors
  • Loading branch information
danielholmes authored Nov 28, 2017
2 parents 19959b8 + 58fb80f commit 6fdbd03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
22 changes: 15 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class RelayCompilerWebpackPlugin {
parser: 'default'
}
};
this.reporter = {};

if (!options) {
throw new Error('You must provide options to RelayCompilerWebpackPlugin.');
Expand Down Expand Up @@ -91,30 +90,39 @@ class RelayCompilerWebpackPlugin {
this.parserConfigs.default.filepaths = watchman ? null : (0, _getFilepathsFromGlob2.default)(options.src, fileOptions);

this.writerConfigs.default.getWriter = (0, _getWriter2.default)(options.src);

this.reporter = options.reporter ? options.reporter : new _relayCompiler.ConsoleReporter({ verbose: false });
}

apply(compiler) {
var _this = this;

compiler.plugin('before-compile', (() => {
var _ref = _asyncToGenerator(function* (compilationParams, callback) {
const errors = [];
try {
const reporter = {
reportError: function reportError(area, error) {
return errors.push(error);
}
};

const runner = new _relayCompiler.Runner({
parserConfigs: _this.parserConfigs,
writerConfigs: _this.writerConfigs,
reporter: _this.reporter,
reporter: reporter,
onlyValidate: false,
skipPersist: true
});

yield runner.compileAll();
} catch (error) {
callback(error);
return;
errors.push(error);
}

if (errors.length) {
callback(errors[0]);
} else {
callback();
}
callback();
});

return function (_x, _x2) {
Expand Down
22 changes: 13 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ class RelayCompilerWebpackPlugin {
},
}

reporter = {}

constructor (options: {
schema: string,
src: string,
extensions: Array<string>,
include: Array<String>,
exclude: Array<String>,
watchman: boolean,
reporter: {reportError: (area: string, error: string) => void},
}) {
if (!options) {
throw new Error('You must provide options to RelayCompilerWebpackPlugin.')
Expand Down Expand Up @@ -87,27 +84,34 @@ class RelayCompilerWebpackPlugin {
this.parserConfigs.default.filepaths = watchman ? null : getFilepathsFromGlob(options.src, fileOptions)

this.writerConfigs.default.getWriter = getWriter(options.src)

this.reporter = options.reporter ? options.reporter : new ConsoleReporter({ verbose: false });
}

apply (compiler: Compiler) {
compiler.plugin('before-compile', async (compilationParams, callback) => {
const errors = []
try {
const reporter = {
reportError: (area, error) => errors.push(error)
}

const runner = new Runner({
parserConfigs: this.parserConfigs,
writerConfigs: this.writerConfigs,
reporter: this.reporter,
reporter: reporter,
onlyValidate: false,
skipPersist: true,
})

await runner.compileAll()
} catch (error) {
callback(error)
return
errors.push(error)
}

if (errors.length) {
callback(errors[0])
} else {
callback()
}
callback()
})
}
}
Expand Down

0 comments on commit 6fdbd03

Please sign in to comment.