Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,36 @@ This repository houses plugins that Rollup considers critical to every day use o

## Plugins Found Here

| | |
| ------------------------------------- | ----------------------------------------------------------------------------------------- |
| [alias](packages/alias) | Define and resolve aliases for bundle dependencies |
| [auto-install](packages/auto-install) | Automatically install dependencies that are imported by a bundle |
| [babel](packages/babel) | Compile your files with Babel |
| [beep](packages/beep) | System beeps on errors and warnings |
| [buble](packages/buble) | Compile ES2015 with buble |
| [commonjs](packages/commonjs) | Convert CommonJS modules to ES6 |
| [data-uri](packages/data-uri) | Import modules from Data URIs |
| [dsv](packages/dsv) | Convert .csv and .tsv files into JavaScript modules with d3-dsv |
| [eslint](packages/eslint) | Verify entry point and all imported files with ESLint |
| [html](packages/html) | Create HTML files to serve Rollup bundles |
| [image](packages/image) | Import JPG, PNG, GIF, SVG, and WebP files |
| [inject](packages/inject) | Scan modules for global variables and injects `import` statements where necessary |
| [json](packages/json) | Convert .json files to ES6 modules |
| [legacy](packages/legacy) | Add `export` declarations to legacy non-module scripts |
| [multi-entry](packages/multi-entry) | Use multiple entry points for a bundle |
| [node-resolve](packages/node-resolve) | Locate and bundle third-party dependencies in node_modules |
| [replace](packages/replace) | Replace strings in files while bundling |
| [run](packages/run) | Run your bundles in Node once they're built |
| [strip](packages/strip) | Remove debugger statements and functions like assert.equal and console.log from your code |
| [sucrase](packages/sucrase) | Compile TypeScript, Flow, JSX, etc with Sucrase |
| [typescript](packages/typescript) | Integration between Rollup and Typescript |
| [url](packages/url) | Import files as data-URIs or ES Modules |
| [virtual](packages/virtual) | Load virtual modules from memory |
| [wasm](packages/wasm) | Import WebAssembly code with Rollup |
| [yaml](packages/yaml) | Convert YAML files to ES6 modules |
| | |
| | |
| --------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| [alias](packages/alias) | Define and resolve aliases for bundle dependencies |
| [auto-install](packages/auto-install) | Automatically install dependencies that are imported by a bundle |
| [babel](packages/babel) | Compile your files with Babel |
| [beep](packages/beep) | System beeps on errors and warnings |
| [buble](packages/buble) | Compile ES2015 with buble |
| [commonjs](packages/commonjs) | Convert CommonJS modules to ES6 |
| [data-uri](packages/data-uri) | Import modules from Data URIs |
| [dsv](packages/dsv) | Convert .csv and .tsv files into JavaScript modules with d3-dsv |
| [dynamic-import-vars](packages/dynamic-import-vars) | Resolving dynamic imports that contain variables. |
| [eslint](packages/eslint) | Verify entry point and all imported files with ESLint |
| [graphql](packages/graphql) | Convert .gql/.graphql files to ES6 modules |
| [html](packages/html) | Create HTML files to serve Rollup bundles |
| [image](packages/image) | Import JPG, PNG, GIF, SVG, and WebP files |
| [inject](packages/inject) | Scan modules for global variables and injects `import` statements where necessary |
| [json](packages/json) | Convert .json files to ES6 modules |
| [legacy](packages/legacy) | Add `export` declarations to legacy non-module scripts |
| [multi-entry](packages/multi-entry) | Use multiple entry points for a bundle |
| [node-resolve](packages/node-resolve) | Locate and bundle third-party dependencies in node_modules |
| [replace](packages/replace) | Replace strings in files while bundling |
| [run](packages/run) | Run your bundles in Node once they're built |
| [strip](packages/strip) | Remove debugger statements and functions like assert.equal and console.log from your code |
| [sucrase](packages/sucrase) | Compile TypeScript, Flow, JSX, etc with Sucrase |
| [typescript](packages/typescript) | Integration between Rollup and Typescript |
| [url](packages/url) | Import files as data-URIs or ES Modules |
| [virtual](packages/virtual) | Load virtual modules from memory |
| [wasm](packages/wasm) | Import WebAssembly code with Rollup |
| [yaml](packages/yaml) | Convert YAML files to ES6 modules |
| | |

## Other Packages Found Here

Expand Down
21 changes: 0 additions & 21 deletions packages/eslint/LICENSE

This file was deleted.

23 changes: 23 additions & 0 deletions packages/graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @rollup/plugin-graphql ChangeLog

## v1.0.0

_2019-02-18_

### Features

- Add support for multiple exports

## v0.1.0

_2017-01-16_

### Features

- Use `graphql-tag/loader` directly [PR #3](https://github.com/kamilkisiela/rollup-plugin-graphql/pull/3)

## v0.0.1

_2017-01-07_

- First release
102 changes: 102 additions & 0 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-graphql
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-graphql
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-graphql
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-graphql

[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

# @rollup/plugin-graphql

🍣 A Rollup plugin which Converts .gql/.graphql files to ES6 modules.

## Requirements

This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v10.0.0+) and Rollup v1.20.0+.

## Install

Using npm:

```console
npm install @rollup/plugin-graphql --save-dev
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```js
import graphql from '@rollup/plugin-graphql';

export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs',
},
plugins: [graphql()],
};
```

Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).

With an accompanying file `src/index.js`, you can import GraphQL files or named queries/mutations:

```js
// src/index.js

