我在 Laravel8 中使用 laravel-mix 撰寫反應代碼并編譯。
在正常使用中,一切正常。但是當我想使用下面的行將 mp4 檔案加載到 react 中時:
require(`./files/${mp4file}`)
我mp4在使用 laravel-mix 編譯期間通過以下方式加載檔案時收到此錯誤npm run dev:
ERROR in ./resources/files/5.mp4 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
(Source code omitted for this binary file)
webpack compiled with 1 error
注意:我有這個程式create-react-app,它運行良好,在build和期間沒有問題run。
我該如何解決這個錯誤?
uj5u.com熱心網友回復:
一天后,我找到了解決方案,
您需要構建自定義加載器。為此,我將此行添加到webpack.mix.js:
mix.extend("addWebpackLoaders", (webpackConfig, loaderRules) => {
loaderRules.forEach((loaderRule) => {
webpackConfig.module.rules.push(loaderRule);
});
});
mix.addWebpackLoaders([
{
test: /\.(mp4|svg|jpe?g|gif)$/,
use: [
{
loader: 'file-loader',
}
]
}
]);
通過這種方式,您已經file-loader為諸如mp4, svg, 之類的檔案添加了gif。之后你就可以運行了npm run dev。
這個鏈接對我有幫助:https : //github.com/laravel-mix/laravel-mix/issues/145
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/358422.html
