Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot convert object to primitive value with core-js 3.1.3 + webpack #566

Closed
PikachuEXE opened this issue May 28, 2019 · 15 comments
Closed

Comments

@PikachuEXE
Copy link

Env

  • webpack 4.32.2
  • babel-loader 8.0.6
  • core-js 3.1.3

Not sure what else should be added
Will update this list if more entries are needed

Description

I already applied patch from rails/webpacker#2031 to fix #514
But now I got this error
Below are screenshots to the backtrace

1
2
3
4

@jacobp100
Copy link

I've narrowed it down to this statement being broken,

$defineProperty({}, Symbol.toStringTag, { value: 'test' })

As you can see in the above code, webpack seems to add this to every harmony module

@zloirock
Copy link
Owner

In which engine? Could you add a reproducible example?

@jacobp100
Copy link

IE11. If you run my exact example in IE11, it throws an exception

@zloirock
Copy link
Owner

@jacobp100 you really think that this example is not tested and still no one run webpack + babel-loader + core-js@3 where it will be added in modules by default? Seems the problem how it's used - configs, possible conflicts with other polyfills, etc.

@PikachuEXE
Copy link
Author

I can see the backtrace as:

1. Webpack require => Define property with defineProperty on Symbol.toStringTag

// define __esModule on exports
__webpack_require__.r = function(exports) {
	if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
		Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
	}
	Object.defineProperty(exports, '__esModule', { value: true });
};

2. Calling core-js symbol polyfill of defineProperty in core-js/modules/es.symbol.js

Key statement is key = toPrimitive(key, true)

var $defineProperty = function defineProperty(it, key, D) {
  if (it === ObjectPrototype) $defineProperty(ObjectPrototypeSymbols, key, D);
  anObject(it);
  key = toPrimitive(key, true);
  anObject(D);
  if (has(AllSymbols, key)) {
    if (!D.enumerable) {
      if (!has(it, HIDDEN)) nativeDefineProperty(it, HIDDEN, createPropertyDescriptor(1, {}));
      it[HIDDEN][key] = true;
    } else {
      if (has(it, HIDDEN) && it[HIDDEN][key]) it[HIDDEN][key] = false;
      D = nativeObjectCreate(D, { enumerable: createPropertyDescriptor(0, false) });
    } return setSymbolDescriptor(it, key, D);
  } return nativeDefineProperty(it, key, D);
};

3. Calling toPrimitive in core-js/internals/to-primitive.js

var isObject = require('../internals/is-object');

// 7.1.1 ToPrimitive(input [, PreferredType])
// instead of the ES6 spec version, we didn't implement @@toPrimitive case
// and the second argument - flag - preferred type is a string
module.exports = function (it, S) {
  if (!isObject(it)) return it;
  var fn, val;
  if (S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it))) return val;
  if (typeof (fn = it.valueOf) == 'function' && !isObject(val = fn.call(it))) return val;
  if (!S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it))) return val;
  throw TypeError("Can't convert object to primitive value");
};

That means value referenced by Symbol.toStringTag is incompatible with ToPrimitive

Here is a bit of console debugging in IE 11:
1
2
3
4

If I remove import "core-js/modules/es.symbol" the error is gone
But I have no idea what else will be broken

@PikachuEXE
Copy link
Author

It seems that jscomp_symbol_xxxx (the polyfill) is from Google Maps JS file
https://maps.googleapis.com/maps/api/js?v=3&libraries=places

Loading it after the file containing core-js symbol polyfill would solve this issue

@jacobp100
Copy link

Thanks @PikachuEXE - we had exactly the same issue with Google Maps, and managed to fix it with your solution 😀

@Cobertos
Copy link

This also seems to be an issue with Google's Recaptcha API. Removing it from the project or making it load after your packed bundle fixes IE11. The file in question that I had to move was //www.google.com/recaptcha/api.js?h1=en&render=explicit.

The specific IE11 error I was getting was:

SCRIPT5005: String expected in the regenerator-runtime library that seems to extend from this Symbol polyfill conflict.

image

Kind of annoying...

@firestar300
Copy link

@Cobertos Did you find a fix ?

@Cobertos
Copy link

@firestar300 Yeah, you have to make sure recaptcha loads after any other script that messes with Symbol, including polyfills or webpacked bundles. Moving it to just before the end of the <body> tag fixed it for our apps.

@firestar300
Copy link

Thanks :)

@greatwitenorth
Copy link

I'm having the exact same issue as @PikachuEXE. Same stack trace and everything, but I'm not using Google maps or recaptcha on my page. Any other ideas as to why this error might be happening in ie11?

@erictonussi
Copy link

I reverted to core-js 2.5.7 and all the module.export errors stopped displaying.

@judygab
Copy link

judygab commented Aug 24, 2020

It seems that jscomp_symbol_xxxx (the polyfill) is from Google Maps JS file
https://maps.googleapis.com/maps/api/js?v=3&libraries=places

Loading it after the file containing core-js symbol polyfill would solve this issue

@PikachuEXE what do you mean by file containing core-js symbol polyfil?

@PikachuEXE
Copy link
Author

Any file that import core-js/features/symbol or other polyfill requiring that module
@judygab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants