-
-
Notifications
You must be signed in to change notification settings - Fork 615
Add GraphQL plugin #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add GraphQL plugin #585
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
77e8c4f
chore(graphql): literally import @kocal/rollup-plugin-graphql
Kocal 7922b9a
chore(graphql): migrate plugin
Kocal f225154
doc: list graphql and dynamic-import-vars plugins
Kocal 5056d6d
chore(graphql): use named export
Kocal 32e004e
Merge remote-tracking branch 'upstream/master' into graphql-plugin
shellscape 6f049c5
chore: update readme, make tests use src
shellscape 32ad749
chore(eslint): remove static license file, this is added dynamically …
shellscape 1d3899f
chore: few things to clean up, tests use compiled again
shellscape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
| [](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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()] | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }; | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| query GetHero { | ||
| hero { | ||
| id | ||
| name | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.