Skip to content

Conversation

@MSNev
Copy link
Collaborator

@MSNev MSNev commented May 5, 2023

No description provided.

// ApplicationInsights, Telemetry
// };
// }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is no longer needed as it's auto injected as part of the rollup build step for the browser bundles

export default createConfig(banner,
{
namespace: "Microsoft.ApplicationInsights3",
namespace: "Microsoft.ApplicationInsights",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now the setting for the "global" namespace, previously we had some code during the module initialization that would "reassign" (copy a reference) to the original global namespace for backward compatibility.

export default createConfig(banner,
{
namespace: "Microsoft.ApplicationInsights3",
namespace: "Microsoft.ApplicationInsights",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resetting all of the global namespaces back to the standard default "global" name as apart from the AISKU, none of the other browser bundles included the code to "assign" back to the global namespace (which was an unintended bug)

};
};

const getIntro = (format, theNameSpace, moduleName, theVersion) => {
Copy link
Collaborator Author

@MSNev MSNev May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplistically, this uses the rollup intro and outro config options to "take control" of the prefix and postfix (the code rollup wraps around the generated files) so that we can more efficiently create multiple namespaces (Microsoft.ApplicationInsights and Microsoft.ApplicationInsightsX (where X is the major version).

This also allow us to add additional debugging information to assist with supporting environment that "load" multiple major versions.

theIntro += prefix + "var undef = \"undefined\";\n";
if (format === "umd") {
// UMD supports loading via requirejs and
theIntro += prefix + "typeof exports === \"object\" && typeof module !== undef ? factory(exports) :\n";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main code that is injected to the top of every browser bundle (we don't run/change this for the main node (package.json) entrypoints -- currently as I don't believe we need this because of the way npm will wrap everything in a closure).

Once minified the new code is only around 200 bytes more, compared with "adding" the code and creating a new entrypoint (and adding code like this to every module) would add around 1k.

The previous UMD format generated by rollup for UMD format look like

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.Microsoft = global.Microsoft || {}, global.Microsoft.ApplicationInsights3 = global.Microsoft.ApplicationInsights3 || {})));
})(this, (function (exports) { 'use strict';

This new code changes this to look like

(function (global, factory) {
    var undef = "undefined";
    typeof exports === "object" && typeof module !== undef ? factory(exports) :
    typeof define === "function" && define.amd ? define(["exports"], factory) :
    (function(global){
        var nsKey, key, nm, theExports = {}, modName = "es5_ai_3_0_1", msMod="__ms$mod__";
        var mods={}, modDetail=mods[modName]={}, ver="3.0.1";
        var baseNs=global, nsKey="Microsoft", baseNs=baseNs[nsKey]=(baseNs[nsKey]||{});
        // Versioned namespace "Microsoft.ApplicationInsights3"
        var exportNs=baseNs, nsKey="ApplicationInsights3", exportNs=exportNs[nsKey]=(exportNs[nsKey]||{});
        // Global namespace "Microsoft.ApplicationInsights"
        var destNs=baseNs, nsKey="ApplicationInsights", destNs=destNs[nsKey]=(destNs[nsKey]||{});
        var expNsDetail=(exportNs[msMod]=(exportNs[msMod] || {})), expNameVer=(expNsDetail["v"]=(expNsDetail["v"] || []));
        var destNsDetail=(destNs[msMod]=(destNs[msMod] || {})), destNameVer=(destNsDetail["v"]=(destNsDetail["v"] || []));
        (destNsDetail["o"]=(destNsDetail["o"] || [])).push(mods);
        factory(theExports);
        for(var key in theExports) {
            // Always set the imported value into the "export" versioned namespace (last-write wins)
            nm="x", exportNs[key]=theExports[key], expNameVer[key]=ver;
            // Copy over any named element that is not already present (first-write wins)
            typeof destNs[key]===undef ? (nm="n", destNs[key]=theExports[key]) && (destNameVer[key]=ver) : !destNameVer[key] && (destNameVer[key]="---");
            (modDetail[nm] = (modDetail[nm] || [])).push(key);
        }
    })(typeof globalThis !== undef ? globalThis : global || self);
})(this, (function (exports) {
'use strict';

The "support" / "debug" additions added as part of this populate a __ms$mod___ property to both namespaces, where the "global" one (Microsoft.ApplicationInsights in this case) will include a v object which enumerates every property name with the version that was applied to the namespace, for "unknown" (if an earlier version of the SDK was already loaded) this is set to ---. It also contains an o which is an array of objects that includes the module name, n array with the property keys applied from this named module as well as an optional x array with exported names from this module that where not applied to the global namespace

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@MSNev MSNev force-pushed the MSNev/MainRelease branch from 4e049b0 to b97ee42 Compare May 5, 2023 18:19
@MSNev MSNev force-pushed the MSNev/MainRelease branch from b97ee42 to d23756e Compare May 5, 2023 18:25
@MSNev MSNev merged commit 2507674 into main May 5, 2023
@MSNev MSNev added this to the 3.0.2 milestone May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants