Skip to content

Commit

Permalink
fix(nuxt): Add e2e-ci and serve-static targets
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Feb 28, 2024
1 parent 6576325 commit 809183c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 1 deletion.
13 changes: 12 additions & 1 deletion e2e/nuxt/src/nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
cleanupProject,
killPorts,
newProject,
readJson,
runCLI,
uniq,
} from '@nx/e2e/utils';
Expand All @@ -16,7 +17,7 @@ describe('Nuxt Plugin', () => {
unsetProjectNameAndRootFormat: false,
});
runCLI(
`generate @nx/nuxt:app ${app} --unitTestRunner=vitest --projectNameAndRootFormat=as-provided`
`generate @nx/nuxt:app ${app} --unitTestRunner=vitest --projectNameAndRootFormat=as-provided e2eTestRunner=cypress`
);
runCLI(
`generate @nx/nuxt:component --directory=${app}/src/components/one --name=one --nameAndDirectoryFormat=as-provided --unitTestRunner=vitest`
Expand Down Expand Up @@ -54,4 +55,14 @@ describe('Nuxt Plugin', () => {
runCLI(`run ${app}:build-storybook --verbose`);
checkFilesExist(`${app}/storybook-static/index.html`);
}, 300_000);

it('should have build, serve, build-static, server-static targets', () => {
runCLI(`show project ${app} --json > targets.json`);

const targets = readJson('targets.json');
expect(targets.targets['build']).toBeDefined();
expect(targets.targets['serve']).toBeDefined();
expect(targets.targets['serve-static']).toBeDefined();
expect(targets.targets['build-static']).toBeDefined();
});
});
2 changes: 2 additions & 0 deletions packages/nuxt/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('app', () => {
expect(projectConfig.targets.build).toBeUndefined();
expect(projectConfig.targets.serve).toBeUndefined();
expect(projectConfig.targets.test).toBeUndefined();
expect(projectConfig.targets['build-static']).toBeUndefined();
expect(projectConfig.targets['serve-static']).toBeUndefined();
});

it('should create all new files in the correct location', async () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/nuxt/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export async function addE2e(host: Tree, options: NormalizedSchema) {
bundler: 'vite',
skipFormat: true,
devServerTarget: `${options.projectName}:serve`,
webServerCommands: {
default: `${getPackageManagerCommand().exec} nx serve ${
options.projectName
}`,
},
ciWebServerCommand: `nx run ${options.projectName}:serve-static`,
baseUrl: 'http://localhost:4200',
jsx: true,
addPlugin: true,
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/generators/init/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function addPlugin(tree: Tree) {
options: {
buildTargetName: 'build',
serveTargetName: 'serve',
serveStaticTargetName: 'serve-static',
},
});
}
Expand Down
60 changes: 60 additions & 0 deletions packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ exports[`@nx/nuxt/plugin not root project should create nodes 1`] = `
"my-app": {
"root": "my-app",
"targets": {
"acme-build-static": {
"cache": true,
"command": "nuxt build --prerender",
"dependsOn": [
"^acme-build-static",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"nuxt",
],
},
],
"options": {
"cwd": "my-app",
},
"outputs": [
"{workspaceRoot}/dist/my-app/",
],
},
"acme-serve-static": {
"executor": "@nx/web:file-server",
"options": {
"buildTarget": "acme-build-static",
"port": 4200,
"staticFilePath": "{projectRoot}/dist",
},
},
"build-something": {
"cache": true,
"command": "nuxt build",
Expand Down Expand Up @@ -68,12 +98,42 @@ exports[`@nx/nuxt/plugin root project should create nodes 1`] = `
"dist/my-app/",
],
},
"build-static": {
"cache": true,
"command": "nuxt build --prerender",
"dependsOn": [
"^build-static",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"nuxt",
],
},
],
"options": {
"cwd": ".",
},
"outputs": [
"dist/my-app/",
],
},
"serve": {
"command": "nuxt dev",
"options": {
"cwd": ".",
},
},
"serve-static": {
"executor": "@nx/web:file-server",
"options": {
"buildTarget": "build-static",
"port": 4200,
"staticFilePath": "{projectRoot}/dist",
},
},
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe('@nx/nuxt/plugin', () => {
{
buildTargetName: 'build',
serveTargetName: 'serve',
buildStaticTargetName: 'build-static',
serveStaticTargetName: 'serve-static',
},
context
);
Expand Down Expand Up @@ -89,6 +91,8 @@ describe('@nx/nuxt/plugin', () => {
{
buildTargetName: 'build-something',
serveTargetName: 'my-serve',
buildStaticTargetName: 'acme-build-static',
serveStaticTargetName: 'acme-serve-static',
},
context
);
Expand Down
53 changes: 53 additions & 0 deletions packages/nuxt/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export const createDependencies: CreateDependencies = () => {
export interface NuxtPluginOptions {
buildTargetName?: string;
serveTargetName?: string;
serveStaticTargetName?: string;
buildStaticTargetName?: string;
}

export const createNodes: CreateNodes<NuxtPluginOptions> = [
Expand Down Expand Up @@ -108,6 +110,15 @@ async function buildNuxtTargets(

targets[options.serveTargetName] = serveTarget(projectRoot);

targets[options.serveStaticTargetName] = serveStaticTarget(options);

targets[options.buildStaticTargetName] = buildStaticTarget(
options.buildStaticTargetName,
namedInputs,
buildOutputs,
projectRoot
);

return targets;
}

Expand Down Expand Up @@ -148,6 +159,46 @@ function serveTarget(projectRoot: string) {
return targetConfig;
}

function serveStaticTarget(options: NuxtPluginOptions) {
const targetConfig: TargetConfiguration = {
executor: '@nx/web:file-server',
options: {
buildTarget: `${options.buildStaticTargetName}`,
staticFilePath: '{projectRoot}/dist',
port: 4200,
},
};

return targetConfig;
}

function buildStaticTarget(
buildStaticTargetName: string,
namedInputs: {
[inputName: string]: any[];
},
buildOutputs: string[],
projectRoot: string
) {
const targetConfig: TargetConfiguration = {
command: `nuxt build --prerender`,
options: { cwd: projectRoot },
cache: true,
dependsOn: [`^${buildStaticTargetName}`],
inputs: [
...('production' in namedInputs
? ['production', '^production']
: ['default', '^default']),

{
externalDependencies: ['nuxt'],
},
],
outputs: buildOutputs,
};
return targetConfig;
}

async function getInfoFromNuxtConfig(
configFilePath: string,
context: CreateNodesContext,
Expand Down Expand Up @@ -216,5 +267,7 @@ function normalizeOptions(options: NuxtPluginOptions): NuxtPluginOptions {
options ??= {};
options.buildTargetName ??= 'build';
options.serveTargetName ??= 'serve';
options.serveStaticTargetName ??= 'serve-static';
options.buildStaticTargetName ??= 'build-static';
return options;
}

0 comments on commit 809183c

Please sign in to comment.