@graphql-tools/webpack-loader
Advanced tools
Comparing version 6.2.1-alpha-9d2b5611.0 to 6.2.1-alpha-a8c7b966.0
@@ -17,5 +17,5 @@ 'use strict'; | ||
// A map docString -> graphql document | ||
var docCache = {}; | ||
const docCache = {}; | ||
// A map fragmentName -> [normalized source] | ||
var fragmentSourceMap = {}; | ||
const fragmentSourceMap = {}; | ||
function cacheKeyFromFragment(fragment) { | ||
@@ -30,9 +30,9 @@ return normalize(graphql.print(fragment)); | ||
function processFragments(ast) { | ||
var astFragmentMap = {}; | ||
var definitions = []; | ||
for (var i = 0; i < ast.definitions.length; i++) { | ||
var fragmentDefinition = ast.definitions[i]; | ||
const astFragmentMap = {}; | ||
const definitions = []; | ||
for (let i = 0; i < ast.definitions.length; i++) { | ||
const fragmentDefinition = ast.definitions[i]; | ||
if (fragmentDefinition.kind === graphql.Kind.FRAGMENT_DEFINITION) { | ||
var fragmentName = fragmentDefinition.name.value; | ||
var sourceKey = cacheKeyFromFragment(fragmentDefinition); | ||
const fragmentName = fragmentDefinition.name.value; | ||
const sourceKey = cacheKeyFromFragment(fragmentDefinition); | ||
// We know something about this fragment | ||
@@ -59,7 +59,7 @@ if (fragmentSourceMap.hasOwnProperty(fragmentName) && !fragmentSourceMap[fragmentName][sourceKey]) { | ||
function parseDocument(doc) { | ||
var cacheKey = normalize(doc); | ||
const cacheKey = normalize(doc); | ||
if (docCache[cacheKey]) { | ||
return docCache[cacheKey]; | ||
} | ||
var parsed = graphql.parse(doc, { | ||
const parsed = graphql.parse(doc, { | ||
noLocation: true, | ||
@@ -77,3 +77,3 @@ }); | ||
function isSDL(doc) { | ||
return !doc.definitions.some(function (def) { return graphql.isExecutableDefinitionNode(def); }); | ||
return !doc.definitions.some(def => graphql.isExecutableDefinitionNode(def)); | ||
} | ||
@@ -103,11 +103,16 @@ function removeDescriptions(doc) { | ||
function expandImports(source, options) { | ||
var lines = source.split(/\r\n|\r|\n/); | ||
var outputCode = options.importHelpers | ||
? "\n var useUnique = require('@graphql-tools/webpack-loader-runtime').useUnique;\n var unique = useUnique();\n " | ||
: "\n " + webpackLoaderRuntime.uniqueCode + "\n "; | ||
lines.some(function (line) { | ||
const lines = source.split(/\r\n|\r|\n/); | ||
let outputCode = options.importHelpers | ||
? ` | ||
var useUnique = require('@graphql-tools/webpack-loader-runtime').useUnique; | ||
var unique = useUnique(); | ||
` | ||
: ` | ||
${webpackLoaderRuntime.uniqueCode} | ||
`; | ||
lines.some(line => { | ||
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') { | ||
var importFile = line.slice(1).split(' ')[1]; | ||
var parseDocument_1 = "require(" + importFile + ")"; | ||
var appendDef = "doc.definitions = doc.definitions.concat(unique(" + parseDocument_1 + ".definitions));"; | ||
const importFile = line.slice(1).split(' ')[1]; | ||
const parseDocument = `require(${importFile})`; | ||
const appendDef = `doc.definitions = doc.definitions.concat(unique(${parseDocument}.definitions));`; | ||
outputCode += appendDef + os.EOL; | ||
@@ -121,4 +126,4 @@ } | ||
this.cacheable(); | ||
var options = this.query || {}; | ||
var doc = parseDocument(source); | ||
const options = this.query || {}; | ||
let doc = parseDocument(source); | ||
// Removes descriptions from Nodes | ||
@@ -128,8 +133,10 @@ if (options.noDescription) { | ||
} | ||
var headerCode = "\n var doc = " + JSON.stringify(doc) + ";\n "; | ||
var outputCode = ''; | ||
const headerCode = ` | ||
var doc = ${JSON.stringify(doc)}; | ||
`; | ||
let outputCode = ''; | ||
// Allow multiple query/mutation definitions in a file. This parses out dependencies | ||
// at compile time, and then uses those at load time to create minimal query documents | ||
// We cannot do the latter at compile time due to how the #import code works. | ||
var operationCount = doc.definitions.reduce(function (accum, op) { | ||
const operationCount = doc.definitions.reduce((accum, op) => { | ||
if (op.kind === graphql.Kind.OPERATION_DEFINITION) { | ||
@@ -142,5 +149,5 @@ return accum + 1; | ||
if (options.esModule) { | ||
return "export default " + identifier; | ||
return `export default ${identifier}`; | ||
} | ||
return "module.exports = " + identifier; | ||
return `module.exports = ${identifier}`; | ||
} | ||
@@ -150,5 +157,7 @@ if (operationCount > 1) { | ||
} | ||
outputCode += "\n " + exportDefaultStatement('doc') + "\n "; | ||
var importOutputCode = expandImports(source, options); | ||
var allCode = [headerCode, importOutputCode, outputCode, ''].join(os.EOL); | ||
outputCode += ` | ||
${exportDefaultStatement('doc')} | ||
`; | ||
const importOutputCode = expandImports(source, options); | ||
const allCode = [headerCode, importOutputCode, outputCode, ''].join(os.EOL); | ||
return allCode; | ||
@@ -155,0 +164,0 @@ } |
@@ -13,5 +13,5 @@ import os from 'os'; | ||
// A map docString -> graphql document | ||
var docCache = {}; | ||
const docCache = {}; | ||
// A map fragmentName -> [normalized source] | ||
var fragmentSourceMap = {}; | ||
const fragmentSourceMap = {}; | ||
function cacheKeyFromFragment(fragment) { | ||
@@ -26,9 +26,9 @@ return normalize(print(fragment)); | ||
function processFragments(ast) { | ||
var astFragmentMap = {}; | ||
var definitions = []; | ||
for (var i = 0; i < ast.definitions.length; i++) { | ||
var fragmentDefinition = ast.definitions[i]; | ||
const astFragmentMap = {}; | ||
const definitions = []; | ||
for (let i = 0; i < ast.definitions.length; i++) { | ||
const fragmentDefinition = ast.definitions[i]; | ||
if (fragmentDefinition.kind === Kind.FRAGMENT_DEFINITION) { | ||
var fragmentName = fragmentDefinition.name.value; | ||
var sourceKey = cacheKeyFromFragment(fragmentDefinition); | ||
const fragmentName = fragmentDefinition.name.value; | ||
const sourceKey = cacheKeyFromFragment(fragmentDefinition); | ||
// We know something about this fragment | ||
@@ -55,7 +55,7 @@ if (fragmentSourceMap.hasOwnProperty(fragmentName) && !fragmentSourceMap[fragmentName][sourceKey]) { | ||
function parseDocument(doc) { | ||
var cacheKey = normalize(doc); | ||
const cacheKey = normalize(doc); | ||
if (docCache[cacheKey]) { | ||
return docCache[cacheKey]; | ||
} | ||
var parsed = parse(doc, { | ||
const parsed = parse(doc, { | ||
noLocation: true, | ||
@@ -73,3 +73,3 @@ }); | ||
function isSDL(doc) { | ||
return !doc.definitions.some(function (def) { return isExecutableDefinitionNode(def); }); | ||
return !doc.definitions.some(def => isExecutableDefinitionNode(def)); | ||
} | ||
@@ -99,11 +99,16 @@ function removeDescriptions(doc) { | ||
function expandImports(source, options) { | ||
var lines = source.split(/\r\n|\r|\n/); | ||
var outputCode = options.importHelpers | ||
? "\n var useUnique = require('@graphql-tools/webpack-loader-runtime').useUnique;\n var unique = useUnique();\n " | ||
: "\n " + uniqueCode + "\n "; | ||
lines.some(function (line) { | ||
const lines = source.split(/\r\n|\r|\n/); | ||
let outputCode = options.importHelpers | ||
? ` | ||
var useUnique = require('@graphql-tools/webpack-loader-runtime').useUnique; | ||
var unique = useUnique(); | ||
` | ||
: ` | ||
${uniqueCode} | ||
`; | ||
lines.some(line => { | ||
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') { | ||
var importFile = line.slice(1).split(' ')[1]; | ||
var parseDocument_1 = "require(" + importFile + ")"; | ||
var appendDef = "doc.definitions = doc.definitions.concat(unique(" + parseDocument_1 + ".definitions));"; | ||
const importFile = line.slice(1).split(' ')[1]; | ||
const parseDocument = `require(${importFile})`; | ||
const appendDef = `doc.definitions = doc.definitions.concat(unique(${parseDocument}.definitions));`; | ||
outputCode += appendDef + os.EOL; | ||
@@ -117,4 +122,4 @@ } | ||
this.cacheable(); | ||
var options = this.query || {}; | ||
var doc = parseDocument(source); | ||
const options = this.query || {}; | ||
let doc = parseDocument(source); | ||
// Removes descriptions from Nodes | ||
@@ -124,8 +129,10 @@ if (options.noDescription) { | ||
} | ||
var headerCode = "\n var doc = " + JSON.stringify(doc) + ";\n "; | ||
var outputCode = ''; | ||
const headerCode = ` | ||
var doc = ${JSON.stringify(doc)}; | ||
`; | ||
let outputCode = ''; | ||
// Allow multiple query/mutation definitions in a file. This parses out dependencies | ||
// at compile time, and then uses those at load time to create minimal query documents | ||
// We cannot do the latter at compile time due to how the #import code works. | ||
var operationCount = doc.definitions.reduce(function (accum, op) { | ||
const operationCount = doc.definitions.reduce((accum, op) => { | ||
if (op.kind === Kind.OPERATION_DEFINITION) { | ||
@@ -138,5 +145,5 @@ return accum + 1; | ||
if (options.esModule) { | ||
return "export default " + identifier; | ||
return `export default ${identifier}`; | ||
} | ||
return "module.exports = " + identifier; | ||
return `module.exports = ${identifier}`; | ||
} | ||
@@ -146,5 +153,7 @@ if (operationCount > 1) { | ||
} | ||
outputCode += "\n " + exportDefaultStatement('doc') + "\n "; | ||
var importOutputCode = expandImports(source, options); | ||
var allCode = [headerCode, importOutputCode, outputCode, ''].join(os.EOL); | ||
outputCode += ` | ||
${exportDefaultStatement('doc')} | ||
`; | ||
const importOutputCode = expandImports(source, options); | ||
const allCode = [headerCode, importOutputCode, outputCode, ''].join(os.EOL); | ||
return allCode; | ||
@@ -151,0 +160,0 @@ } |
{ | ||
"name": "@graphql-tools/webpack-loader", | ||
"version": "6.2.1-alpha-9d2b5611.0", | ||
"version": "6.2.1-alpha-a8c7b966.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -10,3 +10,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@graphql-tools/webpack-loader-runtime": "6.2.1-alpha-9d2b5611.0", | ||
"@graphql-tools/webpack-loader-runtime": "6.2.1-alpha-a8c7b966.0", | ||
"tslib": "~2.0.1" | ||
@@ -13,0 +13,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
307
+ Added@graphql-tools/webpack-loader-runtime@6.2.1-alpha-a8c7b966.0(transitive)
- Removed@graphql-tools/webpack-loader-runtime@6.2.1-alpha-9d2b5611.0(transitive)
Updated@graphql-tools/webpack-loader-runtime@6.2.1-alpha-a8c7b966.0