Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 8, 2019. It is now read-only.
/docsPublic archive

shimming modules

Hanna Neuman edited this pageAug 22, 2017 ·27 revisions

Not all JS files can be used directly with webpack. The file might be in an unsupported module format, or not even in any module format.

Webpack provides several loaders to make these files work with webpack.

The examples on this userequire to keep them short. You will usually want to configure them in your webpack config. SeeUsing loaders.

Importing

If a file has dependencies that are not imported viarequire(), you will need to use one of these loaders.

The imports loader allows you to use modules that depend on specific global variables.

This is useful for third-party modules that rely on global variables like$ orthis being thewindow object.The imports loader can add the necessary require('whatever') calls, so those modules work with webpack.

Examples:

file.js expects a global variable$ and you have a modulejquery that should be used.

require("imports-loader?$=jquery!./file.js")

file.js expects its configuration on a global variablexConfig and you want it to be{value:123}.

require("imports-loader?xConfig=>{value:123}!./file.js")

file.js expect thatthis is the global context.

require("imports-loader?this=>window!./file.js") orrequire("imports-loader?this=>global!./file.js")

pluginProvidePlugin

This plugin makes a module available as a variable in every module. The module is required only if you use the variable.

Example: Make$ andjQuery available in every module without writingrequire("jquery").

newwebpack.ProvidePlugin({$:"jquery",jQuery:"jquery","window.jQuery":"jquery"})

Exporting

The file doesn't export its value.

This loader exports variables from inside the file.

Examples:

The file sets a variable in the global context withvar XModule = ....

var XModule = require("exports-loader?XModule!./file.js")

The file sets multiple variables in the global context withvar XParser, Minimizer.

var XModule = require("exports-loader?Parser=XParser&Minimizer!./file.js"); XModule.Parser; XModule.Minimizer

The file sets a global variable withXModule = ....

require("imports-loader?XModule=>undefined!exports-loader?XModule!./file.js") (import to not leak to the global context)

The file sets a property onwindowwindow.XModule = ....

require("imports-loader?window=>{}!exports-loader?window.XModule!./file.js")

Fixing broken module styles

Some files use a module style wrong. You may want to fix this by teaching webpack to not use this style.

Disable some module styles

Examples:

Broken AMD

require("imports-loader?define=>false!./file.js")

Broken CommonJs

require("imports-loader?require=>false!./file.js")

configuration optionmodule.noParse

This disables parsing by webpack. Therefore you cannot use dependencies. This may be useful for prepackaged libraries.

Example:

{module:{noParse:[/XModule[\\\/]file\.js$/,path.join(__dirname,"web_modules","XModule2")]}}

Note:exports andmodule are still available and usable. You may want to undefine them with theimports-loader.

This loader evaluates code in the global context, just like you would add the code into a script tag. In this mode every normal library should work.require,module, etc. are undefined.

Note: The file is added as string to the bundle. It is not minimized by webpack, so use a minimized version. There is also no dev tool support for libraries added by this loader.

Exposing

There are cases where you want a module to export itself to the global context.

Don't do this unless you really need this. (Better use the ProvidePlugin)

This loader exposes the exports to a module to the global context.

Example:

Exposefile.js asXModule to the global context

require("expose-loader?XModule!./file.js")

Another Example:

   require('expose-loader?$!expose?jQuery!jquery');      ...   $(document).ready(function() {   console.log("hey");   })

By making jQuery available as a global namespace in our file containing jQuery code or the root file, you can use jQuery everywhere in your project. This works very well if you plan to implement Bootstrap(boot up the project) in your Webpack project.

Note: Using too much global name-spacing will make your app less efficient. If you are planning to use a lot of global namespaces, consider implementing something likeBabel runtime to your project.


Order of loaders

In rare cases when you have to apply more than one technique, you need to use the correct order of loaders:

inlined:expose-loader!imports-loader!export-loaders, configuration: expose before imports before exports.

webpack 👍

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp