@buluoXu
2017-10-17T02:18:10.000000Z
字数 4256
阅读 1700
angularjs系列教程
npm
webpack
Webpack 是德国开发者 Tobias Koppers 开发的模块加载器,在 Webpack 当中, 所有的资源都被当作是模块。
对于开发人员而言,好的包管理器可以让工作事半功倍。现在流行的编程语言大多有自己的包管理系统。近年来,Web开发越来越火,其开发工具也随之越来越好用了,而 Webpack 就是一款专为Web开发设计的包管理器。它能够很好地管理、打包Web开发中所用到的HTML、Javascript、CSS以及各种静态文件(图片、字体等),让开发过程更加高效。
Webpack 的火热离不开Facebook的react框架的推出。
Webpack是基于nodejs开发的模块。。
//test nodejs
//进入cmd控制台
node -v //v0.12.7 目前最新的node版本是v4.2.1
//test npm
npm -v
//Linux 、Mac
$ npm install webpack -g
//windows
npm install webpack -g
//test webpack
webpack
这样webpack的命令就可使使用啦
1、创建目录、初始化项目
mkdir webpack-test //创建目录
cd webpack-test //进入目录
npm init //一路回车,最有输入yes,初始化项目 生成package.json文件
2、添加webpack配置文件
webpack.config.js
var path = require('path');
module.exports = {
entry: path.resolve(__dirname, 'app/main.js'),//入口文件
output: {//输出文件
path: path.resolve(__dirname, 'dist'),//输出目录dist
filename: 'bundle.js',//文件名
},
};
3、目录结构
4、项目代码
a、inde.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>webpack-test</title>
</head>
<body>
<script src="./dist/bundle.js"></script>
</body>
</html>
b、app/main.js代码
'use strict';
var component = require('./component.js');
document.body.appendChild(component());
c、app/component.js代码
'use strict';
module.exports = function () {
var mycom=require('./common.js');
var element = document.createElement('h1');
element.innerHTML = mycom('Hello world 大家好嗡嗡222嗡2323啊');
return element;
};
d、app/common.js代码
'use strict';
module.exports = function (val) {
return "<span style='color:red'>"+val+"</span>";
};
打包后的代码
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var component = __webpack_require__(1);
document.body.appendChild(component());
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
module.exports = function () {
var mycom=__webpack_require__(2);
var element = document.createElement('h1');
element.innerHTML = mycom('Hello world 大家好嗡嗡222嗡2323啊');
return element;
};
/***/ },
/* 2 */
/***/ function(module, exports) {
'use strict';
module.exports = function (val) {
return "<span style='color:red'>"+val+"</span>";
};
/***/ }
/******/ ]);
5、CSS及图片的引用
require('./bootstrap.css');
require('./myapp.less');
require('./myapp.sass');
var img = document.createElement('img');
img.src = require('./glyph.png');
上边的是 JavaScript 代码, CSS 跟 LESS, 还有图片, 被直接引用了
实际上 CSS 被转化为<style>
标签, 而图片可能被转化成 base64 格式的 dataUrl
但是要主要在 webpack.config.js 文件写好对应的 loader(请看第三节配置)
module.exports = {
entry: './main.js',
output: {
path: path.resolve(__dirname + "/dist/"),
filename: "[name]-bundle-[chunkhash].js",
hash: true
},
module: {
loaders: [
{ test: /\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders
{ test: /\.css$/, loader: 'style-loader!css-loader' },
{test: /\.scss$/, loader: ExtractTextPlugin.extract("style-loader", "css!sass")},//分离css
{test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'} // inline base64 URLs for <=8k images, direct URLs for the rest
{test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,loader: "file-loader?name=./fonts/[name].[ext]"},//读取字体文件
]
}
};
详情请访问webpack官网loaders
详情请访问webpack官网plugins