Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to load AMD modules from modules compiled with Webpack (at runtime over the network)?

Is there a way to load AMD modules from modules compiled with Webpack (at runtime over the network)?

f.e., I have

import Blah from './Blah'
import AmdModule from './AmdModule'

where AmdModule is an AMD module that has a single define() call in it. I don't want webpack to compile that file and I don't want webpack to include it in the bundle. Maybe that file doesn't even exist at compile time, but will exist on the asset server. I want that file to be loaded at runtime, over the network.

Is there some way to make Webpack do that kind of stuff? I was thinking maybe to import that file with RequireJS, but then it breaks Webpack modules because there's no way to wait for the module to load and then export something asynchronously in webpack modules.

Ideally, I'd like for webpack to wait for those files to be loaded from the network before executing the module that needs it.

Is something like this possible?

As requirement, the to-be-loaded-by-network file can not be compiled, it must remain an AMD define() module loaded over the network, untouched, no source map needed.

like image 774
trusktr Avatar asked Jan 25 '17 19:01

trusktr


People also ask

What kind of modules can webpack support?

Webpack supports the following module types natively: ECMAScript modules. CommonJS modules. AMD modules.

What does webpack do with node modules?

Webpack is a static module bundler for JavaScript applications. It takes modules, whether that's a custom file that we created or something that was installed through NPM, and converts these modules to static assets.

Does webpack bundle Node_modules?

Webpack allows you to define externals - modules that should not be bundled. When bundling with Webpack for the backend - you usually don't want to bundle its node_modules dependencies. This library creates an externals function that ignores node_modules when bundling in Webpack.

How does webpack bundling work?

When webpack processes your application, it internally builds a dependency graph from one or more entry points and then combines every module your project needs into one or more bundles, which are static assets to serve your content from.


1 Answers

Yes, you can, just add to externals:

module.exports = {
   ... (all config stuff),
   output: {
     filename: "[name].js",
     path: path,
     libraryTarget: 'amd'
   },
   externals : [
      'module the AmdModule is requiring'
  ]
}

I'm using it to pack my module/plugin to be load in a framework and the modules uses some modules from the target framework, so webpack should not include the modules. Externals does avoid include the framework modules inside my plugin module and still waiting for those modules to load on run time.

Externals are exactly for it:

Applications and externals

You can also use the externals option to import an existing API into applications. For example, if you want to use jQuery from a CDN via a separate tag while still explicitly declaring it as a dependency via require("jquery"), you would specify it as external like so: { externals: { jquery: "jQuery" } }.

@NicoD: External Indicates dependencies that should not be bundled by webPack but instead remain requested by the resulting bundle. To get the module you have to use a loader as script.js

like image 121
Adriano Spadoni Avatar answered Dec 13 '22 14:12

Adriano Spadoni