我正在尝试在我自己的应用程序中为其组件包含另一个库的 css。作为参考,我正在尝试使用这个数据表库:https ://github.com/filipdanic/spicy-datatable 。
在文档中,它指出Out of the box, spicy-datatable is bare-bones. Include this CSS starter file in your project to get the look from the demo. Edit it to suit your needs.
我尝试在我正在构建的组件顶部导入样式表,如下所示:import * as spicy from 'spicy-datatable/src/sample-styles.css';
在我自己的组件文件中。它没有样式。我尝试将原始代码放入我index.scss
文件assets/styles
夹中的文件中 - 没有用。我试着把它放在我自己的样式文件中./component.scss
——没有用。
我目前将它们设置为:
import * as styles from './component.scss';
import * as spicy from 'spicy-datatable/src/sample-styles.css';
并且出现错误:
Module parse failed: Unexpected token (4:0)
You may need an appropriate loader to handle this file type.
webpack.config.js
const dirNode = 'node_modules';
const dirApp = path.join(__dirname, 'client');
const dirAssets = path.join(__dirname, 'assets');
/**
* Webpack Configuration
*/
module.exports = {
entry: {
vendor: ['lodash'],
bundle: path.join(dirApp, 'index')
},
resolve: {
modules: [dirNode, dirApp, dirAssets]
},
plugins: [],
module: {
rules: [
// BABEL
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/,
options: {
compact: true
}
},
// CSS / SASS
{
test: /\.(scss)$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[path]___[name]__[local]___[hash:base64:5]'
}
},
'sass-loader'
]
},
// IMAGES
{
test: /\.(jpe?g|png|gif)$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
}
]
}
};
.babelrc
"plugins": [
[
"react-css-modules",
{
"filetypes": {
".scss": {
"syntax": "postcss-scss"
}
},
"webpackHotModuleReloading": true
}
]
我不确定是否需要添加一些东西来专门处理.css
文件,这是我第一次使用 CSS 模块。我认为 react-css-modules 是这样做的,所以我不太确定为什么 CSS 文件没有正确加载。
编辑:
编辑了我的 webpack 以包含 CSS:
{
test: /\.(css)$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[path]___[name]__[local]___[hash:base64:5]'
}
}
]
},
错误消失了,但样式仍然没有出现。