在android上的cordova app中使用web pack build时发现文件未找到错误

时间:2022-01-08 15:17:58

I want to wrap a react based web app which gets build using webpack in a cordova container for an Android device. I successfully built the cordova app for iOS and everything worked fine. However, when building for android, static resources like images and fonts that are required in javascript using webpack's file-loader, aren't loaded correctly. The web view tries to load all resources like an image from file:///android_asset/www/build/myImage.png, but logs a file not found error. If I link - for example - the same image directly in the index.html via an image tag, the image is loaded correctly, but changing the publicPath attribute in my webpack config file didn't work.

我想包装一个基于反应的Web应用程序,该应用程序使用webpack在Android设备的cordova容器中构建。我成功构建了适用于iOS的cordova应用程序,一切正常。但是,在为Android构建时,使用webpack的文件加载器在javascript中所需的静态资源(如图像和字体)未正确加载。 Web视图尝试从file:///android_asset/www/build/myImage.png加载所有资源,如图像,但记录文件未找到错误。如果我通过图像标记直接在index.html中链接 - 例如 - 相同的图像,则图像被正确加载,但是更改webpack配置文件中的publicPath属性不起作用。

Any help is very much appreciated.

很感谢任何形式的帮助。

Thanks a lot in advance

非常感谢提前

2 个解决方案

#1


0  

My brute force solution:

我的蛮力解决方案:

Pre-React Build: add "homepage": "./", to app’s package.json.

Pre-React Build:将“主页”:“。/”添加到app的package.json中。

Post-React Build, and Pre-Cordova Run Android: in www/static/js, find/replace “static/media” with “android_asset/www/static/media” for all image/pdf files in the main###.js file (not the .map file).

反应后构建和Pre-Cordova运行Android:在www / static / js中,为主###中的所有image / pdf文件查找/替换“static / media”和“android_asset / www / static / media”。 js文件(不是.map文件)。

#2


0  

My solution was to add "homepage": "/android_asset/www/" to package.json, it works perfectly with dynamic paths,

我的解决方案是将“homepage”:“/ android_asset / www /”添加到package.json,它与动态路径完美配合,

#1


0  

My brute force solution:

我的蛮力解决方案:

Pre-React Build: add "homepage": "./", to app’s package.json.

Pre-React Build:将“主页”:“。/”添加到app的package.json中。

Post-React Build, and Pre-Cordova Run Android: in www/static/js, find/replace “static/media” with “android_asset/www/static/media” for all image/pdf files in the main###.js file (not the .map file).

反应后构建和Pre-Cordova运行Android:在www / static / js中,为主###中的所有image / pdf文件查找/替换“static / media”和“android_asset / www / static / media”。 js文件(不是.map文件)。

#2


0  

My solution was to add "homepage": "/android_asset/www/" to package.json, it works perfectly with dynamic paths,

我的解决方案是将“homepage”:“/ android_asset / www /”添加到package.json,它与动态路径完美配合,