Skip to content

Commit

Permalink
Write "type": "commonjs" package.json to .next
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jan 25, 2022
1 parent 6a10972 commit cb2a449
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -529,6 +529,13 @@ export default async function build(
await recursiveDelete(distDir, /^cache/)
}

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

// We need to write the manifest with rewrites before build
// so serverless can import the manifest
await nextBuildSpan
Expand Down
10 changes: 10 additions & 0 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -41,6 +41,7 @@ import { DecodeError } from '../../shared/lib/utils'
import { Span, trace } from '../../trace'
import { getProperError } from '../../lib/is-error'
import ws from 'next/dist/compiled/ws'
import { promises as fs } from 'fs'

const wsServer = new ws.Server({ noServer: true })

Expand Down Expand Up @@ -431,6 +432,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 })

// 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"}'
)

const configs = await this.getWebpackConfig(startSpan)

Expand Down

0 comments on commit cb2a449

Please sign in to comment.