我正嘗試在我的react-on-rails
應用上使用react-datetime
。爲了使日期時間開箱即用,我需要導入GH頁面上提到的CSS。在react_on_rails應用程序中導入.css時Webpack加載器失敗?
在我的應用程序,我複製/粘貼CSS到一個文件中我命名爲DateTime.css
:
...
import DateTime from 'react-datetime';
import '../../schedules/stylesheets/DateTime.css';
...
export default class AddDate extends React.Component {
但它給我這個錯誤:
VM45976:1 Uncaught Error: Module parse failed: /Users/some/path/to/my/project/App/components/schedules/stylesheets/DateTime.css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .rdt {
| position: relative;
| }
這似乎是CSS加載程序不加工。我試過這個純粹的反應應用程序(create-react-app
),它的工作。當我在react_on_rails裏面做的時候它就破了。
這是我的WebPack配置ATM(標準外的現成react_on_rails):
const webpack = require('webpack');
const { resolve } = require('path');
const ManifestPlugin = require('webpack-manifest-plugin');
const webpackConfigLoader = require('react-on-rails/webpackConfigLoader');
const configPath = resolve('..', 'config');
const { devBuild, manifest, webpackOutputPath, webpackPublicOutputDir } =
webpackConfigLoader(configPath);
const config = {
context: resolve(__dirname),
entry: {
'webpack-bundle': [
'es5-shim/es5-shim',
'es5-shim/es5-sham',
'babel-polyfill',
'./app/bundles/App/startup/registration',
],
},
output: {
// Name comes from the entry section.
filename: '[name]-[hash].js',
// Leading slash is necessary
publicPath: `/${webpackPublicOutputDir}`,
path: webpackOutputPath,
},
resolve: {
extensions: ['.js', '.jsx'],
},
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: 'development', // use 'development' unless process.env.NODE_ENV is defined
DEBUG: false,
}),
new ManifestPlugin({ fileName: manifest, writeToFileEmit: true }),
],
module: {
rules: [
{
test: require.resolve('react'),
use: {
loader: 'imports-loader',
options: {
shim: 'es5-shim/es5-shim',
sham: 'es5-shim/es5-sham',
},
},
},
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: 'css-loader',
exclude: /node_modules/,
},
],
],
},
};
module.exports = config;
if (devBuild) {
console.log('Webpack dev build for Rails'); // eslint-disable-line no-console
module.exports.devtool = 'eval-source-map';
} else {
console.log('Webpack production build for Rails'); // eslint-disable-line no-console
}
我很新的的WebPack,以及不知道如何,我可以添加裝載機,使其工作,我怎麼能申請DateTime.css
文件,我必須適用於react-datetime
?
編輯:增加了css-loader(同時更新了上面的webpack)。它不再抱怨我沒有正確的加載器,但CSS不起作用。
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: 'css-loader',
exclude: /node_modules/,
},
],
你的配置中沒有'css-loader'。看一看[css-loader docs](https://github.com/webpack-contrib/css-loader),你就可以立即開始運行。另外,如果你想提取CSS到一個單獨的文件,你將需要[ExtractTextPlugin](https://github.com/webpack-contrib/extract-text-webpack-plugin) –
@PavelDenisjuk,感謝堡rec!我安裝了'css-loader',並且錯誤消息停止了。但是它仍然沒有顯示CSS。任何想法爲什麼會發生? – Iggy