[英]Webpack Dynamic Import Expression Not Working
I am using react with webpack.我正在使用与 webpack 的反应。
I got a folder with hundreds of SVGs in it.我有一个文件夹,里面有数百个 SVG。
Thereby I am using webpacks dynamic import syntax like so import('../images_svg/' + svgData.path + '.svg')
sadly this doesn't work.因此,我正在使用 webpacks 动态导入语法,如
import('../images_svg/' + svgData.path + '.svg')
遗憾的是这不起作用。 Also I am using the svg-inline-loader.我也在使用 svg-inline-loader。
That's why I get the following exception:这就是为什么我收到以下异常:
<--- Last few GCs --->
95733 ms: Mark-sweep 1301.0 (1434.6) -> 1303.2 (1434.6) MB, 61.8 / 0.0 ms [allocation failure] [scavenge might not succeed].
95794 ms: Mark-sweep 1303.2 (1434.6) -> 1305.4 (1434.6) MB, 60.9 / 0.0 ms [allocation failure] [scavenge might not succeed].
95856 ms: Mark-sweep 1305.4 (1434.6) -> 1312.2 (1418.6) MB, 62.3 / 0.0 ms [last resort gc].
95922 ms: Mark-sweep 1312.2 (1418.6) -> 1319.1 (1418.6) MB, 65.9 / 0.0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 000001788583FA99 <JS Object>
1: stringify [native json.js:178] [pc=000002CFEA0A3CF7] (this=0000017885846091 <a JSON with map 0000010551613B91>,E=0000023A39E19981 <Very long string[11977826]>,F=0000017885804241 <undefined>,S=0000017885804241 <undefined>)
2: arguments adaptor frame: 1->3
3: /* anonymous */ [C:\Users\phili\Documents\Kunden\step1\app\node_modules\webpack\lib\EvalDevToolModuleTemplatePlugin.js:~17...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
How can I dynamically import all SVGs using webpack, without getting a heap out of memory error?如何使用 webpack 动态导入所有 SVG,而不会出现内存不足错误?
Updated solution更新的解决方案
A big thanks to Dan Abramov (creator of Redux).非常感谢 Dan Abramov(Redux 的创建者)。 He suggested me to use the
public
folder as described in the create-react-app
readme and to not import the SVGs via webpack:他建议我使用
create-react-app
自述文件中描述的public
文件夹,而不是通过 webpack 导入 SVG:
Adding asssets outside of the module system 在模块系统之外添加资产
The create-react-app
readme states: create-react-app
自述文件指出:
Normally we recommend importing stylesheets, images, and fonts from JavaScript.
通常我们建议从 JavaScript 导入样式表、图像和字体。 The public folder is useful as a workaround for a number of less common cases:
public 文件夹可用作一些不太常见的情况的解决方法:
One of those cases is:其中一种情况是:
You have thousands of images and need to dynamically reference their paths.
您有数以千计的图像,需要动态引用它们的路径。
So now I am using this fetch library, which was already included in the config (generated by create-react-app
after ejecting)所以现在我正在使用这个fetch库,它已经包含在配置中(在弹出后由
create-react-app
生成)
Using fetch I could load the images dynamically from the public
folder and start webpack without getting ever again a memory issue.使用 fetch 我可以从
public
文件夹动态加载图像并启动 webpack,而不会再次出现内存问题。
Old solution旧解决方案
A solution is to use一个解决方案是使用
node --max_old_space_size=8000 scripts/start.js
to get it working.让它工作。 But it took approximately 10 minutes to load.
但是加载大约需要 10 分钟。 To get it start faster we can use webpack's
cache-loader
.为了让它更快地启动,我们可以使用 webpack 的
cache-loader
。 Thereby I reduced the loading time to one minute.因此我将加载时间减少到一分钟。
See this thread to the problem https://github.com/webpack/webpack/issues/5747请参阅此线程以解决问题https://github.com/webpack/webpack/issues/5747
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.