英文:
Webpack and .css
问题
当包含一个 `.css` 文件时,
import BaseTable, { Column } from 'react-base-table'
import 'react-base-table/styles.css'
我得到了这个错误:
ERROR in ./node_modules/react-base-table/styles.css
模块解析失败:意外的标记 (1:0)
你可能需要一个适当的加载器来处理这种文件类型。
| .BaseTable {
| box-shadow: 0 2px 4px 0 #eeeeee;
| background-color: #ffffff;
@ ./wwwroot/source/optionsList.jsx 14:0-38
@ ./wwwroot/source/app.js
我的 `webpack.config.js` 看起来像这样:
more webpack.config.js
const path = require('path');
module.exports = {
entry: { 'main': './wwwroot/source/app.js' },
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
module: {
rules: [
{ test: /\.jsx?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
}
};
**在 `webpack` 中是否缺少解析 `.css` 的配置?**
**编辑 1**
根据评论中的建议,我将 `webpack.config.js` 文件修改为以下内容:
const path = require('path');
module.exports = {
entry: { 'main': './wwwroot/source/app.js' },
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: false,
importLoaders: 2,
sourceMap: true
}
}
]
},
{ test: /\.jsx?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
}
};
我得到了不同的错误:
ERROR in ./wwwroot/source/optionsList.jsx
模块未找到:错误:无法解析 'style-loader' 在 '/Users/ivan/Documents/Projects/OptionsAPI' 中
@ ./wwwroot/source/optionsList.jsx 14:0-38
@ ./wwwroot/source/app.js
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! moviesapi@1.0.0 wbp: `webpack`
npm ERR! 退出状态 2
npm ERR!
npm ERR! 电影 API@1.0.0 wbp 脚本失败。
npm ERR! 这可能不是 npm 的问题。很可能有额外的日志输出。
npm ERR! 可以在以下位置找到此运行的完整日志:
npm ERR! /Users/ivan/.npm/_logs/2020-01-06T18_31_42_598Z-debug.log
**编辑 2**
在使用 `npm install style-loader css-loader --save-dev` 安装加载器后,出现了新错误:
npm run wbp
ERROR in ./node_modules/css-loader/dist/cjs.js?{"modules":false,"importLoaders":2,"sourceMap":true,"minimize":true}!./node_modules/react-base-table/styles.css
模块构建失败:验证错误:无效的选项对象。CSS 加载器已经使用一个不匹配 API 模式的选项对象。
- 选项有一个未知属性 'minimize'。这些属性是有效的:
object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals?, esModule? }
at validate (/Users/ivanfigueredo/Documents/Projects/OptionsAPI/node_modules/schema-utils/dist/validate.js:85:11)
at Object.loader (/Users/ivanfigueredo/Documents/Projects/OptionsAPI/node_modules/css-loader/dist/index.js:34:28)
@ ./node_modules/react-base-table/styles.css 2:26-87
@ ./wwwroot/source/optionsList.jsx
@ ./wwwroot/source/app.js
希望这些翻译有所帮助。如果需要更多信息或帮助,请告诉我。
英文:
mm2:OptionsAPI ivan$ npm -v
6.13.4
When including a .css
file,
import BaseTable, { Column } from 'react-base-table'
import 'react-base-table/styles.css'
I get an error like this:
ERROR in ./node_modules/react-base-table/styles.css
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .BaseTable {
| box-shadow: 0 2px 4px 0 #eeeeee;
| background-color: #ffffff;
@ ./wwwroot/source/optionsList.jsx 14:0-38
@ ./wwwroot/source/app.js
My webpack.config.js
looks like this:
more webpack.config.js
const path = require('path');
module.exports = {
entry: { 'main': './wwwroot/source/app.js' },
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
module: {
rules: [
{ test: /\.jsx?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
}
};
Am I missing something in the webpack
for parsing .css
?
EDIT 1
As per the recommendation in the comments, I modified the webpack.config.js
file to this:
const path = require('path');
module.exports = {
entry: { 'main': './wwwroot/source/app.js' },
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: false,
importLoaders: 2,
sourceMap: true,
minimize: true
}
}
]
},
{ test: /\.jsx?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
}
};
I get a different error:
ERROR in ./wwwroot/source/optionsList.jsx
Module not found: Error: Can't resolve 'style-loader' in '/Users/ivan/Documents/Projects/OptionsAPI'
@ ./wwwroot/source/optionsList.jsx 14:0-38
@ ./wwwroot/source/app.js
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! moviesapi@1.0.0 wbp: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the moviesapi@1.0.0 wbp 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/ivan/.npm/_logs/2020-01-06T18_31_42_598Z-debug.log
EDIT 2
After Install the loaders with npm install style-loader css-loader --save-dev
new error:
npm run wbp
ERROR in ./node_modules/css-loader/dist/cjs.js?{"modules":false,"importLoaders":2,"sourceMap":true,"minimize":true}!./node_modules/react-base-table/styles.css
Module build failed: ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
- options has an unknown property 'minimize'. These properties are valid:
object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals?, esModule? }
at validate (/Users/ivanfigueredo/Documents/Projects/OptionsAPI/node_modules/schema-utils/dist/validate.js:85:11)
at Object.loader (/Users/ivanfigueredo/Documents/Projects/OptionsAPI/node_modules/css-loader/dist/index.js:34:28)
@ ./node_modules/react-base-table/styles.css 2:26-87
@ ./wwwroot/source/optionsList.jsx
@ ./wwwroot/source/app.js
答案1
得分: 1
将以下内容添加到您的 rules
中,并运行 npm install style-loader css-loader --save-dev
。
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: false,
importLoaders: 2,
sourceMap: true
}
}
]
}
英文:
Add this to your rules
and run npm install style-loader css-loader --save-dev
.
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: false,
importLoaders: 2,
sourceMap: true
}
}
]
}
答案2
得分: 0
尝试导入'./react-base-table/styles.css'或'./styles.css'
如果仍然失败,请尝试使用内联样式进行调试。
英文:
Try
import './react-base-table/styles.css' or
import './styles.css'
If those still fails, try inline styling for debugging
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论