jiboulex jiboulex - 16 days ago 6
Javascript Question

How to specify resolvers dependencies in webpack?

I'm trying to switch from RequireJs to webpack to deal with js loading, sass processing & other stuff like that.

In RequireJs it's possible to specify dependencies for modules

// file.js
require(['moduleA','moduleB'],function(A,B) {
...
}


And I want to specify that moduleA requires jquery for example

// requirejs.config.js
require.config({
...
shim:{
moduleA:{
deps:['jquery']
}
}
})


I'm trying to find a way to do the same thing in webpack

// webpack.config.js
module.exports = {
entry: toObject(glob.sync('src/**/*.js*')),
output: {
path: './bin',
filename: '[name].js'
},
resolve: {
alias: {
'moduleA': '/path/to/moduleA',
...
}
}
};

function toObject(paths) {
// I return an object of paths
}


But I can't find a way to specify that moduleA needs another module !

Does anyone has a clue here ?

Answer

You don't need to. You just require() whatever module you need, and it would be available.

The reason you need to do this with RequireJS is that RequireJS is asynchronous, modules are loaded asynchronously, and thus it needs to know what to load first. Webpack (and CommonJS in general) is synchronous. By the time you enter the module, everything is already loaded.