gpt4 book ai didi

javascript - 在 Webpack 中使用 CSS

转载 作者:行者123 更新时间:2023-11-29 21:04:10 32 4
gpt4 key购买 nike

我继承了一个使用 webpack 的网络应用程序。在我的应用程序中,我有一个名为“pub”的目录,如下所示:

./pub
/styles
app.css
/images
brand.png

我整个早上都在尝试通过 webpack 使用这些,但没有成功。在我的 webpack.config.js 文件中,我有以下内容:

const path = require('path');

const projectRoot = path.resolve(__dirname, '../');

module.exports = {
entry: {
app: './src/index.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192
}
}
]
}
]
}
};

然后,在我的 index.js 文件中,我有以下内容:

import logoImage from './public/images/brand.png';
require("css!./public/css/app.css");

当我运行 webpack 时,我收到一条错误消息:

BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders.
You need to specify 'css-loader' instead of 'css',
see https://webpack.js.org/guides/migrating/#automatic-loader-module-name-extension-removed

我不太明白这个错误。当我查看它,然后查看我的 webpack.config.js 文件时,在我看来我正在使用 css-loader。除此之外,一旦 require 语句起作用,我如何在我的网页中使用样式。我只是想将 webpack 与网络应用程序一起使用,并想导入我的品牌和 CSS,但我无法弄明白。

最佳答案

你不需要CSS!在你的需求声明中

require("css!./public/css/app.css");

你可以直接使用

require("./public/css/app.css");

因为您正在测试文件:

{
test: /\.css$/, // <-- here
loader: "style-loader!css-loader"
},


或者在你的 webpack 配置中没有规则

// No test in rules matched but you tell webpack
// explicitly to use the css loader
require("style-loader!css-loader!./public/css/app.css");

关于javascript - 在 Webpack 中使用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892166/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com