在没有 CSS 模块的情况下在 Webpack 5 中动态加载 CSS?
我正在开发一个遗留项目(基于 jQuery)。通过简单地将样式标签附加到标题中,可以使用 Javascript 添加一些 CSS:
$("head").append("<link href='" + path + "' rel='stylesheet' type='text/css' media='screen' />")
我无法使用 CSS 模块重写代码,所以我想知道是否有一种方法可以捆绑使用 Webpack 5 通过 Javascript 注入的 CSS。
I am working on a legacy project (based on jQuery). Some CSS is added with Javascript by simply appending the style-tag to the header:
$("head").append("<link href='" + path + "' rel='stylesheet' type='text/css' media='screen' />")
I cannot rewrite the code using CSS modules so I am wondering if there is a way to bundle this CSS that gets injected via Javascript using Webpack 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一种使用 require.context 使其工作的方法。
下面的行使某个文件夹内的所有 js 文件都可用作构建依赖项:
这实际上使用了它们(jQuery 代码就在那里,因为这个遗留项目正在使用 jQuery,重要的部分是
req_js(key)
):关于 require.context 的更多有趣的背景信息在这里:什么是`require.context`?
Found a way to make it work using require.context.
The following line makes all js files inside a certain folder available as build dependencies:
And this actually uses them (the jQuery code is just there because this legacy project is using jQuery, the important part is
req_js(key)
):More interesting background info regarding require.context here: What is `require.context`?