Skip to content

Commit c1c1df5

Browse files
authored
Merge pull request #498 from liam-hq/exclude_prisma_from_bundle_via_webpack_config
🔧 Update Next.js configuration to exclude '@prisma/internals'
2 parents 4cee786 + 839cc1e commit c1c1df5

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

frontend/apps/erd-web/next.config.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,47 @@ const gitCommitHash = execSync('git rev-parse --short HEAD').toString().trim()
66
const releaseDate = new Date().toISOString().split('T')[0]
77

88
const nextConfig: NextConfig = {
9-
// NOTE: Exclude '@prisma/internals' from the client-side bundle
10-
// This module is server-side only and should not be included in the client build
9+
// NOTE: Exclude Prisma-related packages from the bundle
10+
// These packages are installed separately in the node_modules/@prisma directory
11+
// Excluding them prevents `Error: Cannot find module 'fs'` errors in the build process
1112
webpack: (config) => {
12-
config.resolve.alias = {
13-
...config.resolve.alias,
14-
'@prisma/internals': false,
13+
if (Array.isArray(config.externals)) {
14+
config.externals.push(
15+
'@prisma/debug',
16+
'@prisma/engines',
17+
'@prisma/engines-version',
18+
'@prisma/fetch-engine',
19+
'@prisma/generator-helper',
20+
'@prisma/get-platform',
21+
'@prisma/internals',
22+
'@prisma/prisma-schema-wasm',
23+
'@prisma/schema-files-loader',
24+
)
25+
} else {
26+
config.externals['@prisma/debug'] = '@prisma/debug'
27+
config.externals['@prisma/engines'] = '@prisma/engines'
28+
config.externals['@prisma/engines-version'] = '@prisma/engines-version'
29+
config.externals['@prisma/fetch-engine'] = '@prisma/fetch-engine'
30+
config.externals['@prisma/generator-helper'] = '@prisma/generator-helper'
31+
config.externals['@prisma/get-platform'] = '@prisma/get-platform'
32+
config.externals['@prisma/internals'] = '@prisma/internals'
33+
config.externals['@prisma/prisma-schema-wasm'] =
34+
'@prisma/prisma-schema-wasm'
35+
config.externals['@prisma/schema-files-loader'] =
36+
'@prisma/schema-files-loader'
1537
}
38+
config.plugins.push({
39+
// biome-ignore lint/suspicious/noExplicitAny: webpack types are incomplete so we need to use any here
40+
apply: (compiler: any) => {
41+
compiler.hooks.afterEmit.tap('InstallPrismaInternals', () => {
42+
execSync('node scripts/install-prisma-internals.mjs', {
43+
stdio: 'inherit',
44+
cwd: __dirname,
45+
})
46+
})
47+
},
48+
})
49+
1650
return config
1751
},
1852
outputFileTracingIncludes: {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { execSync } from 'node:child_process'
2+
import fs from 'node:fs'
3+
import path from 'node:path'
4+
import { fileURLToPath } from 'node:url'
5+
6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = path.dirname(__filename)
8+
9+
const main = () => {
10+
const dotNextPath = path.resolve(__dirname, '../.next')
11+
const nodeModulesPath = path.join(dotNextPath, 'node_modules')
12+
13+
if (!fs.existsSync(dotNextPath)) {
14+
fs.mkdirSync(dotNextPath, { recursive: true })
15+
}
16+
if (!fs.existsSync(nodeModulesPath)) {
17+
fs.mkdirSync(nodeModulesPath, { recursive: true })
18+
}
19+
20+
process.chdir(nodeModulesPath)
21+
22+
execSync('npm install @prisma/internals', { stdio: 'inherit' })
23+
24+
const packageJsonPath = path.join(nodeModulesPath, 'package.json')
25+
const packageLockJsonPath = path.join(nodeModulesPath, '.package-lock.json')
26+
if (fs.existsSync(packageJsonPath)) {
27+
fs.unlinkSync(packageJsonPath)
28+
}
29+
if (fs.existsSync(packageLockJsonPath)) {
30+
fs.unlinkSync(packageLockJsonPath)
31+
}
32+
}
33+
34+
main()

0 commit comments

Comments
 (0)