将样式加载器与 webpack 一起使用时出现“未定义窗口"错误

编程入门 行业动态 更新时间:2024-10-27 18:33:59
本文介绍了将样式加载器与 webpack 一起使用时出现“未定义窗口"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

构建服务器端 React 应用程序,在使用 Webpack 时,我遇到了 Style-Loader 问题.

Building a server side react app and while using Webpack I am having issues with Style-Loader.

我使用的是^0.23.1"版本,当运行脚本来捆绑和构建时,Style-Loader 出现了问题.

I am using version "^0.23.1" and when running a script to bundle and build there is an issue from Style-Loader.

问题是窗口未定义

webpack:///./node_modules/style-loader/lib/addStyles.js?:23 return window && document && document.all && !window.atob;

有人遇到过这个问题吗?在浏览了堆栈和样式加载器的 Github 问题后,我没有找到任何解决方案.

Has anyone run into this issue? After looking through Stack and the Github issues for style-loader I am not finding any solution.

这是我的 webpack 文件:

Here is my webpack file:

const path = require('path'); const webpack = require('webpack'); module.exports = { // webpack to use node target: 'node', entry: './src/index.js', output: { filename: 'client-build.js', path: path.resolve(__dirname, 'build/public'), publicPath: '/build/public' }, module: { rules: [ { test: /.js$|.jsx$/, loader: 'babel-loader', exclude: '/node_modules/', options: { presets: [ '@babel/preset-react' ] } }, { test: /.(s*)css$/, loader: ['style-loader', 'css-loader', 'sass-loader'] }, { test: /.jpeg$|.gif$|.png$|.svg$|.woff$|.ttf$|.wav$|.mp3$|.jpg$|.pdf$/, loader: 'file-loader', query: { name: 'assets/img/[name].[ext]' }, }, ] }, plugins: [ new webpack.ProvidePlugin({ "React": "react", }), ], }

如果还有什么想看的我可以留言.

If there is anything else you need to see I can post it.

推荐答案

style-loader 尝试将样式注入网站的 head (window/document),在您的服务器上呈现/执行时将不存在.

style-loader tries to inject styles into the head of the website (window / document), which will be non-existent on your server on render / execution.

你需要从你的服务器配置中删除这个加载器并用其他东西替换它(例如 ExtractTextPlugin 或 MiniCSSExtractplugin,取决于你的 webpack版本)

You need to remove this loader from your server-config and replace it with something else (e.g. ExtractTextPlugin or MiniCSSExtractplugin, depending on your webpack version)

更多推荐

将样式加载器与 webpack 一起使用时出现“未定义窗口"错误

本文发布于:2023-08-04 01:26:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   加载   窗口   错误   未定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!