Skip to content

Commit

Permalink
feat(vue): nuxt executors
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Oct 30, 2023
1 parent 349e8af commit ac50f3c
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 34 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
"ng-packagr": "~16.2.0",
"node-fetch": "^2.6.7",
"npm-package-arg": "11.0.1",
"nuxi": "^3.9.1",
"nx": "17.0.0-rc.2",
"octokit": "^2.0.14",
"open": "^8.4.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"nx",
"typescript",
"@nx/cypress",
"@nx/playwright"
"@nx/playwright",
"@nx/jest"
]
}
]
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt/docs/build-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Nuxt build executor examples
description: This page contains examples for the @nx/nuxt:build executor.
---
4 changes: 4 additions & 0 deletions packages/nuxt/docs/serve-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Nuxt serve executor examples
description: This page contains examples for the @nx/nuxt:serve executor.
---
13 changes: 12 additions & 1 deletion packages/nuxt/executors.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
{
"executors": {}
"executors": {
"build": {
"implementation": "./src/executors/build/build.impl",
"schema": "./src/executors/build/schema.json",
"description": "Build with Nuxt."
},
"serve": {
"implementation": "./src/executors/serve/serve.impl",
"schema": "./src/executors/serve/schema.json",
"description": "Serve with Nuxt."
}
}
}
1 change: 1 addition & 0 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"migrations": "./migrations.json"
},
"dependencies": {
"nuxi": "^3.9.1",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
Expand Down
Empty file.
33 changes: 33 additions & 0 deletions packages/nuxt/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ExecutorContext } from '@nx/devkit';
import { NuxtBuildExecutorOptions } from './schema';

// Required because dep is ESM package. Changing moduleResolution to NodeNext causes other issues unfortunately.
export function loadNuxiDynamicImport() {
return Function('return import("nuxi")')() as Promise<typeof import('nuxi')>;
}

export async function* nuxtBuildExecutor(
options: NuxtBuildExecutorOptions,
context: ExecutorContext
) {
const projectRoot =
context.projectsConfigurations.projects[context.projectName].root;
const { runCommand } = await loadNuxiDynamicImport();
try {
await runCommand('build', [projectRoot], {
overrides: {
typescript: {
builder: 'shared',
},
imports: {
autoImport: false,
},
},
});
return { success: true };
} catch (e) {
return { success: false, error: e };
}
}

export default nuxtBuildExecutor;
4 changes: 4 additions & 0 deletions packages/nuxt/src/executors/build/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { convertNxExecutor } from '@nx/devkit';
import nuxtBuildExecutor from './build.impl';

export default convertNxExecutor(nuxtBuildExecutor);
5 changes: 5 additions & 0 deletions packages/nuxt/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface NuxtBuildExecutorOptions {
rootDir: string;
prerender?: boolean;
dotenv?: string;
}
36 changes: 36 additions & 0 deletions packages/nuxt/src/executors/build/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": 2,
"outputCapture": "direct-nodejs",
"title": "Nuxt Prod Builder",
"cli": "nx",
"description": "Builds a Nuxt application for production.",
"type": "object",
"presets": [
{
"name": "Default minimum setup",
"keys": []
}
],
"properties": {
"rootDir": {
"type": "string",
"description": "The root directory of the application to bundle.",
"x-completion-type": "directory",
"x-priority": "important",
"default": "."
},
"prerender": {
"description": "Pre-render every route of your application. (note: This is an experimental flag. The behavior might be changed.)",
"type": "boolean",
"default": false
},
"dotenv": {
"description": "Point to another .env file to load, relative to the root directory.",
"type": "string",
"default": "."
}
},
"definitions": {},
"required": [],
"examplesFile": "../../../docs/build-examples.md"
}
4 changes: 4 additions & 0 deletions packages/nuxt/src/executors/serve/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { convertNxExecutor } from '@nx/devkit';
import nuxtServeExecutor from './serve.impl';

