Skip to content

Commit

Permalink
Use full path for the typescript plugin appDir generated import
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Serpa committed Mar 25, 2023
1 parent ebcd550 commit 75400bc
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ interface Options {

function createTypeGuardFile(
fullPath: string,
relativePath: string,
importPath: string,
options: {
type: 'layout' | 'page'
slots?: string[]
}
) {
return `// File: ${fullPath}
import * as entry from '${relativePath}'
import * as entry from '${importPath}'
import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface'
type TEntry = typeof entry
Expand Down Expand Up @@ -393,9 +393,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 All @@ -422,7 +419,6 @@ export class NextTypesPlugin {
const IS_LAYOUT = /[/\\]layout\.[^./\\]+$/.test(mod.resource)
const IS_PAGE = !IS_LAYOUT && /[/\\]page\.[^.]+$/.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) {
Expand All @@ -435,26 +431,20 @@ export class NextTypesPlugin {
'app',
relativePathToApp.replace(/\.(js|jsx|ts|tsx|mjs)$/, '.ts')
)
const relativeImportPath = path
.join(
distDirRelative,
path.relative(typePath, ''),
relativePathToRoot.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
)
.replace(/\\/g, '/')
const importPath = mod.resource.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
const assetPath = assetDirRelative + '/' + typePath.replace(/\\/g, '/')

if (IS_LAYOUT) {
const slots = await collectNamedSlots(mod.resource)
assets[assetPath] = new sources.RawSource(
createTypeGuardFile(mod.resource, relativeImportPath, {
createTypeGuardFile(mod.resource, importPath, {
type: 'layout',
slots,
})
)
} else if (IS_PAGE) {
assets[assetPath] = new sources.RawSource(
createTypeGuardFile(mod.resource, relativeImportPath, {
createTypeGuardFile(mod.resource, importPath, {
type: 'page',
})
)
Expand Down

0 comments on commit 75400bc

Please sign in to comment.