随笔

webpack

想要订制性只能学喽…

webpack 核心:

  • Entry
    入口
  • Output
    输出
  • Loaders
    文件加载
  • Plugins
    插件

tip: 使用webpack-dev-server时,webpack编译的东西是存储在内存中的

配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
console.log(path.resolve(__dirname, './build/assets'));
module.exports = {
entry: [
'./src/entry.js',
'webpack-dev-server/client?http://127.0.0.1:8089/',
'webpack/hot/dev-server'
],
output: {
path: path.resolve(__dirname, './build'),
filename: "assets/bundle.js",
publicPath: '/'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: "babel-loader"
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader"
}, {
loader: "sass-loader"
}]
})
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin('style.css'),
new HtmlWebpackPlugin({
filename: './index.html',
template: './index.html',
hash: false,
})
]
}
// dev-serve.js
var WebpackDevServer = require('webpack-dev-server');
var webpack = require('webpack');
var config = require('./webpack.config.js');
var path = require('path');
var compiler = webpack(config);
var server = new WebpackDevServer(compiler,{
hot:true,
filename: config.output.filename,
publicPath: config.output.publicPath,
stats: {
colors: true
}
})
server.listen(8089,'localhost',()=>{});

参考链接:
[https://www.qianduandao.com/webpack-four-core-concepts/]

本文链接:https://note.lilonghe.net//post/webpack.html

-- EOF --