Skip to content

Commit

Permalink
fix: #34711 ensure distDir inside project folder (#34795)
Browse files Browse the repository at this point in the history
Fixes #34711
  • Loading branch information
sincovschi committed Feb 25, 2022
1 parent aecfe4d commit 3d5dd58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -147,6 +147,7 @@ export default class HotReloader {
private buildId: string
private middlewares: any[]
private pagesDir: string
private distDir: string
private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete
private runtime?: 'nodejs' | 'edge'
Expand All @@ -170,12 +171,14 @@ export default class HotReloader {
{
config,
pagesDir,
distDir,
buildId,
previewProps,
rewrites,
}: {
config: NextConfigComplete
pagesDir: string
distDir: string
buildId: string
previewProps: __ApiPreviewProps
rewrites: CustomRoutes['rewrites']
Expand All @@ -185,6 +188,7 @@ export default class HotReloader {
this.dir = dir
this.middlewares = []
this.pagesDir = pagesDir
this.distDir = distDir
this.clientStats = null
this.serverStats = null
this.serverPrevDocumentHash = null
Expand Down Expand Up @@ -435,15 +439,15 @@ export default class HotReloader {
startSpan.stop() // Stop immediately to create an artificial parent span

await this.clean(startSpan)

// Ensure distDir exists before writing package.json
await fs.mkdir(this.config.distDir, { recursive: true })
await fs.mkdir(this.distDir, { recursive: true })

const distPackageJsonPath = join(this.distDir, 'package.json')

// Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
await fs.writeFile(
join(this.config.distDir, 'package.json'),
'{"type": "commonjs"}'
)
await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')

const configs = await this.getWebpackConfig(startSpan)

Expand Down
1 change: 1 addition & 0 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -380,6 +380,7 @@ export default class DevServer extends Server {

this.hotReloader = new HotReloader(this.dir, {
pagesDir: this.pagesDir!,
distDir: this.distDir,
config: this.nextConfig,
previewProps: this.getPreviewProps(),
buildId: this.buildId,
Expand Down

0 comments on commit 3d5dd58

Please sign in to comment.