borodean / postcss-assets

An asset manager for PostCSS
MIT License
537 stars 32 forks source link

Help!resove failed #68

Closed jylzs369 closed 7 years ago

jylzs369 commented 7 years ago

below is a simple example, but it doesn't work as expected and I don't know what is wrong here.

structure

-dist  -images  -css -src  -images  -css index.html

CSS

background: resolve('icons/apple.png') no-repeat;

gulpfile

var gulp = require('gulp'),
    postcss = require('gulp-postcss'),
    assets = require('postcss-assets');

var options = {
    loadPaths: ['images/']
};

gulp.task('assets', function () {
    return gulp.src('src/*.css')
        .pipe(postcss([assets(options)]))
        .pipe(gulp.dest('dist'));
})

gulp.task('default', ['assets']);

console

Error: Asset not found or unreadable: icons/apple.png
borodean commented 7 years ago

@jylzs369 the paths set in the postcss-assets' options are relative to a place from which the program is being run. I assume that you run Gulp from the root of you project, so those paths should be relative to it. Thus, the loadPaths should be like this:

var options = {
    loadPaths: ['src/images/']
};

Also, you may consider adding a basePath:

var options = {
    basePath: `src`,
    loadPaths: ['images/'],
};
jylzs369 commented 7 years ago

I see the problem, thank you for help!