问题描述
我正在使用Webpack捆绑使用d3和 d3.slider 模块,但这是一个更普遍的问题-我如何要求()一个使用功能(此处为d3.slider)对模块(此处为d3)打补丁的模块())?
I am using webpack to bundle a visualisation built with d3 and a d3.slider module, yet this is a more general problem - how do I require() a module which patches a module (d3 here) with a function (here d3.slider()) ?
package.json:
package.json:
{
"name": "d3_slider_error",
"version": "0.0.1",
"description": "",
"main": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server"
},
"dependencies": {
"d3": "^3.5.15",
"d3-slider": "git+ssh://[email protected]:MasterMaps/d3-slider.git",
"webpack": "^1.9.10",
"webpack-dev-server": "^1.9.0",
"html-webpack-plugin": "^1.5.0"
}
}
webpack.config.js:
webpack.config.js :
var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
module.exports = {
entry: "./src/main.js", // APP_PATH
output: {
filename: 'main.js',
path: path.resolve('./dist') // BUILD_PATH
},
module: {
loaders: [
//
]
},
resolve: {
modulesDirectories: ['node_modules']
},
plugins: [
new HtmlWebpackPlugin({title : "d3-slider-error"})
]
};
src/main.js:
src/main.js:
var d3 = require('d3');
require('d3.slider');
var timeSlider = d3.slider();
console.log("Done!");
此日志:TypeError:d3.slider不是函数
This logs: TypeError: d3.slider is not a function
推荐答案
我找到了一个丑陋的解决方案,但是由于没有其他事情出现,因此我将其发布在这里.我将此添加到package.json依赖项:
I found an ugly'ish solution, but since nothing else came up, I'm posting it here. I add this to package.json dependencies:
"script-loader": "git+ssh://[email protected]:webpack/script-loader.git"
然后在某处,例如在入口点(假设d3.slider位于node_modules中):
Then somewhere, for example in the entry point (assuming that d3.slider is in node_modules):
require("script!d3.slider");
脚本加载器在全局环境中一次执行脚本.应该可以解决所有类似的问题,旧脚本等.
Script-loader executes the script in the global environment once. Should work for all similar problems, legacy scripts etc. Done.
这篇关于注入功能的NodeJS how-to require模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!