简体   繁体   English

将本地json数据从终端加载到d3.js和Webpack

[英]Load local json data from terminal to d3.js and webpack

I have a chart made with d3.js that uses a local json file. 我有一个使用本地json文件的d3.js制成的图表。

In order to visualize the data I have to use a web server, so I decided to used webpack because it also ofered hot-reload. 为了可视化数据,我必须使用Web服务器,因此我决定使用Webpack,因为它还会提供热重载。

The thing is I'm limited to the selected file ( data.json ) because it's the file that appears in the entry point ( index.js ): 问题是我仅限于所选文件( data.json ),因为它是出现在入口点( index.js )中的文件:

index.js index.js

d3.json("data.json", function(error, d) {
    // get data and draw chart

And when I want to show the chart I use npm start and go to localhost:8080 当我想显示图表时,我使用npm start并转到localhost:8080

package.json 的package.json

{
  "name": "tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server",
    "build": "babel src -d lib",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-preset-env": "^1.3.3",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "style-loader": "^0.16.1"
  }
}

webpack.config.js webpack.config.js

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  inject: 'body'
})
module.exports = { 
  entry: './src/index.js', 
  output: { 
    path: path.resolve('dist'), 
    filename: 'index_bundle.js'
  }, 
  module: { 
    loaders: [ 
      { test: /\.js$/, 
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      { test:/\.css$/,
        loader: ['style-loader', 'css-loader'],
        exclude: /node_modules/
      },
    ]
  }, 
  plugins: [
    HtmlWebpackPluginConfig,
    new CopyWebpackPlugin([
      { from: 'src/data.json'}
    ])
    ]
}

How could I pass the json file as a parameter? 如何传递json文件作为参数? something like: 就像是:

npm start "another_data.json"

or with node? 或与节点?

node "another_data.json"

Well you can use process.argv which is array containing all command line arguments. 好吧,您可以使用process.argv,它是包含所有命令行参数的数组。 You can try: 你可以试试:

process.argv.forEach(function (item, index) {
  console.log(index + ': ' + val);
});

So if you call node app.js "ok" "non" , you get something like that: 因此,如果您将node app.js "ok" "non"称为node app.js "ok" "non" ,则会得到以下信息:

0: /usr/src/node/node-v4.4.7-linux-x64/bin/node
1: /home/scrapbook/tutorial/app.js
2: ok
3: non

Or with npm: 或使用npm:

{
  "name": "adsf",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "special": "node app.js"
  },
  "author": "",
  "license": "ISC"
}

With app.js: 使用app.js:

console.log(process.env.npm_config_myVar);

Call npm run special --myVar="special.js" 调用npm run special --myVar="special.js"

Result should be: 结果应为:

special.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM