1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Better Module Imports With Aliases as a webpack til

This commit is contained in:
jbranchaud
2016-04-05 09:41:32 -05:00
parent 775b6c0b1e
commit b50ff62bc7
2 changed files with 42 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
# Better Module Imports With Aliases
Depending on how your JavaScript project is structured, you can end up with
import statements that look like this:
```javascript
import SomeComponent from 'app/assets/javascripts/components/SomeComponent.jsx';
```
or this:
```javascript
import SomeComponent from '../components/SomeComponent.jsx';
```
The first is simply too long and the second is both ugly and brittle to
changes in file location. This can all be _resolved_ with a
[Webpack](https://webpack.github.io/) alias.
```
// webpack.config.js
resolve: {
alias: {
components: "app/assets/javascripts/components",
},
},
```
Webpack will use this `alias` when resolving module imports like the
following updated example:
```javascript
import SomeComponent from 'components/SomeComponent.jsx';
```
See the
[`resolve.alias`](https://webpack.github.io/docs/configuration.html#resolve-alias)
section of the Webpack docs for more details.
h/t Vidal Ekechukwu