vue-cli icon indicating copy to clipboard operation
vue-cli copied to clipboard

css assets use absolute path got error by required in local folder structure but not remain string

Open johnnywang1994 opened this issue 4 years ago • 10 comments

Version

5.0.1

Environment info

  System:
    OS: macOS 11.6.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.17.5 - ~/.nvm/versions/node/v14.17.5/bin/node
    Yarn: 1.22.11 - ~/.nvm/versions/node/v14.17.5/bin/yarn
    npm: 6.14.14 - ~/.nvm/versions/node/v14.17.5/bin/npm
  Browsers:
    Chrome: 98.0.4758.109
    Edge: Not Found
    Firefox: Not Found
    Safari: 15.3
  npmGlobalPackages:
    @vue/cli: 5.0.1

Steps to reproduce

  1. install latest v5.0.1 @vue/cli in global
  2. copy src/assets to public/assets
  3. edit App.vue as following
<template>
  <!-- <img alt="Vue logo" src="/assets/logo.png"> -->
  <div id="logo"></div>
  <HelloWorld msg="Welcome to Your Vue.js App"/>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

#logo {
  width: 200px;
  height: 200px;
  margin: auto;
  background: url('/assets/logo.png') center / cover no-repeat;
}
</style>
  1. yarn serve

  2. errors in terminal

Compiled with problems:X

ERROR in ./src/App.vue?vue&type=style&index=0&id=7ba5bd90&lang=css (./node_modules/css-loader/dist/cjs.js??clonedRuleSet-12.use[1]!./node_modules/vue-loader/dist/stylePostLoader.js!./node_modules/postcss-loader/dist/cjs.js??clonedRuleSet-12.use[2]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/App.vue?vue&type=style&index=0&id=7ba5bd90&lang=css) 5:36-80

Module not found: Error: Can't resolve '/assets/logo.png' in '/Users/wangj/Desktop/johnny/prac/absolute-path/src'

What is expected?

in v4.5.0 the absolute path in url will remain original string which is same with absolute path in

What is actually happening?

absolute path in url got mistakenly required in local env


I just found out that this error is due to css-loader's "url" options by default sets to "true", just sets the css options "url" to false as following solved this issue, webpack-style-loader-css-loader-url-path-resolution-not-working

const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({
  transpileDependencies: true,
  css: {
    loaderOptions: {
      css: {
        url: false,
      }
    }
  },
}

johnnywang1994 avatar Mar 08 '22 01:03 johnnywang1994

https://cli.vuejs.org/guide/html-and-static-assets.html#url-transform-rules

johnnywang1994 avatar Mar 08 '22 01:03 johnnywang1994

https://cli.vuejs.org/guide/html-and-static-assets.html#url-transform-rules

From docs: - If the URL is an absolute path (e.g. /images/foo.png), it will be preserved as-is. No notes about special behavior on url in css.

In this issue URL is an absolute path (/assets/logo.png), but is not preserved as-is and loaded as a module. In v4 it works as expected.

ShGKme avatar Mar 17 '22 13:03 ShGKme

Bump, exactly! Should be reopened imo, have exactly the same problem 👍

JulianSchoenbaechler avatar Apr 21 '22 01:04 JulianSchoenbaechler

Bump, exactly! Should be reopened imo, have exactly the same problem

+1

fjavier-lazaro-bbva avatar May 09 '22 15:05 fjavier-lazaro-bbva

Bump, exactly! Should be reopened imo, have exactly the same problem 👍

we agree!

ghost avatar Jul 04 '22 10:07 ghost

Spent hours trying to figure this out, so I agree, this should definitely be looked at again and can be very easily be reproduced on a clean vue-cli 5 project. Everything is exactly as described here:

https://cli.vuejs.org/guide/html-and-static-assets.html#url-transform-rules

From docs: - If the URL is an absolute path (e.g. /images/foo.png), it will be preserved as-is. No notes about special behavior on url in css.

In this issue URL is an absolute path (/assets/logo.png), but is not preserved as-is and loaded as a module. In v4 it works as expected.

We used an absolute path, but webpack was still trying to look for the asset inside /src not from /public.

Setting url to false fixed the issue for us as well.

ainarend avatar Jul 21 '22 11:07 ainarend

I used an absolute path ('/') to import a file into main.js from public dir and had the same problem.

vvizden avatar Aug 13 '22 15:08 vvizden

Okay, but i just found out that setting url option to false, breaks relative file paths in SFC style block. So for example, now

/fonts/icomoon.eot

as a absolute path works and compiles, but using relative path in SFC style block is not working, with 404 for the asset being referenced:

background-image: url('../assets/checkbox-check.svg');

checkbox-check.svg:1 GET http://localhost:8080/assets/checkbox-check.svg 404 (Not Found)

ainarend avatar Aug 15 '22 19:08 ainarend

/a.png regard as require('/a.png') by css-loader ;

  • see https://github.com/webpack-contrib/css-loader#url
  • so only config the
  css: {
    loaderOptions: {
      css: {
        url: {
          filter: url => url[0] !== '/'
        }
      }
    }
  }
  • the logic of url should be resolved also see: https://github.com/webpack-contrib/css-loader/blob/master/src/utils.js#L1233

lefreet avatar Jan 09 '23 06:01 lefreet

What a fucking question it is My understanding is that the absolute path will not be dealt with, and I have been troubled by this issue all morning I searched through the vue documentation and stackhoverflow. Finally, I'm glad I found it here. The project has been running well until today when I upgraded @ vue/cli 5.0.8. Damn it

bitjerry avatar Mar 28 '23 06:03 bitjerry