我使用babel.js并在我的代码中有一个新的模块foo
foo.js:
export function foo(number) {
return number + 42;
}
一堆大的旧文件,其中一切都是全球性的。我需要从遗留代码中调用foo函数。
bar.js:
...
var result = foo(0);
...
但是我不能导入foo因为那时我的bar.js将是一个模块,并且不能从其他旧代码中获取。有没有办法导入模块 并保留我的bar.js全球?
答案 0 :(得分:1)
I had a somewhat similar problem recently. I ended up polluting window
object with everything I need in legacy code.
I created separate register.js
module for this purpose and included it to my webpack build:
import ClassA from './ClassA'
import ClassB from './ClassB'
import * as utils from './utils'
Object.assign(window, utils)
Object.assign(window, {ClassA, ClassB})