【发布时间】:2018-01-08 02:46:48
【问题描述】:
我有一个 React 组件,我正在尝试导入 css。
import './Example.css';
class Example extends Component {
return <div className="example-class">Example</div>
}
我的文件结构是这样的
build
-index.js
src
-index.js
-Example.css
我的webpack.config.js 如下所示。
目前一切都在构建,但 css 似乎没有被拾取。
谁能看到我需要做什么才能在构建后访问我的类名?
var path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.js',
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /(node_modules|bower_components|build)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
},
{
test: /\.css$/,
use: [ 'css-loader' ]
}
]
},
externals: {
'react': 'commonjs react'
}
};
【问题讨论】:
-
您是否遇到任何错误。目前发生了什么
-
@ShubhamKhatri 它不识别 css
-
你的css文件在哪个文件夹中
-
@ShubhamKhatri 更新了问题
-
能否尝试在webpack代码中添加
context: path.resolve(__dirname, 'src')
标签: javascript reactjs webpack ecmascript-6