Skip to content

Commit 1ba655d

Browse files
authored
Update README.md
1 parent e047221 commit 1ba655d

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
1-
"# vue-file-loader"
1+
# vue-file-loader
22

33
this is a file-loader wrapper for Vue and Webpack, solve the image relative path issues when HTML and CSS are not in the same directory.
44

5+
if your output directory structure is like this:
6+
7+
```
8+
index.html
9+
images
10+
1.png
11+
2.png
12+
styles
13+
1.css
14+
2.css
15+
script
16+
1.js
17+
2.js
18+
```
19+
1.css load 1.png, the url in source code may be write like this `url(images/1.png)`, to support that, your webpack config maybe like this:
20+
```
21+
{
22+
test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,
23+
use: [{ loader: 'file-loader', options: { publicPath: '../', name: '/images/[name].[ext]?[hash]' }]
24+
}
25+
```
26+
then webpack will translate `images/1.png` to `../images/1.png`, this does solve the issues, but wait, if your html has img tag that:
27+
```
28+
<img src="image/2.png">
29+
```
30+
webpack will translate `images/2.png` to `../images/2.png`, it leads html load `2.png` failure, so file-loader can't tell the picture from HTML or CSS.
31+
vue-file-loader just solving this issues. your webpack config can be like this:
32+
33+
```
34+
{
35+
test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,
36+
use: [{ loader: 'vue-file-loader', options: { publicPath: '', publicStylePath: '../', name: '/images/[name].[ext]?[hash]' }]
37+
}
38+
```
39+
or
40+
```
41+
{
42+
test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,
43+
use: [{
44+
loader: 'url-loader',
45+
options: {
46+
limit: 2048,
47+
publicPath: '',
48+
publicStylePath: '../',
49+
fallback: 'vue-file-loader',
50+
name: '/images/[name].[ext]?[hash]'
51+
}
52+
]
53+
}
54+
```
55+
vue-file-loader willchoose `publicPath` or `publicStylePath` based on the image from HTML or CSS.

0 commit comments

Comments
 (0)