Skip to content

Commit 85dae13

Browse files
committed
refactor: extract createContextObject
1 parent bda6534 commit 85dae13

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/import.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
isVMModuleAvailable,
88
pathToFileURLString,
99
getCallerDirname,
10-
shallowMergeContext,
1110
createGlobalObject,
11+
createContextObject,
1212
resolveModuleSpecifier
1313
} from './utils'
1414

@@ -83,16 +83,13 @@ Enable '--experimental-vm-modules' CLI option or replace it with dynamic 'import
8383
const moduleFileURLString = pathToFileURLString(moduleFilename)
8484

8585
const globalObject = createGlobalObject(globals, useCurrentGlobal)
86-
const contextObject = shallowMergeContext(
86+
const contextObject = createContextObject(
8787
{
8888
__dirname: dirname,
8989
__filename: moduleFilename
9090
},
9191
globalObject
9292
)
93-
if (!('global' in contextObject)) {
94-
contextObject.global = contextObject
95-
}
9693
contextObject[IMPORTS] = {}
9794
const context = createContext(contextObject)
9895

src/require.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { nanoid } from 'nanoid'
55
import {
66
isInESModuleScope,
77
getCallerDirname,
8-
shallowMergeContext,
98
createGlobalObject,
9+
createContextObject,
1010
resolveModuleSpecifier
1111
} from './utils'
1212

@@ -30,7 +30,7 @@ export const requireFromString = (
3030
contextModule.require = createRequire(moduleFilename)
3131

3232
const globalObject = createGlobalObject(globals, useCurrentGlobal)
33-
const contextObject = shallowMergeContext(
33+
const contextObject = createContextObject(
3434
{
3535
__dirname: contextModule.path,
3636
__filename: contextModule.filename,
@@ -40,9 +40,6 @@ export const requireFromString = (
4040
},
4141
globalObject
4242
)
43-
if (!('global' in contextObject)) {
44-
contextObject.global = contextObject
45-
}
4643

4744
runInNewContext(code, contextObject, {
4845
filename: moduleFilename,

src/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const forEachPropertyKey = (
5454
Object.getOwnPropertySymbols(context).forEach(callbackfn)
5555
}
5656

57-
export const shallowMergeContext = (target: Context, source: Context): Context => {
57+
const shallowMergeContext = (target: Context, source: Context): Context => {
5858
forEachPropertyKey(source, propertyKey => {
5959
Object.defineProperty(target, propertyKey, {
6060
...Object.getOwnPropertyDescriptor(source, propertyKey)
@@ -93,6 +93,14 @@ export const createGlobalObject = (globals: Context, useCurrentGlobal: boolean):
9393
return globalObject
9494
}
9595

96+
export const createContextObject = (moduleContext: Context, globalObject: Context): Context => {
97+
const contextObject: Context = shallowMergeContext(moduleContext, globalObject)
98+
if (!('global' in contextObject)) {
99+
contextObject.global = contextObject
100+
}
101+
return contextObject
102+
}
103+
96104
export const resolveModuleSpecifier = (specifier: string, dirname: string): string => {
97105
const specifierPath = fileURLStringToPath(specifier)
98106
return specifierPath.startsWith('.') || isAbsolute(specifierPath)

0 commit comments

Comments
 (0)