根据react-datepicker
文档,我尝试导入css
文件,但 react 会抛出错误,因为You may need an appropriate loader to handle this file type
. 我已经包含css-loader
在webpack-config
文件中。有什么遗漏吗?以下是文件的import
一部分jsx
import React, { Component } from 'react';
import moment from 'moment';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
以下是 webpack.config.js
const Notifier = require('webpack-build-notifier');
const path = require('path');
const plugins = [new Notifier({ title: 'Webpack' })];
const output = path.join(__dirname, 'public', 'javascripts');
const babelConfig = {
presets: [
['@babel/preset-env', {
useBuiltIns: 'usage',
targets: {
browsers: [
'last 2 versions',
'ie >= 11',
],
},
}],
'@babel/react',
],
};
const cssConfig = [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader' },
];
const sassConfig = [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader' },
{ loader: 'sass-loader' },
];
module.exports = {
entry: { bundle: ['./src/client/router.jsx'] },
output: { filename: '[name].js', path: output },
devtool: 'source-map',
resolve: { extensions: ['.js', '.jsx', '.json', '.css', '.scss'] },
module: {
rules: [
{
test: /\.css$/,
exclude: /node_modules/,
use: cssConfig,
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: sassConfig,
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: babelConfig,
},
],
},
plugins,
};