Rombelirk
Rombelirk

Reputation: 11

How to shorten the absolute path when using webpack v.2?

I want to use absolute path resolving rules in my project, but when I import a module like this

import Component from "/home/components/Component.js"

it cannot be resolved by webpack, only this works:

 import Component from "/users/username/home/components/component.js"

I tried to specify context in my webpack.config, but it made no sense:

context: __dirname

Since I work on windows how do I change my webpack.config to be able to import modules like in the first snippet? In other words how to change my absolute path root for webpack v.2?

Upvotes: 0

Views: 562

Answers (1)

Tom Van Rompaey
Tom Van Rompaey

Reputation: 3586

You can use resolve.alias to shorten import paths:

webpack.config.js

...
resolve: {
  alias: {
    home: '/users/username/home/',
  },
},
...

Then you will be able to import a module like this:

import Component from "home/components/Component.js"

More information on the official documentation: https://webpack.js.org/configuration/resolve/

Upvotes: 1

Related Questions