我正试图在我的react-on-rails
应用程序上使用react-on-rails
。为了使日期时间显示出来,我需要导入GH页面中提到的CSS。
在我的应用程序中,我将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加载程序不起作用了。我在纯粹的react (create-react-app
)上尝试过这一点,它成功了。当我在react_on_rails里面做的时候它就坏了。
这是我的webpack配置自动取款机(标准开箱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的新手,不知道如何添加加载程序使其工作,如何应用必须应用于react-datetime
的react-datetime
文件?
编辑:添加了css-加载程序(也更新了上面的webpack )。它不再抱怨我没有正确的加载程序,但CSS不工作。
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: 'css-loader',
exclude: /node_modules/,
},
],
发布于 2017-06-11 13:44:25
在概念上有两种不同的方法来处理这个问题。
1.使用CSS模块。这样,CSS就会与JS捆绑在一起,webpack一加载JS模块/包,它就会自动将CSS样式元素添加到头部。在我的项目中,我有这样做的规则(注意,我们同时使用css-loader
和style-loader
):
{
test: /\.css$/,
use: ['style-loader', {
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]'
}
}]
}
有关css-加载程序modules
在这个链接上的更多信息。
2.使用ExtractTextPlugin.这样,您所有的CSS将被解压到一个单独的文件。配置需要两样东西:由插件创建的插件和加载器配置:
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// Add this to your webpack plugins array
new ExtractTextPlugin('styles.css')
并将此添加到您的规则中:
{
test: /\.css$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader']
})
}
这将创建一个styles.css
文件,并将从JS导入的所有CSS放入该文件。
https://stackoverflow.com/questions/44466037
复制相似问题