Skip to content

Commit 2fb4de8

Browse files
committed
Fix module resolution failure when outside main module
We now depend on `require.resolve` instead of `require.main`. Closes #30
1 parent 9bb47b3 commit 2fb4de8

6 files changed

+27
-38
lines changed

lib/construction/strategy/ConstructionStrategyCommonJs.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ export class ConstructionStrategyCommonJs implements IConstructionStrategy<any>
2929
try {
3030
object = this.requireCurrentRunningModuleIfCurrent(options.moduleState, options.requireName);
3131
} catch {
32-
if (!this.req.main) {
33-
throw new Error(`Corrupt Node.js state: Could not find a main module.`);
34-
}
3532
// Always require relative from main module, because Components.js will in most cases just be dependency.
36-
object = this.req.main.require(options.requireName.startsWith('.') ?
33+
object = this.req(options.requireName.startsWith('.') ?
3734
Path.join(process.cwd(), options.requireName) :
38-
options.requireName);
35+
this.req.resolve(options.requireName, { paths: [ options.moduleState.mainModulePath ]}));
3936
}
4037

4138
// Determine the child of the require'd element

test/integration/instantiateFile-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('construction with component configs as files', () => {
1414
let manager: ComponentsManager<any>;
1515
beforeEach(async() => {
1616
moduleState = <any> {
17-
mainModulePath: __dirname,
17+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
1818
importPaths: {
1919
'http://example.org/': `${__dirname}/../`,
2020
},

test/integration/instantiateResourceConfigComponent-test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Path from 'path';
12
import type { Resource, RdfObjectLoader } from 'rdf-object';
23
import { mocked } from 'ts-jest/utils';
34
import { ComponentsManager } from '../../lib/ComponentsManager';
@@ -19,8 +20,10 @@ describe('construction with component configs as Resource', () => {
1920
let settings: IConstructionSettings;
2021
beforeEach(async() => {
2122
manager = await ComponentsManager.build({
22-
mainModulePath: __dirname,
23-
moduleState: <any> {},
23+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
24+
moduleState: <any> {
25+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
26+
},
2427
async moduleLoader() {
2528
// Register nothing
2629
},

test/integration/instantiateResourceConfigComponentMapped-test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Path from 'path';
12
import type { Resource, RdfObjectLoader } from 'rdf-object';
23
import { mocked } from 'ts-jest/utils';
34
import { ComponentsManager } from '../../lib/ComponentsManager';
@@ -21,8 +22,10 @@ describe('construction with mapped component configs as Resource', () => {
2122
let settings: IConstructionSettings;
2223
beforeEach(async() => {
2324
manager = await ComponentsManager.build({
24-
mainModulePath: __dirname,
25-
moduleState: <any> {},
25+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
26+
moduleState: <any> {
27+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
28+
},
2629
async moduleLoader() {
2730
// Register nothing
2831
},

test/integration/instantiateResourceConfigRaw-test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Path from 'path';
12
import type { RdfObjectLoader } from 'rdf-object';
23
import { ComponentsManager } from '../../lib/ComponentsManager';
34
import type { IConfigConstructorPool } from '../../lib/construction/IConfigConstructorPool';
@@ -19,8 +20,10 @@ describe('construction with component configs as Resource', () => {
1920
let settings: IConstructionSettings;
2021
beforeEach(async() => {
2122
manager = await ComponentsManager.build({
22-
mainModulePath: __dirname,
23-
moduleState: <any> {},
23+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
24+
moduleState: <any> {
25+
mainModulePath: Path.posix.join(__dirname, '../../__mocks__'),
26+
},
2427
async moduleLoader() {
2528
// Register nothing
2629
},

test/unit/construction/strategy/ConstructionStrategyCommonJs-test.ts

+9-26
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,15 @@ describe('ConstructionStrategyCommonJs', () => {
4848
if (path === 'mainmodulepath/unknown.js') {
4949
return;
5050
}
51-
return true;
52-
});
53-
req.main = <any> {
54-
require(path: string) {
55-
if (path === 'othermodule') {
56-
return requireOther;
57-
}
58-
if (Path.join(process.cwd(), 'myfile.js')) {
59-
return requireFile;
60-
}
61-
throw new Error(`Main require not found for ${path}`);
62-
},
63-
};
51+
if (path === 'othermodule') {
52+
return requireOther;
53+
}
54+
if (Path.join(process.cwd(), 'myfile.js')) {
55+
return requireFile;
56+
}
57+
throw new Error(`Require not found for ${path}`);
58+
});
59+
req.resolve = <any> ((arg: string) => arg);
6460
moduleState = {
6561
componentModules: {},
6662
contexts: {},
@@ -175,19 +171,6 @@ describe('ConstructionStrategyCommonJs', () => {
175171
})).toThrow(new Error('Failed to get module element a.X from module currentmodule'));
176172
});
177173

178-
it('without requireElement and constructor in another module with undefined req.main should throw', () => {
179-
req.main = undefined;
180-
expect(() => constructionStrategy.createInstance({
181-
settings,
182-
moduleState,
183-
requireName: 'othermodule',
184-
requireElement: undefined,
185-
callConstructor: false,
186-
args: [],
187-
instanceId: 'myinstance',
188-
})).toThrow(new Error(`Corrupt Node.js state: Could not find a main module.`));
189-
});
190-
191174
it('with requireElement to class and without constructor in the current module', () => {
192175
expect(constructionStrategy.createInstance({
193176
settings,

0 commit comments

Comments
 (0)