rollup-plugin-graphql
Version:
Convert graphql files to ES6 modules
64 lines (48 loc) • 1.61 kB
JavaScript
import { createFilter } from 'rollup-pluginutils';
import loader from 'graphql-tag/loader';
import { EOL } from 'os';
var toESModules = function (source) {
if (typeof source !== "string") {
return source;
}
return replaceRequires(replaceModuleExports(source));
};
function replaceModuleExports(source) {
return source.replace('module.exports = doc', 'export default doc');
}
function replaceRequires(source) {
var imports = {};
var index = 0;
// replace a require statement with a variable
source = source.replace(/require\(([^)]+)\)/ig, function (match, path) {
path = path.replace(/[\"\']+/g, '');
if (!imports[path]) {
imports[path] = "frgmt" + (++index);
}
return imports[path];
});
// prepare import statements
var importsOutput = Object.keys(imports)
.map(function (path) { return ("import " + (imports[path]) + " from \"" + path + "\";"); })
.join(EOL);
return importsOutput + EOL + source;
}
function graphql (options) {
if ( options === void 0 ) options = {};
// path filter
var filter = createFilter(options.include, options.exclude);
// only .graphql and .gql files
var filterExt = /\.(graphql|gql)$/i;
return {
name: 'graphql',
transform: function transform (source, id) {
if (!filter(id)) { return null; }
if (!filterExt.test(id)) { return null; }
// XXX: this.cachable() in graphql-tag/loader
var code = toESModules(loader.call({cacheable: function cacheable () {}}, source));
return { code: code };
}
};
}
export default graphql;
//# sourceMappingURL=rollup-plugin-graphql.es.js.map