让前端飞

【webpack】一步步的看webpack-1

2021-01-20  本文已影响0人  程序员佩奇

本文主要从最新的webpack4入手,慢慢的学习webpack的相关知识点,进行汇总

第一次轻轻的接触

跑一个小例子感受一下

src/index.js

import bar from './bar';

bar();

src/bar.js

export default function bar() {
  //
}

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
      path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  }
};

page.html

<!doctype html>
<html>
  <head>
    ...
  </head>
  <body>
    ...
    <script src="dist/bundle.js"></script>
  </body>
</html>

然后在命令行运行 webpack 就会创建 bundle.js

Version: webpack 4.43.0
Time: 82ms
Built at: 2020/06/22 下午5:20:25
    Asset       Size  Chunks             Chunk Names
bundle.js  951 bytes       0  [emitted]  main
Entrypoint main = bundle.js
[0] ./src/index.js + 1 modules 72 bytes {0} [built]
    | ./src/index.js 32 bytes [built]
    | ./src/bar.js 40 bytes [built]

//这里会报WARNING,是webpack4后新增了`development`、`production`和`none`环境变量的指定,既然官方推荐了我们可以加一下
//./node_modules/.bin/webpack --mode production


WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

官方教程

上一篇下一篇

猜你喜欢

热点阅读