从Create-React-App升级到Next.js-CSS样式表不起作用

时间:2018-10-07 12:00:52

标签: reactjs create-react-app serverside-rendering next.js ssr

最近我们发现我们必须在我们的React项目中使用SSR。 我检查了我所知道的每种方法,并几乎测试了在中等和其他站点上发现的所有方法。经过大量的工作,我决定我们必须迁移到Next JS。

虽然所有内容的迁移过程都很好,但是对于样式表而言。

在旧版应用中,我们使用webpack将样式与项目捆绑在一起,一切都很好。

这是webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const port = process.env.PORT || 3000;
const extractSCSS = new ExtractTextPlugin('./[name].css');

// const UglifyJS = require('uglifyjs-webpack-plugin');
module.exports = {

  mode: 'development',
  output: {
    filename: 'bundle.[hash].js',
    publicPath: '/'
  },
  devtool: 'source-map',
  module: {
    rules: [

      // First Rule

      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: ['babel-loader'],

      },

      // Second Rule

      {
        test: /\.scss$/,
        use: ['css-hot-loader'].concat(extractSCSS.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader?sourceMap',
              options: { alias: { '../img': '../public/img' }, sourceMap: true }
            },
            {
              loader: 'sass-loader',
              options: {
                sourceMap: true
              }
            }
          ]
        }))
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },

        {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader'
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader'
        ]
      }
    ]
  },
  plugins: [

    new HtmlWebpackPlugin({
      template: 'public/index.html',
      favicon: 'public/favicon.ico'

    }),


    extractSCSS,

  ],
  devServer: {
    host: 'localhost',
    port: port,
    historyApiFallback: true,
    open: true
  }
};

在迁移应用程序后,我的next.config.js如下所示:

// next.config.js
const withSass = require('@zeit/next-sass')
const withCSS = require('@zeit/next-css')

module.exports = withCSS( withSass(
  {
    webpack(config, options) {
      config.module.rules.push({
        test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 100000
          }
        }
      },
      )

      return config
    },

  }
))

问题在于,所有内容均可正确呈现,但其中没有样式表,并且不包含任何样式。有谁可以帮助我解决这个问题?

4 个答案:

答案 0 :(得分:3)

仅使用node_modules中的CSS,就不需要此高级配置。

3个简单步骤:

  1. 安装next-css插件:
npm install --save @zeit/next-css
  1. 在根目录中创建next.config.js并包含以下内容:
// next.config.js 
const withCSS = require('@zeit/next-css')

module.exports = withCSS({
  cssLoaderOptions: {
    url: false
  }
})
  1. 现在您应该能够像这样从node_modules导入样式表:
import 'bootstrap-css-only/css/bootstrap.min.css';

注意:使用Next v 8 +

背景: 我花了几个小时试图简单地导入作为node_module安装的CSS,各种解决方案大多都是棘手的解决方法,但是如上所示,有一个简单的解决方案。 它由以下核心团队成员之一提供:https://spectrum.chat/next-js/general/ignoring-folders-files-specifically-fonts~4f68cfd5-d576-46b8-adc8-86e9d7ea0b1f

答案 1 :(得分:0)

这不是一个真正的答案,但是Next.js中的CSS真是太烂了!我发现自己一直在努力使其工作,因此我决定遵循他们的文档并简单地使用:

const App = () => {
  return (
    {style}
    <div/>
  );
}

let style = (<style jsx>
{`
.someClass {}
`}
</style> )

export default App;

这样,您可以像没有任何外部导入的常规HTML中那样拥有CSS

source

答案 2 :(得分:0)

您无需同时使用CSS和withSass插件。

如果您使用的是Sass,withSass插件会将其编译为CSS。

只需确保将路径添加到 Head 组件内的_document.js文件中的CSS文件中,如下所示:

<Head>
  <link rel="stylesheet" href="/_next/static/style.css" />
</Head>

答案 3 :(得分:0)

对于导入CSS,您可以使用nextJS的 Head 组件。

import Head from 'next/head';
<Head>
    <link rel="stylesheet" href="path of the css" />
</Head>