Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): add support for experimental appDir #16132

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/generated/packages/next/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
"default": false,
"description": "Do not add dependencies to `package.json`.",
"x-priority": "internal"
},
"appDir": {
"type": "boolean",
"default": false,
"description": "Enable experimental app directory for the project",
"x-prompt": "Do you want to use experimental app/ in this project?"
}
},
"required": [],
Expand Down
20 changes: 19 additions & 1 deletion packages/next/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import {
getProjects,
NxJsonConfiguration,
readJson,
readProjectConfiguration,
Tree,
Expand Down Expand Up @@ -418,4 +417,23 @@ describe('app', () => {

expect(tree.exists('apps/my-app/public/.gitkeep')).toBe(true);
});

describe('--appDir', () => {
it('should generate app directory instead of pages', async () => {
await applicationGenerator(tree, {
name: 'testApp',
style: 'css',
appDir: true,
});

expect(tree.exists('apps/testApp/pages/styles.css')).toBeFalsy();

expect(tree.exists('apps/testApp/app/global.css'));
expect(tree.exists('apps/testApp/app/page.tsx'));
expect(tree.exists('apps/testApp/app/layout.tsx'));
expect(tree.exists('apps/testApp/app/api/hello/route.ts'));
expect(tree.exists('apps/testApp/app/page.module.css'));
expect(tree.exists('apps/testApp/app/favicon.ico'));
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function GET(request: Request) {
return new Response('Hello, from API!')
}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- styleContent %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Head from 'next/head';
import './global.<%= stylesExt %>';

export const metadata = {
title: 'Nx Next App',
description: 'Generated by create-nx-workspace',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<Head>
<title>Welcome to <%= name %>!</title>
</Head>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- pageStyleContent %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<% if (styledModule && styledModule !== 'styled-jsx') {
var wrapper = 'StyledPage';
%>import styled from '<%= styledModule %>';<% } else {
var wrapper = 'div';
%>
<%- style !== 'styled-jsx' ? `import styles from './page.module.${style}';` : '' %>
<% }
%>

<% if (styledModule && styledModule !== 'styled-jsx') { %>
const StyledPage = styled.div`<%- pageStyleContent %>`;
<% }%>

export async function Index() {
/*
* Replace the elements below with your own.
*
* Note: The corresponding styles are in the ./<%= fileName %>.<%= style %> file.
*/
return (
<<%= wrapper %><% if (!styledModule) {%> className={styles.page}<% } %>>
<%- styledModule === 'styled-jsx' ? `<style jsx>{\`${pageStyleContent}\`}</style>` : `` %>
<%- appContent %>
</<%= wrapper %>>
);
};

export default Index;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const nextConfig = {
// See: https://github.com/gregberge/svgr
svgr: false,
},
<% if(appDir) { %>
experimental: {
appDir: true
},
<% } %>
};

module.exports = withLess(withNx(nextConfig));
Expand All @@ -32,6 +37,11 @@ const nextConfig = {
// See: https://github.com/gregberge/svgr
svgr: false,
},
<% if(appDir) { %>
experimental: {
appDir: true
},
<% } %>
};

module.exports = withStylus(withNx(nextConfig));
Expand All @@ -50,6 +60,11 @@ const nextConfig = {
// See: https://github.com/gregberge/svgr
svgr: false,
},
<% if(appDir) { %>
experimental: {
appDir: true
},
<% } %>
};

module.exports = withNx(nextConfig);
Expand All @@ -64,6 +79,11 @@ const nextConfig = {
// See: https://github.com/gregberge/svgr
svgr: false,
},
<% if(appDir) { %>
experimental: {
appDir: true
},
<% } %>
};

module.exports = withNx(nextConfig);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,27 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) {

generateFiles(
host,
join(__dirname, '../files'),
join(__dirname, '../files/common'),
options.appProjectRoot,
templateVariables
);

if (options.appDir) {
generateFiles(
host,
join(__dirname, '../files/app'),
join(options.appProjectRoot, 'app'),
templateVariables
);
} else {
generateFiles(
host,
join(__dirname, '../files/pages'),
join(options.appProjectRoot, 'pages'),
templateVariables
);
}

if (options.unitTestRunner === 'none') {
host.delete(`${options.appProjectRoot}/specs/${options.fileName}.spec.tsx`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function normalizeOptions(

const fileName = 'index';

const appDir = options.appDir ?? false;

const styledModule = /^(css|scss|less|styl)$/.test(options.style)
? null
: options.style;
Expand All @@ -55,6 +57,7 @@ export function normalizeOptions(

return {
...options,
appDir,
name: names(options.name).fileName,
projectName: appProjectName,
linter: options.linter || Linter.EsLint,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface Schema {
swc?: boolean;
customServer?: boolean;
skipPackageJson?: boolean;
appDir?: boolean;
}
6 changes: 6 additions & 0 deletions packages/next/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
"default": false,
"description": "Do not add dependencies to `package.json`.",
"x-priority": "internal"
},
"appDir": {
"type": "boolean",
"default": false,
"description": "Enable experimental app directory for the project",
"x-prompt": "Do you want to use experimental app/ in this project?"
}
},
"required": [],
Expand Down