A Webpack plugin for loading dsv files (for example .csv).
Install via npm:
npm install --save-dev dsv-loader
You can require dsv data like this:
const data = require('dsv-loader!./data.csv');
The loader will translate the data.csv
file into a JSON Object.
To require dsv files like this: require('data.csv')
, you can add the dsv-loader to your webpack config:
module: {
rules: [{
test: /\.(c|d|t)sv$/, // load all .csv, .dsv, .tsv files with dsv-loader
use: ['dsv-loader'] // or dsv-loader?delimiter=,
}]
}
data.csv
Hello | World |
---|---|
42 | "fish" |
foo | bar |
const data = require(data.csv) //[{"Hello": "42", "World": "fish"}, {"Hello": "foo", "World": "bar"}, columns: ["Hello", "World"]]
delimiter
Tells the loader which delimiter is used to seperate the data. Default: ','
Examples:
const data = require('dsv-loader?delimiter=;!./data.csv'); //load data seperated by semicolon
const data = require('dsv-loader?delimiter=x!./data.csv'); //load data seperated by an 'x'
rows
Tells the loader if it should use the first row as columns row. Default: Yes
Examples:
const data = require('dsv-loader!./data.csv'); //[{"Hello": "42", "World": "fish"}, {"Hello": "foo", "World": "bar"}, columns: ["Hello", "World"]]
const data = require('dsv-loader?rows!./data.csv'); //[{"Hello": "World"}, { "42": "fish"},{ "foo": "bar"}
- Inspired by webpack's json-loader
- DSV parsing done with dsv by Mike Bostock