oorst
oorst

Reputation: 979

Define import path at compile time with webpack define plugin

I'm trying to define the path of an import statement at compile time in a webpack build. The path is defined by a configuration file that can change with every build.

I'm using DefinePlugin, but without any luck so far:

plugins: [
  new webpack.DefinePlugin({
    __PATH__: JSON.stringify('./path/to/src')
  })
]

And then in compiled code:

import Foo from __PATH__

Running webpack then results in a Module parse failed: Unexpected token error, obviously because __PATH__ is not a valid path string. I expected that the __PATH__ token would be replaced with the string from the DefinePlugin options.

Is this sort of thing possible? Am I going about it the right way?

Upvotes: 1

Views: 383

Answers (1)

oorst
oorst

Reputation: 979

The best way to do this is with an alias:

resolve: {
  alias: {
    ‘@alias’: ‘./path/to/arc‘
  }
}

Upvotes: 1

Related Questions