-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Make
getModuleFromFilename
compatible with ESM (#10061)
This removes usage of `require.main.filename` to bring better ESM compatability. An extra bonus in that now `createGetModuleFromFilename` is ESM compatible, we can use it in the Anr worker when `appRootPath` is supplied (ie. Electron only for now). ###⚠️ Fingerprinting This may change `module` fingerprinting for ESM since previously when `require.main.filename` was not available, `process.cwd()` was used.
- Loading branch information
Showing
6 changed files
with
68 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,33 @@ | ||
import { getModuleFromFilename } from '../src/module'; | ||
import { createGetModuleFromFilename } from '../src/module'; | ||
|
||
describe('getModuleFromFilename', () => { | ||
test('Windows', () => { | ||
expect( | ||
getModuleFromFilename('C:\\Users\\Tim\\node_modules\\some-dep\\module.js', 'C:\\Users\\Tim\\', true), | ||
).toEqual('some-dep:module'); | ||
const getModuleFromFilenameWindows = createGetModuleFromFilename('C:\\Users\\Tim\\', true); | ||
const getModuleFromFilenamePosix = createGetModuleFromFilename('/Users/Tim/'); | ||
|
||
expect(getModuleFromFilename('C:\\Users\\Tim\\some\\more\\feature.js', 'C:\\Users\\Tim\\', true)).toEqual( | ||
'some.more:feature', | ||
describe('createGetModuleFromFilename', () => { | ||
test('Windows', () => { | ||
expect(getModuleFromFilenameWindows('C:\\Users\\Tim\\node_modules\\some-dep\\module.js')).toEqual( | ||
'some-dep:module', | ||
); | ||
expect(getModuleFromFilenameWindows('C:\\Users\\Tim\\some\\more\\feature.js')).toEqual('some.more:feature'); | ||
}); | ||
|
||
test('POSIX', () => { | ||
expect(getModuleFromFilename('/Users/Tim/node_modules/some-dep/module.js', '/Users/Tim/')).toEqual( | ||
'some-dep:module', | ||
); | ||
|
||
expect(getModuleFromFilename('/Users/Tim/some/more/feature.js', '/Users/Tim/')).toEqual('some.more:feature'); | ||
expect(getModuleFromFilename('/Users/Tim/main.js', '/Users/Tim/')).toEqual('main'); | ||
expect(getModuleFromFilenamePosix('/Users/Tim/node_modules/some-dep/module.js')).toEqual('some-dep:module'); | ||
expect(getModuleFromFilenamePosix('/Users/Tim/some/more/feature.js')).toEqual('some.more:feature'); | ||
expect(getModuleFromFilenamePosix('/Users/Tim/main.js')).toEqual('main'); | ||
}); | ||
|
||
test('.mjs', () => { | ||
expect(getModuleFromFilename('/Users/Tim/node_modules/some-dep/module.mjs', '/Users/Tim/')).toEqual( | ||
'some-dep:module', | ||
); | ||
expect(getModuleFromFilenamePosix('/Users/Tim/node_modules/some-dep/module.mjs')).toEqual('some-dep:module'); | ||
}); | ||
|
||
test('.cjs', () => { | ||
expect(getModuleFromFilename('/Users/Tim/node_modules/some-dep/module.cjs', '/Users/Tim/')).toEqual( | ||
'some-dep:module', | ||
); | ||
expect(getModuleFromFilenamePosix('/Users/Tim/node_modules/some-dep/module.cjs')).toEqual('some-dep:module'); | ||
}); | ||
|
||
test('node internal', () => { | ||
expect(getModuleFromFilename('node.js', '/Users/Tim/')).toEqual('node'); | ||
expect(getModuleFromFilename('node:internal/process/task_queues', '/Users/Tim/')).toEqual('task_queues'); | ||
expect(getModuleFromFilename('node:internal/timers', '/Users/Tim/')).toEqual('timers'); | ||
expect(getModuleFromFilenamePosix('node.js')).toEqual('node'); | ||
expect(getModuleFromFilenamePosix('node:internal/process/task_queues')).toEqual('task_queues'); | ||
expect(getModuleFromFilenamePosix('node:internal/timers')).toEqual('timers'); | ||
}); | ||
}); |