【发布时间】:2018-01-21 12:42:33
【问题描述】:
我有一个使用本地 json 文件的 d3.js 制作的图表。
为了可视化数据,我必须使用网络服务器,所以我决定使用 webpack,因为它还提供热重载。
问题是我仅限于所选文件 (data.json),因为它是出现在入口点 (index.js) 中的文件:
index.js
d3.json("data.json", function(error, d) {
// get data and draw chart
当我想显示图表时,我使用npm start 并转到localhost:8080
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
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'}
])
]
}
如何将 json 文件作为参数传递?类似:
npm start "another_data.json"
还是使用节点?
node "another_data.json"
【问题讨论】:
标签: javascript json node.js d3.js