Nx react application is not generating optimized bundle for production build - webpack-5

My microfrontend react application is created using Nx Tool. It has been created using Nrwl react executor and the present configuration is untouched & is the default configuration provided by Nx.
When I build the application in production mode, the bundle generated is not minimized & not performance optimized. In project.json, flags were set for optimized code, but they were not being honoured.
In project.json, if I replace the value of *targets > build > options > "webpackConfig"* from **custom webpack path** to **"#nrwl/react/plugins/webpack"**, it honors the optimization flags and works as expected, but it doesnt generate remoteEntry.js file which is required by the host app to render the micro frontend app.
I also tried customizing the webpack based on this [official documentation](https://nx.dev/recipes/other/customize-webpack#module-federation), but the code returns error
Changes in webpack i tried with:
const withModuleFederation = require('#nrwl/react/module-federation');
const moduleFederationConfig = require('./module-federation.config');
const { ModuleFederationPlugin } = require("webpack").container;
/* ========1 Orginal Code snippet================= */
//Creates remoteEntry.js file, but not uglified code
module.exports = withModuleFederation({
...moduleFederationConfig,
});
/* =======2 ================== */
//Returns error: NX customWebpack is not a function
/* module.exports = {
plugins: [
new ModuleFederationPlugin({
...moduleFederationConfig,
}),
],
}; */
Steps to replicate:
Code exists here: https://stackblitz.com/edit/react-ts-azwv9t or get from GIT: https://github.com/kalingaCoder/react_MF
Do npm install
Do npm run build-cart
check bundle output here: dist\apps\cart ( files should be minimized, but that is not happening)
Files to check:
apps\cart\project.json : *targets > build > configurations > production*
apps\cart\webpack.config.js
apps\host\module-federation.config.js

Issue is fixed now, Earlier I was using #nrwl version 14, after migrating to latest, the issue is resolved.
Ran the below command to upgrade
nx migrate latest # same as nx migrate #nrwl/workspace#latest

Related

Running JUnits with JavaFX dependencies on Jenkins

I'm trying to setup pipeline on Jenkins for our JavaFX application. Everything works fine, but I have one problem by starting unit tests. Each test, which somehow uses JavaFX classes fails with following root cause:
Caused by: java.lang.IllegalAccessException: class net.bytebuddy.description.annotation.AnnotationDescription$ForLoadedAnnotation cannot access interface com.sun.javafx.beans.IDProperty (in module javafx.base) because module javafx.base does not export com.sun.javafx.beans to unnamed module #7ee955a8
I tried a following things:
setting env variable JAVA_OPTS
JAVA_OPTS=--module-path /path/to/javafx --add-modules javafx.controls,javafx.base
adding compiler options in gradle
tasks.withType(Test).configureEach {
doFirst {
options.compilerArgs += [
'--module-path', '/path/to/javafx',
'--add-modules', 'javafx.controls,javafx.base',
]
classpath = files()
}
}
but nothing changed. I'm using the OpenJDK compiled by Azul. I took the version with JavaFX (JDKFX) but I'm not able to make this tests running (tests without any references to JavaFX are running fine). I'm also able to compile the application. Do you have any sugestions?
The problem was, like mentioned in comments, that it should not be added to the compilerArgs but to the runtime and additionally I placed it in wrong build.gradle file. The final configuration which solved all problems was to add following to the main build.gradle file:
allprojects {
tasks.withType(Test).configureEach {
jvmArgs += [
'--add-modules', 'javafx.controls,javafx.base,javafx.graphics',
'--add-opens', 'javafx.graphics/com.sun.javafx.application=ALL-UNNAMED',
'--add-opens', 'javafx.base/com.sun.javafx.beans=ALL-UNNAMED',
]
}
}

Build and test V8 from AOSP for the older version on Ubuntu 18.04.5 LTS

I am trying to run the unit tests for the V8 present in the AOSP's lollipop release: external/chromium_org/v8
by following the documentation from https://v8.dev/docs/build. But the build itself is constantly failing.
Steps followed:
Export the depot_tools path
gclient sync
install dependencies using ./build/install-build-deps.sh (This script was not present by default in the source code, so had to copy manually from the higher version)
gm x64.release
I have installed all the dependencies and followed all the steps from the documentation mentioned above but when I do:
gm x64.release
the build fails with the following output:
# echo > out/x64.release/args.gn << EOF
is_component_build = false
is_debug = false
target_cpu = "x64"
use_goma = false
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
EOF
# gn gen out/x64.release
ERROR at //build/config/BUILDCONFIG.gn:71:7: Undefined identifier
if (os == "chromeos") {
^-
I have tried building the it with gn as well by following the manual workflow but I am ending up with the same errors. I also tried setting the os variable to linux in the gn args list but there as well I get the unknown identifier error.
I see that the v8 used in the AOSP project differs a lot in terms of files from the main source code with the same version. The helper script tools/dev/gm.py is also not present by default so I am using one from the higher version. It would be great if anyone could suggest if there's any different set of steps I should be following or any other resources I can refer to in order to build the V8 present in the AOSP project
Version: V8 3.29.88.17
OS: Ubuntu 18.04.5 LTS
Architecture: x86_64
3.29 is seriously old; I'm not surprised that it won't build with current tools. Rule of thumb: when building old software, use the tools that were used to build it back then.
In the case at hand: try make x64.release.check -jN with N being the number of CPU cores you have.
I see that the v8 used in the AOSP project differs a lot in terms of files from the main source code with the same version.
The "lollipop-release" branch contains V8 3.27.34.15, whereas "lollipop-mr1-release" contains V8 3.29.88.17 which you quoted. Does that explain the differences?

Test fails with `flutter test` but passes when debugged from test file

How can I read a file using a relative path when running flutter test?
When running flutter test the File('path/to/file') needs an absolute path but a relative path works when debugging.
I feel like I'm missing something obvious. Using VS Code, I can run a test by starting debug while in one of the test files. By doing so, the test will pass when using a relative path to read a file. However, when I run flutter test from the terminal, the same test fails because it is unable to find the file unless I change it to use an absolute path in which case the test will pass.
This is the line of code in the test that tries to create a model from the json file:
final tUserModel = UserModel.fromJson(json.decode(fixture('user_cached.json')));
The fixture function is imported from a different file and implemented as follows:
import 'dart:io';
String fixture(String name) => File('test/fixtures/$name').readAsStringSync();
When using that relative path I get the following output from terminal:
flutter test
00:03 +3 ~1 -1: loading /home/user/MyApp/test/features/user/data/datasources/user_local_data_source_test.dart [E]
Failed to load "/home/user/MyApp/test/features/user/data/datasources/user_local_data_source_test.dart": Cannot open file, path = 'test/fixtures/user_cached.json' (OS Error: No such file or directory, errno = 2)
dart:io _File.readAsStringSync
fixtures/fixture_reader.dart 3:60 fixture
features/user/data/datasources/user_local_data_source_test.dart 27:40 main.<fn>
package:test_api Declarer.group
package:flutter_test/src/test_compat.dart 226:13 group
features/user/data/datasources/user_local_data_source_test.dart 25:3 main
===== asynchronous gap ===========================
package:test_api RemoteListener.start
/tmp/flutter_tools.DRSZQD/flutter_test_listener.IPDUJJ/listener.dart 16:25 serializeSuite
/tmp/flutter_tools.DRSZQD/flutter_test_listener.IPDUJJ/listener.dart 43:36 main
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 1.19.0-4.1.pre, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.46.0)
[✓] Connected device (3 available)
• No issues found!
How can I get flutter test to work with a relative file path?
Is there another way to run all tests from VS Code?
Try this.
String fixture(String name) {
var dir = Directory.current.path;
if (dir.endsWith('/test')) {
dir = dir.replaceAll('/test', '');
}
return File('$dir/test/fixtures/$name').readAsStringSync();
}
The issue has been raised a few times here: https://github.com/flutter/flutter/issues/20907
This worked for me:
import 'dart:io';
import 'package:path/path.dart';
String fixture(String name) {
final testDirectory = join(
Directory.current.path,
Directory.current.path.endsWith('test') ? '' : 'test',
);
return File('$testDirectory/fixtures/$name').readAsStringSync();
}
Thanks to: https://github.com/flutter/flutter/issues/20907#issuecomment-466185328

Robolectric unit tests using legacy instead of binary resources only when running multi-module unit test run config

I have a multi-module android project. I have a bunch of unit tests in each module and I have always been able to run them all at once using a run configuration like this one:
Many of my tests use a base class that runs with RobolectricTestRunner. This base class looks like this:
#RunWith(RobolectricTestRunner::class)
#Config(application = AndroidTest.ApplicationStub::class,
manifest = Config.NONE,
sdk = [21])
abstract class AndroidTest {
#Suppress("LeakingThis")
#Rule #JvmField val injectMocks = InjectMocksRule.create(this#AndroidTest)
fun application(): Application = ApplicationProvider.getApplicationContext()
internal class ApplicationStub : Application()
}
**When running these tests using the above config, I get the error **
[Robolectric] NOTICE: legacy resources mode is deprecated; see http://robolectric.org/migrating/#migrating-to-40
This makes many of my tests fail with ResourceNotFoundException
However, when I run tests only in a specific module, everything passes. This is because Robolectric now uses Binary resources:
[Robolectric] sdk=21; resources=BINARY
I have followed the migration instructions in build.gradle files for each module, having added the following in each android block:
testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
One clue I have found but have been unable to fix is this when I run the ALL UNIT TEST task:
WARNING: No manifest file found at build/intermediates/merged_manifests/debug/../../library_manifest/debug/AndroidManifest.xml.
Falling back to the Android OS resources only.
No such manifest file: build/intermediates/merged_manifests/debug/../../library_manifest/debug/AndroidManifest.xml
To remove this warning, annotate your test class with #Config(manifest=Config.NONE).
I have tried, as you have seen, to add the manifest=Config.NONE, which had no effect (and is now deprecated anyway).
Edit: Also tried android.enableUnitTestBinaryResources = true in settings.gradle, but this prevents the app from building due to it being a deprecated flag in the current gradle tools.
Thanks for any help provided!
So with the default unit test run platform being changed to Gradle in Android Studio, I managed to figure out a way to run unit tests in multiple modules all at once, circumventing the Robolectric bug.
First, go into run configurations and create a new Gradle Config.
Then, as the gradle project, select the top level project.
For arguments, use --tests "*"
And now for the gradle tasks, this is a little bit more error-prone. Here is an example of how I have it setup for my project:
:androidrma:cleanTestGoogleDebugUnitTest :androidrma:testGoogleDebugUnitTest
:calendar:cleanTestDebugUnitTest :calendar:testDebugUnitTest
:gamification:cleanTest :gamification:test
:player:cleanTest :player:test
:playlists:cleanTest :playlists:test
:sleepjournal:cleanTest :sleepjournal:test
:sound-content-filters:cleanTest :sound-content-filters:test
Please note that I inserted new lines between each module for more clarity here, in the tasks, just separate each entry with a space.
For your app module, in my case named androidrma, you must use your build variants name in the cleanTestUnitTest and testUnitTest , in my case being GoogleDebug.
If we look at the calendar module, it is an android module, , so it still operates with the same logic as the appModule.
However, if you look at player, playlists, sleepjournal, etc. those are pure kotlin modules. The tasks thus differ in their syntax.
Once you have entered all this information and everything is functioning, I recommend checking "store as project file" checkbox at the top right of the run config setup screen.
This works in Android Studio 4.2 as well as Arctic Fox, haven't tested on other versions.

How to disable ng-test execution during prod ng build

I am pretty new to angular-cli based projects.
I have created my angular-cli project. I am wondering is there a way we can disable/enable ng-test execution during ng build --prod. The reason why i need this is, in my CI-CD env (where i am building a Prod release), i don't need npm test or any karma related stuff,where as i need them only in my local testing.
I tried the below options in my .angular-cli.json, but it didn't work either.
(a) I tried to comment out test in my .angular-cli.json
// "test": {
// "karma": {
// // "config": "./karma.conf.js"
// }
// },
(b) I tried to comment only the karma section
How can i control over test execution in prod build?
Maybe it's a bit late to answer this but I ran in the same conserns and found this solution :
In your "tsonfig.app.json", you could add this line :
"exclude": ["src/test.ts", "**/*.spec.ts"]