Skip to content

Commit

Permalink
feat(nextjs): add support for experimental appDir
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Apr 5, 2023
1 parent ee1f7c1 commit d661600
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 2 deletions.
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 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
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

0 comments on commit d661600

Please sign in to comment.