Skip to content

Commit

Permalink
Add workaround to support graphql-persisted-document-loader
Browse files Browse the repository at this point in the history
Fixes: #21
  • Loading branch information
larixer committed May 28, 2018
1 parent f302880 commit 5235a14
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "persistgraphql-webpack-plugin",
"version": "0.5.2",
"version": "0.5.3",
"description": "PersistGraphQL Webpack Plugin",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 5235a14

Please sign in to comment.