browserify 集成使用
Like browserify, webpack analyzes all the node-style require()
calls in your app and builds a bundle that you can serve up to the browser using a <script>
tag.
Instead of doing
$ browserify main.js > bundle.js
do
$ webpack main.js bundle.js
webpack doesn't write to stdout. You need to specify a filename. It can't write to stdout because, unlike browserify, it may generate multiple output files.
The best way to [[configure | configuration]] webpack is with a webpack.config.js
file. It's loaded from current directory, when running the [[executable | CLI]].
So
$ browserify --entry main.js --outfile bundle.js
maps to webpack
with this config:
module.exports = {
entry: "./main.js",
output: {
filename: "bundle.js"
}
}
Note: A
webpack.config.js
should export the configuration, hence themodule.exports = {...}
in the above example.
outfile
If you want to emit the output files to another directory:
$ browserify --outfile js/bundle.js
{
output: {
path: path.join(__dirname, "js"),
filename: "bundle.js"
}
}
入口
$ browserify --entry a.js --entry b.js
{
entry: [
"./a.js",
"./b.js"
]
}
转换
browserify uses transforms to preprocess files. webpack uses loaders. Loaders are functions that take source code as an argument and return (modified) source code. Like transforms they run in node.js, can be chained, and can be asynchronous. Loaders can take additional parameters by query strings. Loaders can be used from require()
calls. Transforms can be specified in the package.json
. browserify
applies configured transforms for each module. Within the webpack configuration you select the modules by RegExp. In the common case you specify loaders in the webpack.config.js
:
$ browserify --transform coffeeify
{
module: {
loaders: [
{ test: /\.coffee$/, loader: "coffee-loader" }
]
}
}
Note: It's possible to use browserify transforms with webpack and the transform-loader.
调试
$ browserify -d
# Add inlined SourceMap
$ webpack --devtool inline-source-map
# Add inlined SourceMaps
$ webpack --devtool source-map
# Emit SourceMaps as separate file
$ webpack --devtool eval
# Emit SourceUrls within evals (faster)
$ webpack --devtool eval-source-map
# Emit inlined SourceMaps within evals
$ webpack --debug
# Add more debugging information to the source
$ webpack --output-pathinfo
# Add comments about paths to source code
# (Useful when using no or the eval devtool)
$ webpack -d
# = webpack --devtool source-map --debug --output-pathinfo
扩展 extension
$ browserify --extension coffee
{
resolve: {
extensions: ["", ".js", ".coffee"]
}
}
单独 standalone
browserify --standalone MyLibrary
{
output: {
library: "MyLibrary",
libraryTarget: "umd"
}
}
// webpack --output-library MyLibrary --output-library-target umd
忽略 ignore
$ browserify --ignore file.js
{
plugins: [
new webpack.IgnorePlugin(/file\.js$/)
]
}
node 全局
$ browserify --insert-globals
$ browserify --detect-globals
You can enable/disable these node globals individually:
{
node: {
filename: true,
dirname: "mock",
process: false,
global: true
}
}
忽略错误 ignore-missing
$ browserify --ignore-missing
webpack prints errors for each missing dependency, but doesn't fail to build a bundle. You are free to ignore these errors. The require
call will throw an error on runtime.
无解析 noparse
$ browserify --noparse=file.js
module.exports = {
module: {
noParse: [
/file\.js$/
]
}
};
创建信息
$ browserify --deps
$ browserify --list
$ webpack --json
外部要求
webpack does not support external requires. You cannot expose the require
function to other scripts. Just use webpack for all scripts on a page or do it like this:
{
output: {
library: "require",
libraryTarget: "this"
}
}
// entry point
module.exports = function(parentRequire) {
return function(module) {
switch(module) {
case "through": return require("through");
case "duplexer": return require("duplexer");
}
return parentRequire(module);
};
}(typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : function() {
throw new Error("Module '" + module + "' not found")
});
多包
With browserify you can create a commons bundle that you can use in combination with bundles on multiple pages. To generate these bundles you exclude the common stuff with the --exclude
-x
option. Here is the example from the browserify README:
$ browserify -r ./robot > static/common.js
$ browserify -x ./robot.js beep.js > static/beep.js
$ browserify -x ./robot.js boop.js > static/boop.js
webpack supports multi-page compilation and has a plugin for the automatic extraction of common modules:
var webpack = require("webpack");
{
entry: {
beep: "./beep.js",
boop: "./boop.js",
},
output: {
path: "static",
filename: "[name].js"
},
plugins: [
// ./robot is automatically detected as common module and extracted
new webpack.optimize.CommonsChunkPlugin("common.js")
]
}
<script src="common.js"></script>
<script src="beep.js"></script>
接口
无需学更多. 只需要配置对象访问 webpack
接口:
var webpack = require("webpack");
webpack({
entry: "./main.js",
output: {
filename: "bundle.js"
}
}, function(err, stats) {
err // => fatal compiler error (rar)
var json = stats.toJson() // => webpack --json
json.errors // => array of errors
json.warnings // => array of warnings
});
第三方工具映射
browserify | webpack |
---|---|
watchify | webpack --watch |
browserify-middleware | [[webpack-dev-middleware]] |
beefy | [[webpack-dev-server]] |
deAMDify | webpack |
decomponentify | component-webpack-plugin |
list of source transforms | [[list of loaders]], transform-loader |