css - webpack 可以在不复制所需文件的情况下捆绑 css/scss/less 吗?

标签 css webpack webpack-style-loader

假设我有以下两个组件,直接需要相同的实用程序 css 文件。

第一部分:

import component1Css from '/path/to/component1.scss'
import someOtherUtilityCss from './path/to/some-other-css.scss'
...
export default Component1;

第二部分:

import component2Css from './path/to/component2.scss'
import someOtherUtilityCss from './path/to/some-other-css.scss'
...
export default Component2;

然后我将它们包含在我的应用程序中:

主要应用:

import someLayoutCss from './path/to/some-layout.css';
import Component1 from './path/to/component1'
import Component2 from './path/to/component2'

...

export default App;

我希望捆绑系统知道只导入 some-other-css.scss 一次。

style-loader + css-loader 是否已经开箱即用?

此外,

如何处理内部 css 导入?

如果我通过 import 语句在 javascript 中导入了 cssFile1 和 cssFile2:

import cssFile1 from 'path/to/file1.scss'
import cssFile2 from 'path/to/file2.scss'

而且cssFile1和cssFile2内部都导入了cssFile3,那么cssFile3的内容会不会在file1.css和file2.css中出现重复?或者 sass-loader 会解决这个问题并只包含一次 cssFile3 吗?

最佳答案

官方文档说:

Deduplication

If you use some libraries with cool dependency trees, it may occur that some files are identical. Webpack can find these files and deduplicate them. This prevents the inclusion of duplicate code into your bundle and instead applies a copy of the function at runtime.

https://webpack.github.io/docs/optimization.html#deduplication

虽然我还没有尝试过。

关于css - webpack 可以在不复制所需文件的情况下捆绑 css/scss/less 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820662/

相关文章:

css - 为什么 !important 属性没有被应用?

javascript - 导入 Typescript 时找不到 Webpack 模块

angular - 更新到 Angular v6 - 找不到模块 : Error: Can't resolve 'fs'

javascript - Webpack css-loader : "Module not found: Error: Can' t resolve 'main.css' in . ..”

webpack - 样式达到 10000 个字符时被截断

css - 如何去除卡片中的填充和设计?

html - 某些 SVG 作为 base64 无法使用 CSS 正确显示,但在 img 标签内的 HTML 中可以正常显示

javascript - Webpack 找不到入口模块

reactjs - React 服务器端渲染 CSS 模块

html - 将 div 移到最右边 css 和 HTML