babel-plugin-inline-import-graphql-ast
Advanced tools
Comparing version 1.1.1 to 1.2.0
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
@@ -15,5 +15,5 @@ | ||
var _requireResolve = require('require-resolve'); | ||
var _resolve = require('resolve'); | ||
var _requireResolve2 = _interopRequireDefault(_requireResolve); | ||
var _resolve2 = _interopRequireDefault(_resolve); | ||
@@ -25,83 +25,87 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var BabelInlineImportHelper = function () { | ||
function BabelInlineImportHelper() { | ||
_classCallCheck(this, BabelInlineImportHelper); | ||
} | ||
function BabelInlineImportHelper() { | ||
_classCallCheck(this, BabelInlineImportHelper); | ||
} | ||
_createClass(BabelInlineImportHelper, null, [{ | ||
key: 'shouldBeInlined', | ||
value: function shouldBeInlined(givenPath, extensions) { | ||
var accept = typeof extensions === 'string' ? [extensions] : extensions || BabelInlineImportHelper.extensions; | ||
_createClass(BabelInlineImportHelper, null, [{ | ||
key: 'shouldBeInlined', | ||
value: function shouldBeInlined(givenPath, extensions) { | ||
var accept = typeof extensions === 'string' ? [extensions] : extensions || BabelInlineImportHelper.extensions; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = accept[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var extension = _step.value; | ||
try { | ||
for (var _iterator = accept[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var extension = _step.value; | ||
if (givenPath.endsWith(extension)) { | ||
return true; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
if (givenPath.endsWith(extension)) { | ||
return true; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: 'getContents', | ||
value: function getContents(givenPath, reference) { | ||
if (!reference) { | ||
throw new Error('"reference" argument must be specified'); | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: 'getContents', | ||
value: function getContents(givenPath, reference) { | ||
if (!reference) { | ||
throw new Error('"reference" argument must be specified'); | ||
} | ||
var mod = (0, _requireResolve2.default)(givenPath, _path2.default.resolve(reference)); | ||
var filepath = void 0; | ||
try { | ||
filepath = _resolve2.default.sync(givenPath, { | ||
basedir: _path2.default.dirname(reference), | ||
extensions: ['graphql', 'gql'] | ||
}); | ||
} catch (e) { | ||
throw new Error('Path \'' + givenPath + '\' could not be found for \'' + reference + '\''); | ||
} | ||
if (!mod || !mod.src) { | ||
throw new Error('Path \'' + givenPath + '\' could not be found for \'' + reference + '\''); | ||
} | ||
return _fs2.default.readFileSync(filepath).toString(); | ||
} | ||
}, { | ||
key: 'transformRelativeToRootPath', | ||
value: function transformRelativeToRootPath(path, rootPathSuffix) { | ||
if (this.hasRoot(path)) { | ||
var withoutRoot = path.substring(1, path.length); | ||
return '' + BabelInlineImportHelper.root + (rootPathSuffix || '') + '/' + withoutRoot; | ||
} | ||
if (typeof path === 'string') { | ||
return path; | ||
} | ||
throw new Error('ERROR: No path passed'); | ||
} | ||
}, { | ||
key: 'hasRoot', | ||
value: function hasRoot(string) { | ||
if (typeof string !== 'string') { | ||
return false; | ||
} | ||
return _fs2.default.readFileSync(mod.src).toString(); | ||
} | ||
}, { | ||
key: 'transformRelativeToRootPath', | ||
value: function transformRelativeToRootPath(path, rootPathSuffix) { | ||
if (this.hasRoot(path)) { | ||
var withoutRoot = path.substring(1, path.length); | ||
return '' + BabelInlineImportHelper.root + (rootPathSuffix || '') + '/' + withoutRoot; | ||
} | ||
if (typeof path === 'string') { | ||
return path; | ||
} | ||
throw new Error('ERROR: No path passed'); | ||
} | ||
}, { | ||
key: 'hasRoot', | ||
value: function hasRoot(string) { | ||
if (typeof string !== 'string') { | ||
return false; | ||
} | ||
return string.substring(0, 1) === '/'; | ||
} | ||
}]); | ||
return string.substring(0, 1) === '/'; | ||
} | ||
}]); | ||
return BabelInlineImportHelper; | ||
return BabelInlineImportHelper; | ||
}(); | ||
BabelInlineImportHelper.extensions = ['.raw', '.text', '.gql', '.graphql']; | ||
BabelInlineImportHelper.extensions = ['.graphql', '.gql']; | ||
BabelInlineImportHelper.root = global.rootPath || process.cwd(); | ||
exports.default = BabelInlineImportHelper; |
@@ -1,71 +0,91 @@ | ||
var _templateObject = _taggedTemplateLiteral(['', ''], ['', '']); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var gql = require('graphql-tag').default; | ||
var parse = require('babylon').parse; | ||
var _templateObject = _taggedTemplateLiteral(['', ''], ['', '']); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _path = require('path'); | ||
exports.default = function (_ref) { | ||
var t = _ref.types; | ||
var _path2 = _interopRequireDefault(_path); | ||
var BabelInlineImport = function BabelInlineImport() { | ||
_classCallCheck(this, BabelInlineImport); | ||
var _graphqlTag = require('graphql-tag'); | ||
return { | ||
visitor: { | ||
ImportDeclaration: { | ||
exit: function exit(path, state) { | ||
var givenPath = path.node.source.value; | ||
var reference = state && state.file && state.file.opts.filename; | ||
var extensions = state && state.opts && state.opts.extensions; | ||
var _graphqlTag2 = _interopRequireDefault(_graphqlTag); | ||
if (_helper2.default.shouldBeInlined(givenPath, extensions)) { | ||
if (path.node.specifiers.length > 1) { | ||
throw new Error('Destructuring inlined import is not allowed. Check the import statement for \'' + givenPath + '\''); | ||
} | ||
debugger; | ||
var _babylon = require('babylon'); | ||
// Here we detect the use of Meteor by checking global.meteorBabelHelpers | ||
if (global.meteorBabelHelpers && _helper2.default.hasRoot(reference)) { | ||
reference = _helper2.default.transformRelativeToRootPath(reference); | ||
} | ||
var _helper = require('./helper'); | ||
var id = path.node.specifiers[0].local.name; | ||
var content = _helper2.default.getContents(givenPath, reference); | ||
var _helper2 = _interopRequireDefault(_helper); | ||
graphqlAST = gql(_templateObject, content); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var babelAST = parse("const obj = " + JSON.stringify(graphqlAST)); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var objExp = babelAST.program.body[0].declarations[0].init; | ||
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } | ||
var variable = t.variableDeclarator(t.identifier(id), t.objectExpression(objExp.properties)); | ||
var filterFn = function filterFn(line) { | ||
return !(line[0] === '#' && line.slice(1).split(' ')[0] === 'import') && !(line === ''); | ||
}; | ||
path.replaceWith(t.variableDeclaration('const', [variable])); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
var stripImports = function stripImports(source) { | ||
return source.split('\n').filter(filterFn).join('\n'); | ||
}; | ||
return new BabelInlineImport(); | ||
var expandImports = function expandImports(source, resolvePath, reference) { | ||
reference = _path2.default.dirname(reference); | ||
var queryAST = (0, _graphqlTag2.default)(_templateObject, stripImports(source)); | ||
var lines = source.split('\n'); | ||
var defs = queryAST.definitions; | ||
lines.some(function (line) { | ||
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') { | ||
var importFile = line.slice(1).split(' ')[1].slice(1, -1); | ||
var fragmentAST = (0, _graphqlTag2.default)(_templateObject, _helper2.default.getContents(importFile, _path2.default.resolve(reference, resolvePath))); | ||
defs = [].concat(_toConsumableArray(defs), _toConsumableArray(fragmentAST.definitions)); | ||
} | ||
return line.length !== 0 && line[0] !== '#'; | ||
}); | ||
return _extends({}, queryAST, { definitions: defs }); | ||
}; | ||
var _helper = require('./helper'); | ||
exports.default = function (_ref) { | ||
var t = _ref.types; | ||
return { | ||
visitor: { | ||
ImportDeclaration: { | ||
exit: function exit(path, state) { | ||
var shouldBeInlined = _helper2.default.shouldBeInlined, | ||
hasRoot = _helper2.default.hasRoot, | ||
transformRelativeToRootPath = _helper2.default.transformRelativeToRootPath, | ||
getContents = _helper2.default.getContents; | ||
var _helper2 = _interopRequireDefault(_helper); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { default: obj }; | ||
} | ||
var givenPath = path.node.source.value; | ||
var reference = state && state.file && state.file.opts.filename; | ||
var extensions = state && state.opts && state.opts.extensions; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
if (_helper2.default.shouldBeInlined(givenPath, extensions)) { | ||
if (path.node.specifiers.length > 1) { | ||
throw new Error('Destructuring inlined import is not allowed. Check the import statement for \'' + givenPath + '\''); | ||
} | ||
// Here we detect the use of Meteor by checking global.meteorBabelHelpers | ||
if (global.meteorBabelHelpers && hasRoot(reference)) { | ||
reference = transformRelativeToRootPath(reference); | ||
} | ||
var content = getContents(givenPath, reference); | ||
var graphqlAST = expandImports(content, givenPath, reference); | ||
var babelAST = (0, _babylon.parse)('const obj = ' + JSON.stringify(graphqlAST)); | ||
var objExp = babelAST.program.body[0].declarations[0].init; | ||
var variable = t.variableDeclarator(t.identifier(path.node.specifiers[0].local.name), t.objectExpression(objExp.properties)); | ||
path.replaceWith(t.variableDeclaration('const', [variable])); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-inline-import-graphql-ast", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Babel plugin to make raw files importable", | ||
@@ -30,3 +30,3 @@ "author": "Alexander Roberts <detrohutt@gmail.com>", | ||
"graphql-tag": "^2.0.0", | ||
"require-resolve": "0.0.2" | ||
"resolve": "^1.3.3" | ||
}, | ||
@@ -33,0 +33,0 @@ "devDependencies": { |
@@ -6,7 +6,7 @@ # Babel Inline Import GraphQL AST | ||
While this package is intended as an alternative to [graphql-tag/loader](http://dev.apollodata.com/react/webpack.html), it does NOT currently support importing [fragments](http://dev.apollodata.com/react/webpack.html#Fragments) with `#import`. Work on this is underway. | ||
Prior to v1.2.0, [fragments](http://dev.apollodata.com/react/webpack.html#Fragments) (#import statements) are not supported | ||
## Known use cases | ||
Somewhat replaces `graphql-tag/loader` in projects where Webpack is unavailable(i.e. [NextJS](https://github.com/zeit/next.js/)) | ||
Replaces `graphql-tag/loader` in projects where Webpack is unavailable(i.e. [NextJS](https://github.com/zeit/next.js/)) | ||
@@ -42,10 +42,15 @@ ## Examples | ||
```javascript | ||
// productFragment.graphql | ||
fragment productFragment on Product { | ||
name | ||
description | ||
weight | ||
} | ||
// productsQuery.graphql | ||
#import "./productFragment.graphql" | ||
query products { | ||
products { | ||
productId | ||
name | ||
description | ||
weight | ||
...productFragment | ||
} | ||
@@ -89,26 +94,7 @@ } | ||
By default, Babel-Inline-Import is compatible with the following file extensions: | ||
Babel-Inline-Import is compatible with the following file extensions: | ||
* .raw | ||
* .text | ||
* .graphql | ||
* .gql | ||
## Customize | ||
If you want to enable different file extensions, you can define them in your `.babelrc` file | ||
```javascript | ||
{ | ||
"plugins": [ | ||
["babel-plugin-inline-import", { | ||
"extensions": [ | ||
".json", | ||
".sql" | ||
] | ||
}] | ||
] | ||
} | ||
``` | ||
## How it works | ||
@@ -126,3 +112,5 @@ | ||
**Note for NextJS users** - instead of BABEL_DISABLE_CACHE, you'll need to manually clear the node_modules/.cache folder to re-transpile your .gql/.graphql files. The easiest way to do this is using node scripts. You can either prepend your normal `start` script command with `rimraf ./node_modules/.cache` or create a separate script i.e. `clean`. Note you'd need the rimraf dependency installed in this example | ||
## Credits | ||
This package is a modified version of [babel-plugin-inline-import](https://www.npmjs.com/package/babel-plugin-inline-import) |
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
13770
157
113
+ Addedresolve@^1.3.3
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedrequire-resolve@0.0.2
- Removedpath-extra@1.0.3(transitive)
- Removedrequire-resolve@0.0.2(transitive)
- Removedx-path@0.0.2(transitive)