假设我有一个文件:
//nonModuled.js
//A non moduled file , let's say I can't "module" it
console.log('0');
function go(a)
{
console.log('go:' + a);
}
我还有另一个文件想要获取 go
函数:
//1.js
require('./nonModuled.js');
当我运行 html 文件时,我确实看到了 console.log
,但我收到了 go
函数的错误:
我确实明白为什么会发生这种情况。另外 - 我知道我可以做这个补丁:
//nonModuled.js
//A non moduled file , let's say I can't touch it
console.log('hello');
window.go = function go(a)
{
console.log('go:' + a);
}
然后在 1.js
文件中,访问 window.go
但这看起来很笨拙。
所以我问:
问题:
如何正确获取go
函数?
如果我能做这样的事情那就太好了:
var a= require('./nonModuled.js');
a.go()
有什么帮助吗?
最佳答案
如果您无法使 nonModule.js
正确导出 go
,您可以使用 exports loader导入它:
const go = require('exports-loader?go!./nonModule.js');
关于javascript - Webpack - 从非模块文件加载函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42907837/