Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

feat: use NODE_PATH for root directory(fix #396) #516

Merged
merged 1 commit into from
Dec 17, 2018
Merged

feat: use NODE_PATH for root directory(fix #396) #516

merged 1 commit into from
Dec 17, 2018

Conversation

yaodingyd
Copy link
Contributor

Description

Add a babel plugin to use NODE_PATH for root

Review

  • Check the copy
  • ...
  • ...

Pre-merge checklist

  • ...
  • ...

Screenshots

Before After
Image Image

@pedronauck
Copy link
Member

Sorry dude, but I don't think that this is the best strategy for NODE_PATH. You can simply put the resolved NODE_PATH on the resolve.modules on webpack instead of using some new babel plugin.

@yaodingyd
Copy link
Contributor Author

@pedronauck like the updated?

@pedronauck
Copy link
Member

pedronauck commented Dec 17, 2018

you need to set some additional code:

  1. change populateNodePath on this file to:
const populateNodePath = () => {
  // We support resolving modules according to `NODE_PATH`.
  // It works similar to `NODE_PATH` in Node itself:
  // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
  // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
  // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
  // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
  // We also resolve them to make sure all tools using them work consistently.
  envDotProp.set(
    'node.path',
    envDotProp
      .get('node.path', '')
      .split(path.delimiter)
      .filter((folder: any) => folder && !path.isAbsolute(folder))
      .map((folder: any) => path.resolve(root, folder))
      .join(path.delimiter)
  )
}
  1. then set resolve.modules like that:
config.resolve.modules
    // prioritize our own
    .add(paths.ownNodeModules)
    .add(paths.appNodeModules)
    .add('node_modules')
    .add(srcPath)
    .add(paths.root)
    .merge([
      envDotProp
        .get('node.path')
        .split(path.delimiter)
        .filter(Boolean),
    ])

And don't forget to submit your pull request on IssueHunt
https://issuehunt.io/repos/125596489/issues/396

use resolve.modules to add NODE_PATH
@yaodingyd
Copy link
Contributor Author

@pedronauck thanks! updated

@pedronauck
Copy link
Member

thanks dude 🙏

@pedronauck pedronauck merged commit cc86f93 into doczjs:master Dec 17, 2018
@yaodingyd
Copy link
Contributor Author

thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants