From c62785e9360367cb3d3efabef4d92f4a46413cc9 Mon Sep 17 00:00:00 2001 From: Darcy Parker Date: Fri, 8 Jan 2021 11:09:02 -0500 Subject: [PATCH 1/5] Add .gitignore. Ignore node_modules, dist and .tmp dirs --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15ede3c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.tmp +dist +node_modules From 4490b40cbb0c0785f6fc93ce15f8fb7712ccf5b2 Mon Sep 17 00:00:00 2001 From: Darcy Parker Date: Fri, 8 Jan 2021 12:18:50 -0500 Subject: [PATCH 2/5] package.json: Add package.json * Include package-lock.json * Installs rollup & graceful-fs as dev-dependencies which will be used to wrap JSONCrush in module formats; cjs, esm & umd. * Defines preinstall script to install dev-dependencies & build modules in dist folder * See https://github.com/npm/npm/issues/10366#issuecomment-226360668 * rollup & graceful-fs are only used for build and not required by JSONCrush; therefore they are dev-dependencies * Defines paths to 3 module variants: main, browser & module for cjs, esm and umd modules * Defines path to typescript types definition --- package-lock.json | 30 ++++++++++++++++++++++++++++++ package.json | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..ccf18ec --- /dev/null +++ b/package-lock.json @@ -0,0 +1,30 @@ +{ + "name": "jsoncrush", + "version": "1.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "fsevents": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz", + "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==", + "dev": true, + "optional": true + }, + "graceful-fs": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz", + "integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==", + "dev": true + }, + "rollup": { + "version": "2.36.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.36.1.tgz", + "integrity": "sha512-eAfqho8dyzuVvrGqpR0ITgEdq0zG2QJeWYh+HeuTbpcaXk8vNFc48B7bJa1xYosTCKx0CuW+447oQOW8HgBIZQ==", + "dev": true, + "requires": { + "fsevents": "~2.1.2" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..2303fe7 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "JSONCrush", + "version": "1.0.1", + "description": "Compress JSON in URL friendly strings", + "main": "dist/JSONCrush.cjs.js", + "module": "dist/JSONCrush.esm.js", + "browser": "dist/JSONCrush.umd.js", + "types": "JSONCrush.d.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "rollup -c", + "preinstall": "npm install --ignore-scripts && npm run build" + }, + "engines": { + "node": ">=10.0.0" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/KilledByAPixel/JSONCrush.git" + }, + "keywords": [ + "JSON", + "URL", + "URI", + "compression" + ], + "author": "KilledByAPixel", + "license": "MIT", + "bugs": { + "url": "https://github.com/KilledByAPixel/JSONCrush/issues" + }, + "homepage": "https://github.com/KilledByAPixel/JSONCrush", + "devDependencies": { + "graceful-fs": "^4.2.4", + "rollup": "^2.36.1" + }, + "dependencies": {} +} From a6da303a7500d3dfa93406b570439530ed337beb Mon Sep 17 00:00:00 2001 From: Darcy Parker Date: Fri, 8 Jan 2021 12:22:27 -0500 Subject: [PATCH 3/5] rollup.config.js: Simple config to wrap JSONCrush.js in 3 module formats --- rollup.config.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 rollup.config.js diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..b133667 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,30 @@ +import pkg from './package.json'; +import * as path from 'path'; +import {readFileSync, writeFileSync, existsSync, mkdirSync} from 'graceful-fs'; + +//Before exporting rollup config: +//* The rollup input requires a statement with exports for modules +//* The input ./JSONCrush.js does not have exports because it may be used in +// contexts that do not support it. (such as ClosureCompiler) +//* This sources JSONCrush.js, appends `exports` statement, and writes result to +// target .tmp/JSONCrush.js +//* Note .tmp is in .gitignore +const inputFileName = 'JSONCrush.js'; +const input = path.join(__dirname, inputFileName); +const tmpDir = path.join(__dirname, '.tmp'); +if (!existsSync(tmpDir)) { mkdirSync(tmpDir); } +const tmpInput = path.join(tmpDir, inputFileName); +writeFileSync(tmpInput, readFileSync(input) + '\nexport {JSONCrush, JSONUncrush};'); + +//Minimal rollup config to wrap JSONCrush in the module types cjs, es and umd +export default { + input: tmpInput, + output: [ + // CommonJS (for Node) + { file: pkg.main, format: 'cjs' }, + // ES Module + { file: pkg.module, format: 'es' }, + // browser-friendly UMD build + { name: 'JSONCrush', file: pkg.browser, format: 'umd' } + ] +}; From c4a88c3f02e9b5afbf90b05ddcb4b75b7d1a4e0b Mon Sep 17 00:00:00 2001 From: Darcy Parker Date: Fri, 8 Jan 2021 12:22:51 -0500 Subject: [PATCH 4/5] JSONCrush.d.ts: Typescript definition --- JSONCrush.d.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 JSONCrush.d.ts diff --git a/JSONCrush.d.ts b/JSONCrush.d.ts new file mode 100644 index 0000000..fb2deb2 --- /dev/null +++ b/JSONCrush.d.ts @@ -0,0 +1,3 @@ +//Typescript type definitions for JSONCrush +export type JSONCrush = (string: string, maxSubstringLength?: number) => string; +export type JSONUncrush = (string: string) => string; From 0694502511d418391e615884925d1507dfccec80 Mon Sep 17 00:00:00 2001 From: Darcy Parker Date: Fri, 8 Jan 2021 13:45:26 -0500 Subject: [PATCH 5/5] README.md: Update with instructions on using modules --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dea6d2..8b3074e 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,26 @@ This simple system allows for excellent compression of uri encoded JSON strings # [TRY THE LIVE DEMO!](https://killedbyapixel.github.io/JSONCrush) +# Install + +```bash +npm install https://github.com/KilledByAPixel/JSONCrush.git +``` + +`npm install` creates modules for Common JS, ES Module, and UMD modules in `/dist` folder. + +Alternatively, include JSONCrush.js or JSONCrush.min.js in your script; + # How to Use -* Include JSONCrush.js or JSONCrush.min.js in your script +```js +//NodeJS: Require +const {JSONCrush, JSONUncrush} = require('JSONCrush'); + +//ES Module import +import {JSONCrush, JSONUncrush} from 'JSONCrush'; +``` + * Pass a JSON string to JSONCrush to compress it into an encoded URI component. * This result can be safely used in a URL. * To decode, first call searchParams.get() or decodeURIComponent() on the string before uncrushing.