// import a GraphQL Document from a GraphQL file,
import schema from './schema.graphql';

// or import named Query/Mutation
import { BatmanQuery, JokerMutation } from './schema.graphql';
```

#### Fragments

Thanks to [graphql-tag](https://github.com/apollographql/graphql-tag), fragments import is supported by using `#import "..."`.

Given the following file `heroFragment.graphql`:

```graphql
fragment HeroFragment on Hero {
id
name
}
```

You can import it like this:

```graphql
#import "./heroFragment.graphql"

query AllHeroes {
heros {
...HeroFragment
}
}
```

## Options

### `exclude`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)

[LICENSE (MIT)](/LICENSE)
72 changes: 72 additions & 0 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "@rollup/plugin-graphql",
"version": "1.0.0",
"publishConfig": {
"access": "public"
},
"description": "Convert .gql/.graphql files to ES6 modules",
"license": "MIT",
"repository": "rollup/plugins",
"author": "rollup",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/graphql#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"module": "dist/index.es.js",
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm run build && pnpm run lint",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm run test -- --verbose && pnpm run test:ts",
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:graphql && pnpm run lint:package",
"lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md",
"lint:graphql": "prettier --write \"test/fixtures/**/*.graphql\"",
"lint:js": "eslint --fix --cache src test types --ext .js,.ts",
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
"prebuild": "del-cli dist",
"prepare": "pnpm run build",
"prepublishOnly": "pnpm run lint && pnpm run test",
"pretest": "pnpm run build",
"test": "ava",
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
},
"files": [
"dist",
"types",
"README.md",
"LICENSE"
],
"keywords": [
"rollup",
"plugin",
"graphql",
"apollo",
"import",
"modules"
],
"peerDependencies": {
"graphql": ">=0.9.0",
"rollup": "^1.20.0 || ^2.0.0"
},
"dependencies": {
"@rollup/pluginutils": "^4.0.0",
"graphql-tag": "^2.2.2"
},
"devDependencies": {
"@rollup/plugin-buble": "^0.21.3",
"graphql": "^14.1.1",
"rollup": "^2.23.0"
},
"types": "types/index.d.ts",
"ava": {
"babel": {
"compileEnhancements": false
},
"files": [
"!**/fixtures/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
}
}
13 changes: 13 additions & 0 deletions packages/graphql/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import buble from '@rollup/plugin-buble';

import pkg from './package.json';

export default {
input: 'src/index.js',
external: [...Object.keys(pkg.dependencies), 'os'],
output: [
{ file: pkg.main, format: 'cjs', sourcemap: true, exports: 'auto' },
{ file: pkg.module, format: 'es', sourcemap: true }
],
plugins: [buble()]
};
36 changes: 36 additions & 0 deletions packages/graphql/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createFilter } from '@rollup/pluginutils';
import loader from 'graphql-tag/loader';

import { toESModules } from './toESModules';

export default function graphql({ include, exclude } = {}) {
// path filter
const filter = createFilter(include, exclude);
// only .graphql and .gql files
const filterExt = /\.(graphql|gql)$/i;

return {
name: 'graphql',
transform(source, id) {
if (!filter(id)) return null;
if (!filterExt.test(id)) return null;

// XXX: this.cachable() in graphql-tag/loader
const code = toESModules(
loader.call(
{
cacheable() {}
},
source
)
);

const map = { mappings: '' };

return {
code,
map
};
}
};
}
49 changes: 49 additions & 0 deletions packages/graphql/src/toESModules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable import/prefer-default-export */

import { EOL } from 'os';

/**
* Convert CommonJS exports to ES modules exports
* @param source Source code to convert
* @returns The converted source code
*/
export function toESModules(source) {
if (typeof source !== 'string') {
return source;
}

return replaceRequires(replaceModuleExports(source));
}

function replaceModuleExports(source) {
return source
.replace('module.exports = doc', 'export default doc')
.replace(
/module\.exports\["(.*)"] = oneQuery\(doc, "(.*)"\)/g,
(match, g1, g2) => `export const ${g1} = oneQuery(doc, "${g2}")`
);
}

function replaceRequires(source) {
const imports = {};
let index = 0;

// replace a require statement with a variable
source = source.replace(/require\(([^)]+)\)/gi, (match, path) => {
path = path.replace(/["']+/g, '');

if (!imports[path]) {
index += 1;
imports[path] = `frgmt${index}`;
}

return imports[path];
});

// prepare import statements
const importsOutput = Object.keys(imports)
.map((path) => `import ${imports[path]} from "${path}";`)
.join(EOL);

return importsOutput + EOL + source;
}
6 changes: 6 additions & 0 deletions packages/graphql/test/fixtures/basic/basic.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query GetHero {
hero {
id
name
}
}
2 changes: 2 additions & 0 deletions packages/graphql/test/fixtures/basic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export { default as doc } from './basic.graphql';
Loading