webpack学习笔记

[TOC]

看懂webpack.config.js

  1. entry
1
2
3
module.exports={
entry:'./path/to/my/entry/file.js'
};

默认值是./src/index.js;入口起点

  1. output
1
2
3
4
5
6
7
8
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'
}
};

path定义导入的模块,filename指bunble生成的名件名称及地址。输出文件默认放入./dist文件夹中

  1. loader

loader 能够import任何类型的模块,例如css

1
2
3
4
5
6
7
8
9
10
11
12
const path = require('path');

module.exports = {
output: {
filename: 'my-first-webpack.bundle.js'
},
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' }
]
}
};

“嘿,webpack 编译器,当你碰到「在 require()/import 语句中被解析为 ‘.txt’ 的路径」时,在你对它打包之前,先 >使用 raw-loader 转换一下。”

  1. plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
const webpack = require('webpack'); // 用于访问内置插件

module.exports = {
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' }
]
},
plugins: [
new HtmlWebpackPlugin({template: './src/index.html'})
]
};

该插件为应用程序生成html文件,并注入所有生成的bundle。

资源管理

加载images图像为例

  • npm install --save-dev file-loader

  • webpack.config

1
2
3
4
5
6
7
8
9
10
11
12
13

module.exports = {
....
module: {
rules: [
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
}
]
}
}
  • src/icon.jpg
  • src/index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import _ from 'lodash';
import './style.css';
import Icon from './icon.png';

function component() {
var element = document.createElement('div');

// lodash,现在由此脚本导入
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
element.classList.add('hello');

var myIcon = new Image();
myIcon.src = Icon;
element.appendChild(myIcon);
  • src/style.css
1
2
3
.hello{
background: url('./icon.png');
}
  • npm run build

其他加载,css,fonts,data。

管理输出

以下插件/操作均为方便管理dist文件夹,不需要手动更改

  • 动态生成bunble.js文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const path = require('path');

module.exports = {
- entry: './src/index.js',
+ entry: {
+ app: './src/index.js',
+ print: './src/print.js'
+ },
output: {
- filename: 'bundle.js',
+ filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
  • npm install --save-dev html-webpack-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  const path = require('path');
+ const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: {
app: './src/index.js',
print: './src/print.js'
},
+ plugins: [
+ new HtmlWebpackPlugin({
+ title: '管理输出'
+ })
+ ],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
}
};

执行npm run build该插件会默认生成index.html文件,覆盖/更改原index.html文件

  • npm install --save-dev clean-webpack-plugin
1
2
3
4
5
6
+ const CleanWebpackPlugin = require('clean-webpack-plugin');//需改为{CleanWebpackPlugin},否则会有报错

....

plugins: [
+ new CleanWebpackPlugin(),

作用为清理dist文件夹中之前生成的不需要的旧文件。
这一节管理输出一直有报错,未能正常显示页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Uncaught ReferenceError: ducument is not defined
at app.bundle.js:9
at Module.<anonymous> (app.bundle.js:9)
at r (app.bundle.js:1)
at app.bundle.js:1
at app.bundle.js:1
(anonymous) @ app.bundle.js:9
(anonymous) @ app.bundle.js:9
r @ app.bundle.js:1
(anonymous) @ app.bundle.js:1
(anonymous) @ app.bundle.js:1
//浏览器报错

Entrypoint undefined = index.html//终端报错,有人说是插件本身的问题

按照官网操作来的,这报错真的不明白,之后再看看怎么解决唔,看看github相关插件的issue

吐血!document拼写错误,见devtool: 'inline-source-map',可跟踪错误至源文件

0%