Skip to content

Umd globals not working ? #6697

Closed
Closed
@MohamedLamineAllal

Description

@MohamedLamineAllal

Describe the bug

globals in the moduleoption of type: umd is there to remap the module name asigned to global. That's how it is used in babel @babel/plugin-transform-modules-umd

{
  "plugins": [
    [
      "@babel/plugin-transform-modules-umd",
      {
        "globals": {
          "es6-promise": "Promise"
        }
      }
    ]
  ]
}

exposing global.Promise rather than the default global.es6Promise.

swc seeming to go with global.fileName. Which normally a config like:

{
  "$schema": "http://json.schemastore.org/swcrc",
  "module": {
    "type": "umd",
    "globals": {
      "index": "HeroJs"
    }
  },
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true,
      "dts": true,
      "dynamicImport": true
    },
    "baseUrl": ".",
    "paths": {
      "/*": ["./src/*"]
    }
  },
  "sourceMaps": true
}

Should replace the global.index with global.HeroJs for an index.js module file. That's not happening:

The output still went as:

    else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {});

Full output file:

(function(global, factory) {
    if (typeof module === "object" && typeof module.exports === "object") factory(exports);
    else if (typeof define === "function" && define.amd) define([
        "exports"
    ], factory);
    else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {});
})(this, function(exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    function _export(target, all) {
        for(var name in all)Object.defineProperty(target, name, {
            enumerable: true,
            get: all[name]
        });
    }
    _export(exports, {
        Class1: function() {
            return Class1;
        },
        Class2: function() {
            return Class2;
        },
        Hero: function() {
            return Hero;
        }
    });
    function _instanceof(left, right) {
        if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
            return !!right[Symbol.hasInstance](left);
        } else {
            return left instanceof right;
        }
    }
    function _classCallCheck(instance, Constructor) {
        if (!_instanceof(instance, Constructor)) {
            throw new TypeError("Cannot call a class as a function");
        }
    }
    var Class1 = function() {
        "use strict";
        function Class1() {
            _classCallCheck(this, Class1);
        }
        var _proto = Class1.prototype;
        _proto.go = function go() {
            return 1;
        };
        return Class1;
    }();
    var Class2 = function() {
        "use strict";
        function Class2() {
            _classCallCheck(this, Class2);
        }
        var _proto = Class2.prototype;
        _proto.start = function start() {
            return 1;
        };
        return Class2;
    }();
    var Hero = function Hero() {
        "use strict";
        _classCallCheck(this, Hero);
        this.c1 = new Class1();
        this.c2 = new Class2();
    };
});

//# sourceMappingURL=index.swc.umd.js.map

Input code

File name index.js


function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
var Class1 = function() {
    "use strict";
    function Class1() {
        _classCallCheck(this, Class1);
    }
    var _proto = Class1.prototype;
    _proto.go = function go() {
        return 1;
    };
    return Class1;
}();
var Class2 = function() {
    "use strict";
    function Class2() {
        _classCallCheck(this, Class2);
    }
    var _proto = Class2.prototype;
    _proto.start = function start() {
        return 1;
    };
    return Class2;
}();
var Hero = function Hero() {
    "use strict";
    _classCallCheck(this, Hero);
    this.c1 = new Class1();
    this.c2 = new Class2();
};
export { Class1 as Class1 };
export { Class2 as Class2 };
export { Hero as Hero };

Config

{
  "$schema": "http://json.schemastore.org/swcrc",
  "module": {
    "type": "umd",
    "globals": {
      "index": "HeroJs"
    }
  },
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true,
      "dts": true,
      "dynamicImport": true
    },
    "baseUrl": ".",
    "paths": {
      "/*": ["./src/*"]
    }
  },
  "sourceMaps": true
}

Playground link

https://play.swc.rs/?version=1.3.23&code=H4sIAAAAAAAAA52S3WqEMBCF732KqVdZWITN7dIrKfQB9n4JIa5SycgYuy2L796JSST2vxUxP%2BckZz6TZrLadWjhrHs1jrXq%2B7o1%2Bkl0dnTKarOHGrlLk3ZIO7gVwE%2FXgLhbLZA62GzMye0f1xJewZornF4H80CEJMpaWYsONIeCgqUAUPxCE6sqd8dlh7mYi2dFUHvLAe5Xg0gZ5TQa4OROuzKsSZa4SOTVvId1bTfuo3GN9F8feh4IHXJo0Ktl6JgiGINcXTArCy64ySPjJrJwiFuHJk6GXY%2FFLDh5hZT%2FgZS%2FhZQ%2FQMovIPmYyeWcy8TfUGWG%2Bmho89v8%2BBvaz4n8osjjJyrtL4i%2Faengc03mmvQal2heBmSuW7pffAdj74MoV1FuxIWEpaVl4Q2cY2xrWAMAAA%3D%3D&config=H4sIAAAAAAAAA0WNTQoDIQxG75K1227mBKXQQ1gnM1j8I1GoiHdvMli6Svi%2Bl5cBb3awDSiWGEk37qnaD2yALlp25EsFI5hEhw2M00C1dGJVhG%2FShZwZV2sg%2BuSPriaXYyFk%2Flc2neFHThHFvDcNBtReZEKLuwjPkF%2BCaO5TafrpjpQfDNfRerCknp%2FLUqnh%2FAIpHc%2BU0gAAAA%3D%3D

Expected behavior

globs to work like with babel and allow renaming (mapping).

Actual behavior

globals not working.

Version

"@swc/cli": "^0.1.57", "@swc/core": "^1.3.23"

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions