Upgrading to 6.2.1 and the latest Nativescript-angular (if using angular) and the latest proplugins; should fix MOST issues that 6.2.0 had. However, I still highly recommend the webpack rewrite rules at the bottom of the post to solve ALL the issues.
NativeScript follows the SemVer process; so normally they would have bumped a major version on breaking changed. This break is mostly an un-attended side effects of a couple things. And unfortunately they didn't realize it broke things, so we now have 6.2 which breaks things... Oh well, mistakes happen, life will go on. And so this post is to make you aware of what things you want to change when you upgrade to NS 6.2.0...
To Upgrade or not to Upgrade, that is the question!
I do want to make sure I am VERY clear -- I do NOT think this was a bad change, nor do I think NS 6.2 is a bad release! My apps will be upgraded to it. It offers a lot of good features and fixes. It is just that being aware of the issues will help you make sure you don't have any issues with it when you do upgrade to it.
Lets dig in to the issue.
So lets get some details, first about 3 major versions ago in 2017, my business partner and buddy Nathan Walker proposed on issue 4041 to move the nativescript tns-core-modules
into its own namespace. @nativescript
along with other nativescript pieces being put into that parent namespace...
So based on this you might guess where the source of the issue lies now.
If you read the above issue; you will notice Peter (another awesome developer) and I both recommended that they do this in the 6.0 release. I personally felt like a hard break would be the safest course of action. Just eliminate tns-core-modules
totally. Since they already were totally breaking the build tools, and also breaking other plugins things in v6.0 -- they might as well add this to the pile so we deal with all the breaking changes at one time in plugins. Unfortunately, our advice wasn't taken, or it was missed and so here we are with a very interesting breaking change that I honestly did NOT foresee when I recommended they do it in the initial 6.0.
There are three different issues here that stem from the same cause:
First, if you are using NativeScript-Angular and use any third party plugins -- you might find that they are failing because NativeScript-Angular was moved into the @nativescript/angular
namespace. The compatibility imports for the old namespace; is apparently missing a couple redirects so plugins trying to load them fail. I'm sure these plugins will be updated shortly; but right now they are broken. (This has been fixed in the latest version of NativeScript-Angular)
I do NOT know yet if NS-Vue, NS-Svelte, or React-NativeScript have any issues; but odds are a fairly low with them because they are third party and were not moved into the @nativescript
namespace, so apps depending on this probably are NOT affected directly (other than by any other plugins.)
Second, because of the design of the tns-core-modules
redirect, the redirects waste a little bit of running memory. If your app or plugin imports tns-core-modules/BLAH
(which all apps do before 6.2) then webpack will give you Object #1 tns-core-modules-blah
, however, when a plugin or the core modules loads @nativescript/core/BLAH
you get Object #2 nativescript-core-blah
-- they LOOK the same but they are different objects each using memory. This does increase a little bit some parsing and memory that the V8 has to use because you now have basically more than a single framework in memory. Some of this is mitigated based on how objects are tracked; but some of it isn't. So this creates some other un-intended side effects of little bit more GC pressure, more ram usage, and the nasty pitfall we will discuss later...
Finally, if you are using any plugin that does any sort of class monkey patching, they will now all fail -- in this category is things like my very own nativescript-platform-css
, nativescript-orientation
. The reason why is because of the above allocation issues. Object #1 != Object #2. monkey patch Object #2, doesn't monkey patch Object #1. This is a corner case; as it requires you to actually replace the class (i.e. the top level object). So any plugins that rely on nativescript-global-events which extends and enhances the page class; were affected.
As an aside: The good news is virtually every one of my plugins on https://proplugins.org has been updated to have a NS 6.2 version. If you find a plugin that no longer works after you updated to 6.2; let us know and we will attempt to get it fixed. The 6.1 -> 6.2 fixes are very minor; so they can be rapidly put into the plugins.
One final note -- Please see the method to update the webpack at the bottom to have webpack rewrite tns-core-modules
to @nativescript/core
this is still highly recommended as some plugins will still be broken if you are still using the tns-core-modules
redirect version.
The low level nitty gritty details - Proceed with caution.
For those who are interested in the nitty-gritty; we will walk through why #2 and #3 occur in NS 6.2. Please note this is VERY simplified -- The actual TS -> JS code is a bit more complex and a wrapped object. However, this should give you an idea of the why's...
V8 basically keeps a object map of how a object looks in memory; so if I have file "Blah" with class HiYa -- that looks like this:
export class HiYa { function hi() { /* do something */ } }
When it exports it has a unique signature. In NativeScript this is compiled down to ES5 code. So this will basically look like this when all said and done:
function HiYa() {} HiYa.prototype.hi = function() { /* do something */} module.exports = HiYa;
Now when I do a const HiYaRunnerMaster = require('@nativescript/core/blah');
The Blah.js file is loaded into memory; parsed and then a runnable object representation of this is passed back to HiYaRunner
, lets call this HiYaRunnerMaster
. So far so good.
Now for the NativeScript tns-core-module redirects / compatibility layer.. This is the exact code that you see in tns-core-modules/*
in 6.2... It basically redirects to the @nativescript/core version of the file. Seems simple, and unless you are paying close attention you might not even see why this actually is a problem.
function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); __export(require("@nativescript/core/blah"));
So when I do a const HiYaRunnerFromTNS = require('tns-core-modules/blah');
it loads the above code and runs it. . It first defines the __export
function, then runs that function on what it gets from the require of @nativescript/core/blah . Seems simple enough. Lets break this down into each step to what the v8/JavaScriptCore engines do.
const tempObject = require('@nativescript/core/blah');
-- THIS object is the same/identicalHiYaRunnerMasterObject
as we got earlier when we ran it. Every time we do this specific require statement, we get the exact same object. Which is what we want!__export(tempObject);
This runs the export function, passing in ourHiYaRunnerMasterObject
which so far doesn't seem like an issue.for (key in HiYaRunnerMasterObject) {
we are looping through every property inHiYaRunnerMasterObject
if (!exports.hasOwnProperty(key)
verify the exports variable doesn't have this key property already.exports[key] = HiYaRunnerMasterObject[key]
- And behind the scenes
exports
is passed to theHiYaRunnerFromTNS
at the end.
Did anyone else see what happened in step 5? First, exports
is a 100% BRAND NEW OBJECT. (our Object #2). We then COPIED all the properties/functions from Object #1 (HiYaRunnerMasterObject
) onto this new Object #2 (exports
). Each copy is actually a new piece of memory that point to the old object's key value. So the new exports[Key] = old.value
. in simplistic terms has two separate memory slots; "Key" and Value". So exports[key]
is always a new memory pointer (of key
) that either points to the original object's [key]; or to its own copy of a un-boxed object. So the memory usage could be 50% of the original object to the exact same memory usage as the original object, all depending on what the values (boxed or unboxed) are. In NativeScript it should be much closer to adding the 50% additional memory, not the 100% as a worst case number as almost all values on a nativescript key are objects.
At the end, in step 6 we used the brand new object to assign to HiYaRunnerFromTNS
. Whoops; so we now have TWO different objects in memory with two different sets of memory usage.
This means that if you monkey patch the HiYaRunnerFromTNS
copy; you are not actually monkey patching the actual original and running version. So in several of my plugins, this caused them to break as the monkey patching basically never occurred as far as the running copy of NativeScript was concerned.
In addition their is one more potential gotcha; I'll make a simple example:
let Obj1 = {a: {value: 1}}; // Original Object
let Obj2 = {a: Obj1.a}; // Copied key object
If I do Obj1.a.value = 2;
then Obj2.a.value ===
2 because both a
's point to the exact same memory location with a "value" key in it . However, if I do Obj1.a = {value: 10}
then Obj1.a
then creates a brand NEW object which also has a value key; of which it has the value of 10 in it. And Obj2.a
continues to point to the OLD original object with the value = 1. So even though your objects initially started pointing to the exact same value in memory. A new object assignment will replace just that objects location.
Why does this matter? When since the running instance of NativeScript is the @nativescript/core
versions of the object. And your applications code will all currently require tns-core-module
versions; you DO initially have two objects but they should all start out pointing to the same memory locations. Just like our example earlier (Obj1.a = Obj2.b
) However, if you change a value that is NOT using a setter
-- then the value you just changed won't be propagated to the running version, and you will have Obj1.a = {value: 10}
and Obj2.a = {Value: 1}
and you won't have a clue you just did a noop
statement.
I do want to say the above potential gotcha scenario is very UNLIKELY, all code that I can think of runs through a setter; a setter function should be getting and setting the original object's value, and thus they shouldn't go out of sync. But corner cases can and do occur. So my recommendation for anyone updating to NS 6.2 is to do...
Recommendations
- Long term, replace ALL
tns-core-modules/
with@nativescript/core/
and allnativescript-angular/
with@nativescript/angular/
everywhere in your app. No longer have ANYTHING pointing totns-core-modules/
ornativescript-angular
-- pretend those old namespaces no longer exist and were deleted. - Update to the latest @proplugins if you are using any @proplugins, as most of them have already made the transition, as this will at least save you time on that aspect.
Short term tip (and highly recommended until they have a solid fix):
Richard Smith, posted a good tip that can quickly get you working -- you can add a rule to your `webpack.config.js` file. Find the alias section and add:
"nativescript-angular": "@nativescript/angular", "tns-core-modules": "@nativescript/core",
So it looks like:
alias: { "~": appFullPath, "nativescript-angular": "@nativescript/angular", "tns-core-modules": "@nativescript/core" },