Skip to content

Commit

Permalink
Polyfill WeakMap for IE11 (#37)
Browse files Browse the repository at this point in the history
* Replace WeakMap with object

This update replaces the WeakMap with a regular Object for the cache within
`create-emotion` core. This is to improve IE11 support without adding a (bloated)
polyfill.

* Revert "Replace WeakMap with object"

This reverts commit 563004f.

* Implement a light weight WeakMap polyfill from Google
  • Loading branch information
Jon Quach committed Jan 17, 2019
1 parent 2ae0ab9 commit 7def3f7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const coverageList = [
'!src/create-emotion/**/*.{js,jsx}',
'!src/create-emotion-styled/index.{js,jsx}',
'!src/**/testHelpers.{js,jsx}',
'!src/polyfills/**/*.{js,jsx}',
]

module.exports = Object.assign({}, jestConfig, {
Expand Down
1 change: 1 addition & 0 deletions src/create-emotion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from './utils'
import StyleSheet from './sheet'
import type {PrefixOption, ClassNameArg} from './utils'
import '../polyfills/WeakMap'

type StylisPlugins = Function[] | null | Function

Expand Down
49 changes: 49 additions & 0 deletions src/polyfills/WeakMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2012 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/

if (typeof WeakMap === 'undefined') {
;(function() {
var defineProperty = Object.defineProperty
var counter = Date.now() % 1e9

var WeakMap = function() {
this.name = '__st' + ((Math.random() * 1e9) >>> 0) + (counter++ + '__')
}

WeakMap.prototype = {
set: function(key, value) {
var entry = key[this.name]
if (entry && entry[0] === key) entry[1] = value
else
defineProperty(key, this.name, {
value: [key, value],
writable: true,
})
return this
},
get: function(key) {
var entry
return (entry = key[this.name]) && entry[0] === key
? entry[1]
: undefined
},
delete: function(key) {
var entry = key[this.name]
if (!entry) return false
var hasValue = entry[0] === key
entry[0] = entry[1] = undefined
return hasValue
},
has: function(key) {
var entry = key[this.name]
if (!entry) return false
return entry[0] === key
},
}

window.WeakMap = WeakMap
})()
}

0 comments on commit 7def3f7

Please sign in to comment.