Skip to content

Commit

Permalink
fix(importer): support nodejs module default main file "index.js" whe…
Browse files Browse the repository at this point in the history
…n "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".
  • Loading branch information
3cp committed Mar 10, 2018
1 parent dcdd0f1 commit e050868
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/importer/strategies/amodro.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const path = require('path');
const amodroTrace = require('../../build/amodro-trace');
const cjsTransform = require('../../build/amodro-trace/read/cjs');

// nodejs default main is index.js
const DEFAULT_MAIN = 'index.js';

let AmodroStrategy = class {

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

applies() {
if (!this.package.main) {
logger.debug('This package did not specify a "main" file in package.json. Skipping');
let defaultMainPath = path.join(process.cwd(), 'node_modules', this.package.name, DEFAULT_MAIN);

if (fs.existsSync(defaultMainPath)) {
this.package.main = DEFAULT_MAIN;
return true;
}

logger.debug('This package neither specified a "main" file in package.json, ' +
'nor provided default ' + DEFAULT_MAIN + ' file. Skipping');
return false;
}

Expand Down

0 comments on commit e050868

Please sign in to comment.