2

Say I have a file :

//nonModuled.js
//A non moduled file  , let's say I can't "module" it

console.log('0');
function go(a)
    {
        console.log('go:' + a);
    }

And I have another file which I want to get the go function :

//1.js
require('./nonModuled.js');

When I run the html file , I do see the console.log , But I get an error for the go function :

enter image description here

I do understand why it's happening. Also - I know that I can do this patch :

//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);
    }

And then in the 1.js file , access window.go but that seems clumsy.

And so I ask :

Question:

How can I get the go function properly ?

It would be nice if I could do something like :

var a= require('./nonModuled.js');
a.go()

Any help ?

Royi Namir
  • 131,490
  • 121
  • 408
  • 714
  • Why not export `go` from `nonModule.js`? – SimpleJ Mar 20 '17 at 16:06
  • @SimpleJ _A non moduled file , let's say I can't touch it_ , it's a library with many complicated IIFEs and I don't want to get into that. However I did find a solution using `exports_loader` I will post an answer which may help others. Thanks – Royi Namir Mar 20 '17 at 16:08
  • I see. I found the same thing, but your example made me think you had access to modify `nonModule.js`, so I thought you might just not know how dependencies in webpack worked. – SimpleJ Mar 20 '17 at 16:10

1 Answers1

2

If you're unable to make nonModule.js properly export go, you could use the exports loader to import it:

const go = require('exports-loader?go!./nonModule.js');
SimpleJ
  • 10,139
  • 6
  • 37
  • 75