React + Webpack 无法解析文件加载器
2020-01-27
1009
我试图将典型的 HTML 站点迁移到“轻量级”React 应用程序。因此,我安装了没有
create-react-app
的 React。
我配置了 Webpack,然后配置了 file-loader 以在 CSS 文件中使用字体。但编译时仍然出现错误,因为它无法识别文件加载器(我还尝试了 ttf-loader 和 url-loader )。
我一直在阅读类似的问题,但它们的解决方案都不适用于这种情况。
项目的目录结构如下所示这个:
- webpack.config.js
-
src
- index.js
-
css
- style.css
-
fonts
- pgroofrunners.ttf
这是我当前的 Webpack 配置 (webpack.config.js):
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: path.resolve(__dirname, 'src/index'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
use: ['babel-loader']
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}
]
}
]
},
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
port: 9000
},
plugins: [
new HtmlWebpackPlugin({
template: "src/index.html"
})
]
};
因此,当我运行
webpack-dev-server --mode development
时,它会编译,但随后崩溃并出现以下错误:
C:\Web\pwa-static2>yarn serve yarn run v1.17.3 $ webpack-dev-server --mode development i 「wds」: Project is running at http://localhost:9000/ i 「wds」: webpack output is served from / i 「wds」: Content not from webpack is served from C:\Web\app1\dist × 「wdm」: Hash: 77bf7ce09014ddcd0764 Version: webpack 4.41.5 Time: 3901ms Built at: 2020-01-27 13:35:42 Asset Size Chunks Chunk Names bundle.js 1.43 MiB main [emitted] main index.html 2.75 KiB [emitted] Entrypoint main = bundle.js [0] multi (webpack)-dev-server/client?http://localhost:9000 ./src/index 40 bytes {main} [built] [./node_modules/ansi-html/index.js] 4.16 KiB {main} [built] [./node_modules/react-dom/index.js] 1.33 KiB {main} [built] [./node_modules/react/index.js] 190 bytes {main} [built] [./node_modules/strip-ansi/index.js] 161 bytes {main} [built] [./node_modules/webpack-dev-server/client/index.js?http://localhost:9000] (webpack)-dev-server/client?http://localhost:9000 4.29 KiB {main} [built] [./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built] [./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built] [./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.91 KiB {main} [built] [./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built] [./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built] [./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built] [./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built] [./src/css/style.css] 335 bytes {main} [built] [failed] [1 error] [./src/index.js] 207 bytes {main} [built] + 30 hidden modules ERROR in ./src/css/style.css 1:0 Module parse failed: Unexpected character '@' (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders > @font-face { | font-family: pgRoofRunners; | src: url("../fonts/pgroofrunners.ttf"); @ ./src/index.js 3:0-25 Child html-webpack-plugin for "index.html": 1 asset Entrypoint undefined = index.html [./node_modules/html-webpack-plugin/lib/loader.js!./src/index.html] 2.99 KiB {0} [built] [./node_modules/lodash/lodash.js] 528 KiB {0} [built] [./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built] [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built] i 「wdm」: Failed to compile.
我猜这就是 **src/css/style.css* 中应用程序崩溃的原因:
@font-face {
font-family: pgRoofRunners;
src: url("../fonts/pgroofrunners.ttf");
}
如何我能解决这个问题吗?
2个回答
看起来您正在使用 webpack 在某处导入 CSS。为了理解 CSS,webpack 也需要一个加载器。通常,您的 module.rules 中有类似这样的内容:
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
或者,如果您不使用
MiniCssExtractorPlugin
:
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
Narigo
2020-01-27
而不是文件加载程序,我使用了URL-LOADER(需要安装)。
453158865
demkovych
2020-01-27