-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fdd339
commit 1ad92d4
Showing
5 changed files
with
169 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 contains 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 contains 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 contains 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,144 @@ | ||
--- | ||
summary: Setup a Monorepo with AdonisJS, Next.js and Tuyau | ||
--- | ||
|
||
In this guide, we'll be setting up a Monorepo with AdonisJS, Next.js and Tuyau. | ||
|
||
You can find an example of it in this [repository](https://github.com/mohitxskull/tuyau-nextjs-adonisjs-stackblitz-demo). | ||
|
||
# Monorepo | ||
|
||
We'll be using `pnpm` to manage the dependencies in our Monorepo. | ||
|
||
Let's create an empty folder called `acme` and initialize `package.json` with `pnpm` : | ||
|
||
```sh | ||
pnpm init | ||
``` | ||
|
||
Now we need to tell `pnpm` where our apps are located in the folder and for that we need to add a `apps` folder and [`pnpm-workspace.yaml`](https://pnpm.io/workspaces) file as `pnpm` do not support the `workspaces` field in `package.json`: | ||
|
||
```yaml | ||
# pnpm-workspace.yaml | ||
packages: | ||
- apps/* | ||
``` | ||
As a result, we'll have a `acme` folder with the following structure: | ||
|
||
``` | ||
acme | ||
├── package.json | ||
├── apps | ||
└── pnpm-workspace.yaml | ||
``` | ||
## AdonisJS | ||
Now let's do a `cd` to our `apps` folder and create a adonisjs project: | ||
```sh | ||
pnpm create adonisjs@latest backend | ||
``` | ||
|
||
For more information on creating an adonisjs project, check out [adonisjs](https://docs.adonisjs.com/guides/getting-started/installation). | ||
|
||
``` | ||
acme | ||
├── package.json | ||
├── apps | ||
├── apps/backend | ||
└── pnpm-workspace.yaml | ||
``` | ||
|
||
## Tuyau | ||
|
||
For installation of the Tuyau framework, check out the [installation](/docs/installation) guide. | ||
|
||
## Next.js | ||
|
||
To create a Next.js project, run the following command in `./acme/apps` folder: | ||
|
||
```sh | ||
pnpx create-next-app@latest frontend --typescript | ||
``` | ||
|
||
For more information on creating a Next.js project, check out [create-next-app](https://nextjs.org/docs/getting-started/installation). | ||
|
||
``` | ||
acme | ||
├── package.json | ||
├── apps | ||
├── apps/backend | ||
├── apps/frontend | ||
└── pnpm-workspace.yaml | ||
``` | ||
|
||
### Importing the API Definition | ||
|
||
To import the API definition from your AdonisJS project generated by the `tuyau:generate` command, you must import the `.adonisjs/index.ts` file in your Next.js project. | ||
|
||
For that please follow the [installation](/docs/installation) guide from [here](/docs/installation#sharing-the-api-definition). | ||
|
||
### Configuration | ||
|
||
As we are directly importing `.ts` files into our Next.js project, we must configure the `tsconfig.json` and `next.config.ts` files. | ||
|
||
```ts | ||
// next.config.ts | ||
import type { NextConfig } from 'next'; | ||
|
||
const nextConfig: NextConfig = { | ||
reactStrictMode: true, | ||
transpilePackages: ['@acme/backend'], | ||
webpack: (config) => { | ||
config.resolve.extensionAlias = { | ||
'.js': ['.ts', '.tsx', '.js', '.jsx'], | ||
'.mjs': ['.mts', '.mjs'], | ||
'.cjs': ['.cts', '.cjs'], | ||
}; | ||
return config; | ||
}, | ||
}; | ||
|
||
export default nextConfig; | ||
``` | ||
|
||
Decorators are not enabled by default in TypeScript, So we need to enable them in `tsconfig.json` else we'll see errors during `tsc --noEmit` (typecheck). | ||
|
||
```json | ||
// tsconfig.json | ||
{ | ||
"compilerOptions": { | ||
"experimentalDecorators": true | ||
} | ||
} | ||
``` | ||
|
||
### Testing | ||
|
||
We have successfully created a Monorepo with AdonisJS, Next.js and Tuyau, but let's test if everything is working as expected. | ||
|
||
For that add the `typecheck` script to the `package.json` in `./acme/apps/frontend`: | ||
|
||
```json | ||
// package.json | ||
{ | ||
"scripts": { | ||
"typecheck": "tsc --noEmit" | ||
} | ||
} | ||
``` | ||
|
||
Run the `typecheck` script and see if everything is working as expected. | ||
|
||
If you see any errors coming from `../backend` folder, check them and if they are related to missing types, add required reference files in the `./.adonisjs/index.ts` file like this: | ||
|
||
```ts | ||
/// <reference path="../adonisrc.ts" /> | ||
/// <reference path="../config/auth.ts" /> | ||
|
||
export * from './api.js' | ||
``` | ||
|
||
As your project grows you will need to add more reference files here. |
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
packages: | ||
- packages/* | ||
- playgrounds/* | ||
- docs | ||
- docs |