gpt4 book ai didi

css - 使用 Webpack 和 React 加载 CSS

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:48 25 4
gpt4 key购买 nike

我正在构建一个应用程序,它使用 socket.io 使用客户端-服务器通信和服务器-谷歌云平台通信。对于前端部分,我使用 React,其中有两个组件 App 和 Touch。将来我会改变元素结构,但现在看起来像这样

我的元素结构如下:

nodemodulesFolder
publicFolder
jsFolder
client.js
socket.io.js
socket.io.js
socket.io.js.map
touch.js
viewFolder
App.js
index.css
Touch.js
index.js
routes.js
.dockerignore
.env
.gitignore
deployment.yaml
Dockerfile
index.js
ingress.yaml
Jenkinsfike
package.json
package-lock.json
server.js
webpack.config.js

我的 CSS 文件如下所示:

body { 
font-family: sans-serif;
}



p {
color: black;
}

a {
font-weight: bold;
text-decoration: none;
color: #2a2a2a;
}

a:visited {
font-weight: normal;
}

a:hover {
text-decoration: underline;
}


.wrapper {
width: 90vw;
padding-top: 100px;
margin: auto;
width: 50%;
margin-bottom: 160px;
}

.greyText {
opacity: 0.4;
}

.buttonWrapper{
margin: auto;
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 140px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
background: #fff;
border-top: 2px solid #ddd;
}

#ResultText {
width: 80vw;
margin-bottom: 100%;
overflow: auto;
}

#ResultText span {
display: inline-block;
margin-top: 10px;
}

#sessionSpeechData {
width: 80vw;
}

#sessionSpeechData span {
display: inline-block;
margin-top: 10px;
}

.nl-Adjective {
background-color: #1ada47;
padding: 3px;
border-radius: 5px;
}

.nl-Noun {
background-color: #151ffa;
padding: 3px;
border-radius: 5px;
color: white;
}

.nl-Verb {
background-color: #ff1616;
padding: 3px;
border-radius: 5px;
}


.hiddenForms {
opacity: 0.2;
}

h1 {
color: black;
}


/* ==========================================================================
Media Queries
========================================================================== */

/*========== Non-Mobile First Method ==========*/

/*Above */

@media only screen and (min-width: 1201px) {}

/* Large Devices, Wide Screens */

@media only screen and (max-width: 1200px) {}

/* Medium Devices, Desktops */

@media only screen and (max-width: 992px) {}

/* Small Devices, Tablets */

@media only screen and (max-width: 768px) {}

/* Extra Small Devices, Phones */

@media only screen and (max-width: 480px) {}

/* Custom, iPhone Retina */

@media only screen and (max-width: 320px) {}

@media print, (-o-min-device-pixel-ratio: 5/4), (-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi) {}

/* ==========================================================================
Helper classes
========================================================================== */

.hidden {
display: none !important;
visibility: hidden;
}

.visuallyhidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}

.visuallyhidden.focusable:active, .visuallyhidden.focusable:focus {
clip: auto;
height: auto;
margin: 0;
overflow: visible;
position: static;
width: auto;
}

.invisible {
visibility: hidden;
}

.clearfix:before, .clearfix:after {
content: " ";
display: table;
}

.clearfix:after {
clear: both;
}

.clearfix {
*zoom: 1;
}

/* ==========================================================================
Print styles
========================================================================== */

@media print {
*, *:before, *:after {
background: transparent !important;
color: #000 !important;
box-shadow: none !important;
text-shadow: none !important;
}
a, a:visited {
text-decoration: underline;
}
a[href]:after {
content: " (" attr(href) ")";
}
abbr[title]:after {
content: " (" attr(title) ")";
}
a[href^="#"]:after, a[href^="javascript:"]:after {
content: "";
}
pre, blockquote {
border: 1px solid #999;
page-break-inside: avoid;
}
thead {
display: table-header-group;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
p, h2, h3 {
orphans: 3;
widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
}


.btn {
background-color: #4285f4;
color: #fff;
width:140px;
height:100px;
}

.btn:hover {
background-color: #296CDB;
}

.btn:focus {
background-color: #0F52C1;
outline: 0;
}

.btn:active {
background-color: #0039A8;
}


#startRecButton {
background-color: #1ada47;
margin-right:80px;
}


#startRecButton:hover {
background-color: #1ada47;
}

#startRecButton:focus {
background-color: rgb(62, 180, 38);
outline: 0;
}

#startRecButton:active {
background-color: rgb(64, 114, 44);
}

但是我在编译的时候遇到如下错误:

/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babel-core/lib/transformation/file/index.js:558
throw err;
^

SyntaxError: /Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/public/views/index.css: Unexpected token, expected ; (1:5)
> 1 | body {
| ^
2 | font-family: sans-serif;
3 | }
4 |
at Parser.pp$5.raise (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp.unexpected (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:1761:8)
at Parser.pp.semicolon (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:1742:38)
at Parser.pp$1.parseExpressionStatement (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:2236:8)
at Parser.parseExpressionStatement (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:5934:20)
at Parser.pp$1.parseStatement (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:1911:17)
at Parser.parseStatement (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:5910:22)
at Parser.pp$1.parseBlockBody (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:2268:21)
at Parser.pp$1.parseTopLevel (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:1778:8)
at Parser.parse (/Users/raulvilloravalencia/OneDrive - Universidad Politécnica de Madrid/Documents/cp-test-app-2/cp-test-app/node_modules/babylon/lib/index.js:1673:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ start: `webpack && node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/raulvilloravalencia/.npm/_logs/2018-09-28T10_17_39_236Z-debug.log

我的 webpack.config.js 文件如下:

'use strict'

module.exports = {
entry: __dirname + '/public/index.js',
output: {
path: __dirname + '/public/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
},
{ test: /\.css$/,
use:[ {loader: 'css-loader'} ]
}
]
},
resolve: {
extensions: ['.js', '.jsx']
}
};

最佳答案

你还需要样式加载器

先安装style-loader

npm install style-loader --save-dev

Webpack 3 版本

  { test: /\.css$/,
use:[ {loader: 'style-loader!css-loader'} ]
}

webpack 4 版本

  {
test: /\.css$/,
loader: "style-loader!css-loader"
}

关于css - 使用 Webpack 和 React 加载 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52553359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com