9.2.0 • Published 3 years ago

@yolkai/nx-next v9.2.0

Weekly downloads
17
License
ISC
Repository
github
Last release
3 years ago

nx-next

An Nx plugin for Next.js applications.

Nrwl publishes its own Next.js plugin, however it does not support launching a TypeScript custom server. With our own plugin, we have a simpler implementation and support for launching a TypeScript custom server.

Installation

npm install --save @yolkai/nx-next
# or
yarn add @yolkai/nx-next
# or
pnpm add @yolkai/nx-next

Usage

workspace.json:

{
  "version": 1,
  "projects": {
    "my-project": {
      "architect": {
        "build": {
          "builder": "@yolkai/nx-next:build",
          "options": {
            "root": "apps/my-project",
            "outputPath": "dist/apps/my-project"
          }
        },
        "customServer": {
          "builder": "@nrwl/node:build",
          "options": {
            "outputPath": "dist/apps/my-project/.server",
            "main": "apps/my-project/server/index.ts",
            "tsConfig": "apps/my-project/server/tsconfig.json",
            "assets": []
          }
        },
        "serve": {
          "builder": "@yolkai/nx-next:serve",
          "options": {
            "buildTarget": "my-project:build",
            "customServerTarget": "my-project:customServer",
            "port": 3007
          },
          "configurations": {
            "dev": {
              "dev": true,
              "skipBuild": false
            },
            "production": {
              "dev": false,
              "skipBuild": true
            },
            "coverage": {
              "dev": false
            }
          }
        },
        "dev": {
          "builder": "@nrwl/workspace:run-commands",
          "options": {
            "commands": [
              {
                "command": "node_modules/.bin/env-cmd --file apps/my-project/my-project.dev.env nx run my-project:serve:dev"
              }
            ]
          }
        }
      }
    }
  }
}

Builders

Build the Next.js app

Run this command prior to running the app in production mode.

nx run my-project:build

Build the custom server

If your app uses a custom server, run this command prior to running the app in production mode.

nx run my-project:customServer

Serve the app for development

The custom server will be built automatically before serving.

nx run my-project:serve:dev

Serve the app for production

nx run my-project:serve:production

Next.js config

// apps/my-project/next.config.js

const { withNx } = require('@yolkai/nx-next');
module.exports = withNx('my-project')({});

Custom server (optional)

// apps/my-project/server/index.ts

import { StartServerFn } from '@yolkai/nx-next';
import http from 'http';

const startServer: StartServerFn = async (nextApp, options) => {
  const handle = nextApp.getRequestHandler();
  await nextApp.prepare();
  const httpServer = http.createServer((req, res) => {
    handle(req, res);
  });

  return new Promise((resolve, reject) => {
    httpServer.on('error', (err: Error) => {
      if (err) {
        reject(err);
      }
    });
    httpServer.listen(options.port, () => {
      console.log(`listening on http://${options.host}:${options.port}`);
      resolve();
    });
  });
};

export { startServer };
9.2.0

3 years ago

9.1.0

4 years ago

9.0.0

4 years ago

0.0.1-alpha.0

4 years ago

9.0.0-alpha.0

4 years ago

8.12.0-alpha.5

4 years ago

8.12.0-alpha.4

4 years ago

8.12.0-alpha.3

4 years ago

8.12.0-alpha.2

4 years ago

8.12.0-alpha.1

4 years ago

8.12.0-alpha.0

4 years ago

8.11.0-alpha.3

4 years ago

8.11.0-alpha.4

4 years ago

8.11.0-alpha.5

4 years ago

8.11.0-alpha.2

4 years ago

8.11.0-alpha.1

4 years ago

8.11.0-alpha.0

4 years ago

8.10.0-alpha.11

4 years ago

8.10.0-alpha.10

4 years ago

8.10.0-alpha.9

4 years ago

8.10.0-alpha.7

4 years ago

8.10.0-alpha.6

4 years ago

8.10.0-alpha.5

4 years ago

8.10.0-alpha.4

4 years ago

8.10.0-alpha.3

4 years ago

8.10.0-alpha.2

4 years ago

8.10.0-alpha.1

4 years ago

8.10.0-alpha.0

4 years ago

0.0.5-alpha.3

4 years ago

0.0.5-alpha.1

4 years ago

0.0.5-alpha.0

4 years ago

0.0.4-alpha.8

4 years ago

0.0.4-alpha.7

4 years ago

0.0.4-alpha.6

4 years ago

0.0.4-alpha.5

4 years ago

0.0.4-alpha.3

4 years ago

0.0.4-alpha.2

4 years ago

0.0.4-alpha.1

4 years ago

0.0.4-alpha.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago