babel-plugin-inline-import-graphql-ast
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -44,11 +44,33 @@ "use strict"; | ||
nodePath = _ref3.opts.nodePath; | ||
var importNames = curPath.node.specifiers; | ||
var importPath = curPath.node.source.value; | ||
if (importPath.endsWith('.graphql') || importPath.endsWith('.gql')) { | ||
// Find and fetch the file, using node resolution/NODE_PATH if necessary. | ||
var fallbackPaths = nodePath ? nodePath.split(_path.delimiter) : process.env.NODE_PATH; | ||
var doc = processDoc(createDoc(importPath, babelPath, fallbackPaths)); | ||
var replacements = buildReplacements((0, _multiOp.createDocPerOp)(doc), curPath.node.specifiers); | ||
replacements.length > 1 ? curPath.replaceWithMultiple(replacements) : curPath.replaceWith(replacements[0]); | ||
var absPath = resolve(importPath, babelPath); | ||
if (!(0, _fs.existsSync)(absPath)) absPath = require.resolve(importPath, { | ||
paths: fallbackPaths | ||
}); | ||
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)); | ||
} else { | ||
var doc = processDoc(createDoc(source, absPath)); | ||
var replacements = buildReplacements((0, _multiOp.createDocPerOp)(doc), importNames); | ||
replacements.length > 1 ? curPath.replaceWithMultiple(replacements) : curPath.replaceWith(replacements[0]); | ||
} | ||
} | ||
function isSchemaLike(source) { | ||
var content = source.split(/(\r\n|\r|\n)+/).filter(function (line) { | ||
return !line.startsWith('#'); | ||
}).filter(function (line) { | ||
return line.length > 0; | ||
}); | ||
var operationsPattern = /^(query|mutation|subscription)/; | ||
return !operationsPattern.test(content[0]); | ||
} | ||
function buildReplacements(docs, specifiers) { | ||
@@ -91,8 +113,3 @@ return specifiers.map(function (_ref4) { | ||
function createDoc(importPath, babelPath, paths) { | ||
var absPath = resolve(importPath, babelPath); | ||
if (!(0, _fs.existsSync)(absPath)) absPath = require.resolve(importPath, { | ||
paths | ||
}); | ||
var source = (0, _fs.readFileSync)(absPath).toString(); | ||
function createDoc(source, absPath) { | ||
var ast = null; | ||
@@ -99,0 +116,0 @@ var fragmentDefs = []; |
# ChangeLog | ||
## v2.3.0 (March 8, 2018) | ||
### Features | ||
* Initial support for schema-like files | ||
### Maintenance | ||
* Fix typos in README | ||
## v2.2.0 (February 17, 2018) | ||
### features | ||
### Features | ||
@@ -7,0 +17,0 @@ * Multiple operations in a single GraphQL file |
{ | ||
"name": "babel-plugin-inline-import-graphql-ast", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Babel plugin to make .gql/.graphql files importable", | ||
@@ -34,2 +34,3 @@ "author": "Alexander Roberts <detrohutt@gmail.com>", | ||
"babel-jest": "^22.2.2", | ||
"dedent": "^0.7.0", | ||
"eslint": "^4.17.0", | ||
@@ -36,0 +37,0 @@ "eslint-config-standard": "^11.0.0-beta.0", |
@@ -1,2 +0,5 @@ | ||
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3AYURHRU7PMCL) | ||
[![npm Version](https://img.shields.io/npm/v/babel-plugin-inline-import-graphql-ast.svg)](https://www.npmjs.com/package/babel-plugin-inline-import-graphql-ast) | ||
[![npm Downloads](https://img.shields.io/npm/dm/babel-plugin-inline-import-graphql-ast.svg)](https://www.npmjs.com/package/babel-plugin-inline-import-graphql-ast) | ||
[![npm License](https://img.shields.io/npm/l/babel-plugin-inline-import-graphql-ast.svg)](https://www.npmjs.com/package/babel-plugin-inline-import-graphql-ast) | ||
[![donate](https://img.shields.io/badge/donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3AYURHRU7PMCL) | ||
@@ -27,3 +30,3 @@ # Babel Inline Import GraphQL AST | ||
... | ||
export graphql(myQuery)(myComponent) | ||
export default graphql(myQuery)(myComponent) | ||
``` | ||
@@ -39,2 +42,8 @@ | ||
#### Schema-like `.gql`/`.graphql` files | ||
This package was originally intended only for frontend graphql files containing operations, which are to be parsed into GraphQL AST syntax before being inlined into the code. | ||
There is now limited support for files containing types and schema definitions. | ||
Specifically, only default import syntax is supported, and the entire file will be inlined as raw text. If there are specific features you'd like to see for use with schema-like files, let me know. | ||
#### In `.gql`/`.graphql` files | ||
@@ -166,3 +175,3 @@ | ||
"scripts": { | ||
"start": "rimraf ./node_modules/.cache/babel-loader &&" | ||
"start": "rimraf ./node_modules/.cache/babel-loader && node index.js" | ||
} | ||
@@ -169,0 +178,0 @@ } |
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
21045
231
187
18