-
去
官网看初始化教程
-
创建文件并运行以下代码
npm init -y
-
本地安装webpack 和 webpack-cli
yarn add webpack@4 webpack-cli@3 --dev
-
新建src文件夹 并新建index.js文件
console.log('hi')
-
npx webpack
本地运行 webpack -
你会发现生成了一个
main.js
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t){console.log("hi")}]);
-
新建文件
x.js
export default 'xxx'
-
在
index.js
中导入import x from './x.js' console.log(x)
-
再次运行
npx webpack
...console.log("xxx")}]);
会看到
x.js
被成功解析出来 -
干掉 warning 提示
新建 webpack.config.js
module.exports = {
mode: 'development',
};
-
webpack 配置 entry 和 output
const path = require('path'); module.exports = { mode: 'development', entry: './src/index.js', output: { filename: 'index.js', }, };
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
// filename: 'index.js',
filename: '[name].[contenthash]'
},
};
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
module.exports = {
entry: 'index.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'index_bundle.js',
},
plugins: [new HtmlWebpackPlugin()],
};
webpack.config.js
删除多余的代码
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'index.[contenthash].js'
},
plugins: [new HtmlWebpackPlugin({
title: '我的 webpack demo',
template: 'src/assets/index.html'
})],
};
src/assets/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<div id="#app"></div>
</body>
</html>
(css-loader)[https://webpack.js.org/loaders/css-loader/]
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
],
},
};
``webpack.config.js`
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'index.[contenthash].js'
},
plugins: [new HtmlWebpackPlugin({
title: '我的 webpack demo',
template: 'src/assets/index.html'
})],
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
};
单独引入css文件
(css-extract)[https://v4.webpack.js.org/plugins/mini-css-extract-plugin/]
webpack.config.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
plugins: [new MiniCssExtractPlugin()],
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
};
"scripts": {
"start": "webpack-dev-server --open",
"build": "rm -rf dist && webpack --config webpack.config.prod.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
(wabpack-dev-server)[https://github.com/webpack/webpack-dev-server]
webpack div server 会在内存中完成这一切,而不输出dist目录
webpack.config.js
devtool: 'inline-source-map',
devServer: {
after: function (app, server, compiler) {
// do fancy stuff
}
},
package.json
"scripts": {
+ "start": "webpack-dev-server --open",
"build": "rm -rf && webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
在开发时,我们的页面在 localhost:8080
,JS 直接访问后端接口(如 https://xiedaimala.com
或 http://localhost:3000
)会报跨域错误。
为了解决这个问题,可以在 webpack.config.js 中添加如下配置:
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://xiedaimala.com',
changeOrigin: true,
},
},
},
};