Skip to content

Commit

Permalink
Register in AMD and UMD if both available
Browse files Browse the repository at this point in the history
  • Loading branch information
FagnerMartinsBrack committed Aug 26, 2016
1 parent 583c524 commit 92f8e2b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = function (grunt) {
urls: [
'http://127.0.0.1:9998/',
'http://127.0.0.1:9998/amd.html',
'http://127.0.0.1:9998/environment-with-amd-and-umd.html',
'http://127.0.0.1:9998/encoding.html?integration_baseurl=http://127.0.0.1:9998/'
]
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A simple, lightweight JavaScript API for handling cookies
* [RFC 6265](https://tools.ietf.org/html/rfc6265) compliant
* Useful [Wiki](https://github.com/js-cookie/js-cookie/wiki)
* Enable [custom encoding/decoding](#converters)
* **~800 bytes** gzipped!
* **~900 bytes** gzipped!

**If you're viewing this at https://github.com/js-cookie/js-cookie, you're reading the documentation for the master branch.
[View documentation for the latest release (2.1.2).](https://github.com/js-cookie/js-cookie/tree/v2.1.2#readme)**
Expand Down
9 changes: 7 additions & 2 deletions src/js.cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
* Released under the MIT license
*/
;(function (factory) {
var registeredInModuleLoader = false;
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
registeredInModuleLoader = true;
}
if (typeof exports === 'object') {
module.exports = factory();
} else {
registeredInModuleLoader = true;
}
if (!registeredInModuleLoader) {
var OldCookies = window.Cookies;
var api = window.Cookies = factory();
api.noConflict = function () {
Expand Down
15 changes: 15 additions & 0 deletions test/environment-with-amd-and-umd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Cookie Test Suite - Environment with AMD and UMD</title>
<link href="../node_modules/qunitjs/qunit/qunit.css" rel="stylesheet">
<script src="amd-config.js"></script>
<script src="../node_modules/requirejs/require.js"></script>
<script src="environment-with-amd-and-umd.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
28 changes: 28 additions & 0 deletions test/environment-with-amd-and-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require(['qunit'], function (QUnit) {
QUnit.start();

QUnit.module('Environment with AMD and UMD', {
beforeEach: function () {
window.exports = {};
window.module = {
exports: window.exports
};
},
afterEach: function () {
delete window.module;
}
});

QUnit.test('js-cookie need to register itself in AMD and UMD', function (assert) {
assert.expect(2);
var done = assert.async();
require(['/src/js.cookie.js'], function () {
var actual = typeof window.module.exports;
var expected = 'function';
assert.strictEqual(actual, expected, 'should register a function in module.exports');
assert.notOk(!!window.Cookies, 'should not register globally in AMD/UMD environments');
done();
});
});

});

0 comments on commit 92f8e2b

Please sign in to comment.