To use webpack, first you need to run:

npm install webpack

2. Create a webpack.config.js file:

module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: __dirname
}
};

3. Using Command.js style to exports files, for example:

//alert-button.js

var _ = require('lodash');
module.exports = {
setupButtons: function() {
var alertButtons = document.querySelectorAll('[data-alert]');
_.each(alertButtons, function(button) {
button.addEventListener('click', function() {
alert(button.innerText);
});
});
}
};

Here, we use lodash in the file, therefore, we need to require lodash into the file.

//index.js

'use strict';
var AlertButtons = require('./alert-buttons');
var LameDomBinding = require('./lame-dom-binding'); document.addEventListener('DOMContentLoaded', function() {
AlertButtons.setupButtons();
LameDomBinding.bindEls(document.getElementById('textarea1'), document.getElementById('textarea2'));
});

index.js file use alert-button.js and lamedombinding.js, therefore, we also need to require those two fiels.

//lamedombinding.js

module.exports = {
bindEls: function(el1, el2) {
el1.addEventListener('keyup', function() {
el2.value = el1.value;
});
el2.addEventListener('keyup', function() {
el1.value = el2.value;
});
}
};

4. Our aim at replace all the javascript included files with only one file: bundle.js:

we run:

webpack --watch  

The bundle.js file will be created, then we can include only bundle.js into the html file.

[Javascript] Introduce to Webpack的更多相关文章

  1. 爬虫逆向基础,理解 JavaScript 模块化编程 webpack

    关注微信公众号:K哥爬虫,QQ交流群:808574309,持续分享爬虫进阶.JS/安卓逆向等技术干货! 简介 在分析一些站点的 JavaScript 代码时,比较简单的代码,函数通常都是一个一个的,例 ...

  2. Webpack 打包 Javascript 详细介绍

    本篇我们主要介绍Webpack打包 Javascript.当然,除了可以打包Javascript之外,webpack还可以打包html.但是这不是我们本篇的重点.我们可以参考 Webpack HTML ...

  3. 转:入门Webpack,看这篇就够了

    写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...

  4. 入门Webpack,看这篇就够了

    来源于:http://www.jianshu.com/p/42e11515c10f 写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有 ...

  5. webpack初学

    写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...

  6. webpack之基础学习

    webpack工作原理: 通过一个入口文件,main.js开始找到你的项目的所有依赖文件,使用loaders处理它们,最后打包为一个浏览器可识别的JavaScript文件. Webpack的核心原理 ...

  7. 想入门webpack,这篇就够了

    申明:本文转载自简书 文/zhangwang(简书作者)原文链接:http://www.jianshu.com/p/42e11515c10f#著作权归作者所有,转载请联系作者获得授权,并标注" ...

  8. webpack3中文版使用参考文档--全面解析webpack.config.js

    Webpack目前官方发布的最新版本是3.1.0,相对于2.0的怎么本,在语法上没有变动,只是新增了功能.使用webpack,需要事先安装node.js,并对node.js生态有一些基本的了解,比如( ...

  9. webpack配置这一篇就够

    最近看了一篇好文,根据这个文章重新梳理了一遍webpack打包过程,以前的一些问题也都清楚了,在这里分享一下,同时自己也做了一些小的调整 原文链接:http://www.jianshu.com/p/4 ...

随机推荐

  1. 函数lock_rec_get_nth_bit

    /*********************************************************************//** Gets the nth bit of a rec ...

  2. easyui资源

    官网地址:http://www.jeasyui.com/index.php(相关文档示例,有demo, tutorial, documentation) 未混淆的源码:http://jquery-ea ...

  3. Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别?

    一个帖子的整理: Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别如果你不带参数的实例化:Handler ...

  4. POJ 2001 字典树(入门题)

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...

  5. WebOb的简单介绍

    在之前的文章中我写了有关于如何使用PasteDeploy生成WSGI的Application.在Openstack的源码中,除了 PasteDeploy外,还有一个和WSGI密切相关的工具包WebOb ...

  6. 【UR #12】实验室外的攻防战(BIT)

    [题目链接] http://uoj.ac/problem/180 [题意] 给定两个1..n的排列AB,只有当ai<ai+1才能交换ai和ai+1,问是否能够将A转换为B. [思路] 令a[i] ...

  7. javascript——继承

    内容: 1.继承的概念.继承分为那几种继承及各种继承的区别 2.js中有那几种继承方式及各种继承的优缺点 3.总结

  8. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  9. 【noip2005】篝火晚会

    题解: 首先我们要知道一个性质: 把长度为n的序列变成目标序列最多需要n个操作 证明1: 我们可以将原序列上每位上的数字向目标序列相同位置的数字连一条有向边 如: 原序列:   1 2 3 目标序列: ...

  10. Unity3D知识点

    世界空间(World):整个虚拟世界的3d空间,在Unity3d中以米作为单位,如长100米宽100米高100米的立体空间. 屏幕空间(Screen):屏幕2d空间,大小就是屏幕的大小,以像素作为单位 ...