Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

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).