Royi Namir
Royi Namir

Reputation: 148704

Webpack - Load a function from a non-moduled file?

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 ?

Upvotes: 2

Views: 61

Answers (1)

SimpleJ
SimpleJ

Reputation: 14788

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');

Upvotes: 2

Related Questions