Skip to content

Commit 8989c54

Browse files
committed
Use import instead of require
1 parent b563eef commit 8989c54

File tree

1 file changed

+33
-28
lines changed
  • turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs

1 file changed

+33
-28
lines changed

turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime.ts

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,40 @@ function loadChunkPath(chunkPath: ChunkPath, source?: SourceInfo): void {
143143
}
144144
}
145145

146-
function loadChunkUncached(chunkPath: ChunkPath) {
146+
async function loadChunkAsyncUncached(
147+
source: SourceInfo,
148+
chunkPath: ChunkPath
149+
): Promise<void> {
147150
// resolve to an absolute path to simplify `require` handling
148151
const resolved = path.resolve(RUNTIME_ROOT, chunkPath)
149152

150-
const chunkModules: CompressedModuleFactories = require(resolved)
151-
for (const [moduleId, moduleFactory] of Object.entries(chunkModules)) {
152-
if (!moduleFactories[moduleId]) {
153-
if (Array.isArray(moduleFactory)) {
154-
const [moduleFactoryFn, otherIds] = moduleFactory
155-
moduleFactories[moduleId] = moduleFactoryFn
156-
for (const otherModuleId of otherIds) {
157-
moduleFactories[otherModuleId] = moduleFactoryFn
153+
try {
154+
// The chunk we are loading is a CJS module, when importing it via import()
155+
// the module.exports become the `default` property of the esm module
156+
// namespace.
157+
const chunkModules: CompressedModuleFactories = (await import(resolved))
158+
.default
159+
for (const [moduleId, moduleFactory] of Object.entries(chunkModules)) {
160+
if (!moduleFactories[moduleId]) {
161+
if (Array.isArray(moduleFactory)) {
162+
const [moduleFactoryFn, otherIds] = moduleFactory
163+
moduleFactories[moduleId] = moduleFactoryFn
164+
for (const otherModuleId of otherIds) {
165+
moduleFactories[otherModuleId] = moduleFactoryFn
166+
}
167+
} else {
168+
moduleFactories[moduleId] = moduleFactory
158169
}
159-
} else {
160-
moduleFactories[moduleId] = moduleFactory
161170
}
162171
}
172+
} catch (e) {
173+
let errorMessage = `Failed to load chunk ${chunkPath}`
174+
if (source) {
175+
errorMessage += ` from ${stringifySourceInfo(source)}`
176+
}
177+
throw new Error(errorMessage, {
178+
cause: e,
179+
})
163180
}
164181
}
165182

@@ -176,23 +193,11 @@ function loadChunkAsync(
176193

177194
let entry = chunkCache.get(chunkPath)
178195
if (entry === undefined) {
179-
try {
180-
// Load the chunk synchronously
181-
loadChunkUncached(chunkPath)
182-
entry = loadedChunk
183-
} catch (e) {
184-
let errorMessage = `Failed to load chunk ${chunkPath}`
185-
if (source) {
186-
errorMessage += ` from ${stringifySourceInfo(source)}`
187-
}
188-
189-
// Cache the failure promise, future requests will also get this same rejection
190-
entry = Promise.reject(
191-
new Error(errorMessage, {
192-
cause: e,
193-
})
194-
)
195-
}
196+
// A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection.
197+
// Handling the rejection will not trigger unhandled rejections.
198+
entry = loadChunkAsyncUncached(source, chunkPath).then(
199+
() => void chunkCache.set(chunkPath, loadedChunk)
200+
)
196201
chunkCache.set(chunkPath, entry)
197202
}
198203
// TODO: Return an instrumented Promise that React can use instead of relying on referential equality.

0 commit comments

Comments
 (0)