简体   繁体   English

使用extract-text-webpack-plugin和在HTML标头中链接合并的CSS文件之间有什么区别?

[英]What is the difference between using extract-text-webpack-plugin and linking a merged CSS file in an HTML header?

From what I understand extract-text-webpack-plugin bundles all css files imported in your React components into a single separate CSS file. 据我了解,extract-text-webpack-plugin将所有在您的React组件中导入的css文件捆绑到一个单独的CSS文件中。 The separate CSS file can then be referenced in your HTML header to prevent FOUC (Flash Of Unstyled Content). 然后,可以在HTML标头中引用单独的CSS文件,以防止FOUC(未样式化内容的闪烁)。 Using extract-text-webpack-plugin counteracts some of the benefits of importing your CSS in your React component js files such as hot-loading. 使用extract-text-webpack-plugin抵消了将CSS导入React组件js文件中的一些好处,例如热加载。

What is then the difference between using extract-text-webpack-plugin and replacing all stylesheet imports in your component files with a single link to a merged CSS file in your HTML template header? 那么,使用extract-text-webpack-plugin和用指向HTML模板标题中的合并CSS文件的单个链接替换组件文件中的所有样式表导入之间有什么区别?

Does it matter whether you use CSS modules or import your CSS? 使用CSS模块还是导入CSS都无关紧要?

UPDATE: Added example for clarification. 更新:添加了示例进行说明。

Scenario A: 方案A:

  1. component1.css (imported in component1.js) component1.css(导入到component1.js中)
  2. component2.css (imported in component2.js) component2.css(导入到component2.js中)
  3. bundled CSS file generated by extract-text-webpack-plugin (called in the HTML header) 由extract-text-webpack-plugin生成的捆绑CSS文件(在HTML标头中调用)

Scenario B: 方案B:

  1. component1.css (not referenced in js files) component1.css(在js文件中未引用)
  2. component2.css (not referenced in js files) component2.css(在js文件中未引用)
  3. master CSS file merged using SASS, Laravel mix.style method etc. (called in the HTML header) 使用SASS,Laravel mix.style方法等合并的主CSS文件(在HTML标头中调用)

Why go with Scenario A and not B? 为什么选择方案A,而不选择方案B?

Good question ariebear! 好问题ariebear!

If you want to use css modules, then yes, there's definitely a benefit to importing the css files into your js files/react components. 如果要使用css模块,则可以,将css文件导入到js文件/反应组件中绝对有好处。 The main one is that you no longer have to concern yourself with scoping issues, or worry about writing the same class in 2 different areas. 最主要的一点是,您不必再担心范围问题,也不必担心在2个不同区域编写同一堂课。 The cascade becomes localized to each component. 级联变为局部于每个组件。

If you're not using css modules, then there isn't much benefit at all. 如果您不使用css模块,那么根本没有太多好处。 Sure, you get hot reloading, but there are other solutions available for that. 当然,您可以进行热重装,但是还有其他解决方案可用。

Hope that helps! 希望有帮助!

The advantages to combining all your stylesheets into one is that the browser makes a separate request for each stylesheet. 将所有样式表组合为一个优点是,浏览器会为每个样式表分别发出请求。 If you combine several into one, you will only make one request. 如果将多个合并为一个,则只会发出一个请求。 Using this plugin, your styles will no longer be inlined into your JS bundle, which can improve performance as the CSS bundle will be loaded in parallel to the JS bundle. 使用此插件,您的样式将不再内联到您的JS捆绑包中,因为CSS捆绑包将与JS捆绑包并行加载,因此可以提高性能。

You're asking a lot of different questions here, so here's my best shot. 您在这里提出了许多不同的问题,所以这是我的最佳选择。

You'd use extract-text-plugin in production. 您将在生产中使用extract-text-plugin。 Hot reloading isn't necessary in production and you're right that it gets rid of FOUC. 热重装在生产中不是必需的,并且您可以摆脱FOUC,这是正确的。 There are a few other advantages listed straight from the source as well. 从源头直接列出了其他一些优点。

You can still import your .css into your components, if you wish to- or keep them separate. 如果愿意,仍可以将.css导入到组件中,或者将它们分开。 It does matter if you import or require your CSS, that has to do with if you're using es2015 or not. 是否导入或需要CSS都很重要,这与是否使用es2015有关。

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

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