Skip to content

Normalize main property - #220

Merged
leo merged 1 commit into
vercel:masterfrom
eliperelman:extensionless-main
Jan 24, 2017
Merged

leo merged 1 commit into
vercel:masterfrom
eliperelman:extensionless-main

Conversation

@eliperelman

@eliperelman eliperelman commented Jan 13, 2017 •

Copy link
Copy Markdown
Contributor

This should ensure that a package.json main entry with or without the .js extension should still be found by the filesystem.

Fixes #2

@rauchg

rauchg commented Jan 13, 2017 •

Copy link
Copy Markdown
Member

Question: does npm handle the case where:

{
  "main": "test"
}

and you have test/index.js ? In that case, this will require stat calls, not just analysis of the string

@eliperelman

Copy link
Copy Markdown
Contributor Author

@rauchg hmm, I believe you are correct. I'll revise soon.

@eliperelman

eliperelman commented Jan 16, 2017 •

Copy link
Copy Markdown
Contributor Author

@rauchg so, I've got another version here, but there's a complication with using stat. Since these files aren't globbed yet, all we have are relative paths until the glob a few lines down. That means we have two options:

  • Glob the package main separate from the rest of the files so we can determine with stat which file to then add to the search. Would add a bit of complexity to the searching part, but easily doable.
  • Sniff the string for an extension, and if its missing, add an extension-ed main and an indexed main to the list of paths to search. This keeps the searching simpler at the cost of adding an extra path to search which may not turn anything up.

At the moment I've opted for the second option which is in the current commit. If you would still prefer to glob and stat separately from the other files as a special case as in the first option, let me know and I will make the change. Thanks!

@lukeed

lukeed commented Jan 20, 2017

Copy link
Copy Markdown
Contributor

You can use require.resolve which handles direct & implicit index paths.

require.resolve('./src/scripts/app')
//=> '/Users/.../src/scripts/app.js'
require.resolve('./src/scripts/views')
//=> '/Users/.../src/scripts/views/index.js'

@eliperelman

Copy link
Copy Markdown
Contributor Author

@lukeed great idea!

@eliperelman

eliperelman commented Jan 20, 2017 •

Copy link
Copy Markdown
Contributor Author

I think the require.resolve approach would work if the current working directory was relative to the file we were working in, but that's not the case. In order to use that, we need to resolve based on the path parameter, and once that's done, there's not much point to adding it to the _search collection, so this would probably need to be pushed separately, something like:

const search = Array.prototype.concat
  .apply([], (await Promise.all(search_
    .map(file => glob(file, {cwd: path, absolute: true, dot: true})))))

if (pkg.main) {
  search.push(require.resolve(resolve(path, pkg.main)));
}

Essentially, resolving the file path of pkg.main will return our absolute path, negating the need to push it into _search for globbing.

@leo leo changed the title Normalize package.json main entry to .js file before file search, fixes #2 Normalize main property Jan 24, 2017
@leo
leo merged commit 6e62d87 into vercel:master Jan 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants