Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modules #13

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.tmp
dist
node_modules
3 changes: 3 additions & 0 deletions JSONCrush.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//Typescript type definitions for JSONCrush
export type JSONCrush = (string: string, maxSubstringLength?: number) => string;
export type JSONUncrush = (string: string) => string;
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "JSONCrush",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad the author didn't claim the jsoncrush package name, but overloading via different casing doesn't seem like a great solution here: this will cause problems on filesystems that aren't strictly case-sensitive, e.g. apple filesystem. Are there any known issues with npm installing a namespaced package from a git repo? If not, consider something like @killedbyapixel/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"
Comment on lines +10 to +12
Copy link

@antialias antialias Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bundle should be built when npm publish (or the yarn equivalent) is run. I suggest invoking build in scripts.prepare.

},
"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": {}
}
30 changes: 30 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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' }
]
};