基础配置
基础配置
const config = {
// 定义入口
entry: {
app: path.join(__dirname, "app")
},
// 定义包体文件
output: {
// 输出目录
path: path.join(__dirname, "build"),
// 输出文件名
filename: "[name].js"
// 使用 hash 作为文件名
// filename: "[name].[chunkhash].js",
},
// 定义如何处理
module: {
rules: [
{
test: /\.js$/,
use: "babel-loader",
exclude: /node_modules/
}
]
},
// 添加额外插件操作
plugins: [new webpack.DefinePlugin()]
};
module.exports = [{
entry: './app.js',
output: ...,
...
}, {
entry: './app.js',
output: ...,
...
}]
我们代码中的
const config = {
resolve: {
alias: {
/*...*/
},
extensions: [
/*...*/
],
modules: [
/*...*/
]
}
};
资源加载
const config = {
module: {
rules: [
{
// **Conditions**
test: /\.js$/, // Match files
enforce: "pre", // "post" too
// **Restrictions**
include: path.join(__dirname, "app"),
exclude: path => path.match(/node_modules/),
// **Actions**
use: "babel-loader"
}
]
}
};
// Process foo.png through url-loader and other matches
import "url-loader!./foo.png";
// Override possible higher level match completely
import "!!url-loader!./bar.png";
/******/ (function(modules) { // webpackBootstrap
...
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony default export */ __webpack_exports__["default"] = ((text = "Hello world") => {
const element = document.createElement("div");
element.innerHTML = text;
return element;
});
/***/ })
/******/ ]);
use: ["style-loader", "css-loader"]