From 5235a14f4fe85692261bdc565ba1a05c7d6e5671 Mon Sep 17 00:00:00 2001 From: Victor Vlasenko Date: Mon, 28 May 2018 09:40:42 +0300 Subject: [PATCH] Add workaround to support graphql-persisted-document-loader Fixes: #21 --- index.js | 15 ++++++++++++++- package.json | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9ffa4c8..c7cce7e 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,20 @@ PersistGraphQLPlugin.prototype.apply = function(compiler) { compilation.modules.forEach(function(module) { if (!self.options.excludeRegex.test(module.resource)) { if (self.options.graphqlRegex.test(module.resource)) { - graphQLString += graphql.print(eval(module._source._value)); + // Workaround for `graphql-persisted-document-loader` + // It does not generate eval-friendly source code at the moment + var sourceLines = module._source._value.split(/\r\n|\r|\n/); + if ( + sourceLines + .slice(-1) + .pop() + .indexOf('doc.documentId') === 0 + ) { + // Swap last two lines of source code + var len = sourceLines.length; + sourceLines = sourceLines.slice(0, len - 2).concat([sourceLines[len - 1], sourceLines[len - 2]]); + } + graphQLString += graphql.print(eval(sourceLines.join('\n'))); } else if (self.options.jsRegex.test(module.resource)) { var literalContents = ExtractFromJs.findTaggedTemplateLiteralsInJS(module._source._value, 'gql'); var queryList = literalContents.map(ExtractFromJs.eliminateInterpolations); diff --git a/package.json b/package.json index 6bd4e4a..ab4cb87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "persistgraphql-webpack-plugin", - "version": "0.5.2", + "version": "0.5.3", "description": "PersistGraphQL Webpack Plugin", "main": "index.js", "scripts": {