Skip to content

Commit

Permalink
Add option for languagePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
danielholmes committed Dec 7, 2018
1 parent 135a508 commit 3a8c315
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 155 deletions.
28 changes: 0 additions & 28 deletions dist/getFileFilter.js

This file was deleted.

116 changes: 71 additions & 45 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var _relayCompiler = require("relay-compiler");

var _RelayLanguagePluginJavaScript = _interopRequireDefault(require("relay-compiler/lib/RelayLanguagePluginJavaScript"));

var _RelaySourceModuleParser = _interopRequireDefault(require("relay-compiler/lib/RelaySourceModuleParser"));

var _graphqlCompiler = require("graphql-compiler");

var _fs = _interopRequireDefault(require("fs"));
Expand All @@ -12,8 +14,6 @@ var _path = _interopRequireDefault(require("path"));

var _getSchema = _interopRequireDefault(require("./getSchema"));

var _getFileFilter = _interopRequireDefault(require("./getFileFilter"));

var _getWriter = _interopRequireDefault(require("./getWriter"));

var _getFilepathsFromGlob = _interopRequireDefault(require("./getFilepathsFromGlob"));
Expand All @@ -32,7 +32,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
// This implements graphql-compiler GraphQLReporter
// https://github.com/facebook/relay/blob/v1.7.0/packages/graphql-compiler/reporters/GraphQLReporter.js
// Wasn't able to find a way to import the GraphQLReporter interface to declare that it is implemented
class TemporaryReporter {
class RaiseErrorsReporter {
reportMessage(message) {// process.stdout.write('Report message: ' + message + '\n');
}

Expand All @@ -48,32 +48,9 @@ class TemporaryReporter {

class RelayCompilerWebpackPlugin {
constructor(options) {
_defineProperty(this, "runner", void 0);

_defineProperty(this, "parserConfigs", {
js: {
baseDir: '',
getFileFilter: _getFileFilter.default,
getParser: _relayCompiler.JSModuleParser.getParser,
getSchema: () => {},
filepaths: null
},
graphql: {
baseDir: '',
getParser: _graphqlCompiler.DotGraphQLParser.getParser,
getSchema: () => {},
filepaths: null
}
});
_defineProperty(this, "parserConfigs", void 0);

_defineProperty(this, "writerConfigs", {
js: {
getWriter: (...any) => {},
isGeneratedFile: filePath => filePath.endsWith('.js') && filePath.includes('__generated__'),
parser: 'js',
baseParsers: ['graphql']
}
});
_defineProperty(this, "writerConfigs", void 0);

if (!options) {
throw new Error('You must provide options to RelayCompilerWebpackPlugin.');
Expand All @@ -95,30 +72,78 @@ class RelayCompilerWebpackPlugin {
throw new Error(`Could not find the [src] provided (${options.src})`);
}

const extensions = options.extensions !== undefined ? options.extensions : ['js'];
const language = (options.languagePlugin || _RelayLanguagePluginJavaScript.default)();

const extensions = options.extensions !== undefined ? options.extensions : language.inputExtensions;
const sourceParserName = extensions.join('/');
const include = options.include !== undefined ? options.include : ['**'];
const exclude = options.exclude !== undefined ? options.exclude : ['**/node_modules/**', '**/__mocks__/**', '**/__tests__/**', '**/__generated__/**'];
this.parserConfigs = this.createParserConfigs({
sourceParserName,
languagePlugin: language,
include,
exclude,
schema: options.schema,
getParser: options.getParser,
baseDir: options.src,
extensions
});
this.writerConfigs = this.createWriterConfigs({
baseDir: options.src,
sourceParserName,
languagePlugin: language
});
}

createParserConfigs({
baseDir,
getParser,
sourceParserName,
languagePlugin,
include,
exclude,
schema,
extensions
}) {
const schemaFn = typeof schema === 'string' ? () => (0, _getSchema.default)(schema) : () => schema;
const sourceModuleParser = (0, _RelaySourceModuleParser.default)(languagePlugin.findGraphQLTags);
const fileOptions = {
extensions,
include,
exclude
};
const schemaFn = typeof options.schema === 'string' ? () => (0, _getSchema.default)(options.schema) : () => options.schema;

if (options.getParser !== undefined) {
this.parserConfigs.js.getParser = options.getParser;
}
return {
[sourceParserName]: {
baseDir,
getFileFilter: sourceModuleParser.getFileFilter,
getParser: getParser || sourceModuleParser.getParser,
getSchema: schemaFn,
filepaths: (0, _getFilepathsFromGlob.default)(baseDir, fileOptions)
},
graphql: {
baseDir,
getParser: _graphqlCompiler.DotGraphQLParser.getParser,
getSchema: schemaFn,
filepaths: (0, _getFilepathsFromGlob.default)(baseDir, _objectSpread({}, fileOptions, {
extensions: ['graphql']
}))
}
};
}

this.parserConfigs.js.baseDir = options.src;
this.parserConfigs.js.getSchema = schemaFn;
this.parserConfigs.js.filepaths = (0, _getFilepathsFromGlob.default)(options.src, fileOptions);
const languagePlugin = (0, _RelayLanguagePluginJavaScript.default)();
this.writerConfigs.js.getWriter = (0, _getWriter.default)(languagePlugin, options.src);
this.parserConfigs.graphql.baseDir = options.src;
this.parserConfigs.graphql.getSchema = schemaFn;
this.parserConfigs.graphql.filepaths = (0, _getFilepathsFromGlob.default)(options.src, _objectSpread({}, fileOptions, {
extensions: ['graphql']
}));
createWriterConfigs({
baseDir,
sourceParserName,
languagePlugin
}) {
return {
[languagePlugin.outputExtension]: {
getWriter: (0, _getWriter.default)(languagePlugin, baseDir),
isGeneratedFile: filePath => filePath.endsWith('.graphql.' + languagePlugin.outputExtension) && filePath.includes('__generated__'),
parser: sourceParserName,
baseParsers: ['graphql']
}
};
}

compile(issuer, request) {
Expand All @@ -128,10 +153,11 @@ class RelayCompilerWebpackPlugin {
const errors = [];

try {
// Can this be set up in constructor and use same instance every time?
const runner = new _relayCompiler.Runner({
parserConfigs: _this.parserConfigs,
writerConfigs: _this.writerConfigs,
reporter: new TemporaryReporter(),
reporter: new RaiseErrorsReporter(),
onlyValidate: false,
skipPersist: true
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"scripts": {
"clean": "rimraf dist",
"flow": "flow",
"format": "prettier-standard '{src}/**/*.js'",
"format:check": "standard '{src}/**/*.js'",
"format": "prettier-standard 'src/**/*.js'",
"format:check": "standard 'src/**/*.js'",
"lint": "eslint --fix 'src/**/*.js'",
"lint:check": "eslint 'src/**/*.js'",
"test": "jest test --env node",
Expand Down
23 changes: 0 additions & 23 deletions src/getFileFilter.js

This file was deleted.

Loading

0 comments on commit 3a8c315

Please sign in to comment.