Webpack案例学习
2016-07-06 20:55
330 查看
注:本来自己是打算学习一下requirejs的,结果从社区了解到Webpack的功能更强大而且更好用,就搜索到了篇文章。
本文是照着阮一峰老师的demo进行的练习。详见https://github.com/ruanyf/webpack-demos。这些列子都写得很简单明了,让我学习起来感觉轻松不少!
2、其次要安装Webpack和webpack-dev-server:
3、然后clone这个.git到本地,到各个目录项并启动webpack-dev-server:
然后你就可以在浏览器 http://127.0.0.1:8080 中看到页面了。
Webpack可以用来像browserify那样进行前端模块儿话构建,但是,Webpack要更强大。
Webpack的配置文件是webpack.config.js:
配置文件弄好了,就可以直接启动Webpack了
上面展示的是无参数启动,下面这些参数是需要知道的:
webpack – for building once for development
webpack -p – for building once for production (minification)
webpack –watch – for continuous incremental build
webpack -d – to include source maps
webpack –colors – for making things pretty
To produce a production ready application, you could write scripts field in your package.json file as following.
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
本文是照着阮一峰老师的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
上面展示的是无参数启动,下面这些参数是需要知道的:
webpack – for building once for development
webpack -p – for building once for production (minification)
webpack –watch – for continuous incremental build
webpack -d – to include source maps
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 fileMultiple 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
相关文章推荐
- webpack如何与gulpfile联合的使用
- WebPack常用功能介绍
- 关于webpack最好的文档
- webpack&gulp集成简介
- webpack2
- webpack的使用(1)
- 解决webpack打包慢的解决办法汇总
- webpack打包第三方类库的正确姿势
- 一步一步webpack
- [Webpack 2] Ensure all source files are included in test coverage reports with Webpack
- [Webpack 2] Add Code Coverage to tests in a Webpack project
- [Webpack 2] Use Karma for Unit Testing with Webpack
- [Webpack 2] Expose modules to dependencies with Webpack
- Webpack 常用命令总结
- (三)webpack入门——webpack功能集合的demo
- [Webpack 2] Import a non-ES6 module with Webpack
- webpack configuration
- [Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin
- [Webpack 2] Hashing with Webpack for long term caching
- [Webpack 2] Polyfill Promises for Webpack 2