webdev doesn't allow setting optimization level
Hi!
The documentation in https://dart.dev/web/deployment says:
Use the webdev build command to create a deployable version of your app. This command converts your code to JavaScript and saves the result as build/web/main.dart.js. You can use any option available to dart compile js with webdev build.
If we check the dart compile js help section, we can see it supports optmization levels:
dart compile js --help Compile Dart to JavaScript.
Usage: dart compile js [arguments]
-h, --help Print this usage information. -h -v Show detailed information about all options. -o, --output Write the output to . -O<0,1,2,3,4> Set the compiler optimization level (defaults to -O1). -O0 No optimizations (only meant for debugging the compiler). -O1 Default (includes whole program analyses and inlining). -O2 Safe production-oriented optimizations (like minification). -O3 Potentially unsafe optimizations (see -h -v for details). -O4 More agressive unsafe optimizations (see -h -v for details).
However, if we check the webdev build help section, we can see it doesn't show anything related to these flags:
webdev build --help Run builders to build a package.
Usage: webdev build [arguments] -h, --help Print this usage information. -o, --output A directory to write the result of a build to. Or a mapping from a top-level directory in the package to the directory to write a filtered build output to. For example "web:deploy". A value of "NONE" indicates that no "--output" value should be passed to
build_runner. (defaults to "web:build") -r, --[no-]release Build with release mode defaults for builders. (defaults to on) --[no-]build-web-compilers If a dependency onbuild_web_compilersis required to run. (defaults to on) -e, --[no-]enable-expression-evaluation Enable expression evaluation features in the debugger. (defaults to on) -v, --verbose Enables verbose logging.Run "webdev help" to see global options.
If we try to pass the optimization level anyway, it doesn't work:
webdev build -O2 Could not find an option with short name "-O".
Usage: webdev build [arguments] ...
Another alternative I tried is webdev build -- -O2, but it doesn't work too:
webdev build -- -O2 [INFO] Run "build_runner help" to see global options.Unhandled exception: Bad state: Unable to start build daemon. #0 _handleDaemonStartup.
(package:build_daemon/client.dart:79:21) #1 _runUserCode (dart:async/stream_pipe.dart:11:23) #2 Stream.firstWhere. (dart:async/stream.dart:1708:9) #3 _RootZone.runGuarded (dart:async/zone.dart:1582:10) #4 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13) #5 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7) #6 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7) #7 _SyncBroadcastStreamController._sendDone. (dart:async/broadcast_stream_controller.dart:399:22) #8 _BroadcastStreamController._forEachListener (dart:async/broadcast_stream_controller.dart:322:15) #9 _SyncBroadcastStreamController._sendDone (dart:async/broadcast_stream_controller.dart:398:7) #10 _BroadcastStreamController.close (dart:async/broadcast_stream_controller.dart:268:5) #11 _AsBroadcastStreamController.close (dart:async/broadcast_stream_controller.dart:505:24) #12 _RootZone.runGuarded (dart:async/zone.dart:1582:10) #13 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13) #14 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7) #15 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7) #16 _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:87:11) #17 _EventSinkWrapper.close (dart:async/stream_transformers.dart:21:11) #18 _StringAdapterSink.close (dart:convert/string_conversion.dart:241:11) #19 _LineSplitterSink.close (dart:convert/line_splitter.dart:141:11) #20 _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:132:24) #21 _RootZone.runGuarded (dart:async/zone.dart:1582:10) #22 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13) #23 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7) #24 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7) #25 _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:87:11) #26 _EventSinkWrapper.close (dart:async/stream_transformers.dart:21:11) #27 _StringAdapterSink.close (dart:convert/string_conversion.dart:241:11) #28 _Utf8ConversionSink.close (dart:convert/string_conversion.dart:295:20) #29 _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:78:18) #30 _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:132:24) #31 _RootZone.runGuarded (dart:async/zone.dart:1582:10) #32 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13) #33 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7) #34 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7) #35 _SyncStreamControllerDispatch._sendDone (dart:async/stream_controller.dart:792:19) #36 _StreamController._closeUnchecked (dart:async/stream_controller.dart:647:7) #37 _StreamController.close (dart:async/stream_controller.dart:640:5) #38 _Socket._onData (dart:io-patch/socket_patch.dart:2462:21) #39 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10) #40 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:365:11) #41 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:297:7) #42 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:784:19) #43 _StreamController._add (dart:async/stream_controller.dart:658:7) #44 _StreamController.add (dart:async/stream_controller.dart:606:5) #45 new _RawSocket. (dart:io-patch/socket_patch.dart:1949:35) #46 _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1378:18) #47 _microtaskLoop (dart:async/schedule_microtask.dart:40:21) #48 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5) #49 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13) #50 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)
Should webdev's documentation enforce "You can use any option available to dart compile js with webdev build." if the relevant flags don't work? I guess so, it should work.
I'm guessing we've just never gotten around to exposing the optimization levels through webdev but there shouldn't be any reason we can't as far as I know.
I feel like the documentation might be incorrect here. I don't remember exactly, but I faintly recall it's intentional that webdev build's options are distinct, as different compilers can be used (dartdevc, dart compile js, dart compile wasm, etc.).
Configuration for the different compilers should occur through build_web_compilers: https://pub.dev/packages/build_web_compilers/versions/4.1.0-beta.3#compiler-arguments
\cc @jakemac53 in case you have more context.
Correct, it needs to be configured through the build_web_compilers options.
If we wanted to, webdev build could have an alias for this, since it is targeted at specifically dart2js/dart2wasm both of which support -O. And you can pass a (somewhat complicated) command line argument to set that option when spawning the build daemon (something like --define "build_web_compilers:entrypoint=compilers={\"dart2js\":{\"args\":[\"-O2\"]}}"
Thanks, @jakemac53 and @parlough, for clarifying that optimization levels need to be configured through build_web_compilers and for providing the relevant details on how to achieve this. I appreciate the insights!
@bkonyi, based on these details, would it make sense for Webdev to support optimization levels directly as an alias? Alternatively, if this is intentionally excluded, do we need to update the documentation to make it clear that these options must be set via build_web_compilers?