简体   繁体   English

相对于相对css url的相对导入重新设置

[英]rebase relative css url's regarding to their relative imports

I have sass file styles/app.scss with following content: 我有以下内容的sass文件样式/app.scss:

@import '../scripts/jspm_packages/npm/font-awesome@4.6.1/scss/font-awesome';
@import '../scripts/jspm_packages/npm/jquery-ui@1.10.5/themes/base/jquery-ui.css';

@import 'components/base';
@import 'components/cart';
@import 'components/category';
@import 'components/dialog';
@import 'components/form';

I want to generate app.css boundary file with '../scripts/jspm_packages/npm/font-awesome@4.6.1/scss/font-awesome' and '../scripts/jspm_packages/npm/jquery-ui@1.10.5/themes/base/jquery-ui.css included, it's not a problem, but I want url's in this files be rebased relatively to this files paths and some path to dist dir, where app.css will be placed. 我想用'../scripts/jspm_packages/npm/font-awesome@4.6.1/scss/font-awesome'和'../scripts/jspm_packages/npm/jquery-ui@1.10生成app.css边界文件包括.5 / themes / base / jquery-ui.css,这不是问题,但是我希望相对于该文件路径以及dist dir的某些路径,将这些文件中的url重新设置为基础,将app.css放置在其中。

font-awesome.sass has paths to fonts like url('../fonts/somefont.ttf') as well as jquery-ui.css. font-awesome.sass具有字体路径,例如url('../ fonts / somefont.ttf')以及jquery-ui.css。 So we have relative imports and relative url in imported files. 因此,我们在导入文件中有相对导入和相对URL。

So if I have: 所以,如果我有:

dist/
  styles/
    app.css
  fonts/
    'here I want to place all awesome fonts'
  images/
    'here I want to place all images'

And I want to get paths in app.css like url('../fonts/somefont.ttf') and so on.. 我想在app.css中获取路径,例如url('../ fonts / somefont.ttf')等等。

I tried a lot of gulp plugins but they don't rebase relative url's in imported files. 我尝试了很多gulp插件,但它们不会将导入文件中的相对url作为基准。

May be You can help me. 可能是您可以帮助我。 How to achieve that, or may be I have to change something in my project to get this to work as expected. 如何实现这一目标,或者可能是我必须在项目中进行一些更改才能使其按预期工作。

Thanks! 谢谢!

I published today a npm package that does exactly what you need: css-source-map-rebase 我今天发布了一个npm软件包,它完全满足您的需求: css-source-map-rebase

I discovered after many trials and errors that the only reliable way to rebase assets in a CSS relatively to the file they were imported from is to use its source maps. 经过多次试验和错误,我发现相对于从导入文件将CSS中的资产重新设置基础的唯一可靠方法是使用其源映射。 If you think about it it makes sense: the purpose of source maps is actually to maintain a mapping between a line/column pair in the generated file (CSS) and a line/column pair in the source file (SASS, LESS). 如果考虑一下,这是有道理的:源映射的目的实际上是维护生成文件(CSS)中的行/列对与源文件(SASS,LESS)中的行/列对之间的映射。 Once you understand that, writing a module that rebases assets relatively to the source file where they were referenced is pretty much trivial. 一旦您了解了这一点,编写一个相对于资产相对于其被引用的源文件进行基础调整的模块就很简单了。

The npm package I wrote is basically a node stream that take a source map as parameter, consume CSS files and return some CSS with assets rebased. 我编写的npm软件包基本上是一个节点流,该节点流以源映射作为参数,使用CSS文件并返回一些具有重新设置资产的CSS。

For the following sources... 对于以下来源...

entry.scss entry.scss

@import "partial/bar.scss";

.foo {
    background: url('./foo.png');
}

partial/bar.scss 部分/ bar.scss

.bar {
    background: url('./bar.png');
}

...the resulting CSS would be: ...最终的CSS将是:

.bar {
    background: url('partial/bar.png');
}

.foo {
    background: url('foo.png');
}

Don't hesitate to come back to me if you need some help. 如果您需要帮助,请随时与我联系。 By opening issues on the GitHub repository or asking here. 通过在GitHub存储库上打开问题或在此处询问。

EDIT: I just discovered that the repository URL was bad in npm. 编辑:我只是发现存储库URL在npm中是错误的。 Here it is: https://github.com/ericmorand/css-source-map-rebase . 它在这里: https : //github.com/ericmorand/css-source-map-rebase I'll fix in a few minutes. 我待会儿解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM