Skip to content

Commit a2f1d8b

Browse files
Merge pull request #275 from Distributive-Network/wes/module-ext-collsion-247
Make .py CommonJS modules have precedence over .js modules when there…
2 parents 41a1d1d + 82fe284 commit a2f1d8b

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

python/pythonmonkey/require.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,13 @@ def _createRequireInner(*args):
299299
module.paths.push(path + '/node_modules');
300300
module.require.path.push(python.pythonMonkey.dir + '/builtin_modules');
301301
module.require.path.push(python.pythonMonkey.nodeModules);
302+
303+
/* Add a .py loader, making it the first extension to be enumerated so py modules take precedence */
304+
const extCopy = Object.assign({}, module.require.extensions);
305+
for (let ext in module.require.extensions)
306+
delete module.require.extensions[ext];
302307
module.require.extensions['.py'] = loadPythonModule;
308+
Object.assign(module.require.extensions, extCopy);
303309
304310
if (isMain)
305311
{

tests/js/collide.simple

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @file collide.simple
3+
* Test to ensure require module identitifier collision that resolves both py and js
4+
* modules correctly prefers py modules.
5+
* @author Wes Garland <[email protected]>
6+
* @date Mar 2024
7+
*/
8+
9+
const { which } = require('./modules/collide')
10+
const whichjs = require('./modules/collide.js').which;
11+
const whichpy = require('./modules/collide.py').which;
12+
13+
if (which !== 'python')
14+
throw new Error(`python module was not preferred, got ${which} instead`)
15+
16+
if (whichpy !== 'python')
17+
throw new Error(`python module was not explicitly loaded, got ${whichpy} instead`)
18+
19+
if (whichjs !== 'javascript')
20+
throw new Error(`javascript module was not explicitly loaded, got ${whichjs} instead`)

tests/js/modules/collide.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.which = "javascript"

tests/js/modules/collide.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports['which']="python"

0 commit comments

Comments
 (0)