-
Notifications
You must be signed in to change notification settings - Fork 128
webpack options
module.exports = {
context: "/home/proj",
entry: "./entry",
module: {
loaders: [{
test: /\.coffee$/,
include: /lib/, exclude: /test/,
loader: "coffee"
}],
preLoaders: [/*...*/],
postLoaders: [/*...*/]
},
output: {
path: "/home/proj/assets",
filename: "[hash].bundle.js",
chunkFilename: "[id].[hash].bundle.js",
namedChunkFilename: "[name].[hash].js",
sourceMapFilename: "[file].map", // 0.10
hotUpdateChunkFilename: "[id].[hash].bundle-update.js", // 0.11
hotUpdateMainFilename: "[hash].bundle-update.json", // 0.11
publicPath: "/assets/",
jsonpFunction: "webpackJsonp",
hotUpdateFunction: "webpackHotUpdate", // 0.11
pathInfo: true,
library: "Lib",
libraryTarget: "commonjs"
},
recordsInputPath: "/home/proj/records.json", // 0.10
recordsOutputPath: "/home/proj/records.json", // 0.10
recordsPath: "/home/proj/records.json", // 0.10
target: "web",
bail: true,
profile: true,
cache: true,
watch: true,
watchDelay: 200,
debug: true,
devtool: "eval",
hot: true, // 0.11 experimental
amd: { jQuery: true },
node: {
process: "mock",
http: "mock",
console: true,
__filename: "mock",
__dirname: "mock"
},
resolve: {
alias: {
module: "other-module",
module2: "/home/proj/shim-module.js"
},
root: "/home/proj/app",
modulesDirectories: ["modules", "node_modules"],
fallback: "/home/proj/fallback",
extensions: ["", ".client.js", ".js"],
unsafeCache: /^\/home\/proj\/app\/node_modules\// // 0.11
},
resolveLoader: {/*...*/},
provide: {
$: "jquery",
jQuery: "jquery"
},
optimize: {
minChunkSize: 20000,
maxChunks: 5,
minimize: true,
occurenceOrder: true, // 0.10
occurenceOrderPreferEntry: true // 0.10
},
prefetch: [ // 0.10 experimental
"./folder/file"
],
plugins: [
new MyPlugin()
]
}
The base directory for resolving the entry
option. If output.pathinfo
is set, the included pathinfo is shortened to this directory.
Default:
process.cwd()
The entry point for the bundle.
If you pass a string: The string is resolve to a module which is loaded upon startup.
If you pass an array: All modules are loaded upon startup. The last one is exported.
entry: ["./entry1", "./entry2"]
If you pass an object: Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.
entry: {
page1: "./page1",
page2: ["./entry1", "./entry2"]
},
output: {
filename: "[name].bundle.js",
chunkFilename: "[id].bundle.js"
}
Options affecting the normal modules (NormalModuleFactory
)
A array of automatically applied loaders.
Each item can have this properties:
-
test
: A condition that must be met -
exclude
: A condition that must not be met -
include
: A condition that must be met -
loader
: A string of "!" separated loaders -
loaders
: A array of loaders as string
A condition can be a RegExp, or a array of RegExps combound with "and".
If the request starts with !
, automatic loaders are not applied.
Syntax like module.loaders
.
A array of applied pre and post loaders.
If the request starts with !!
, pre and post loaders are not applied.
Options affecting the output.
If you use any hashing ([hash]
or [chunkhash]
) make sure to have a consistent ordering of modules. Use optimize.occurenceOrder
or recordsPath
.
The output directory as absolute path.
[hash]
is replaced by the hash of the compilation.
The filename of the entry chunk as relative path inside the output.path
directory.
[name]
is replaced by the name of the chunk.
[hash]
is replaced by the hash of the compilation.
The filename of non-entry chunks as relative path inside the output.path
directory.
[id]
is replaced by the id of the chunk.
[hash]
is replaced by the hash of the compilation.
[chunkhash]
is replaced by the hash of the chunk. (0.10)
The filename of named chunks as relative path inside the output.path
directory.
[name]
is replaced by the name of the chunk.
[hash]
is replaced by the hash of the compilation.
The filename of the SourceMaps for the Javascript files. They are inside the output.path
directory.
[file]
is replaced by the filename of the Javascript file.
[id]
is replaced by the id of the chunk.
[hash]
is replaced by the hash of the compilation.
Default:
"[file].map"
The filename of the Hot Update Chunks. They are inside the output.path
directory.
[id]
is replaced by the id of the chunk.
[hash]
is replaced by the hash of the compilation. (The last hash stored in the records)
Default:
"[id].[hash].hot-update.js"
The filename of the Hot Update Main File. It is inside the output.path
directory.
[hash]
is replaced by the hash of the compilation. (The last hash stored in the records)
Default:
"[hash].hot-update.json"
The output.path
from the view of the javascript.
// Example
output: {
path: "/home/proj/public/assets",
publicPath: "/assets"
}
// Example CDN
output: {
path: "/home/proj/cdn/assets/[hash]",
publicPath: "http://cdn.example.com/assets/[hash]/"
}
The JSONP function used by webpack for asnyc loading of chunks
Default:
"webpackJsonp"
The JSONP function used by webpack for asnyc loading of hot update chunks
Default:
"webpackHotUpdate"
Include comments with information about the modules.
Default:
false
If set, export the bundle as library. output.library
is the name.
Kind of exporting as library.
var
- Export by setting a variable: var Library = xxx
(default)
this
- Export by setting a property of this
: this["Library"] = xxx
commonjs
- Export by setting a property of exports
: exports["Library"] = xxx
commonjs2
- Export by setting module.exports
: module.exports = xxx
umd
- Export to AMD, CommonJS2 or as property in root
Store/Load compiler state from/to a json file. This will result in persistent ids of modules and chunks.
An absolute path is excepted. recordsPath
is used for recordsInputPath
and recordsOutputPath
if they left undefined.
-
"web"
Compile for usage in a browser-like environment (default) -
"webworker"
Compile as WebWorker -
"node"
Compile for usage in a node.js-like environment
Report the first error als hard error instead of tolerating it.
Capture timing information for each module.
Cache generated modules to improve performance for multiple builds.
Enter watch mode, which rebuilds on file change.
Delay the rebuilt of this time after the first change.
Switch loaders to debug mode.
Choose a developer tool to enhance debugging.
eval
- Each module is executed with eval
and //@ sourceURL
.
source-map
- A SourceMap is emitted. See also output.sourceMapFilename
. (0.10)
inline-source-map
- A SourceMap is added as DataUrl to the Javascript file. (0.10)
Enables Hot Module Replacement. (This requires records data if not in dev-server mode, recordsPath
)
Generates Hot Update Chunks of each chunk in the records. It also enables the API.
Include polyfills or mocks for various node stuff:
-
console
:true
orfalse
-
global
:true
orfalse
-
process
:true
,"mock"
orfalse
-
buffer
:true
,"mock"
orfalse
-
__filename
:true
(real filename),"mock"
("/index.js"
) orfalse
-
__dirname
:true
(real dirname),"mock"
("/"
) orfalse
-
<node buildin>
:true
,"mock"
orfalse
// Default:
{
console: false,
process: true,
global: true,
buffer: true,
__filename: "mock",
__dirname: "mock"
}
Set the value of require.amd
and define.amd
.
Example: amd: { jQuery: true }
Options affecting the resolving of modules.
Replace modules by other modules or paths.
Look of modules in this directory (or directories if you pass an array).
Look of modules in this directories. It'll check these directories in current directory and each parent directory.
Default:
["web_modules", "node_modules"]
Look of modules in this directory (or directories if you pass an array), if no module found in resolve.root
and resolve.modulesDirectories
.
Resolve to files by adding this extensions.
Default:
["", ".webpack.js", ".web.js", ".js"]
Check these fields in the package.json
for suitable files.
Default:
["webpack", "browser", "web", "main"]
Enable aggressive but unsafe caching for the resolving of a part of your files. Changes to cached paths may cause failure (in rar cases). An array of RegExps, only a RegExp or true
(all files) is expected. If the resolved path matches, it'll be cached.
Default:
[]
Like resolve
but for loaders.
// Default:
{
modulesDirectories: ["web_loaders", "web_modules", "node_loaders", "node_modules"],
extensions: ["", ".webpack-loader.js", ".web-loader.js", ".loader.js", ".js"],
packageMains: ["webpackLoader", "webLoader", "loader", "main"]
}
That's a resolveLoader
only property.
It describes alternatives for the module name that are tried.
Defaul:
["*-webpack-loader", "*-web-loader", "*-loader", "*"]
Options affecting the optimization of the compilation.
Merge small chunks that are lower than this min size (in chars). Size is approximated.
Limit the chunk count to a defined value. Chunks are merged until it fits.
Mimimize all javascript output of chunks. Loaders are switched into minimizing mode.
Assign the module and chunk ids by occurence count. Ids that are used often get lower (shorter) ids. This make ids predictable and is recommended. (It is availible as option since 0.10, but was ever activated prior to 0.10)
Default: false
Only availible if optimize.occurenceOrder
is set. Occurences in entry chunks have higher priority. This make entry chunks smaller but increases the overall size.
Default: true
A list of requests for normal modules, which are resolved and built even before a require to them occur. This can boost performance. Try to profile the built first to determine clever prefetching points.
Add additional plugins to the compiler.
var path = require("path");
module.exports = {
context: __dirname,
entry: "./app/app.js",
output: {
path: path.join(__dirname, "public", "assets"),
publicPath: "/assets/",
filename: "[hash].js"
},
module: {
loaders: [
{ test: /\.json$/, loader: "json-loader" },
{ test: /\.coffee$/, loader: "coffee-loader" },
{ test: /\.css$/, loader: "style-loader!css-loader" },
{ test: /\.less$/, loader: "style-loader!css-loader!less-loader" },
{ test: /\.jade$/, loader: "jade-loader" },
{ test: /\.png$/, loader: "url-loader?limit=10000&minetype=image/png" },
{ test: /\.jpg$/, loader: "url-loader?limit=10000&minetype=image/jpg" },
{ test: /\.gif$/, loader: "url-loader?limit=10000&minetype=image/gif" },
{ test: /\.woff$/, loader: "url-loader?limit=10000&minetype=application/font-woff" }
],
preLoaders: [
{
test: [
/\.js$/,
/\.json$/,
],
include: pathToRegExp(path.join(__dirname, "app")),
loader: "jshint-loader"
}
]
},
resolve: {
fallback: path.join(__dirname, "app")
},
cache: true,
amd: { jQuery: true },
optimize: {
minChunkSize: 10000,
maxChunks: 20,
},
plugins: [
]
};
function escapeRegExpString(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\\+\?\.\\\\\^\$\|]/g, "\\\\$&"); }
function pathToRegExp(p) { return new RegExp("^" + escapeRegExpString(p)); }
# development: compile and watch
webpack -d --watch --progress --colors
# development: server
webpack-dev-server -d --colors
# production: compile
webpack -p --progress --colors
webpack 👍