UNPKG

1.89 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
6
7/**
8 * The problem: after GraphQL schema rebuilds, eslint loader keeps validating against
9 * the old schema.
10 *
11 * This plugin replaces options of eslint-plugin-graphql during develop
12 */
13const {
14 store
15} = require(`../redux`);
16
17const eslintConfig = require(`./eslint-config`);
18
19const hasLocalEslint = require(`./local-eslint-config-finder`);
20
21const isEslintRule = rule => {
22 const options = rule && rule.use && rule.use[0] && rule.use[0].options;
23 return options && typeof options.useEslintrc !== `undefined`;
24};
25
26class GatsbyWebpackEslintGraphqlSchemaReload {
27 constructor(options) {
28 (0, _defineProperty2.default)(this, "schema", null);
29 (0, _defineProperty2.default)(this, "findEslintOptions", compiler => compiler.options.module.rules.find(isEslintRule).use[0].options);
30 this.plugin = {
31 name: `GatsbyWebpackEslintGraphqlSchemaReload`
32 };
33 this.options = options || {};
34 }
35
36 apply(compiler) {
37 compiler.hooks.compile.tap(this.plugin.name, () => {
38 const {
39 schema,
40 program
41 } = store.getState();
42
43 if (!this.schema) {
44 // initial build
45 this.schema = schema;
46 return;
47 }
48
49 if (this.schema === schema || hasLocalEslint(program.directory)) {
50 return;
51 }
52
53 this.schema = schema; // Original eslint config object from webpack rules
54
55 const options = this.findEslintOptions(compiler); // Hackish but works:
56 // replacing original eslint options object with updated config
57
58 Object.assign(options, eslintConfig(schema));
59 });
60 }
61
62}
63
64module.exports = GatsbyWebpackEslintGraphqlSchemaReload;
65//# sourceMappingURL=gatsby-webpack-eslint-graphql-schema-reload-plugin.js.map
\No newline at end of file