Skip to content

Commit

Permalink
Set up package for module/nomodule distributions
Browse files Browse the repository at this point in the history
We produce variants of the source code by rollup. Note using a "module"
property is not (yet?) standard but the approach both webpack and
rollup.js suggest going forward.
  • Loading branch information
carhartl committed Sep 7, 2019
1 parent 3fd383c commit 87426ab
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
node_modules
build
dist
.sizecache.json
*.log*
5 changes: 2 additions & 3 deletions Gruntfile.js
Expand Up @@ -24,7 +24,6 @@ module.exports = function (grunt) {
}

grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
qunit: {
all: {
options: {
Expand Down Expand Up @@ -52,8 +51,8 @@ module.exports = function (grunt) {
},
compare_size: {
files: [
'build/js.cookie-<%= pkg.version %>.min.mjs',
'build/js.cookie-<%= pkg.version %>.min.js',
'dist/js.cookie.min.mjs',
'dist/js.cookie.min.js',
'src/js.cookie.js'
],
options: {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -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"
},
Expand All @@ -24,7 +25,7 @@
"url": "git://github.com/js-cookie/js-cookie.git"
},
"files": [
"src/**/*.js",
"dist/**/*",
"SERVER_SIDE.md",
"CONTRIBUTING.md"
],
Expand Down
89 changes: 48 additions & 41 deletions rollup.config.js
@@ -1,46 +1,53 @@
import { terser } from "rollup-plugin-terser";
import filesize from "rollup-plugin-filesize";
import license from "rollup-plugin-license";
import pkg from "./package.json";

export default {
input: "src/js.cookie.mjs",
output: [
// config for <script type="module">
{
dir: "build",
entryFileNames: "[name].min.mjs",
format: "esm"
},
{
dir: "build",
entryFileNames: `[name]-${pkg.version}.min.mjs`,
format: "esm"
},
// config for <script nomodule>
{
dir: "build",
name: "Cookies",
entryFileNames: "[name].min.js",
format: "umd"
},
{
dir: "build",
name: "Cookies",
entryFileNames: `[name]-${pkg.version}.min.js`,
format: "umd"
}
],
// rollup-plugin-uglify only works with babel...
plugins: [
terser(),
license({
banner: {
content:
"/*! <%= pkg.name %> v<%= pkg.version %> | <%= pkg.license %> */",
commentStyle: "none"
export default [
{
input: "src/js.cookie.mjs",
output: [
// config for <script type="module">
{
dir: "dist",
entryFileNames: "[name].mjs",
format: "esm"
},
// config for <script nomodule>
{
dir: "dist",
name: "Cookies",
entryFileNames: "[name].js",
format: "umd"
}
}),
filesize()
]
};
]
},
{
input: "src/js.cookie.mjs",
output: [
// config for <script type="module">
{
dir: "dist",
entryFileNames: "[name].min.mjs",
format: "esm"
},
// config for <script nomodule>
{
dir: "dist",
name: "Cookies",
entryFileNames: "[name].min.js",
format: "umd"
}
],
plugins: [
terser(),
license({
banner: {
content:
"/*! <%= pkg.name %> v<%= pkg.version %> | <%= pkg.license %> */",
commentStyle: "none"
}
}),
filesize()
]
}
];
2 changes: 1 addition & 1 deletion test/encoding.html
Expand Up @@ -5,7 +5,7 @@
<title>JavaScript Cookie Test Suite - Encoding</title>
<link href="../node_modules/qunitjs/qunit/qunit.css" rel="stylesheet">
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="../build/js.cookie.min.js"></script>
<script src="../dist/js.cookie.min.js"></script>
<script src="utils.js"></script>
<script src="encoding.js"></script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Expand Up @@ -5,7 +5,7 @@
<title>JavaScript Cookie Test Suite</title>
<link href="../node_modules/qunitjs/qunit/qunit.css" rel="stylesheet">
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="../build/js.cookie.min.js"></script>
<script src="../dist/js.cookie.min.js"></script>
<script src="utils.js"></script>
<script src="tests.js"></script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion test/missing_semicolon.html
Expand Up @@ -8,7 +8,7 @@
<script src="utils.js"></script>
<script>
(function() {
var contents = window.loadFileSync('../build/js.cookie.min.js');
var contents = window.loadFileSync('../dist/js.cookie.min.js');

if (contents !== null) {
var script = document.createElement('script');
Expand Down
8 changes: 4 additions & 4 deletions test/node.js
Expand Up @@ -2,26 +2,26 @@
exports.node = {
should_load_js_cookie: function (test) {
test.expect(1);
var Cookies = require('../build/js.cookie.min.js');
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('../build/js.cookie.min.js');
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('../build/js.cookie.min.js');
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('../build/js.cookie.min.js');
var Cookies = require('../dist/js.cookie.min.js');
Cookies.remove('anything');
Cookies.remove('anything', { path: '' });
test.done();
Expand Down

0 comments on commit 87426ab

Please sign in to comment.