【问题标题】:extract CSS from js component with webpack使用 webpack 从 js 组件中提取 CSS
【发布时间】:2017-09-20 14:26:03
【问题描述】:

我正在使用 React 并将 CSS 与每个组件分开。 我想从这些文件中提取 CSS。

// ComponentA.js
import "./ComponentA.css"
export default class ComponentA extends React.Component {
    ...
}

// ComponentB.js
import "./ComponentB.css"
export default class ComponentB extends React.Component {
    ...
}

// index.js
import ComponentA from "./ComponentA"
import ComponentB from "./ComponentB"
class App extends React.Component {
    render() {
        return (
            <div>
                <ComponentA />
                <ComponentB />
            </div>
        )
    }
}

在这种情况下,我如何提取和捆绑ComponentA.cssComponentB.css

没有编译整个 JS 源代码?

【问题讨论】:

    标签: css reactjs webpack


    【解决方案1】:

    您需要使用extract-text-plugin 来执行此操作。它会将您的 css 捆绑到一个文件中。

    read more about this plugin

    【讨论】:

    • 我在其他版本中使用extract-text-plugin。但它只适用于 babel,因为 css 是从 JS 源导入的。有没有可能在没有 babel 的情况下使用 extract-text-plugin 的方法?
    • 实例上还有一个提取功能。如果您有多个 ExtractTextPlugin 实例,则应该使用它。 const ExtractTextPlugin = require('extract-text-webpack-plugin'); // 创建多个实例 const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css'); const extractLESS = new ExtractTextPlugin('stylesheets/[name]-two.css');
    猜你喜欢
    • 2018-01-21
    • 2017-06-09
    • 2017-06-04
    • 2016-12-10
    • 2015-05-17
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多