-
Notifications
You must be signed in to change notification settings - Fork 243
Access components from common root #336
Comments
@ctrlplusb I don't see any ref to it in the FAQ as you had suggested. Is there any concrete example or just these clues? |
If I want to use @bradennapier's solution, which file should that snippet go in? |
@bradennapier You posted that you had IDE tooling to inject relative paths. Do you know of any such plugin for Atom? |
Hey Bryan, resolve: merge(
// These extensions are tried when resolving a file.
{
extensions: config.bundleSrcTypes.map(ext => `.${ext}`)
},
// Add to resolve modules to reduce ../ overhead
{
modules: [ './shared', path.resolve(appRootDir.get(), 'src/shared'), 'node_modules' ],
plugins: [
componentResolver('existing-directory', 'undescribed-raw-file')
]
},
// Within aliases we allow specifying ~path to make it relative
// to the appRootDir. This way we don't need to have 'path' within
// the config file.
ifElse(config.webpack_aliases)(() => ({
alias: Object.keys(config.webpack_aliases).reduce((p, c) => {
const a = config.webpack_aliases[c]
if ( a.startsWith('~') && ! a.startsWith('~/') ) {
p[c] = path.resolve(appRootDir.get(), a.replace('~', ''))
} else { p[c] = a }
return p
}, {})
}))
), I am actually not using the aliases anymore as this method of resolving seems to work perfectly for me. I still have the above available in my configFactory so that I can add aliases anytime I want. This method allows me to include "shared" components at different hierarchy levels which is pretty awesome. For example, given this directory structure: Within ProjectDashboard or ProjectSelect I can do import MyComponent from 'components/MyComponent' whereas ALL screens can access import Button from 'components/Button/Modern' obviously you have to make sure you don't create clashes which shouldn't generally be an issue since "shared" components should be used as little as possible. Note: I added the component resolver as well. This essentially allows me to make it resolve {DIR}/{DIR}.js so if I have |
I do not need or inject any kind of relative paths. My setup as described appears to do the job as needed. |
I will have to give consideration to your approach. Thanks for all the info. Is there any simple edit to what you just posted that would allow this work in any folder? I assume a 'dirty' hack would be to place the entire project in |
This sets the entire root "shared" directory as a relative path as well as the trickle-down effect. So for example, I can do things like You do have to be careful though because it can definitely cause conflicts. For example if I had a redux/index in the project then any redux imports would be problematic as it would override them. I haven't converted yet but my plan is to use capitalized directories locally (Redux, Sagas, Types, Reducers) which should avoid most clashes with |
I see. That means both approaches work together. Will implement your code. Thanks! |
@bradennapier how did you make your resolve modules work with server side rendering? I get "cannot find module" errors if I try to use that approach... |
@smirea I believe these are the key pieces to the puzzle
I had the same issue originally. The key is using the relative shared and the absolute shared. Once I had both in there it works across the board both server and client and has been excellent. |
is the resolve: merge(
// These extensions are tried when resolving a file.
{
extensions: config.bundleSrcTypes.map(ext => `.${ext}`)
},
// Add to resolve modules to reduce ../ overhead
{
modules: [ './shared', path.resolve(appRootDir.get(), 'src/shared'), 'node_modules' ],
},
), and it still fails. EDIT: If so, how do you have it configured? |
I don't think it should be, that is for a different purpose, although it's a very nice plugin. Have you restarted your entire process? |
yep, i'm doing Everything works fine if I disable server side rendering, so this is exclusively an issue within the node env. You sure you're also not setting custom Also, how is your |
Its just https://github.com/mgcrea/component-webpack-resolver-plugin which allows me to have a better organizational structure. Are you including "shared" when you try to import? Odd that it is working here. Maybe its something new in Node? I'm on 7.4
import MyComponent from 'components/MyComponent' |
I have made quite a few modifications to how things work so i'm looking to see if I added something else to make it work, but I'm pretty sure that was all I did on that regard. |
|
Ahh I think I know the issue, you can not use the relative paths until you are in the es6 environment. I don't use the resolution technique anywhere but in the main shared folder. |
Closing this for now. |
@bradennapier @smirea @bryaan Where did you folks end up with this? What all did you folks end up modifying or adding to make this work? |
I've changed import appRootDir from 'app-root-dir';
const values = {
// ...
webpackConfig: (webpackConfig, buildOptions) => {
// eslint-disable-next-line no-unused-vars
const { target, mode } = buildOptions;
// Example:
/*
if (target === 'server' && mode === 'development') {
webpackConfig.plugins.push(new MyCoolWebpackPlugin());
}
*/
// Debugging/Logging Example:
/*
if (target === 'server') {
console.log(JSON.stringify(webpackConfig, null, 4));
}
*/
webpackConfig.resolve.modules = ['node_modules', appRootDir.get()];
return webpackConfig;
},
}; Then for other cli things I've used "scripts": {
// ...
"test": "cross-env NODE_PATH=./ jest"
} After this changes applied you can do something like (and yes, it would work everywhere, even outside webpack, if you won't forget to update all the cli commands): import config from 'config';
import { Button } from 'shared/components/ui';
// ... In my project I've renamed Btw., probably it would be better to read the |
actually, if we will read @ctrlplusb what do you think about this? |
Yep. It should work everywhere
On Oct 24, 2017 03:27, "Anuj" <[email protected]> wrote:
@SleepWalker <https://github.com/sleepwalker> With that configuration, are
you able to access paths in shared/app even when in files in server or
client?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#336 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACa11GS6We3NjqnLZbOolhIUFHDaLIBtks5svS8PgaJpZM4Lo3mz>
.
|
Directory Structure:
In my
About/index.js
file I want to do:instead of:
Which can quickly become a problem for large apps requiring multiple walks back up the directory tree.
How can I set this up?
The text was updated successfully, but these errors were encountered: