Webpack案例学习

注:本来自己是打算学习一下requirejs的,结果从社区了解到Webpack的功能更强大而且更好用,就搜索到了篇文章。
本文是照着阮一峰老师的demo进行的练习。详见https://github.com/ruanyf/webpack-demos。这些列子都写得很简单明了,让我学习起来感觉轻松不少!

Webpack要怎么才能使用

1、首先必须要安装node,npm这些环境工具啦!
2、其次要安装Webpack和webpack-dev-server:

$ npm install webpack webpack-dev-server

3、然后clone这个.git到本地,到各个目录项并启动webpack-dev-server:

$ git clone git@github.com:ruanyf/webpack-demos.git
$ cd webpack-demos
$ npm install

然后你就可以在浏览器 http://127.0.0.1:8080 中看到页面了。

再进一步:webpack到底是什么?

Webpack是一个和grunt/gulp相似的前端构建系统,
Webpack可以用来像browserify那样进行前端模块儿话构建,但是,Webpack要更强大。
Webpack的配置文件是webpack.config.js:

// webpack.config.js
module.exports = {
  entry: './main.js',
  output: {
    filename: 'bundle.js'
  }
};

配置文件弄好了,就可以直接启动Webpack了

$ webapck

上面展示的是无参数启动,下面这些参数是需要知道的:

  1. webpack – for building once for development
  2. webpack -p – for building once for production (minification)
  3. webpack –watch – for continuous incremental build
  4. webpack -d – to include source maps
  5. webpack –colors – for making things pretty

    To produce a production ready application, you could write scripts field in your package.json file as following.

// package.json
{
  // ...
  "scripts": {
    "dev": "webpack-dev-server --devtool eval --progress --colors",
    "deploy": "NODE_ENV=production webpack -p"
  },
  // ...
}

目录

Entry file
Multiple entry files
Babel-loader
CSS-loader
Image loader
CSS Module
UglifyJs Plugin
HTML Webpack Plugin and Open Browser Webpack Plugin
Environment flags
Code splitting
Code splitting with bundle-loader
Common chunk
Vendor chunk
Exposing Global Variables
Hot Module Replacement
React router

Entry file

你可能感兴趣的:(web前端)