-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
webpack doesn't resolve relative to resolve.root
#238
Comments
This situation is actually why I wrote the Webpack resolver. It works for me. Can you provide more details about what exactly isn't working? Or a toy test case that fails? Some info that might be helpful:
(BTW: if you're using Sublime, see the end of the README.) |
For reference, here are the existing resolve.root tests: https://github.com/benmosher/eslint-plugin-import/blob/master/resolvers/webpack/test/root.js If you could write a failing test, that would be fantastic. 😄 |
closing for now, if you can provide a failing test case we can reopen? |
ok, I have similar problem, First I tried to add in .eslintrc
and in my serverDev.js I have:
Since first approach was still producing lint errors I tried:
In both cases I get "Unable to resolve path to module" I made this repo to illustrate problem. linting src/test2/index shoudl not produce error since src is root |
Thanks for the test repo, will make identifying the issue a lot simpler. I'll take a look! |
I'm also seeing this problem. Details from my setup follow, in case they help. From "plugins": [
"babel",
"import",
"react"
],
"settings" : {
"import/resolver": {
"webpack": {
"config": "webpack.config.js"
}
}
}, Then resolve: {
root: [
path.resolve(__dirname),
],
alias: {
charts: 'public/charts',
components: 'public/components',
jquery: 'jquery/dist/jquery.slim',
uikit: 'uikit/js/uikit',
},
modulesDirectories: [
'bower_components',
'node_modules',
'web_modules',
],
}, This enables me in my tests to do something like this: import PrimaryNavigation from 'components/common/PrimaryNavigation'; The tests run properly, which suggests that the webpack config is correct. Please let me know if there is any other information I can provide to help. Thanks! |
@chyzwar what is compiling your webpack.js? If the config is set to exports.default, that might explain why it isn't working. If you're using Babel 5, it might be fine. Not obvious to me from the package, though. @ravasthi; can you paste your full Webpack config in a gist and link it? I'm curious if something similar is going on. |
@benmosher Here's a gist with my .eslintrc.json, webpack.config.js, as well as an abbreviated package.json with versions of all the applicable libraries. |
I have the same bug. Here is a gist of my webpack.config.js: |
@moduv5, following @benmosher's comment on my Gist—which I tried and had work—you'll have to do a single |
Also, @benmosher, it looks like this issue can be solved by updating the documentation with an ES6 example containing the explicit module.exports assignment at the end? I'd be happy to take a crack at this if you like. |
I am using babel-node, I try will change syntax in webpack.js to es5. 2016-05-03 2:08 GMT+01:00 Richa Avasthi [email protected]:
|
I don't think you have to change the syntax of the whole file; just change the export line to be the ES5 style. Richa
|
@ravasthi I've reviewed your gist and the suggestions following it, but still no dice. Here's my gist: https://gist.github.com/tmax898/2dcc6b3d51dec76f80a604b607be88c0 I have /public/js in my root, so i would expect to be able to reference files from there on. Thanks in advanced for the help. |
@tmax898 here are a few things I noticed (bear with me, I'm not a webpack expert):
|
@ravasthi very interesting. I didn't realize the resolver required resolve.root. Sounds like a bug! |
@benmosher I probably shouldn't have said that so definitely, but I do know from my experimentation that if I took the resolve.root setting out, things stopped working with the rest of my setup. Previously I had just added resolve.alias, and that didn't work. Richa
|
@ravasthi After changing export to commonjs style, plugin correctly pick up root. I think that plugin should check if it can read resolve part. At the moment plugin check only if file exists. I want to use es modules only, How can I specify root in .eslintrc instead? |
@ravasthi i went through and followed alll your recommendations, but now webpack is not able to find any of the files by alias. I updated my gist if you want to review the syntax i used. I started by following your syntax very closely, but comparing it to the documentation from webpack, i'm am surprised your's builds. I'm not saying your wrong by any means. Just curious how you use your aliases in you code. Thanks again for your help. |
Do you have a minimal project illustrating the problem? As for how it works for me, my webpack config is exactly as in the gist I posted earlier, and I use aliases by doing, e.g.: import PrimaryNavigation from 'components/common/PrimaryNavigation'; from |
I'm going to close this as the initial issue seems to be resolved. I will gladly review any improved documentation PRs you all would be up to submit. 😄 |
What was the conclusion? I'm running into the same issue. My webpack config is (it's not used directly by webpack, but by a wrapper arounf webpack, and this might cause the issue) // ./webpack.json
{
"root": "src",
"devServer": {
"host": "localhost"
}
} and my sublime config is {
"folders": [
{
"path": "."
}
],
"SublimeLinter": {
"linters":
{
"eslint":
{
"chdir": "${project}"
}
}
}
}
I tried a mix of "settings" : {
"resolve.root": "src",
}, "settings" : {
"import/resolver": {
"resolve.root": "src",
},
}, "settings" : {
"import/resolver": {
"webpack": {
"config": "webpack.json",
},
},
}, but none worked |
@gsabran the config needs to have "root" as a subkey of "resolve", i.e. { "resolve": { "root": "src" } } The Webpack resolver currently expects the config to match Webpack 1.x's format. See the Webpack docs for more details. |
@ravasthi @benmosher i forgot to thank you guys for the help and the solutions did work. |
Took a while to hunt down why my config wasn't working and figured out that I needed to add this dependency only after reading through [import-js#238](import-js#238) and noticing [this line in a linked example](https://gist.github.com/ravasthi/abcfee465411fc45a8bc28decb9d8e5e#file-package-json-L24). I think adding this line may add some clarity and save time for several other webpack / eslint noobs (like me).
Took a while to hunt down why my config wasn't working and figured out that I needed to add this dependency only after reading through [#238](#238) and noticing [this line in a linked example](https://gist.github.com/ravasthi/abcfee465411fc45a8bc28decb9d8e5e#file-package-json-L24). I think adding this line may add some clarity and save time for several other webpack / eslint noobs (like me).
webpack lets us define a value for
resolve.root
, and we can import modules relative to the root directly.Eg:
File: webpack.config.js
File: /jsx/components/Button.jsx
File: /jsx/Some/Deep/Nested/Dir/Component.jsx
can be written as
File: /jsx/Some/Deep/Nested/Dir/Component.jsx
Since webpack config has defined a value for
resolve.root
, I can directly import from 'components/Button' rather than from '../../../../components/Button'. This syntax is super helpful when dealing with deep nested directory structures. It would be great if the webpack resolver package could support it.Thanks!
The text was updated successfully, but these errors were encountered: