Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to have image paths resolved using webpack's resolve.alias? #20

Open
oller opened this issue Apr 4, 2016 · 6 comments
Open

Comments

@oller
Copy link

oller commented Apr 4, 2016

It doesn't look like this is currently supported by the loader, but please correct me if I'm wrong!

In my webpack.config.js

  resolve: {
        alias: {
            // Allow friendly reference to core modules
            "core": path.resolve(config.root, "../core")
        }
    },

https://webpack.github.io/docs/configuration.html#resolve-alias

I'm aliasing core to a path, so I can have shorter friendlier paths to the core across my application, this works fine in my js files.

As it stands my images still need relative paths, to reference common assets in the core:

<img src="../../../core/symbols/image.svg">

If the resolve.alias were factored into path resolving, this could been simplified to:

<img src="core/symbols/image.svg">

Would this be possible?

@emaphp
Copy link
Owner

emaphp commented Apr 4, 2016

Hi. Replacing paths with elements in the resolve object is not yet a feature of this loader. For now, paths must be relative to the corresponding image. I'll look into this subject bacause it could be interesting to have.

@vladimiry
Copy link

vladimiry commented Jul 21, 2016

I have such webpack resolve config section defined

        resolve: {
            alias: {
                _common_templates: LIBS.path.join(metadata.paths.srcCustomer, 'templates')
            }
        }

I then do including @include('_templates/footer.html') / @include('~_templates/footer.html') and I'm getting error:

Module not found: Error: Cannot resolve 'file' or 'directory' ./_templates/foo  ter.html in

So aliases have not taken into account and unfortunately it makes underscore-template-loader absolutely not usable in my case.

@vladimiry
Copy link

vladimiry commented Jul 21, 2016

Though I was wrong, resolving happens when I do @include('~_templates/footer.html'), but I get this code injected into the html page instead of the just footer code:

This is how generated html source looks
screenshot from 2016-07-21 14-19-48

I use it with html-webpack-plugin.

PS. I ended up with such form of insertion <%= require('_common_templates/footer.html')({someValue: 123}) %> which works well enough.

@emaphp
Copy link
Owner

emaphp commented Jul 26, 2016

Just to let you guys know: I'm actually working on this. The main problem here is that webpack doesn't provide a straightforward way to obtain the current configuration file. This is currently the implementation of the require macro:

module.exports = {
    require: function (resourcePath) {
        return "require(" + JSON.stringify(loaderUtils.urlToRequest(resourcePath)) + ").apply(null,arguments)";
    },

   //...
};

As you may already figured out, there's no smart postprocessing here. If there's an alias defined in your configuration file, the loader will just ignore it.
My idea is to add a hack to parse the configuration file at startup and then call the macro using that configuration object as its context. And yes, I'm using the word 'hack' unironically, webpack documentation is not a lot of help on this subject.

@dmackerman
Copy link

dmackerman commented Oct 12, 2016

Have you tried using the ~ character in your paths to respect aliases? ie:

<img src="~images/something.png" />

This works for us. We're using Webpack 2.

@itsashis4u
Copy link

@dmackerman Didn't work for me in the .js file, worked in .scss file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants