| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
As developer you want to reuse existing code. As with node.js and web all files are already in the same language, but it is extra work to use your code with the node.js module system and the browser.
The goal of webpack is to bundle CommonJs modules into javascript files which can be loaded by <script>-tags. Simply concatenating all required files has a disadvantage: many code to download (and execute) on page load. Therefore webpack uses the require.ensure function (CommonJs/Modules/Async/A) to split your code automatically into multiple bundles which are loaded on demand. This happens mostly transparent to the developer. Dependencies are resolved for you. The result is a smaller initial code download which results in faster page load.
Another common thing in web development is that some files have to be preprocessed before send to the client (ex. template files). This introduce more complexity to the compile step. webpack supports loaders which process files before including them. You as developer can use such files like any other module.
TL;DR
See example webapp.
// a.js
var b = require("./b");
b.stuff("It works");
// b.js
exports.stuff = function(text) {
console.log(text);
}are compiled to (reformatted)
(/* small webpack header */)
({
0: function(module, exports, require) {
var b = require(1);
b.stuff("It works");
},
1: function(module, exports, require) {
exports.stuff = function(text) {
console.log(text)
}
}
})var a = require("a");
var b = require("b");
require.ensure(["c"], function(require) {
require("b").xyz();
var d = require("d");
});File 1: web.js - code of that file - code of module a and dependencies - code of module b and dependencies File 2: 1.web.js - code of module c and dependencies (but code is not used) - code of module d and dependencies
Initially only web.js is included (and loaded) by your application. 1.web.js is loaded when the call to require.ensure happens. After the second bundle (1.web.js) is loaded, the callback function will be invoked.
See details for exact output.
See more examples.
webpack was built to support most of the code that was coded for node.js environment. For example this works out of the box:
Somethings it happens that browsers require other code than node.js do. webpack allow module developers to specify replacements which are used in the compile process of webpack.
Modules in web_modules replace modules in node_modules. filename.web.js replaces filename.js when required without file extension.
in options: alias: { "http": "http-browserify" }
in shell: --alias http=http-browserify
If the required module is not known while compile time we get into a problem. A solution is the method require.context which takes a directory as parameter and returns a function which behaves like the require function issued from a file in this directory (but only if used for files in that directory). The whole directory is included while compiling, so you have access to all files in it.
We have a directory full of templates, which are compiled javascript files. A template should be loaded by template name.
var requireTemplate = require.context("./templates");
function getTemplate(templateName) {
return requireTemplate("./" + templateName);
}In addition to that webpack uses the require.context function automatically if you use variables or other not parse-able things in the require function. That means the following code behaves like the above:
function getTemplate(templateName) {
return require("./templates/" + templateName);
}
// is compiled like: return require.context("./templates")("./"+templateName)
// which compiles to: return require(123)("./"+templateName)See details for complete example.
When try to store the require function in another variable or try to pass it as parameter, webpack convert it to a require.context(".") to be compatible. There is a warning emitted in this case.
Warning: The complete code in the directory are included. So use it carefully.
You can use a syntax for loader plugins to preprocess files before emitting javascript code to the bundle.
The following example loads the raw content of a file with the raw loader:
var content = require("raw!./file.txt");Multiple loader plugins can be prepended by separating them with !. The loader plugins are resolved like in normal require call but with different default extension.
The raw loader plugin is looked up at modules raw-webpack-web-loader, raw-webpack-loader, raw-web-loader, raw-loader, raw and the following files are looked up: index.webpack-web-loader.js, index.webpack-loader.js, index.web-loader.js, index.loader.js, index, index.js. Note that the web- versions are omitted if loaders are used in node.js.
See example.
The following loaders are included in webpack:
See docs for loader in github repo of the loader.
var a = require("a"); // require modules
var b = require("./b"); // and files
// like in node.js
// polyfill require method to use the new members in node.js too
require = require("webpack/require-polyfill")(require.valueOf());
// create a lazy loaded bundle
require.ensure([], function(require) {
var c = require("c");
// require json
var packageJson = require("../package.json");
// or jade templates, coffee-script, and many more with own loaders
var result = require("./template.jade")(require("./dataFrom.coffee"));
// files are compiled to javascript and packed into the bundle...
});... and compile from the shell with:
webpack lib/input.js js/output.js
try --min to minimize with uglify-js.
The following cases could result in too much code in result file if used wrong:
As node.js specific modules like fs will not work in browser they are not included (by default) and cause an exception. You should replace them by own modules if you want to use them. For some simple modules are replacements included in webpack. Expensive replacements are not needed by everyone, so that are not included by default. You need to specify --alias [module]=[replacement] to use them. A warning saying that some module is missing is emitted in the case you use it without providing a replacement.
Some credit goes to the browserify contributors, you can use replacements provided by them.
Included simple replacements:
Here is a list of possible useful replacements: (intentionally not by default)
webpack offers a command line interface:
after npm install webpack -g you can use the webpack command
if invoked without arguments it prints a usage:
Usage: webpack <options> <input> <output> Options: --single Disable Code Splitting [boolean] [default: false] --min Minimize it with uglifyjs [boolean] [default: false] --filenames Output Filenames Into File [boolean] [default: false] --options Options JSON File [string] --script-src-prefix Path Prefix For JavaScript Loading [string] --libary Stores the exports into this variable [string] --colors Output Stats with colors [boolean] [default: false] --json Output Stats as JSON [boolean] [default: false] --by-size Sort modules by size in Stats [boolean] [default: false] --verbose Output dependencies in Stats [boolean] [default: false] --alias Set a alias name for a module. ex. http=http-browserify [string] --debug Prints debug info to output files [boolean] [default: false] --watch Recompiles on changes (except loaders) [boolean] [default: false] --progress Displays a progress while compiling [boolean] [default: false]
webpack(context, moduleName, [options], callback)
webpack(absoluteModulePath, [options], callback)You can also save this options object in a JSON file and use it with the shell command.
outputJsonpFunction
JSONP function used to load chunks
scriptSrcPrefix
Path from where chunks are loaded
outputDirectory
write files to this directory (absolute path)
output
write first chunk to this file
outputPostfix
write chunks to files named chunkId plus outputPostfix
libary
exports of input file are stored in this variable
minimize
minimize outputs with uglify-js
debug
prints debug info to output files.
watch
recompiles on changes (except loaders)
includeFilenames
add absolute filenames of input files as comments
resolve.alias (object)
replace a module. ex. {"old-module": "new-module"}
resolve.paths (array)
search paths
resolve.extensions (object)
possible extensions for files
default: ["", ".webpack.js", ".web.js", ".js"]
resolve.loaders (array)
extension to loader mappings. ex. [{test: /\.extension$/, loader: "myloader"}]
loads files that matches the RegExp to the loader if no other loader set
resolve.loaderExtensions (array)
possible extensions for loaders
default: [".webpack-web-loader.js", ".webpack-loader.js", ".web-loader.js", ".loader.js", "", ".js"]
resolve.loaderPostfixes (array)
possible postfixes for loaders
default: ["-webpack-web-loader", "-webpack-loader", "-web-loader", "-loader", ""]
parse.overwrites (object)
free module variables which are replaced with a module. ex. { "$": "jquery" }
function(err, source / stats) source if options.output is not set else stats as json see example
You can use [hash] in scriptSrcPrefix, output, outputDirectory, outputPostfix and in the shell parameters. webpack will replace it with a hash of your files, when writing.
Combine the options --colors --watch --progress to get a pretty shell compilation.
| Feature |
sokra/ modules- webpack |
medikoo/ modules- webmake |
substack/ node- browserify |
|---|---|---|---|
| single bundle | yes | yes | yes |
| multiple bundles, Code Splitting | yes | no | no |
| indirect require var r = require; r("./file"); | in directory | include by config option | no |
| concat in require require("./fi" + "le") | yes | yes | no |
| variables in require (local) require("./templates/"+template) | yes, complete directory included | include by config option | no |
| variables in require (global) require(moduleName) | no | include by config option | no |
| node buildin libs require("http"); | yes | no | yes |
| process polyfill | yes, on demand | no | yes, ever |
| module polyfill | yes, on demand | no | no |
| require.resolve | no | no | yes |
| global to window mapping | yes | no | no |
| requirable files | filesystem | directory scope | filesystem |
| different modules with same name | yes | no | yes |
| eliminate duplicate code | no | no | no |
| require JSON | yes | no | no |
| plugins | no | no | yes |
| loaders | yes | no | no |
| compile coffee script | yes | no | yes |
| watch mode | yes | no | yes |
| debug mode | yes | no | yes |
| libaries | on global obj | no | requirable |
| browser replacements | web_modules and .web.js | no | by alias config option |
| compiles with (optional) modules missing | yes, emit warnings | no | no |
You can run the unit tests with npm test.
You can run the browser tests:
cd test/browsertests node build
and open test.html in browser. There must be several OKs in the file, no FAIL and no RED boxes.
You are welcome to contribute by writing issues or pull requests. It would be nice if you open source your own loaders or webmodules. :)
You are also welcome to correct any spelling mistakes or any language issues, because my english is not perfect...
| Back | FazBrowse Home | New Git URL |