export default convertNxExecutor(nuxtServeExecutor);
4 changes: 4 additions & 0 deletions packages/nuxt/src/executors/serve/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface NuxtServeExecutorOptions {
rootDir: string;
dotenv?: string;
}
29 changes: 29 additions & 0 deletions packages/nuxt/src/executors/serve/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": 2,
"outputCapture": "direct-nodejs",
"title": "Nuxt Server",
"cli": "nx",
"description": "Serves a Nuxt application for development.",
"type": "object",
"presets": [
{
"name": "Default minimum setup",
"keys": []
}
],
"properties": {
"host": {
"type": "string",
"description": "Dev server listening host.",
"default": ""
},
"port": {
"type": "number",
"description": "Dev server listening port",
"default": 3000
}
},
"definitions": {},
"required": [],
"examplesFile": "../../../docs/serve-examples.md"
}
29 changes: 29 additions & 0 deletions packages/nuxt/src/executors/serve/serve.impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ExecutorContext } from '@nx/devkit';
import { NuxtServeExecutorOptions } from './schema';
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';

// Required because dep is ESM package. Changing moduleResolution to NodeNext causes other issues unfortunately.
export function loadNuxiDynamicImport() {
return Function('return import("nuxi")')() as Promise<typeof import('nuxi')>;
}

export async function* nuxtServeExecutor(
options: NuxtServeExecutorOptions,
context: ExecutorContext
) {
const projectRoot =
context.projectsConfigurations.projects[context.projectName].root;
yield* createAsyncIterable<{ success: boolean }>(async ({ next, error }) => {
try {
const { runCommand } = await loadNuxiDynamicImport();
await runCommand('dev', [projectRoot]);
next({
success: true,
});
} catch (err) {
error(new Error(`Nuxt app exited with message ${err.message}`));
}
});
}

export default nuxtServeExecutor;
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,20 @@ exports[`app generated files content - as-provided should configure tsconfig and
"sourceRoot": "my-app/src",
"targets": {
"serve": {
"executor": "nx:run-commands",
"executor": "@nx/nuxt:serve",
"outputs": [
"{workspaceRoot}/{projectRoot}/.output",
"{workspaceRoot}/{projectRoot}/.nuxt"
],
"options": {
"command": "npx nuxi dev --port=4200",
"cwd": "my-app"
}
"options": {}
},
"build": {
"executor": "nx:run-commands",
"executor": "@nx/nuxt:build",
"outputs": [
"{workspaceRoot}/{projectRoot}/.output",
"{workspaceRoot}/{projectRoot}/.nuxt"
],
"options": {
"command": "npx nuxi build",
"cwd": "my-app"
}
"options": {}
},
"lint": {
"executor": "@nx/eslint:lint",
Expand Down
14 changes: 4 additions & 10 deletions packages/nuxt/src/generators/application/lib/add-targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ export function addServeTarget(
) {
const projectConfig = readProjectConfiguration(tree, projectName);
projectConfig.targets['serve'] = {
executor: 'nx:run-commands',
executor: '@nx/nuxt:serve',
outputs: [
'{workspaceRoot}/{projectRoot}/.output',
'{workspaceRoot}/{projectRoot}/.nuxt',
],
options: {
command: 'npx nuxi dev --port=4200',
cwd: joinPathFragments(projectRoot),
},
options: {},
};
updateProjectConfiguration(tree, projectName, projectConfig);
}
Expand All @@ -32,15 +29,12 @@ export function addBuildTarget(
) {
const projectConfig = readProjectConfiguration(tree, projectName);
projectConfig.targets['build'] = {
executor: 'nx:run-commands',
executor: '@nx/nuxt:build',
outputs: [
'{workspaceRoot}/{projectRoot}/.output',
'{workspaceRoot}/{projectRoot}/.nuxt',
],
options: {
command: 'npx nuxi build',
cwd: joinPathFragments(projectRoot),
},
options: {},
};
updateProjectConfiguration(tree, projectName, projectConfig);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ exports[`preset should create files (preset = angular-monorepo) 3`] = `

exports[`preset should create files (preset = nuxt-standalone) 1`] = `
{
"executor": "nx:run-commands",
"options": {
"command": "npx nuxi dev --port=4200",
"cwd": ".",
},
"executor": "@nx/nuxt:serve",
"options": {},
"outputs": [
"{workspaceRoot}/{projectRoot}/.output",
"{workspaceRoot}/{projectRoot}/.nuxt",
Expand Down
32 changes: 25 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac50f3c

Please sign in to comment.