Skip to content
This repository was archived by the owner on Dec 1, 2018. It is now read-only.

Commit 2b9e001

Browse files
committed
Added localstorage polyfill
1 parent 9cc9c75 commit 2b9e001

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

config/polyfills/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = [require.resolve('babel-polyfill'), require.resolve('./localstorage')];

config/polyfills/localstorage.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-disable */
2+
if (!window.localStorage) {
3+
Object.defineProperty(
4+
window,
5+
'localStorage',
6+
new function() {
7+
var aKeys = [],
8+
oStorage = {};
9+
Object.defineProperty(oStorage, 'getItem', {
10+
value: function(sKey) {
11+
return sKey ? this[sKey] : null;
12+
},
13+
writable: false,
14+
configurable: false,
15+
enumerable: false,
16+
});
17+
Object.defineProperty(oStorage, 'key', {
18+
value: function(nKeyId) {
19+
return aKeys[nKeyId];
20+
},
21+
writable: false,
22+
configurable: false,
23+
enumerable: false,
24+
});
25+
Object.defineProperty(oStorage, 'setItem', {
26+
value: function(sKey, sValue) {
27+
if (!sKey) {
28+
return;
29+
}
30+
document.cookie =
31+
escape(sKey) + '=' + escape(sValue) + '; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
32+
},
33+
writable: false,
34+
configurable: false,
35+
enumerable: false,
36+
});
37+
Object.defineProperty(oStorage, 'length', {
38+
get: function() {
39+
return aKeys.length;
40+
},
41+
configurable: false,
42+
enumerable: false,
43+
});
44+
Object.defineProperty(oStorage, 'removeItem', {
45+
value: function(sKey) {
46+
if (!sKey) {
47+
return;
48+
}
49+
document.cookie = escape(sKey) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
50+
},
51+
writable: false,
52+
configurable: false,
53+
enumerable: false,
54+
});
55+
Object.defineProperty(oStorage, 'clear', {
56+
value: function() {
57+
if (!aKeys.length) {
58+
return;
59+
}
60+
for (var sKey in aKeys) {
61+
document.cookie = escape(sKey) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
62+
}
63+
},
64+
writable: false,
65+
configurable: false,
66+
enumerable: false,
67+
});
68+
this.get = function() {
69+
var iThisIndx;
70+
for (var sKey in oStorage) {
71+
iThisIndx = aKeys.indexOf(sKey);
72+
if (iThisIndx === -1) {
73+
oStorage.setItem(sKey, oStorage[sKey]);
74+
} else {
75+
aKeys.splice(iThisIndx, 1);
76+
}
77+
delete oStorage[sKey];
78+
}
79+
for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) {
80+
oStorage.removeItem(aKeys[0]);
81+
}
82+
for (
83+
var aCouple, iKey, nIdx = 0, aCouples = document.cookie.split(/\s*;\s*/);
84+
nIdx < aCouples.length;
85+
nIdx++
86+
) {
87+
aCouple = aCouples[nIdx].split(/\s*=\s*/);
88+
if (aCouple.length > 1) {
89+
oStorage[(iKey = unescape(aCouple[0]))] = unescape(aCouple[1]);
90+
aKeys.push(iKey);
91+
}
92+
}
93+
return oStorage;
94+
};
95+
this.configurable = false;
96+
this.enumerable = true;
97+
}()
98+
);
99+
}

config/webpack/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { resolveAppPath, projectDirectory } = require('../paths');
1616
const babelConfig = require('../babel/babel.config');
1717
const eslintConfig = require('../eslint/eslint.config');
1818
const { browsersList } = require('../globals');
19+
const polyfills = require('../polyfills');
1920

2021
const appPath = resolveAppPath('app');
2122
const buildPath = resolveAppPath('dist');
@@ -51,7 +52,7 @@ module.exports = (env = { dev: true }) => {
5152
// Entry points //
5253
////////////////////////////////////////////////
5354
entry: {
54-
app: ['babel-polyfill', './app/app.js'],
55+
app: [...polyfills, './app/app.js'],
5556
},
5657
//////////////////////////////////////////////////
5758
// Output //

0 commit comments

Comments
 (0)