diff --git a/.gitignore b/.gitignore index 15812b0e..d4ca9e24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules -build +dist .sizecache.json *.log* diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a3bd083e..a2426f0e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,7 +13,7 @@ ### Tools We use the following tools for development: -- [Qunit](http://qunitjs.com/) for tests. +- [QUnit](http://qunitjs.com/) for tests. - [NodeJS](http://nodejs.org/download/) required to run grunt. - [Grunt](http://gruntjs.com/getting-started) for task management. diff --git a/Gruntfile.js b/Gruntfile.js index aa51f006..09902dfc 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -24,14 +24,12 @@ module.exports = function (grunt) { } grunt.initConfig({ - pkg: grunt.file.readJSON('package.json'), qunit: { all: { options: { 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/module.html', 'http://127.0.0.1:9998/encoding.html?integration_baseurl=http://127.0.0.1:9998/' ] } @@ -42,34 +40,21 @@ module.exports = function (grunt) { }, eslint: { grunt: 'Gruntfile.js', - source: 'src/**/*.js', + source: 'src/**/*.mjs', tests: 'test/**/*.js' }, - uglify: { - options: { - compress: { - unsafe: true - }, - banner: '/*! <%= pkg.name %> v<%= pkg.version %> | <%= pkg.license %> */\n' - }, - build: { - files: { - 'build/js.cookie.min.js': 'src/js.cookie.js', - 'build/js.cookie-<%= pkg.version %>.min.js': 'src/js.cookie.js' - } - } - }, watch: { options: { livereload: true }, - files: '{src,test}/**/*.js', + files: ['src/**/*.mjs', 'test/**/*.js'], tasks: 'default' }, compare_size: { files: [ - 'build/js.cookie-<%= pkg.version %>.min.js', - 'src/js.cookie.js' + 'dist/js.cookie.min.mjs', + 'dist/js.cookie.min.js', + 'src/js.cookie.mjs' ], options: { compress: { @@ -105,6 +90,7 @@ module.exports = function (grunt) { } }, exec: { + 'rollup': './node_modules/.bin/rollup -c', 'browserstack-runner': 'node_modules/.bin/browserstack-runner --verbose' } }); @@ -116,8 +102,8 @@ module.exports = function (grunt) { } } - grunt.registerTask('test', ['uglify', 'eslint', 'connect:build-qunit', 'qunit', 'nodeunit']); - grunt.registerTask('browserstack', ['uglify', 'exec:browserstack-runner']); + grunt.registerTask('test', ['exec:rollup', 'eslint', 'connect:build-qunit', 'qunit', 'nodeunit']); + grunt.registerTask('browserstack', ['exec:rollup', 'exec:browserstack-runner']); grunt.registerTask('dev', ['test', 'compare_size']); diff --git a/README.md b/README.md index 1b9b1a08..252fc772 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A simple, lightweight JavaScript API for handling cookies * [Heavily](test) tested * No dependency * [Unobtrusive](#json) JSON support +* Supports ES modules * Supports AMD/CommonJS * [RFC 6265](https://tools.ietf.org/html/rfc6265) compliant * Useful [Wiki](https://github.com/js-cookie/js-cookie/wiki) @@ -22,35 +23,61 @@ A simple, lightweight JavaScript API for handling cookies ## Installation +### NPM + +JavaScript Cookie supports [npm](https://www.npmjs.com/package/js-cookie) under the name `js-cookie`. + +``` +$ npm install js-cookie --save +``` + ### Direct download -Download the script [here](https://github.com/js-cookie/js-cookie/blob/latest/src/js.cookie.js) and include it (unless you are packaging scripts somehow else): +The source comes as an ES module. If you download it [here](https://github.com/js-cookie/js-cookie/blob/latest/src/js.cookie.mjs) directly, you must include it as such. + +Example: ```html - + + ``` -Or include it via [jsDelivr CDN](https://www.jsdelivr.com/package/npm/js-cookie): +*Not all browsers support ES modules natively yet*. For this reason the npm package/release +comes with both an ES module as well as an UMD module variant. Include the module along +with the fallback to account for this: ```html - + + ``` -**Do not include the script directly from GitHub (http://raw.github.com/...).** The file is being served as text/plain and as such being blocked -in Internet Explorer on Windows 7 for instance (because of the wrong MIME type). Bottom line: GitHub is not a CDN. +Note the different extensions: `.mjs` denotes an ES module. -### Package Managers +### CDN -JavaScript Cookie supports [npm](https://www.npmjs.com/package/js-cookie) and [Bower](http://bower.io/search/?q=js-cookie) under the name `js-cookie`. +Alternatively, include it via [jsDelivr CDN](https://www.jsdelivr.com/package/npm/js-cookie): -#### NPM -``` - $ npm install js-cookie --save +```html + ``` -### Module Loaders +**Never include the source directly from GitHub (http://raw.github.com/...).** The file +is being served as text/plain and as such may be blocked because of the wrong MIME type. +Bottom line: GitHub is not a CDN. + +## ES Module -JavaScript Cookie can also be loaded as an AMD or CommonJS module. +Example for how to import the ES module from another module: + +```javascript +import Cookies from "./node_modules/js-cookie/dist/js.cookie.mjs"; + +Cookies.set('foo', 'bar'); +``` ## Basic Usage @@ -303,7 +330,7 @@ For vulnerability reports, send an e-mail to `jscookieproject at gmail dot com` ## Manual release steps * Increment the "version" attribute of `package.json` -* Increment the version number in the `src/js.cookie.js` file +* Increment the version number in the `src/js.cookie.mjs` file * If `major` bump, update jsDelivr CDN major version link on README * Commit with the message "Release version x.x.x" * Create version tag in git diff --git a/bower.json b/bower.json deleted file mode 100644 index 9678d99f..00000000 --- a/bower.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "js-cookie", - "license": "MIT", - "main": [ - "src/js.cookie.js" - ], - "ignore": [ - "test", - "Gruntfile.js", - "package.json", - ".gitignore", - ".eslintintignore", - ".eslintrc", - ".tm_properties", - ".travis.yml" - ] -} diff --git a/package.json b/package.json index ec330980..e894d2a4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "js-cookie", "version": "2.2.1", "description": "A simple, lightweight JavaScript API for handling cookies", - "main": "src/js.cookie.js", + "main": "dist/js.cookie.js", + "module": "dist/js.cookie.mjs", "directories": { "test": "test" }, @@ -24,7 +25,7 @@ "url": "git://github.com/js-cookie/js-cookie.git" }, "files": [ - "src/**/*.js", + "dist/**/*", "SERVER_SIDE.md", "CONTRIBUTING.md" ], @@ -37,12 +38,14 @@ "grunt-contrib-connect": "2.1.0", "grunt-contrib-nodeunit": "2.0.0", "grunt-contrib-qunit": "3.1.0", - "grunt-contrib-uglify": "4.0.1", "grunt-contrib-watch": "1.1.0", "grunt-eslint": "22.0.0", "grunt-exec": "3.0.0", "gzip-js": "0.3.2", - "qunitjs": "1.23.1", - "requirejs": "2.3.5" + "qunit": "2.9.2", + "rollup": "^1.20.3", + "rollup-plugin-filesize": "^6.2.0", + "rollup-plugin-license": "^0.12.1", + "rollup-plugin-terser": "^5.1.1" } } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..70206b97 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,55 @@ +import { terser } from "rollup-plugin-terser"; +import filesize from "rollup-plugin-filesize"; +import license from "rollup-plugin-license"; + +export default [ + { + input: "src/js.cookie.mjs", + output: [ + // config for - - - - -
-
- - diff --git a/test/amd.js b/test/amd.js deleted file mode 100644 index 5f655f14..00000000 --- a/test/amd.js +++ /dev/null @@ -1,14 +0,0 @@ -require(['qunit'], function (QUnit) { - QUnit.module('amd'); - - QUnit.start(); - QUnit.test('module loading', function (assert) { - assert.expect(1); - var done = assert.async(); - require(['/src/js.cookie.js'], function (Cookies) { - assert.ok(!!Cookies.get, 'should load the api'); - done(); - }); - }); - -}); diff --git a/test/encoding.html b/test/encoding.html index a2f898bf..6b59ea8d 100644 --- a/test/encoding.html +++ b/test/encoding.html @@ -3,9 +3,9 @@ JavaScript Cookie Test Suite - Encoding - - - + + + diff --git a/test/environment-with-amd-and-umd.html b/test/environment-with-amd-and-umd.html deleted file mode 100644 index 9e444f54..00000000 --- a/test/environment-with-amd-and-umd.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - - JavaScript Cookie Test Suite - Environment with AMD and UMD - - - - - - -
-
- - diff --git a/test/environment-with-amd-and-umd.js b/test/environment-with-amd-and-umd.js deleted file mode 100644 index b4f32502..00000000 --- a/test/environment-with-amd-and-umd.js +++ /dev/null @@ -1,28 +0,0 @@ -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(); - }); - }); - -}); diff --git a/test/index.html b/test/index.html index 6d416912..7f014d95 100644 --- a/test/index.html +++ b/test/index.html @@ -3,10 +3,9 @@ JavaScript Cookie Test Suite - - - - + + + diff --git a/test/missing_semicolon.html b/test/missing_semicolon.html index 385efd57..b1ac380d 100644 --- a/test/missing_semicolon.html +++ b/test/missing_semicolon.html @@ -3,12 +3,9 @@ - - - + + + +
+
+ + diff --git a/test/module.mjs b/test/module.mjs new file mode 100644 index 00000000..46147087 --- /dev/null +++ b/test/module.mjs @@ -0,0 +1,6 @@ +import Cookies from "../dist/js.cookie.min.mjs"; + +QUnit.test("default export", function(test) { + test.expect(1); + test.ok(!!Cookies.get, "should provide API"); +}); diff --git a/test/node.js b/test/node.js index 861ed1b5..d1338de5 100644 --- a/test/node.js +++ b/test/node.js @@ -2,26 +2,26 @@ exports.node = { should_load_js_cookie: function (test) { test.expect(1); - var Cookies = require('../src/js.cookie'); + var Cookies = require('../dist/js.cookie.min.js'); test.ok(!!Cookies.get, 'should load the Cookies API'); test.done(); }, should_not_throw_error_for_set_call_in_node: function (test) { test.expect(0); - var Cookies = require('../src/js.cookie'); + var Cookies = require('../dist/js.cookie.min.js'); Cookies.set('anything'); Cookies.set('anything', { path: '' }); test.done(); }, should_not_throw_error_for_get_call_in_node: function (test) { test.expect(0); - var Cookies = require('../src/js.cookie'); + var Cookies = require('../dist/js.cookie.min.js'); Cookies.get('anything'); test.done(); }, should_not_throw_error_for_remove_call_in_node: function (test) { test.expect(0); - var Cookies = require('../src/js.cookie'); + var Cookies = require('../dist/js.cookie.min.js'); Cookies.remove('anything'); Cookies.remove('anything', { path: '' }); test.done(); diff --git a/test/tests.js b/test/tests.js index 4c1b0c2c..f0877d48 100644 --- a/test/tests.js +++ b/test/tests.js @@ -453,14 +453,3 @@ QUnit.test('Prevent accidentally writing cookie when passing unexpected argument Cookies.getJSON('c', { foo: 'bar' }); assert.strictEqual(Cookies.get('c'), undefined, 'should not write any cookie'); }); - -QUnit.module('noConflict', lifecycle); - -QUnit.test('do not conflict with existent globals', function (assert) { - assert.expect(2); - var Cookies = window.Cookies.noConflict(); - Cookies.set('c', 'v'); - assert.strictEqual(Cookies.get('c'), 'v', 'should work correctly'); - assert.strictEqual(window.Cookies, 'existent global', 'should restore the original global'); - window.Cookies = Cookies; -});