Skip to content

Commit f9f42d7

Browse files
committed
refactor: use existing resolveModuleRoot function instead of module.createRequire
1 parent 847b506 commit f9f42d7

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

packages/runtime/src/helpers/config.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import mod from 'module'
2-
31
import type { NetlifyConfig } from '@netlify/build/types'
42
import destr from 'destr'
53
import { readJSON, writeJSON } from 'fs-extra'
@@ -75,9 +73,9 @@ export const updateRequiredServerFiles = async (publish: string, modifiedConfig:
7573
await writeJSON(configFile, modifiedConfig)
7674
}
7775

78-
export const resolveModuleRoot = (moduleName) => {
76+
export const resolveModuleRoot = (moduleName, paths = [process.cwd()]) => {
7977
try {
80-
return dirname(relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })))
78+
return dirname(relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths })))
8179
} catch {
8280
return null
8381
}
@@ -164,18 +162,16 @@ export const configureHandlerFunctions = async ({
164162
`!${nextRoot}/dist/compiled/webpack/bundle4.js`,
165163
`!${nextRoot}/dist/compiled/webpack/bundle5.js`,
166164
)
167-
}
168165

169-
try {
170166
// on Next 13.5+ there is no longer statically analyzable import to styled-jsx/style
171167
// so lambda fails to bundle it. Next require hooks actually try to resolve it
172168
// and fail if it is not bundled, so we forcefully add it to lambda.
173-
174-
// eslint-disable-next-line n/no-unsupported-features/node-builtins
175-
const nextRequire = mod.createRequire(require.resolve(`next`))
176-
const styledJsxPath = nextRequire.resolve(`styled-jsx/style`)
177-
netlifyConfig.functions[functionName].included_files.push(styledJsxPath)
178-
} catch {}
169+
const styledJsxRoot = resolveModuleRoot('styled-jsx', [join(process.cwd(), nextRoot)])
170+
if (styledJsxRoot) {
171+
const styledJsxStyleModulePath = join(styledJsxRoot, 'style.js')
172+
netlifyConfig.functions[functionName].included_files.push(styledJsxStyleModulePath)
173+
}
174+
}
179175

180176
excludedModules.forEach((moduleName) => {
181177
const moduleRoot = resolveModuleRoot(moduleName)

test/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ describe('onBuild()', () => {
455455
`!node_modules/next/dist/next-server/server/lib/squoosh/**/*.wasm`,
456456
'!node_modules/next/dist/compiled/webpack/bundle4.js',
457457
'!node_modules/next/dist/compiled/webpack/bundle5.js',
458-
'/home/runner/work/next-runtime/next-runtime/node_modules/styled-jsx/style.js',
458+
'node_modules/styled-jsx/style.js',
459459
'!node_modules/sharp/**/*',
460460
]
461461
// Relative paths in Windows are different

test/test-utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path, { dirname } from 'path'
22

33
import cpy from 'cpy'
4-
import { writeJSON, existsSync, ensureDir, readJson, copy } from 'fs-extra'
4+
import { writeJSON, writeFile, existsSync, ensureDir, readJson, copy } from 'fs-extra'
55
import { dir as getTmpDir } from 'tmp-promise'
66

77
const FIXTURES_DIR = `${__dirname}/fixtures`
@@ -26,7 +26,7 @@ const rewriteAppDir = async function (dir = '.next') {
2626

2727
// Move .next from sample project to current directory
2828
export const moveNextDist = async function (dir = '.next', copyMods = false) {
29-
await (copyMods ? copyModules(['next', 'sharp']) : stubModules(['next', 'sharp']))
29+
await (copyMods ? copyModules(['next', 'sharp', 'styled-jsx']) : stubModules(['next', 'sharp', 'styled-jsx']))
3030
await ensureDir(dirname(dir))
3131
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir))
3232

@@ -53,6 +53,9 @@ export const stubModules = async function (modules) {
5353
const dir = path.join(process.cwd(), 'node_modules', mod)
5454
await ensureDir(dir)
5555
await writeJSON(path.join(dir, 'package.json'), { name: mod })
56+
if (mod === `styled-jsx`) {
57+
await writeFile('style.js', '')
58+
}
5659
}
5760
}
5861

0 commit comments

Comments
 (0)