如何在使用 webpack 构建期间将 css-in-js (样式化组件)移动到外部 css 文件 - ReactJS

2024-01-30

当我构建 React 项目时,我试图找出 CSS 文件所在的位置。我正在使用 webpack,如果我使用普通 CSS,我可以为整个项目中使用的所有样式创建一个 CSS 文件。当我使用样式组件在 js 中使用 CSS 时,我没有获得外部 CSS 文件。

webpack.config.js

var path    = require('path');
var hwp     = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');


module.exports = {
    entry: path.join(__dirname, '/src/index.js'),
    output: {
        filename: 'index.js',
        path: path.join(__dirname, './dist'),
        publicPath : '/'
    },
    module:{
        rules:[{
            exclude: /node_modules/,
            test: /\.js$/,
            loader: 'babel-loader'
    },
    {
        test: /\.css$/i,
        use: [
            {
            loader : MiniCssExtractPlugin.loader,
            options : {
                publicPath: '/public/path/to/',
            },
        }, 
        'css-loader'
    ]
      }
    ]
    },
    devServer: {
        historyApiFallback: true,
      },
    plugins:[
        new hwp({template:path.join(__dirname, '/src/index.html')}),
        new MiniCssExtractPlugin({
            filename : '[name].css',
            chunkFilename : '[id].css',
        })
    ]
}

联系方式.js

import React from 'react'
import styled from "styled-components"

const Container = styled.div`
    background-color : red;
`

 function contact() {
    return (
        <Container>
            <h1>
                Welcome to Contacts page
            </h1>
            
        </Container>
    )
}
export default contact

这不受支持styled-components at the moment。来自项目成员 -

We don't support static CSS extraction. You can try out emotion which does.

You might not need static CSS extraction actually, because of several reasons:

There's SSR which sends only critical CSS, instead of all static CSS, for the entire page. You don't even need to do SSR, but can use snapshotting (react-snapshot) or generate a static page (gatsby, et al), which basically saves a SSR result to a html.
Static extraction doesn't generate dynamic CSS, which means your page will either appear broken until the JS executes, or you'll need to defer until the JS is loaded
Caching doesn't actually buy you an advantage, because the JS bundles will likely always change in tandem with the extracted CSS
In v3 we will add preprocessing, which will actually a bigger advantage. As part of that we might support an option for static extraction, since the core library that will bring preprocessing will probably also be integrated into emotion, which does support extraction. So it might become an option. Or not ????

Source https://github.com/styled-components/styled-components/issues/1018

情感也有删除静态 CSS 提取 https://github.com/emotion-js/emotion/blob/bcf40cf2c201534813f7e3070aacd59e9f674f6f/docs/extract-static.mdx

重复的我如何选择 React Styled-Components 来生成物理 CSS 文件? https://stackoverflow.com/questions/44916599/how-can-i-opt-for-react-styled-components-to-generate-a-physical-css-file

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在使用 webpack 构建期间将 css-in-js (样式化组件)移动到外部 css 文件 - ReactJS 的相关文章

随机推荐