Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for webpack.config.js resolve.alias (symlinks) #237

Closed
wants to merge 1 commit into from
Closed

Add support for webpack.config.js resolve.alias (symlinks) #237

wants to merge 1 commit into from

Conversation

matrinox
Copy link

@matrinox matrinox commented Feb 1, 2015

No description provided.

Add note about webpack.config.js resolve.alias
Check for webpack alias before trying to resolve with it
Add specs for webpack config alias support
@jeffmo
Copy link
Contributor

jeffmo commented Feb 11, 2015

Thanks for sending this! A thought:

I know others have wanted the ability to intervene with the module name -> module path lookup process as well; Would it be possible to generalize the module-alias-lookup code into a configurable plugin (rather than building webpack-specific support directly into the module loader)? We've done this in Jest in a few other places and its worked out pretty well. For example: https://github.com/facebook/jest/blob/master/src/TestRunner.js#L139

Specifically if we could move the existing logic in HasteModuleLoader.p._moduleNameToPath out to another module, then callsites for that function could be updated to do something like var moduleNameToPath = require(config.moduleToPathResolver) and it would be possible to build a webpack plugin (as well as any other arbitrary plugin as others have requested) that does alias resolution in general way.

Although we should probably keep the current lookup algorithm as the default, I'd be happy to take the webpack plugin you've essentially written here and include it so it ships with Jest such that webpack alias support is only a minor config-entry away.

@matrinox
Copy link
Author

Your idea is sound, that should be the end goal here. For now, I do think that many would be happy to have even basic webpack support.

@andreyvital
Copy link

Ya ya ya ya ya... news?

👍 💯

@kellyrmilligan
Copy link

does this help with #134 ?

@matrinox
Copy link
Author

@kellyrmilligan It works fine on my end, I use webpack aliases, tests run perfectly without any additional configuration

@kellyrmilligan
Copy link

Is that recent? I'm having issues as mentioned in 134, I have a non-common js module from a third party lib that I'm aliasing into a component and I get a require error saying it can't reuire it in.

@matrinox
Copy link
Author

I haven't tested non-common js modules. Feel free to try that out @kellyrmilligan. All the code does is allow Jest to replace require('...') strings with the respective webpack alias

@codegoat
Copy link

@matrinox curious why the setting of hasWebpackAliases is done within the catch?

I have used this to allow for use of aliases with Jest (thank you for this btw), but the catch is never hit.

hasWebpackAliases is always undefined.

var aliases = Object.keys(aliasesMap);
for (var i = 0; i < aliases.length; i++) {
var alias = aliases[i];
if (moduleName.indexOf(alias + '/') === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the require of the alias does not have a / at the end?

// in the webpack.config.js (resolve.alias)
'some-alias-module': '/absolute/path/to/module'
// somewhere in your code require'n the above alias'd module
var some = require('some-alias-module');

I changed this line to:

if (moduleName.indexOf(alias + '/') === 0 || moduleName === alias) {

@matrinox
Copy link
Author

@codegoat It's been a while now but I think I did that because some people don't have a webpack config path and I didn't bother adding in a way to specify that you want webpack support so it had to be flexible.
If hasWebpackAliases is undefined it means that you didn't export resolve.alias in your webpackConfig file

@cpojer
Copy link
Member

cpojer commented Nov 18, 2015

We now have documentation on how to integrate jest with webpack. For aliases, there is a moduleNameMapper config option. I don't think this particular PR is still relevant.

See #599 for the work tracking node-haste2. There'll be more features to better integrate jest with webpack in the near future.

@cpojer cpojer closed this Nov 18, 2015
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants