Webpack-encore: webpack resolve alias

Created on 20 Jun 2017  路  5Comments  路  Source: symfony/webpack-encore

I need to define a resolve alias see
https://webpack.js.org/configuration/resolve/

To use the following plugin
https://github.com/RobinHerbots/Inputmask#selecting-the-dependencylib

How can i do this?

Most helpful comment

Hi
I have done something like:

// export the final configuration
let config = Encore.getWebpackConfig();
config.resolve.alias = {
    'local': path.resolve(__dirname, './resources/src')
};
module.exports = config;

My use case was only to get relative modules aliased to something else, later dont need to use the dot syntax import './xyz'

Basically, assign built condifuration to a variable and change it before exporting it.

Hope it helps,
cheers

All 5 comments

Hi
I have done something like:

// export the final configuration
let config = Encore.getWebpackConfig();
config.resolve.alias = {
    'local': path.resolve(__dirname, './resources/src')
};
module.exports = config;

My use case was only to get relative modules aliased to something else, later dont need to use the dot syntax import './xyz'

Basically, assign built condifuration to a variable and change it before exporting it.

Hope it helps,
cheers

This is correct! I've added an issue to document this: https://github.com/symfony/symfony-docs/issues/8067. It feels a bit odd to need to "dig into" the webpack configuration like this... but there's no value to adding a helper function to Encore, which just does this exact thing. It does, however, need to be easy to find how to do in the docs :)

Thanks for the issue!

@davidmpaz it works thx

In my case "./" before the path caused problems.
After removing it from the path string, Webpack finally resolved the alias!! 馃帀

let config = Encore.getWebpackConfig();
config.resolve.alias["~"] = path.resolve(__dirname, 'assets/js');
module.exports = config;

And then just like this:

import Header from '~/components/Header.vue'

More Info here:
https://webpack.js.org/configuration/resolve/
https://symfony.com/doc/current/frontend/encore/advanced-config.html

If someone has this problem in a more modern version of Webpack you can just use:

Encore
// ...
.addAliases({
  '~': path.resolve(__dirname, 'assets/js')
})
// ...
;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Growiel picture Growiel  路  3Comments

zek0faws picture zek0faws  路  4Comments

EliuTimana picture EliuTimana  路  4Comments

fanchyfanch picture fanchyfanch  路  3Comments

wenmingtang picture wenmingtang  路  4Comments