【问题标题】:How to use next-css in next.config.js in combination with custom server Express如何结合自定义服务器 Express 使用 next.config.js 中的 next-css
【发布时间】:2019-03-25 16:19:10
【问题描述】:

我有一个使用 express 作为自定义服务器的 nextJS 应用程序。我想使用 next-css 来包含用于样式的 css 文件。如何设置此配置?

已创建一个 next.config.js 文件,其中包含:

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

我的 server.js 文件基于此处给出的示例: https://github.com/zeit/next.js/blob/master/examples/custom-server-express/server.js

在 package.json 我包括以下内容:

"@zeit/next-css": "^1.0.1",
"next": "latest",
"react": "16.4.2",
"react-dom": "16.4.2",
"webpack": "^4.22.0"

在文件中,我使用import './timeline.css' 导入 .css 文件 该css文件包含:

.example {
    font-size: 50px;
  }

终端显示的错误是:

> Failed to build
{ Error: (client) ./components/timeline/timeline.css 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> .example {
|     font-size: 50px;
|   }
 @ ./components/timeline/timeline.js 23:0-25
.....
errors: 
   [ '(client) ./components/timeline/timeline.css 1:0\nModule parse failed: Unexpected token (1:0)\nYou may need an appropriate loader to handle this file type.\n> .example {\n|     font-size: 50px;\n|   }\n @ ./components/timeline/timeline.js 23:0-25\n @ ./pages/calender.js\n @ multi ./pages/calender.js',
....

在我看来,next.config.js 似乎没有正确加载。我怀疑我是否需要将配置作为对象传递给const app = next({ dev }),或者加载了 next.config.js 但 next-css 不起作用。

【问题讨论】:

  • 我没有使用cssnext,而是@zeit/next-css github.com/zeit/next-plugins/tree/master/packages/next-css
  • 你的问题最后解决了吗?我刚才也面临同样的问题。
  • 并没有真正最终使用 material-ui 使用不同的解决方案,只是使用他们的示例来处理自定义 css。

标签: css express next.js


【解决方案1】:

在您的 next.config.js

中尝试以下代码
const withCSS = require('@zeit/next-css')
module.exports = withCSS({cssModules: true})

【讨论】:

  • 我确实尝试过,结果相同。根据文档,对于简单的示例,这应该不是必需的。 github.com/zeit/next-plugins/tree/master/packages/next-css
  • 该示例正在运行。但我无法让它与下一个自定义服务器结合使用。
  • 试试下面的代码 next.config.js const withCSS = require('@zeit/next-css') module.exports = withCSS({ cssModules: true, cssLoaderOptions: { importLoaders: 1, localIdentName: "[local]___[hash:base64:5]", } })
  • 你能详细说明 localIdentName: "[local]___[hash:base64:5]" 会做什么吗?
猜你喜欢
  • 2020-06-29
  • 2014-08-11
  • 2020-04-08
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2020-08-23
相关资源
最近更新 更多