Skip to content

Commit

Permalink
Merge pull request #13 from josh/compile-on-unresolved
Browse files Browse the repository at this point in the history
Lazily compile on module resolve phase
  • Loading branch information
danielholmes authored Dec 1, 2017
2 parents e004cd0 + ba3e77c commit 85eabf7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 54 deletions.
82 changes: 52 additions & 30 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _getSchema = require('./getSchema');

var _getSchema2 = _interopRequireDefault(_getSchema);
Expand Down Expand Up @@ -85,43 +89,61 @@ class RelayCompilerWebpackPlugin {
this.writerConfigs.default.getWriter = (0, _getWriter2.default)(options.src);
}

apply(compiler) {
compile(issuer, request) {
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: reporter,
onlyValidate: false,
skipPersist: true
});
return _asyncToGenerator(function* () {
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: reporter,
onlyValidate: false,
skipPersist: true
});

yield runner.compile('default');
} catch (error) {
errors.push(error);
}

yield runner.compileAll();
} catch (error) {
errors.push(error);
}
if (errors.length) {
throw errors[0];
}
})();
}

cachedCompiler() {
let result;
return (issuer, request) => {
if (!result) result = this.compile(issuer, request);
return result;
};
}

if (errors.length) {
callback(errors[0]);
apply(compiler) {
compiler.plugin('compilation', (compilation, params) => {
const compile = this.cachedCompiler();
params.normalModuleFactory.plugin('before-resolve', (result, callback) => {
if (result && result.request.match(/__generated__/)) {
const request = _path2.default.resolve(_path2.default.dirname(result.contextInfo.issuer), result.request);
compile(result.contextInfo.issuer, request).then(() => {
callback(null, result);
}).catch(error => {
callback(error);
});
} else {
callback();
callback(null, result);
}
});

return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})());
});
}
}

Expand Down
71 changes: 47 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Runner, FileIRParser, ConsoleReporter } from 'relay-compiler'
import fs from 'fs'
import path from 'path'

import getSchema from './getSchema'
import getFileFilter from './getFileFilter'
Expand Down Expand Up @@ -81,32 +82,54 @@ class RelayCompilerWebpackPlugin {
this.writerConfigs.default.getWriter = getWriter(options.src)
}

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: reporter,
onlyValidate: false,
skipPersist: true,
})

await runner.compileAll()
} catch (error) {
errors.push(error)
async compile (issuer: string, request: string) {
const errors = []
try {
const reporter = {
reportError: (area, error) => errors.push(error)
}

if (errors.length) {
callback(errors[0])
} else {
callback()
}
const runner = new Runner({
parserConfigs: this.parserConfigs,
writerConfigs: this.writerConfigs,
reporter: reporter,
onlyValidate: false,
skipPersist: true,
})

await runner.compile('default')
} catch (error) {
errors.push(error)
}

if (errors.length) {
throw errors[0]
}
}

cachedCompiler() {
let result
return (issuer: string, request: string) => {
if (!result) result = this.compile(issuer, request)
return result
}
}

apply (compiler: Compiler) {
compiler.plugin('compilation', (compilation, params) => {
const compile = this.cachedCompiler()
params.normalModuleFactory.plugin('before-resolve', (result, callback) => {
if (result && result.request.match(/__generated__/)) {
const request = path.resolve(path.dirname(result.contextInfo.issuer), result.request)
compile(result.contextInfo.issuer, request).then(() => {
callback(null, result)
}).catch(error => {
callback(error)
})
} else {
callback(null, result)
}
});
})
}
}
Expand Down

0 comments on commit 85eabf7

Please sign in to comment.