Skip to content

Commit d41f44a

Browse files
committed
update readme for browser
1 parent ff90886 commit d41f44a

File tree

8 files changed

+129
-4
lines changed

8 files changed

+129
-4
lines changed

browser/browser.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser/csvtojson.min.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@
297297
},
298298
"scripts": {
299299
"dev": "tsc -w",
300-
"build": "rm -Rf ./v2 && tsc && npm run build:browser",
300+
"build": "rm -Rf ./v2 && tsc && npm run build:browser && npm run build:browser:window",
301301
"build:browser": "webpack --config ./webpack.config.js",
302+
"build:browser:window": "webpack --config ./webpack.config.js --output-library-target=window --output-library=csv --output-filename=csvtojson.min.js",
302303
"test": "rm -Rf .ts-node && TS_NODE_CACHE_DIRECTORY=.ts-node mocha -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",
303304
"travis": "nyc --reporter lcov mocha -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",
304305
"test:debug": "mocha debug -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",

readme.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const csvtojsonV2=require("csvtojson/v2");
4343

4444
* [Quick Start](#quick-start)
4545
* [API](#api)
46+
* [Browser Usage](#browser-usage)
4647
* [Contribution](#contribution)
4748

4849
# Quick Start
@@ -636,3 +637,43 @@ Thank you to all our sponsors! (please ask your company to also support this ope
636637

637638
[![donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DUBQLRPJADJFQ)
638639

640+
# Browser Usage
641+
642+
To use `csvtojson` in browser is quite simple. There are two ways:
643+
644+
**1. Embed script directly into script tag**
645+
646+
There is a pre-built script located in `browser/csvtojson.min.js`. Simply include that file in a `script` tag in `index.html` page:
647+
648+
```html
649+
<script src="node_modules/csvtojson/browser/csvtojson.min.js"></script>
650+
<!-- or use cdn -->
651+
<script src="https://cdn.rawgit.com/Keyang/node-csvtojson/ff908866/browser/csvtojson.min.js"></script>
652+
```
653+
then use a global `csv` function
654+
```html
655+
<script>
656+
csv({
657+
output: "csv"
658+
})
659+
.fromString("a,b,c\n1,2,3")
660+
.then(function(result){
661+
662+
})
663+
</script>
664+
```
665+
666+
667+
668+
**2. Use webpack or browserify**
669+
670+
If a module packager is preferred, just simply `require("csvtojson")`:
671+
672+
```js
673+
var csv=require("csvtojson");
674+
675+
// or with import
676+
import * as csv from "csvtojson");
677+
678+
//then use csv as normal
679+
```

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import { Converter } from "./Converter";
55
const helper = function (param?: Partial<CSVParseParam>, options?: TransformOptions): Converter {
66
return new Converter(param, options);
77
}
8-
8+
helper["csv"] = helper;
99
helper["Converter"] = Converter;
10-
export =helper;
10+
export =helper;

v2/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webpack.config.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var webpack = require('webpack')
2+
var path = require('path')
3+
4+
5+
6+
7+
/*
8+
* SplitChunksPlugin is enabled by default and replaced
9+
* deprecated CommonsChunkPlugin. It automatically identifies modules which
10+
* should be splitted of chunk by heuristics using module duplication count and
11+
* module category (i. e. node_modules). And splits the chunks…
12+
*
13+
* It is safe to remove "splitChunks" from the generated configuration
14+
* and was added as an educational example.
15+
*
16+
* https://webpack.js.org/plugins/split-chunks-plugin/
17+
*
18+
*/
19+
20+
/*
21+
* We've enabled UglifyJSPlugin for you! This minifies your app
22+
* in order to load faster and run less javascript.
23+
*
24+
* https://github.com/webpack-contrib/uglifyjs-webpack-plugin
25+
*
26+
*/
27+
28+
var UglifyJSPlugin = require('uglifyjs-webpack-plugin');
29+
30+
31+
32+
33+
module.exports = {
34+
module: {
35+
rules: []
36+
},
37+
38+
entry: "./index.js",
39+
40+
output: {
41+
filename: 'browser.js',
42+
path: path.resolve(__dirname, 'browser'),
43+
libraryTarget: "commonjs2"
44+
},
45+
46+
mode: 'production',
47+
plugins: [
48+
new UglifyJSPlugin(),
49+
new webpack.IgnorePlugin(/fs/),
50+
],
51+
optimization: {
52+
splitChunks: {
53+
cacheGroups: {
54+
vendors: {
55+
priority: -10,
56+
test: /[\\/]node_modules[\\/]/
57+
}
58+
},
59+
60+
chunks: 'async',
61+
minChunks: 1,
62+
minSize: 30000,
63+
name: true
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)