[关闭]
@buluoXu 2017-10-17T02:18:10.000000Z 字数 4256 阅读 1616

前端自动化工具--webpack

angularjs系列教程 npm webpack


一、Webpack 是什么

Webpack 是德国开发者 Tobias Koppers 开发的模块加载器,在 Webpack 当中, 所有的资源都被当作是模块。
对于开发人员而言,好的包管理器可以让工作事半功倍。现在流行的编程语言大多有自己的包管理系统。近年来,Web开发越来越火,其开发工具也随之越来越好用了,而 Webpack 就是一款专为Web开发设计的包管理器。它能够很好地管理、打包Web开发中所用到的HTML、Javascript、CSS以及各种静态文件(图片、字体等),让开发过程更加高效。
Webpack 的火热离不开Facebook的react框架的推出。

二、安装

Webpack是基于nodejs开发的模块。。

1). 安装nodejs

  1. //test nodejs
  2. //进入cmd控制台
  3. node -v //v0.12.7 目前最新的node版本是v4.2.1
  4. //test npm
  5. npm -v

node -v

npm -v

2). 通过npm安装webpack

  1. //Linux Mac
  2. $ npm install webpack -g
  3. //windows
  4. npm install webpack -g
  5. //test webpack
  6. webpack

这样webpack的命令就可使使用啦

webpack

3).开始使用

1、创建目录、初始化项目

  1. mkdir webpack-test //创建目录
  2. cd webpack-test //进入目录
  3. npm init //一路回车,最有输入yes,初始化项目 生成package.json文件

package.json

2、添加webpack配置文件

webpack.config.js

  1. var path = require('path');
  2. module.exports = {
  3. entry: path.resolve(__dirname, 'app/main.js'),//入口文件
  4. output: {//输出文件
  5. path: path.resolve(__dirname, 'dist'),//输出目录dist
  6. filename: 'bundle.js',//文件名
  7. },
  8. };

3、目录结构
目录结构

4、项目代码

a、inde.html代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>webpack-test</title>
  6. </head>
  7. <body>
  8. <script src="./dist/bundle.js"></script>
  9. </body>
  10. </html>

b、app/main.js代码

  1. 'use strict';
  2. var component = require('./component.js');
  3. document.body.appendChild(component());

c、app/component.js代码

  1. 'use strict';
  2. module.exports = function () {
  3. var mycom=require('./common.js');
  4. var element = document.createElement('h1');
  5. element.innerHTML = mycom('Hello world 大家好嗡嗡222嗡2323啊');
  6. return element;
  7. };

d、app/common.js代码

  1. 'use strict';
  2. module.exports = function (val) {
  3. return "<span style='color:red'>"+val+"</span>";
  4. };

编译效果图

运行效果图

打包后的代码

  1. /******/ (function(modules) { // webpackBootstrap
  2. /******/ // The module cache
  3. /******/ var installedModules = {};
  4. /******/ // The require function
  5. /******/ function __webpack_require__(moduleId) {
  6. /******/ // Check if module is in cache
  7. /******/ if(installedModules[moduleId])
  8. /******/ return installedModules[moduleId].exports;
  9. /******/ // Create a new module (and put it into the cache)
  10. /******/ var module = installedModules[moduleId] = {
  11. /******/ exports: {},
  12. /******/ id: moduleId,
  13. /******/ loaded: false
  14. /******/ };
  15. /******/ // Execute the module function
  16. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  17. /******/ // Flag the module as loaded
  18. /******/ module.loaded = true;
  19. /******/ // Return the exports of the module
  20. /******/ return module.exports;
  21. /******/ }
  22. /******/ // expose the modules object (__webpack_modules__)
  23. /******/ __webpack_require__.m = modules;
  24. /******/ // expose the module cache
  25. /******/ __webpack_require__.c = installedModules;
  26. /******/ // __webpack_public_path__
  27. /******/ __webpack_require__.p = "";
  28. /******/ // Load entry module and return exports
  29. /******/ return __webpack_require__(0);
  30. /******/ })
  31. /************************************************************************/
  32. /******/ ([
  33. /* 0 */
  34. /***/ function(module, exports, __webpack_require__) {
  35. 'use strict';
  36. var component = __webpack_require__(1);
  37. document.body.appendChild(component());
  38. /***/ },
  39. /* 1 */
  40. /***/ function(module, exports, __webpack_require__) {
  41. 'use strict';
  42. module.exports = function () {
  43. var mycom=__webpack_require__(2);
  44. var element = document.createElement('h1');
  45. element.innerHTML = mycom('Hello world 大家好嗡嗡222嗡2323啊');
  46. return element;
  47. };
  48. /***/ },
  49. /* 2 */
  50. /***/ function(module, exports) {
  51. 'use strict';
  52. module.exports = function (val) {
  53. return "<span style='color:red'>"+val+"</span>";
  54. };
  55. /***/ }
  56. /******/ ]);

5、CSS及图片的引用

  1. require('./bootstrap.css');
  2. require('./myapp.less');
  3. require('./myapp.sass');
  4. var img = document.createElement('img');
  5. img.src = require('./glyph.png');

上边的是 JavaScript 代码, CSS 跟 LESS, 还有图片, 被直接引用了
实际上 CSS 被转化为 <style> 标签, 而图片可能被转化成 base64 格式的 dataUrl
但是要主要在 webpack.config.js 文件写好对应的 loader(请看第三节配置)

三、配置

1)loader加载器

  1. module.exports = {
  2. entry: './main.js',
  3. output: {
  4. path: path.resolve(__dirname + "/dist/"),
  5. filename: "[name]-bundle-[chunkhash].js",
  6. hash: true
  7. },
  8. module: {
  9. loaders: [
  10. { test: /\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders
  11. { test: /\.css$/, loader: 'style-loader!css-loader' },
  12. {test: /\.scss$/, loader: ExtractTextPlugin.extract("style-loader", "css!sass")},//分离css
  13. {test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'} // inline base64 URLs for <=8k images, direct URLs for the rest
  14. {test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,loader: "file-loader?name=./fonts/[name].[ext]"},//读取字体文件
  15. ]
  16. }
  17. };

详情请访问webpack官网loaders

2)插件

详情请访问webpack官网plugins

四、相关推荐

  1. Webpack 入门指迷
  2. 前端webpack workflow(二)——Webpack基本使用
  3. webpack官网
  4. webpack 入门及实践

五、咱们动手实践吧!!!

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注