babel-plugin-inline-import-graphql-ast
Advanced tools
Comparing version 2.3.6 to 2.3.7
@@ -6,5 +6,11 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "requireGql", { | ||
enumerable: true, | ||
get: function get() { | ||
return _requireGql.requireGql; | ||
} | ||
}); | ||
exports.default = void 0; | ||
var _path = _interopRequireWildcard(require("path")); | ||
var _path = require("path"); | ||
@@ -15,13 +21,4 @@ var _fs = require("fs"); | ||
var _graphqlTag = _interopRequireDefault(require("graphql-tag")); | ||
var _requireGql = require("./requireGql"); | ||
var _multiOp = require("./multi-op"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var newlinePattern = /(\r\n|\r|\n)+/; | ||
var resolve; | ||
@@ -36,5 +33,3 @@ | ||
if (!resolve) { | ||
resolve = resolveModuleSource || function (src, file) { | ||
return _path.default.resolve((0, _path.dirname)(file), src); | ||
}; | ||
resolve = resolveModuleSource || _requireGql.defaultResolve; | ||
} | ||
@@ -52,3 +47,3 @@ }, | ||
if (importPath.endsWith('.graphql') || importPath.endsWith('.gql')) { | ||
// Find and fetch the file, using node resolution/NODE_PATH if necessary. | ||
// Find the file, using node resolution/NODE_PATH if necessary. | ||
var fallbackPaths = nodePath ? nodePath.split(_path.delimiter) : process.env.NODE_PATH; | ||
@@ -58,10 +53,13 @@ var absPath = resolve(importPath, babelPath); | ||
paths: fallbackPaths | ||
}); // Analyze the file, returning either schema source (str) or a docs object containing ops. | ||
var result = (0, _requireGql.requireGql)(absPath, { | ||
resolve, | ||
nowrap: false | ||
}); | ||
var source = (0, _fs.readFileSync)(absPath).toString(); // If the file doesn't contain query/mutation/subscription, insert raw text, else parse. | ||
if (isSchemaLike(source)) { | ||
curPath.replaceWith(buildVariableAST(source, importNames[0].local.name)); | ||
if (typeof result === 'string') { | ||
curPath.replaceWith(buildVariableAST(result, importNames[0].local.name)); | ||
} else { | ||
var doc = processDoc(createDoc(source, absPath)); | ||
var replacements = buildReplacements((0, _multiOp.createDocPerOp)(doc), importNames); | ||
var replacements = buildReplacements(result, importNames); | ||
replacements.length > 1 ? curPath.replaceWithMultiple(replacements) : curPath.replaceWith(replacements[0]); | ||
@@ -71,16 +69,2 @@ } | ||
function isSchemaLike(source) { | ||
var content = source.split(newlinePattern).filter(function (line) { | ||
return !newlinePattern.test(line); | ||
}).filter(function (line) { | ||
return !line.startsWith('#'); | ||
}).filter(function (line) { | ||
return line.length > 0; | ||
}).map(function (line) { | ||
return line.trimLeft(); | ||
}); | ||
var operationsPattern = /^(fragment|query|mutation|subscription)/; | ||
return !operationsPattern.test(content[0]); | ||
} | ||
function buildReplacements(docs, specifiers) { | ||
@@ -123,64 +107,2 @@ return specifiers.map(function (_ref4) { | ||
exports.default = _default; | ||
function createDoc(source, absPath) { | ||
var ast = null; | ||
var fragmentDefs = []; | ||
return { | ||
processFragments() { | ||
processImports(getImportStatements(source), absPath); | ||
function getImportStatements(src) { | ||
return src.split(newlinePattern).filter(function (line) { | ||
return line.startsWith('#import'); | ||
}); | ||
} | ||
function processImports(imports, relFile) { | ||
imports.forEach(function (statement) { | ||
var fragmentPath = statement.split(/[\s\n]+/g)[1].slice(1, -1); | ||
var absFragmentPath = resolve(fragmentPath, relFile); | ||
var fragmentSource = (0, _fs.readFileSync)(absFragmentPath.replace(/'/g, '')).toString(); | ||
var subFragments = getImportStatements(fragmentSource); | ||
if (subFragments.length > 0) processImports(subFragments, absFragmentPath); // prettier-ignore | ||
fragmentDefs = _toConsumableArray(_graphqlTag.default`${fragmentSource}`.definitions).concat(_toConsumableArray(fragmentDefs)); | ||
}); | ||
} | ||
}, | ||
parse() { | ||
// prettier-ignore | ||
var parsedAST = _graphqlTag.default`${source}`; | ||
parsedAST.definitions = _toConsumableArray(parsedAST.definitions).concat(_toConsumableArray(fragmentDefs)); | ||
ast = parsedAST; | ||
}, | ||
dedupeFragments() { | ||
var seenNames = {}; | ||
ast.definitions = ast.definitions.filter(function (def) { | ||
if (def.kind !== 'FragmentDefinition') return true; | ||
return seenNames[def.name.value] ? false : seenNames[def.name.value] = true; | ||
}); | ||
}, | ||
makeSourceEnumerable() { | ||
var newAST = JSON.parse(JSON.stringify(ast)); | ||
newAST.loc.source = ast.loc.source; | ||
ast = newAST; | ||
}, | ||
get ast() { | ||
return ast; | ||
} | ||
}; | ||
} | ||
function processDoc(doc) { | ||
doc.processFragments(); | ||
doc.parse(); | ||
doc.dedupeFragments(); | ||
doc.makeSourceEnumerable(); | ||
return doc.ast; | ||
} | ||
exports.default = _default; |
# ChangeLog | ||
## v2.3.7 (April 26, 2018) | ||
### Maintenance | ||
* Update dependencies | ||
## v2.3.6 (March 30, 2018) | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "babel-plugin-inline-import-graphql-ast", | ||
"version": "2.3.6", | ||
"version": "2.3.7", | ||
"description": "Babel plugin to make .gql/.graphql files importable", | ||
@@ -22,4 +22,4 @@ "author": "Alexander Roberts <detrohutt@gmail.com>", | ||
"dependencies": { | ||
"@babel/core": "7.0.0-beta.42", | ||
"graphql-tag": "^2.8.0" | ||
"@babel/core": "7.0.0-beta.46", | ||
"graphql-tag": "^2.9.1" | ||
}, | ||
@@ -30,20 +30,20 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.42", | ||
"@babel/preset-env": "^7.0.0-beta.42", | ||
"@babel/cli": "^7.0.0-beta.46", | ||
"@babel/preset-env": "^7.0.0-beta.46", | ||
"babel-core": "7.0.0-bridge.0", | ||
"babel-eslint": "^8.2.2", | ||
"babel-jest": "^22.4.1", | ||
"babel-eslint": "^8.2.3", | ||
"babel-jest": "^22.4.3", | ||
"dedent": "^0.7.0", | ||
"eslint": "^4.18.2", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.9.0", | ||
"eslint-plugin-jest": "^21.13.0", | ||
"eslint-plugin-import": "^2.11.0", | ||
"eslint-plugin-jest": "^21.15.1", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"graphql": "^0.13.1", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"graphql": "^0.13.2", | ||
"husky": "^0.15.0-rc.8", | ||
"jest": "^22.4.2", | ||
"lint-staged": "^7.0.0", | ||
"prettier": "^1.11.1" | ||
"jest": "^22.4.3", | ||
"lint-staged": "^7.0.5", | ||
"prettier": "^1.12.1" | ||
}, | ||
@@ -50,0 +50,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24451
7
284
5
+ Added@babel/code-frame@7.0.0-beta.46(transitive)
+ Added@babel/core@7.0.0-beta.46(transitive)
+ Added@babel/generator@7.0.0-beta.46(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.46(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.46(transitive)
+ Added@babel/helper-split-export-declaration@7.0.0-beta.46(transitive)
+ Added@babel/helpers@7.0.0-beta.46(transitive)
+ Added@babel/highlight@7.0.0-beta.46(transitive)
+ Added@babel/template@7.0.0-beta.46(transitive)
+ Added@babel/traverse@7.0.0-beta.46(transitive)
+ Added@babel/types@7.0.0-beta.46(transitive)
+ Addedbabylon@7.0.0-beta.46(transitive)
- Removed@babel/code-frame@7.0.0-beta.42(transitive)
- Removed@babel/core@7.0.0-beta.42(transitive)
- Removed@babel/generator@7.0.0-beta.42(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.42(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.42(transitive)
- Removed@babel/helper-split-export-declaration@7.0.0-beta.42(transitive)
- Removed@babel/helpers@7.0.0-beta.42(transitive)
- Removed@babel/highlight@7.0.0-beta.42(transitive)
- Removed@babel/template@7.0.0-beta.42(transitive)
- Removed@babel/traverse@7.0.0-beta.42(transitive)
- Removed@babel/types@7.0.0-beta.42(transitive)
- Removedbabylon@7.0.0-beta.42(transitive)
Updated@babel/core@7.0.0-beta.46
Updatedgraphql-tag@^2.9.1