2

I use babel.js and have a new module foo in my code

foo.js:

export function foo(number) {
    return number + 42;
}

And bunch of big old files where everything is global. And I need to call a foo function from that legacy code.

bar.js:

 ...
 var result = foo(0); 
 ...

But I can't just import foo cause then my bar.js will be a module and unavailable from other old code. Is there a way to import module and retain my bar.js global?

3
  • 2
    "But I can't just import foo cause then my bar.js will be a module and unavailable from other old code" You have to import the module file / somehow if you want to use it. What's the context? How can bar.js be used "globally" in the first case. Commented Nov 11, 2015 at 14:23
  • Which module loader are you using? Do you transpile to commonjs? Commented Nov 11, 2015 at 17:22
  • I use webpack. In bar.js a lot of global variable and function that used in other part of application. Commented Nov 12, 2015 at 7:00

1 Answer 1

4

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.