babel-plugin-inline-import-graphql-ast
Advanced tools
Comparing version 1.3.3 to 2.0.0
@@ -65,6 +65,2 @@ Object.defineProperty(exports, "__esModule", { | ||
value: function getContents(givenPath, reference) { | ||
if (!reference) { | ||
throw new Error('"reference" argument must be specified'); | ||
} | ||
var filepath = void 0; | ||
@@ -71,0 +67,0 @@ try { |
@@ -5,4 +5,2 @@ Object.defineProperty(exports, "__esModule", { | ||
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 _templateObject = _taggedTemplateLiteral(['', ''], ['', '']); | ||
@@ -18,2 +16,6 @@ | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _graphqlTag = require('graphql-tag'); | ||
@@ -25,36 +27,8 @@ | ||
var _helper = require('./helper'); | ||
var _helper2 = _interopRequireDefault(_helper); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
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); } } | ||
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } | ||
var filterFn = function filterFn(line) { | ||
return !(line[0] === '#' && line.slice(1).split(' ')[0] === 'import') && !(line === ''); | ||
}; | ||
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 stripImports = function stripImports(source) { | ||
return source.split(_os2.default.EOL).filter(filterFn).join(_os2.default.EOL); | ||
}; | ||
var expandImports = function expandImports(source, resolvePath, reference) { | ||
reference = _path2.default.dirname(reference); | ||
var queryAST = (0, _graphqlTag2.default)(_templateObject, stripImports(source)); | ||
var lines = source.split(_os2.default.EOL); | ||
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 }); | ||
}; | ||
exports.default = function (_ref) { | ||
@@ -65,30 +39,16 @@ var t = _ref.types; | ||
ImportDeclaration: { | ||
exit: function exit(path, state) { | ||
var shouldBeInlined = _helper2.default.shouldBeInlined, | ||
hasRoot = _helper2.default.hasRoot, | ||
transformRelativeToRootPath = _helper2.default.transformRelativeToRootPath, | ||
getContents = _helper2.default.getContents; | ||
exit: function exit(curPath, state) { | ||
var importPath = curPath.node.source.value; | ||
if (importPath.endsWith('.graphql') || importPath.endsWith('.gql')) { | ||
var buildInlineVariable = function buildInlineVariable(graphqlAST) { | ||
var babelAST = (0, _babylon.parse)('const obj = ' + JSON.stringify(graphqlAST)); | ||
var objExp = babelAST.program.body[0].declarations[0].init; | ||
return t.variableDeclarator(t.identifier(curPath.node.specifiers[0].local.name), t.objectExpression(objExp.properties)); | ||
}; | ||
var givenPath = path.node.source.value; | ||
var reference = state && state.file && state.file.opts.filename; | ||
var extensions = state && state.opts && state.opts.extensions; | ||
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])); | ||
var query = createQuery(importPath, state.file.opts.filename); | ||
query.processFragments(); | ||
query.parse(); | ||
query.makeSourceEnumerable(); | ||
curPath.replaceWith(t.variableDeclaration('const', [buildInlineVariable(query.ast)])); | ||
} | ||
@@ -99,2 +59,37 @@ } | ||
}; | ||
}; | ||
}; | ||
function createQuery(queryPath, babelPath) { | ||
var absPath = _path2.default.resolve(_path2.default.dirname(babelPath), queryPath); | ||
var source = _fs2.default.readFileSync(absPath).toString(); | ||
var ast = null; | ||
var fragmentDefs = []; | ||
return { | ||
processFragments: function processFragments() { | ||
var imports = source.split(_os2.default.EOL).filter(function (line) { | ||
return line.startsWith('#import'); | ||
}); | ||
imports.forEach(function (statement) { | ||
var fragmentPath = statement.split(' ')[1].slice(1, -1); | ||
var absFragmentPath = _path2.default.resolve(_path2.default.dirname(absPath), fragmentPath); | ||
var fragmentSource = _fs2.default.readFileSync(absFragmentPath).toString(); | ||
fragmentDefs = [].concat(_toConsumableArray(fragmentDefs), _toConsumableArray((0, _graphqlTag2.default)(_templateObject, fragmentSource).definitions)); | ||
}); | ||
}, | ||
parse: function parse() { | ||
var parsedAST = (0, _graphqlTag2.default)(_templateObject, source); | ||
parsedAST.definitions = [].concat(_toConsumableArray(parsedAST.definitions), _toConsumableArray(fragmentDefs)); | ||
ast = parsedAST; | ||
}, | ||
makeSourceEnumerable: function makeSourceEnumerable() { | ||
var newAST = JSON.parse(JSON.stringify(ast)); | ||
newAST.loc.source = ast.loc.source; | ||
ast = newAST; | ||
}, | ||
get ast() { | ||
return ast; | ||
} | ||
}; | ||
} |
# ChangeLog | ||
### v1.3.3 | ||
## v2.0.0 (July 22, 2017) | ||
### Breaking | ||
* Potentially removed Meteor support. Not sure it worked anyway and there's | ||
an alternative solution for .graphql files in Meteor. | ||
* Removed support for customizable extensions. This was undocumented and left | ||
over from the project I forked. | ||
### Features | ||
* `<query>.loc.source` is now available on the inlined query | ||
### Maintenance | ||
* Major rewrite. Code is now much smaller and more readable. (1 file, ~60 LOC) | ||
* Removed dependence on `resolve` package. | ||
## v1.3.3 | ||
First official release with Windows support | ||
Updated README and CHANGELOG | ||
### v1.3.2 | ||
## v1.3.2 | ||
Test version(debugging Windows problems), published as @winfix tag | ||
First version that works on Windows/Mac/Linux | ||
### v1.3.1 | ||
## v1.3.1 | ||
Test version(debugging Windows problems), published as @winfix tag | ||
First working version on Windows, non-functional on Mac/Linux | ||
### v1.3.0 | ||
## v1.3.0 | ||
Test version(debugging Windows problems), published as @winfix tag | ||
Initial attempt, non-functional | ||
### v1.2.3 | ||
## v1.2.3 | ||
Update name of Github repo | ||
### v1.2.2 | ||
## v1.2.2 | ||
Add required graphql dependency | ||
### v1.2.1 | ||
## v1.2.1 | ||
Update npm keywords | ||
### v1.2.0 | ||
## v1.2.0 | ||
Add support for GraphQL Fragments | ||
Remove support for .raw and .text extensions(left over from forked project) | ||
### v1.1.1 | ||
## v1.1.1 | ||
Update forked README to reflect the new package | ||
### v1.1.0 | ||
## v1.1.0 | ||
Add support for .gql file extension | ||
### v1.0.1 | ||
## v1.0.1 | ||
First functional version | ||
Add ability to import .graphql files as AST(only on Mac/Linux) | ||
Fix package.json info | ||
### v1.0.0 | ||
## v1.0.0 | ||
Initial version. Bad publish, non-functional |
{ | ||
"name": "babel-plugin-inline-import-graphql-ast", | ||
"version": "1.3.3", | ||
"version": "2.0.0", | ||
"description": "Babel plugin to make raw files importable", | ||
@@ -22,4 +22,2 @@ "author": "Alexander Roberts <detrohutt@gmail.com>", | ||
"scripts": { | ||
"test": "mocha test/*.spec.js --require config/mocha.js --compilers js:babel-core/register", | ||
"test-watch": "mocha test/*.spec.js --require config/mocha.js --compilers js:babel-core/register --watch", | ||
"lint-js": "eslint plugin", | ||
@@ -31,4 +29,3 @@ "prepublish": "babel -d build/ plugin/" | ||
"graphql": "^0.9.6", | ||
"graphql-tag": "^2.0.0", | ||
"resolve": "^1.3.3" | ||
"graphql-tag": "^2.0.0" | ||
}, | ||
@@ -35,0 +32,0 @@ "devDependencies": { |
@@ -0,0 +0,0 @@ # Babel Inline Import GraphQL AST |
Sorry, the diff of this file is not supported yet
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
3
13664
151
3
- Removedresolve@^1.3.3
- Removedfunction-bind@1.1.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedis-core-module@2.15.1(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)