Gulp -clean-css cannot set the correct relative path for url () for assets such as fonts and images

I use NPM and Gulp for my package and build manager. I installed "gulp-sass" to support sass and "gulp -clean-css" so that I can @import css files inline. I write my SCSS files to "_frontend / scss / ** / *" and compile them into a single file as "assets / frontend / css / styles.css". In the output location, I have other important resource folders that are css siblings: fonts, img, and js.

My problem is that I cannot get the relative url () paths to be set correctly when I import the "* .css" file. However, this is normal when I import .scss files.

Here is my code (I selected the source files, error log, etc. to simplify the question):

GULPFILE.JS

gulp.task('styles', function() {
    gulp.src(styles.src)
        .pipe(sassGlob())                                   // Support for bundle requires for Sass
        .pipe(cleanCSS({
            // relativeTo: './node_modules',    <-- I tried with different variations here, no luck
            // root: './node_modules',          <-- I tried with different variations here, no luck
            processImport: true
        }))
        .pipe(rename('styles.css'))                      // Name output CSS file
        .pipe(gulp.dest('../assets/frontend/css/'));
});

MAIN.SCSS

OK below

@import "font-awesome/scss/font-awesome.scss";

The output seems to come out with the correct relative paths. Output Example: url (../fonts/FontAwesome/fontawesome-webfont.eot? V = 4.6.1)

Below is NOT

@import '../node_modules/jquery-ui/themes/base/jquery-ui.css';

Example output: url (../node_modules/jquery-ui/themes/base/images/animated-overlay.gif) ^ ^ The above somehow adds "../node_modules". I want him to somehow set this to "../img/vendor/jquery-ui/what/ever/i/like/here"

Here is my directory structure.

_frontend    
 ├─fonts
 │  ├─Beamstyle-Icons
 │  └─Gotham-Light-Regular
 ├─node_modules
 │  ├─jquery-ui
 │  │  ├─scripts
 │  │  └─themes
 │  │      ├─base
 │  │         ├─images
 │  │         └─minified
 │  │             └─images
 │  └─ (other stuff...)
 ├─scripts
 │  ├─app
 │  └─vendor
 └─scss
     ├─config
     ├─helpers
     │  ├─classes
     │  ├─functions
     │  ├─mixins
     │  └─placeholders
     └─src
         ├─blocks
         └─components
assets
 └─frontend
    ├─css
    ├─fonts
    ├─img
    │  ├─Beamstyle-Icons
    │  ├─FontAwesome
    │  └─Gotham-Light-Regular
    └─js

It would be great if anyone could help with this.

+4
source share
2 answers

< 2.4

relativeTo, root target, , rebase: false.

>= 2.4

gulp -clean-css , clean-css 4.x, , rebaseTo, .

+1

, relativeTo.

: (https://www.npmjs.com/package/clean-css)

relativeTo - @import URL-

, relativeTo + destination gulpfile.js dir, css- (, ).

:

\---project
    +---assets
    |   \---stylesheets
    |       |   style.css
    |       |   
    |       \---dist
    |               style.min.css
    | 
    |   \---fonts   
    |               my-font.ttf           
    \---gulp
            gulpfile.js

( gulpfile) '../assets/stylesheets/dist'

:

// Create minified css
gulp.task('minifycss', function() {
    return gulp
        .src('../assets/stylesheets/*.css')
        .pipe(cleancss({relativeTo: '../assets/', compatibility: 'ie8'}))
        .pipe(rename({
            suffix: ".min"                              // add *.min suffix
        }))
        .pipe(gulp.dest('../assets/stylesheets/dist'))
});

:

// style.css :
     src: url("../fonts/my-font.ttf");

// dist/style.min.css :
     src: url("../../fonts/my-font.ttf");
-2

All Articles