假设我有一个文件:
//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
,你可以使用出口装载机 https://github.com/webpack-contrib/exports-loader导入它:
const go = require('exports-loader?go!./nonModule.js');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)