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

webpack alias does not work with src="something" #185

Closed
aep opened this issue Oct 4, 2018 · 8 comments
Closed

webpack alias does not work with src="something" #185

aep opened this issue Oct 4, 2018 · 8 comments

Comments

@aep
Copy link

aep commented Oct 4, 2018

Webpack version:

    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.9"

HTML-Loader version:

    "html-loader": "^0.5.5",

Please tell us about your environment:

Linux stark 4.18.9-arch1-1-ARCH #1 SMP PREEMPT Wed Sep 19 21:19:17 UTC 2018 x86_64 GNU/Linux

Current behavior:

<img src="Dashkit/foo.jpg">
  resolve: {
    extensions: ['.ts', '.js', '.vue', '.json', 'scss'],
    alias: {
      'vue$':     'vue/dist/vue.esm.js',
      'Dashkit':  path.join(__dirname, './Dashkit/src/'),
    },
  },
Module not found: Error: Can't resolve './Dashkit/foo.jpg' in '.../src/components'

Expected/desired behavior:

alias should be applied before resolving

@mistic100
Copy link

I think you need to prefix the src with "~", see vuejs/vue-loader#193
It is similiar to how dependencies (S)CSS can be imported.

@aep
Copy link
Author

aep commented Oct 10, 2018

nope. '~/something' will become '/something' without the alias being applied

@mistic100
Copy link

Strange, it works for me

resolve: {
            alias: {
                '@app': path.join(config.dir, './app')
            }
}
<img src="~@app/images/logo.png">

@aep
Copy link
Author

aep commented Oct 10, 2018

i'm loading the html like so.

import template from "./device_details.html";
console.log(template);
module: {
   rules: [
       {
         test: /\.html$/,
         use: [
             'html-loader',
         ]
       },
   ]
}

@MitchTalmadge
Copy link

I can't get this to work in Angular. I found documentation about it in the vue-loader but no code to support the behavior, so I'm not sure how they implemented it. I of course don't use vue-loader since I am using Angular, but it makes me wonder if html-loader has this functionality but it's not documented? (Or I can't find it.)

https://github.com/vuejs/vue-loader/blob/bcee5b9127800070fe5797fe4be466641b88acc8/docs/guide/asset-url.md#transform-rules

@alexander-akait
Copy link
Member

Example above #185 (comment)

@oodzchen
Copy link

I still meet this problem in webpack5, my alias config is

  resolve: {
    modules: ['node_modules', path.resolve(__dirname, '../src')],
    extensions: ['.js', '.json'],
    alias: {
      '@': path.resolve(__dirname, '../src'),
    },
  },

And i use ejs-template-loader with html-loader to handle the html code. The html code is

<img src="@/images/logo.svg">

which cause the error

Module not found: Error: Can't resolve './@/images/logo.svg' in ...

If I change the image path to ../image/logo.png, it works fine. But the alias is more helpful for me to avoid the path converting problem, how can i make it work?

@AlexeyKosov
Copy link

<img src="@/images/logo.svg">
<img src="~@/images/logo.svg">

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

6 participants