3.7.0: iOS build with --prod not working


#1

Hi there ionites! I just updated to 3.7.0 and everything ok except for the build method in production mode.
I ran the ionic cordova build ios and everything went fine but when I run ionic cordova build ios --prod it fails. The console output is the following:

[14:19:14] build prod started …
[14:19:14] clean started …
[14:19:14] clean finished in 8 ms
[14:19:14] copy started …
[14:19:15] deeplinks started …
[14:19:15] deeplinks finished in 565 ms
[14:19:15] ngc started …
[14:19:22] copy finished in 7.95 s
[14:19:46] ngc finished in 30.45 s
[14:19:46] preprocess started …
[14:19:46] preprocess finished in 125 ms
[14:19:46] webpack started …

<— Last few GCs —>

353230 ms: Mark-sweep 1360.1 (1434.5) -> 1360.0 (1434.5) MB, 270.1 / 0.0 ms [allocation failure] [scavenge might not succeed].
353490 ms: Mark-sweep 1360.0 (1434.5) -> 1360.0 (1434.5) MB, 260.1 / 0.0 ms [allocation failure] [scavenge might not succeed].
353760 ms: Mark-sweep 1360.0 (1434.5) -> 1371.0 (1418.5) MB, 269.2 / 0.0 ms [last resort gc].
354022 ms: Mark-sweep 1371.0 (1418.5) -> 1382.4 (1417.5) MB, 261.1 / 0.0 ms [last resort gc].

<— JS stacktrace —>

==== JS stack trace =========================================

Security context: 0x337edb0cfb51
1: DoJoin(aka DoJoin) [native array.js:~129] [pc=0x251138e97cd9] (this=0x337edb004381 ,w=0x854d9fea1f1 <JS Array[149]>,x=149,N=0x337edb0043c1 ,J=0x337edb0ae4d1 <String[1]: >,I=0x337edb0b46e1 <JS Function ConvertToString (SharedFunctionInfo 0x337edb052dc9)>)
2: Join(aka Join) [native array.js:180] [pc=0x251138c59532] (this=0x337edb004381 ,w=0x854d9fea1f1 <JS …

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node::Abort() [/usr/local/bin/node]
2: node::FatalException(v8::Isolate*, v8::Localv8::Value, v8::Localv8::Message) [/usr/local/bin/node]
3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
4: v8::internal::Factory ::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/usr/local/bin/node]
5: v8::internal::Runtime_StringBuilderJoin(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
6: 0x251135f092a7
Abort trap: 6

Any help would be much appreciated.


#2

It seems to be a memory limit issue. The thing is I can not find a way to set a bigger memory limit for this procedure. I do not know how to pass the --max_old_space_size=X parameter to node via the build commando. Is there a way to set more memory globally?


#3

I face the same issue

So far I found following topics about it:


#4

For me, at least for the moment, the cleanest way to build my app and to don’t face this problem is to downgrade ionic/app-scripts to previous version 2.1.4


#5

You should post your ionic info.

Is it really Ionic 3.7.0 or App-Scripts 3.0 that is causing the problem?


#6

Do you mean me? In my case I could confirm that the update of App-Scripts 3.0 is the reason (but I don’t say the problem is in App-Scripts, I think that it should be linked with Webpack)


#7

That was plural you, whoever posting here with this problem :wink:
But good to know that it is coming from webpack/app-scripts for you, maybe @rindegastos can fix it that way as well.
Still should reported as an issue with app-scripts then on Github.


#8

How many pages etc. do you have? Are you lazy loading? What do you think is causing the memory blowup?

Edit: by the way, did you see this comment?

Added the below line to package.json

“aot”: “node --max-old-space-size=8192 ./node_modules/gulp/bin/gulp.js serve.prod.aot”

and using npm run aot


#9

Thx for the clarification :wink:

As fas as I saw, there are currently 3 open issues regarding the same subject in the app-scripts then on Github (I listed them above and I linked them all together in the GitHub issue tracker)


#10

The size of the app. I’ve got maybe 30/40 pages and modals and other components.
Definitely not a small app but not a big one neither (depends on the point of view :wink:).

I don’t lazy load (yet, because of the duplication of the code of components used in many pages at build time)

Yes I saw the -max-old-space-size (see comment of @epetre in https://github.com/ionic-team/ionic-app-scripts/issues/1076)

I could confirm that this option, -max-old-space-size, is solving the problem too, I was able to build my app.

I just think that, right now, downgrading app-scripts, since it solves the problem too, is a more proper way to build my app if I want or need to release it


#11

If you only got this error since app-scripts 3.0.0 it is valid to open another one and include your ionic info and app information. This seems like an unrelated thing that only produces a similar error.


#12

Sure, here you go:

Let me know if I need to add more informations


#13

We’ve been seeing this issue since @ionic/app-scripts@2.1.4. My own research tells me that the root cause of the issue is somewhere in Webpack 3. Since then, I’ve been using the following two workarounds to get our project to build:

  1. If you build by executing the npm scripts directly, such as npm run ionic:build --prod, then you can modify those scripts in package.json to something like the following:
    "ionic:build": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build"
  2. If you use the Ionic CLI’s commands to build, such as ionic build ios, then you should instead pass this setting to Node by setting the NODE_OPTIONS environment variable to --max_old_space_size=8192. This is due to the fact that the ionic command invokes node on your behalf, and there is no opportunity to pass the --max-old-space-size flag on the command line. This is the solution that we needed for our CI server.

I don’t know if you need to set a value as large as 8192. The builds don’t seem to take an appreciably different amount of time to complete, and it hasn’t blown up in our faces, yet. I personally run the npm scripts directly on my development machine, so I don’t have the environment variable set.

I’ve tested these workarounds on both v2.1.4 and v3.0.0 of @ionic/app-scripts, and our --prod builds are completing successfully on both.


#14

No, exemplary issue :smiley:


#15

Hahaha you’re too kind :wink:


#16

Awesome work, just a note: The CLI should also use ionic:build internally if I remember my tinkering correctly. Did you try setting it in scripts in package.json as well?


#17

The CLI does use ionic:build. The problem is that you need to pass the --max-old-space-size flag to Node itself. Because the CLI invokes Node internally, the flag gets ignored, and the ionic command doesn’t know what to do with that flag, so you can’t pass it on the command line.

The only other way I’ve found to get the CLI to respect this setting is to manually patch the call to node in the CLI executables (such as node_modules/.bin/ionic). If there is anything that the Ionic team could actually “fix” here, it’s probably this, but it seems like the responsibility really belongs to the Webpack team.


#18

@danbucholtz pointed out a one cmd solution to the heap size problem in the issue I opened, respectively:

node --max-old-space-size=4096 /usr/local/bin/ionic cordova build ios --prod

I kind of like the fact that it’s only one cmd which still do ionic and cordova build and that I would not need a package.json modification


#19

We’re working on coming up with a way to do this automatically in the background right now. We’re thinking if we cleverly fork a process we can supply the additional memory arg for you.

We’ll keep everyone posted.

Thanks,
Dan


#20

This solution is beautiful and it worked perfectly on my build! Thank you @reedrichards and @danbucholtz !