简体   繁体   English

如何将CSS保存在单独的文件中?

[英]how to keep CSS in separate files?

By default Gatsby drumps all the css required on a given html document into its header making css caching impossible: 默认情况下,Gatsby将给定html文档所需的所有css敲入其标头中,从而使css缓存无法进行:

import "./foo.css"
import "./bar.css"

results in: 结果是:

<style data-href="/styles.f6ce41623bc6fbf912c0.css">
  .foo{color:green}.bar{color:orange}
</style>

/styles.f6ce41623bc6fbf912c0.css contains exactly that stylesheet. /styles.f6ce41623bc6fbf912c0.css包含该样式表。

is there a way to keep foo.css and bar.css in separate files without loosing the ability to minify and autoprefix everything? 有没有一种方法可以将foo.cssbar.css在单独的文件中,而又不会失去缩小和自动前缀所有内容的功能?

The functionality is called bundling , and Gatsby uses Webpack to do it. 该功能称为捆绑 ,而Gatsby使用Webpack来做到这一点。 You could alter the Webpack configuration , but I'd say there is really no point in doing that. 您可以更改Webpack的配置 ,但是我说这样做真的没有意义。

Bundling and code-splitting resources is a crucial part of why Gatsby is so fast. 捆绑和代码拆分资源是Gatsby如此之快的关键部分。 By bundling the CSS for every page makes sure that only resources required by the current page are loaded. 通过为每个页面捆绑CSS,可以确保仅加载当前页面所需的资源。 Gatsby also caches all of the resources it has loaded, so you don't need to worry so much about resource economy. Gatsby还缓存了已加载的所有资源 ,因此您不必担心资源经济性。

I found a code snippet from a Gatsby issue comment , which replaces inline <style> elements with <link> s, but I wouldn't recommend doing that without going through some performance measurement. 我从Gatsby问题注释中找到了一个代码段,该代码段用<link>替换了内联的<style>元素,但是我建议您不要进行一些性能评估。

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

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