我在我的react应用程序中使用css-modules,但是当我尝试在.js文件中导入外部文件时,我无法全局导入它们,因为我无法将它们包装在:global块中,而webpack将它们解释为要使用的样式作为对象.
如何从外部库导入CSS文件作为全局CSS?这是应该与导入的插件匹配的CSS.
egin src / index.js
import 'draft-js/dist/Draft.css';
//cannot wrap in a :global block since it is imported
import 'react-responsive-carousel/lib/styles/carousel.min.css';
// is wrapped in a :global {...} block, so it works
import './styles/app.css';
Webpack配置,来自Create-React-App
test: /\.css$/,
loader: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: true,
minimize: true,
},
},
'postcss-loader'
]
最佳答案 一个选项是对src样式和node_modules样式有两个单独的规则,并且只在src配置中将模块设置为true.它应该类似于下面(我没有测试过) – 重要的选项是
include和
exclude,并从适用于node_modules的规则中删除模块.
{
test: /\.css$/,
exclude: /node_modules/,
loader: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: true,
minimize: true,
},
},
'postcss-loader'
]
},
{
test: /\.css$/,
include: /node_modules/,
loader: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
minimize: true,
},
},
'postcss-loader'
]
}