Skip to content

Commit 92f8e2b

Browse files
Register in AMD and UMD if both available
Closes js-cookiegh-257. Closes js-cookiegh-244.
1 parent 583c524 commit 92f8e2b

5 files changed

+52
-3
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function (grunt) {
3131
urls: [
3232
'http://127.0.0.1:9998/',
3333
'http://127.0.0.1:9998/amd.html',
34+
'http://127.0.0.1:9998/environment-with-amd-and-umd.html',
3435
'http://127.0.0.1:9998/encoding.html?integration_baseurl=http://127.0.0.1:9998/'
3536
]
3637
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A simple, lightweight JavaScript API for handling cookies
1515
* [RFC 6265](https://tools.ietf.org/html/rfc6265) compliant
1616
* Useful [Wiki](https://github.com/js-cookie/js-cookie/wiki)
1717
* Enable [custom encoding/decoding](#converters)
18-
* **~800 bytes** gzipped!
18+
* **~900 bytes** gzipped!
1919

2020
**If you're viewing this at https://github.com/js-cookie/js-cookie, you're reading the documentation for the master branch.
2121
[View documentation for the latest release (2.1.2).](https://github.com/js-cookie/js-cookie/tree/v2.1.2#readme)**

src/js.cookie.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
* Released under the MIT license
77
*/
88
;(function (factory) {
9+
var registeredInModuleLoader = false;
910
if (typeof define === 'function' && define.amd) {
1011
define(factory);
11-
} else if (typeof exports === 'object') {
12+
registeredInModuleLoader = true;
13+
}
14+
if (typeof exports === 'object') {
1215
module.exports = factory();
13-
} else {
16+
registeredInModuleLoader = true;
17+
}
18+
if (!registeredInModuleLoader) {
1419
var OldCookies = window.Cookies;
1520
var api = window.Cookies = factory();
1621
api.noConflict = function () {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JavaScript Cookie Test Suite - Environment with AMD and UMD</title>
6+
<link href="../node_modules/qunitjs/qunit/qunit.css" rel="stylesheet">
7+
<script src="amd-config.js"></script>
8+
<script src="../node_modules/requirejs/require.js"></script>
9+
<script src="environment-with-amd-and-umd.js"></script>
10+
</head>
11+
<body>
12+
<div id="qunit"></div>
13+
<div id="qunit-fixture"></div>
14+
</body>
15+
</html>

test/environment-with-amd-and-umd.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require(['qunit'], function (QUnit) {
2+
QUnit.start();
3+
4+
QUnit.module('Environment with AMD and UMD', {
5+
beforeEach: function () {
6+
window.exports = {};
7+
window.module = {
8+
exports: window.exports
9+
};
10+
},
11+
afterEach: function () {
12+
delete window.module;
13+
}
14+
});
15+
16+
QUnit.test('js-cookie need to register itself in AMD and UMD', function (assert) {
17+
assert.expect(2);
18+
var done = assert.async();
19+
require(['/src/js.cookie.js'], function () {
20+
var actual = typeof window.module.exports;
21+
var expected = 'function';
22+
assert.strictEqual(actual, expected, 'should register a function in module.exports');
23+
assert.notOk(!!window.Cookies, 'should not register globally in AMD/UMD environments');
24+
done();
25+
});
26+
});
27+
28+
});

0 commit comments

Comments
 (0)