Skip to content

Commit e050868

Browse files
committed
fix(importer): support nodejs module default main file "index.js" when "main" is missing in package.json
When "main" is missing in package.json, nodejs uses default main file "index.js", it will complain when default main file is missing. This fix supports nodejs module with missing "main" in package.json. It solves importing issue on nodejs module "date-fns". #831 is related, that PR solves tracing issue on "date-fns".
1 parent dcdd0f1 commit e050868

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/importer/strategies/amodro.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const path = require('path');
66
const amodroTrace = require('../../build/amodro-trace');
77
const cjsTransform = require('../../build/amodro-trace/read/cjs');
88

9+
// nodejs default main is index.js
10+
const DEFAULT_MAIN = 'index.js';
11+
912
let AmodroStrategy = class {
1013

1114
static inject() { return ['package']; }
@@ -16,7 +19,15 @@ let AmodroStrategy = class {
1619

1720
applies() {
1821
if (!this.package.main) {
19-
logger.debug('This package did not specify a "main" file in package.json. Skipping');
22+
let defaultMainPath = path.join(process.cwd(), 'node_modules', this.package.name, DEFAULT_MAIN);
23+
24+
if (fs.existsSync(defaultMainPath)) {
25+
this.package.main = DEFAULT_MAIN;
26+
return true;
27+
}
28+
29+
logger.debug('This package neither specified a "main" file in package.json, ' +
30+
'nor provided default ' + DEFAULT_MAIN + ' file. Skipping');
2031
return false;
2132
}
2233

0 commit comments

Comments
 (0)