You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ended up writing my own custom function to produce relative asset urls. It is possible due to this.options.file – the filepath of the Sass file being rendered – being available inside the function.
varassetUrl=function(type,$assetPath){varassetsPathForType={image: 'app/media/images',font: 'app/media/fonts'};if(!assetsPathForType[type]){thrownewError(`assetUrl: type ${type} not supported, must be one of ${Object.keys(assetsPathForType)}`);}varassetPath=$assetPath.getValue();varsassFilepath=this.options.file;// Since sassFilepath is a file, we need to normalise for// directory relations.varrelativeToStylesRoot=path.relative(path.dirname(sassFilepath),'sass');varstylesRootToAssets=path.relative('app/styles',assetsPathForType[type]);varstylesToAssetpath=path.join(relativeToStylesRoot,stylesRootToAssets,assetPath);varurlString=`url('${stylesToAssetpath}')`;returnnewsassTypes.String(urlString);};gulp.task('sass',()=>{returngulp.src('sass/**/*.sass').pipe(sass({functions: {'image-url($filepath)': function($filepath){returnassetUrl.call(this,'image',$filepath);},'font-url($filepath)': function($filepath){returnassetUrl.call(this,'font',$filepath);}}}).on('error',sass.logError)).pipe(gulp.dest('app/styles'));});
I am also having trouble with relative URLs. It seems like this suggested approach does not handle relative URLs inside of files that are @importd by this.options.file. The this.options.file only points to the entry SASS file, not the currently processed SASS file.
I ended up writing my own custom function to produce relative asset urls. It is possible due to
this.options.file
– the filepath of the Sass file being rendered – being available inside the function.I will figure out how to apply it to this repo and submit a pull-request. In the meantime, the code is pasted below and an example project is available at https://github.com/henrahmagix/node-sass-relative-asset-url
Related to #5
Input
Output
The text was updated successfully, but these errors were encountered: