question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a vue-cli resolve-url-loader example

See original GitHub issue

What problem does this feature solve?

Looking for similar things in the issues, the only one I could find was this one https://github.com/vuejs/vue-cli/issues/1019.

I’ll use font awesome to illustrate :

This is the project structure :

/node_modules/font-awesome

/src/folder/MyComponent.vue

This style bloc in MyComponent.vue:

...
<style lang="scss">
@import '~font-awesome/scss/font-awesome';
//...
</style>

Throws this error :

These relative modules were not found:

* ../fonts/fontawesome-webfont.eot in ./node_modules/css-loader??ref--8-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/lib??ref--8-oneOf-1-2!./node_modules/sass-loader/lib/l
oader.js??ref--8-oneOf-1-3!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/folder/MyComponent.vue?vue&type=style&index=0&lang=scss

EDIT : Here’s a reproduction repository : https://github.com/Hebilicious/vue-cli-relative-url-import-issue-test The font files are relative to ‘~font-awesome/scss/font-awesome’, but webpack tries to resolve them relatively to ‘MyComponent.vue’.

https://github.com/bholloway/resolve-url-loader solve this problem elegantly, but it needs to be ‘placed’ directly right after css loader :

module.exports = {
  module: {
    loaders: [
      {
        test   : /\.css$/,
        loaders: ['style-loader', 'css-loader', 'resolve-url-loader']
      }, {
        test   : /\.scss$/,
        loaders: ['style-loader', 'css-loader', 'resolve-url-loader', 'sass-loader?sourceMap']
      }
    ]
  }
};

According to https://cli.vuejs.org/guide/webpack.html#modifying-options-of-a-loader

TIP

For CSS related loaders, it’s recommended to use css.loaderOptions instead of directly targeting loaders via chaining. This is because there are multiple rules for each CSS file type and css.loaderOptions ensures you can affect all rules in one single place.

I don’t see any straightforward way to do it for all the styling related rules for vue cli with chain webpack.

There’s a possible (very) dirty workaround, which is to modify @vue/cli-service/lib/config.css.js :

module.exports = (api, options) => {
   //...
    function createCSSRule (lang, test, loader, options) {
      //...
      function applyLoaders (rule, modules) {
       //...
        rule
          .use('css-loader')
          .loader('css-loader')
          .options(cssLoaderOptions)

       //Add resolve-url-loader here

        rule
          .use('resolve-url-loader')
          .loader('resolve-url-loader')
          .options({ //... })

Other ways to go around the problem is to actually have the assets at the relative path file from your source files, or to modify the font-awesome import statements, which isn’t ideal.

What does the proposed API look like?

If there’s a way to do it currently with chain webpack, documenting it would be very helpful.

Otherwise this …

module.exports = {
  css: {
    loaderOptions: {
      resolveUrl: {
        // options here 
      }
    }
  }
}

…would be ideal.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

28reactions
dreuselcommented, Jun 5, 2020

If someone else lands here and like me has no idea what to do with these magical snippets of code that should fix your problem: You’re supposed to put a chainWebpack(config) function in vue.config.js’s module.exports and that’s where it goes. Like so:

module.exports = {
	chainWebpack: (config) => {
		['vue-modules', 'vue', 'normal-modules', 'normal'].forEach(rule => {
			config.module.rule('scss')
				.oneOf(rule)
				.use('resolve-url-loader')
				.loader('resolve-url-loader')
				.before('sass-loader')
				.end()
				.use('sass-loader')
				.loader('sass-loader')
				.tap(options =>
					({...options, sourceMap: true})
				);
		});
	},
}

Note that this is @callumacrae’s solution without the dependency on merge. Obviously you also need to npm install --save resolve-url-loader. After that it works like… well actually, like I’d expect to it work out of the box. Thanks a bunch to everyone for posting their (parts of the) solution.

6reactions
cstuncsikcommented, Dec 17, 2018

After googleing towards found the solution! resolve-url-loader docs says:

source-maps required for loaders preceding resolve-url-loader (regardless of devtool).

so just write the following under what I wrote in my previous comment:

config.module
  .rule('scss')
  .oneOf('vue')
  .use('sass-loader')
  .loader('sass-loader')
  .tap(options => ({
    ...options,
    sourceMap: true,
    sourceMapContents: false
  }))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using resolve-url-loader with vue-cli 3.0.0 - Get Help
I've been working with a project based on vue-cli 2. Just like in this post, I was having sass-loader issues with @font-face declarations ......
Read more >
how to use resolve-url-loader with sass and vue js-Vue.js
Coding example for the question how to use resolve-url-loader with sass and vue js-Vue.js.
Read more >
resolve-url-loader - npm
Resolve URL Loader. NPM. This webpack loader allows you to have a distributed set SCSS files and assets co-located with those SCSS files....
Read more >
use url-loader in vue for images in a specific directory
I need to load all images from a specific assets directory, let's say @/assets/img/pdf , as base64 to embed them into a pdfmake...
Read more >
url-loader | webpack - JS.ORG
resolve ('responsive-loader'), }, }, ], }, ], }, };. The fallback loader will receive the same configuration options as url-loader. For example, to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found