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

Fix type plugin for NX workspaces #47534

Merged
merged 5 commits into from
Apr 3, 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
69 changes: 69 additions & 0 deletions packages/next/src/build/webpack/plugins/next-types-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { NextTypesPlugin } from './next-types-plugin'

describe('next-types-plugin', () => {
it('should generate correct base import path', () => {
const plugin = new NextTypesPlugin({
dir: '/Users/myself/myproject',
distDir: '.next',
appDir: '/Users/myself/myproject/app',
dev: false,
isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
typedRoutes: false,
originalRewrites: undefined,
originalRedirects: undefined,
})
expect(plugin.getRelativePathFromAppTypesDir('page.tsx')).toEqual(
'../../../app/page.tsx'
)
expect(plugin.getRelativePathFromAppTypesDir('layout.tsx')).toEqual(
'../../../app/layout.tsx'
)
expect(plugin.getRelativePathFromAppTypesDir('test/page.tsx')).toEqual(
'../../../../app/test/page.tsx'
)
expect(
plugin.getRelativePathFromAppTypesDir('deeply/nested/page.tsx')
).toEqual('../../../../../app/deeply/nested/page.tsx')
})

it('should generate correct base import path for nx monorepos', () => {
const plugin = new NextTypesPlugin({
dir: '/Users/myself/code/nx-monorepo/apps/myproject',
distDir: '../../dist/apps/myproject/.next',
appDir: '/Users/myself/code/nx-monorepo/apps/myproject/app',
dev: false,
isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
typedRoutes: false,
originalRewrites: undefined,
originalRedirects: undefined,
})
expect(plugin.getRelativePathFromAppTypesDir('layout.tsx')).toEqual(
'../../../../../../apps/myproject/app/layout.tsx'
)
expect(plugin.getRelativePathFromAppTypesDir('test/page.tsx')).toEqual(
'../../../../../../../apps/myproject/app/test/page.tsx'
)
})

it('should generate correct base import path for custom projects', () => {
const plugin = new NextTypesPlugin({
dir: '/Users/myself/code/custom-project/frontend/ui',
distDir: '../dist/ui/.next',
appDir: '/Users/myself/code/custom-project/frontend/ui/app',
dev: false,
isEdgeServer: false,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
typedRoutes: false,
originalRewrites: undefined,
originalRedirects: undefined,
})
expect(plugin.getRelativePathFromAppTypesDir('layout.tsx')).toEqual(
'../../../../../ui/app/layout.tsx'
)
expect(plugin.getRelativePathFromAppTypesDir('test/page.tsx')).toEqual(
'../../../../../../ui/app/test/page.tsx'
)
})
})
36 changes: 25 additions & 11 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ declare module 'next/link' {
}`
}

const appTypesBasePath = path.join('types', 'app')

export class NextTypesPlugin {
dir: string
distDir: string
Expand All @@ -444,6 +446,7 @@ export class NextTypesPlugin {
pageExtensions: string[]
pagesDir: string
typedRoutes: boolean
distDirAbsolutePath: string

constructor(options: Options) {
this.dir = options.dir
Expand All @@ -454,6 +457,7 @@ export class NextTypesPlugin {
this.pageExtensions = options.pageExtensions
this.pagesDir = path.join(this.appDir, '..', 'pages')
this.typedRoutes = options.typedRoutes
this.distDirAbsolutePath = path.join(this.dir, this.distDir)
if (this.typedRoutes && !redirectsRewritesTypesProcessed) {
redirectsRewritesTypesProcessed = true
addRedirectsRewritesRouteTypes(
Expand All @@ -463,6 +467,24 @@ export class NextTypesPlugin {
}
}

getRelativePathFromAppTypesDir(moduleRelativePathToAppDir: string) {
const moduleAbsolutePath = path.join(
this.appDir,
moduleRelativePathToAppDir
)

const moduleInAppTypesAbsolutePath = path.join(
this.distDirAbsolutePath,
appTypesBasePath,
moduleRelativePathToAppDir
)

return path.relative(
moduleInAppTypesAbsolutePath + '/..',
moduleAbsolutePath
)
}

collectPage(filePath: string) {
if (!this.typedRoutes) return

Expand Down Expand Up @@ -503,9 +525,6 @@ export class NextTypesPlugin {
}

apply(compiler: webpack.Compiler) {
// From dist root to project root
const distDirRelative = path.relative(this.distDir + '/..', '.')

// From asset root to dist root
const assetDirRelative = this.dev
? '..'
Expand Down Expand Up @@ -533,7 +552,6 @@ export class NextTypesPlugin {
const IS_PAGE = !IS_LAYOUT && /[/\\]page\.[^.]+$/.test(mod.resource)
const IS_ROUTE = !IS_PAGE && /[/\\]route\.[^.]+$/.test(mod.resource)
const relativePathToApp = path.relative(this.appDir, mod.resource)
const relativePathToRoot = path.relative(this.dir, mod.resource)

if (!this.dev) {
if (IS_PAGE || IS_ROUTE) {
Expand All @@ -542,16 +560,12 @@ export class NextTypesPlugin {
}

const typePath = path.join(
'types',
'app',
appTypesBasePath,
relativePathToApp.replace(/\.(js|jsx|ts|tsx|mjs)$/, '.ts')
)
const relativeImportPath = path
.join(
distDirRelative,
path.relative(typePath, ''),
relativePathToRoot.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
)
.join(this.getRelativePathFromAppTypesDir(relativePathToApp))
.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
.replace(/\\/g, '/')
const assetPath = assetDirRelative + '/' + normalizePathSep(typePath)

Expand Down