我有一个用d3.js制作的图表,它使用本地json文件。
为了可视化数据,我不得不使用一个web服务器,所以我决定使用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"发布于 2017-08-13 20:59:13
您可以使用process.argv,它是包含所有命令行参数的数组。你可以试试:
process.argv.forEach(function (item, index) {
console.log(index + ': ' + val);
});所以,如果你打电话给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或与国家预防机制:
{
"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"
}使用app.js:
console.log(process.env.npm_config_myVar);调用npm run special --myVar="special.js"
其结果应是:
special.jshttps://stackoverflow.com/questions/45661597
复制相似问题