css assets use absolute path got error by required in local folder structure but not remain string
See original GitHub issueVersion
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
- install latest v5.0.1 @vue/cli in global
- copy src/assets to public/assets
- 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>
-
yarn serve
-
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 <template></template>
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,
}
}
},
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:10
- Comments:8
Top Results From Across the Web
Correct path for img on React.js - Stack Overflow
Another way is to keep images in public folder and import them using relative path. For this make an image folder in public...
Read more >The Asset Pipeline - Ruby on Rails Guides
The Asset PipelineThis guide covers the asset pipeline.After reading this guide, you will know: What the asset pipeline is and what it does....
Read more >Building for Production - Vite
When it is time to deploy your app for production, simply run the vite build command. By default, it uses <root>/index.html as the...
Read more >Troubleshooting Common Errors - Gatsby
Errors using gatsby-plugin-image and sharp. Field "image" must not have a selection since type "String" has no subfields; Problems installing sharp with ...
Read more >Asset Bundling (Vite) - The PHP Framework For Web Artisans
Have you started a new Laravel application using our Vite scaffolding but need ... including applications built using Inertia, Vite works best without...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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.
+1