Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/relay-tools/relay-compiler-webpack-plugin
Automatically run the Relay Compiler from Webpack
https://github.com/relay-tools/relay-compiler-webpack-plugin
graphql javascript react reactjs relay relay-compiler relayjs webpack webpack3 webpack4
Last synced: about 1 month ago
JSON representation
Automatically run the Relay Compiler from Webpack
- Host: GitHub
- URL: https://github.com/relay-tools/relay-compiler-webpack-plugin
- Owner: relay-tools
- License: bsd-3-clause
- Created: 2017-06-26T01:46:04.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:43:01.000Z (almost 2 years ago)
- Last Synced: 2024-04-15T08:11:15.418Z (7 months ago)
- Topics: graphql, javascript, react, reactjs, relay, relay-compiler, relayjs, webpack, webpack3, webpack4
- Language: JavaScript
- Homepage:
- Size: 2.16 MB
- Stars: 66
- Watchers: 4
- Forks: 35
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - relay-compiler-webpack-plugin - tools | 64 | (JavaScript)
README
# Relay Compiler Webpack Plugin
[![npm version](https://badge.fury.io/js/relay-compiler-webpack-plugin.svg)](https://badge.fury.io/js/relay-compiler-webpack-plugin)
[![Travis Build Status](https://travis-ci.org/relay-tools/relay-compiler-webpack-plugin.svg?branch=master)](https://travis-ci.org/relay-tools/relay-compiler-webpack-plugin)
![Github Actions Build Status](https://github.com/relay-tools/relay-compiler-webpack-plugin/workflows/Build/badge.svg)Are you running Relay Modern? Are you annoyed with constantly running the `relay-compiler` to generate code, especially
if you're already running Webpack?Well be annoyed no more! Simply install this plugin to automatically hook into Webpack's build process to generate these
files for you.## Installation
1. Add this to your project:
```sh
yarn add --dev relay-compiler-webpack-plugin graphql
# Or if you're using npm
npm install --save-dev relay-compiler-webpack-plugin graphql
```2. Add the plugin to your Webpack configuration:
```javascript
// const RelayCompilerLanguageTypescript = require('relay-compiler-language-typescript').default
const RelayCompilerWebpackPlugin = require('relay-compiler-webpack-plugin')
const path = require('path')module.exports = {
// ... Your existing Webpack configuration
plugins: [
// ...
new RelayCompilerWebpackPlugin({
// languagePlugin: RelayCompilerLanguageTypescript,
schema: path.resolve(__dirname, './relative/path/to/schema.graphql'), // or schema.json
src: path.resolve(__dirname, './relative/path/to/source/files'),
})
]
// ...
}
```
Note: TypeScript projects also needs to add the TypeScript language plugin as shown in the commented out lines in the above snippet.3. :tada:
### Gotchas
If there are multiple versions of GraphQL in your dependency tree it will cause schema validation errors. To get around
this, ensure you have the same graphql version as your relay-compiler version depends on. To assist this you can
[install dependencies as flat](https://yarnpkg.com/lang/en/docs/cli/install/#toc-yarn-install-flat) which ensures only
one version of each dependency.### Plugin hooks
`relay-compiler-webpack-plugin` exposes a few [tapable](https://github.com/webpack/tapable/tree/master) hooks, for plugins or tooling to use.
- `beforeWrite` called before the plugin starts to compile queries
- `afterWrite(compileResult)`: called after writing is complete```js
class MyPlugin {
apply (compiler) {
compiler.hooks.compilation.tap('MyPlugin', async (compilation) => {
RelayCompilerWebpackPlugin.getHooks(compilation).afterWrite.tapPromise(
'MyPlugin', // <-- Set a meaningful name for stacktraces
async (compileResult) => {
if (compileResult === 'HAS_CHANGES') {
await doSomething()
}
}
)
})
}
}
```## Example Project
To see an example of its usage within a project, see
[relay-compiler-webpack-plugin-example](https://github.com/danielholmes/relay-compiler-webpack-plugin-example).## Development
Running tests:
```bash
yarn test
```Running tests with coverage:
```bash
yarn test:coverage
```## License
Relay Compiler Webpack Plugin may be redistributed according to the [BSD 3-Clause License](LICENSE).