Skip to content

Commit

Permalink
Fix relative import path for type plugin in NX workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Serpa committed Apr 3, 2023
1 parent 508422c commit f04cc2f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
50 changes: 50 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,50 @@
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.relativeBaseImportPath).toEqual('../../../app')
})

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.relativeBaseImportPath).toEqual(
'../../../../../../apps/myproject/app'
)
})

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.relativeBaseImportPath).toEqual('../../../../../ui/app')
})
})
16 changes: 9 additions & 7 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 Down Expand Up @@ -463,6 +465,11 @@ export class NextTypesPlugin {
}
}

get relativeBaseImportPath() {
const distDirAbs = path.join(this.dir, this.distDir)
return path.relative(path.join(distDirAbs, appTypesBasePath), this.appDir)
}

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

Expand Down Expand Up @@ -503,9 +510,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 @@ -542,14 +546,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, ''),
this.relativeBaseImportPath,
relativePathToRoot.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
)
.replace(/\\/g, '/')
Expand Down

0 comments on commit f04cc2f

Please sign in to comment.