entry 指示 webpack 应该使用哪一个模块开始构建其内部依赖关系图。html
entry 的 默认值是 ./src/index.js。webpack
能够在 webpack.config.js(webpack 的配置文件)里自定义。web
// webpack.config.js module.exports = { entry: './path/to/my/entry/file.js' }
output 告诉 webpack 在何处派发出它建立的包,以及如何命名这些文件。npm
output 的默认值 是 ./dist/main.js浏览器
能够在 webpack.config.js(webpack 的配置文件)里自定义。babel
// webpack.config.js const path = require('path'); module.exports = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js' } };
webpack 只能解析 JavaScript 和 JSON 文件。优化
Loader 容许 webpack 处理其余类型的文件,并将它们转换为可由应用程序使用并添加到依赖关系图中的有效模块。ui
loaders 在 webpack 配置里有两个属性:插件
// webpack.config.js const path = require('path'); module.exports = { output: { filename: 'my-first-webpack.bundle.js' }, module: { rules: [ { test: /\.jsx?$/, use: 'babel-loader' } ] } };
虽然 loader 用于转换某些类型的模块,可是能够利用插件执行更普遍的任务,好比包优化、资产管理和环境变量注入。code
// webpack.config.js const HtmlWebpackPlugin = require('html-webpack-plugin'); const webpack = require('webpack'); module.exports = { module: { rules: [ { test: /\.txt$/, use: 'raw-loader' } ] }, plugins: [ new HtmlWebpackPlugin({ template: './src/index.html' }) ] };
html-webpack-plugin 这个插件的用途是 为应用程序自动生成 html 文件,而且自动注入。
经过将 mode 参数设置为 development、production 或 none,你能够启用与每一个环境相对应的 webpack 内置优化。 默认值是 production。
mode
// webpack.config.js module.exports = { mode: 'production' // mode default value is 'production' };
// npm command line in Webpack Cli webpack --mode=production
浏览器兼容