webpack初试

一个例子

有这么一个项目
demo
-- index.html
-- js
---- scripts.js
---- module1.js
---- module2.js
其中,index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>My Webpack Page</h1>
    <script src="js/scripts.js"></script>
</body>
</html>

scripts.js、module1.js、module2.js

// scripts.js
require('./module1.js');
require('./module2.js');

// module1.js
console.log("module1 stuff");

// module2.js
console.log("module2 stuff");

显然,require是commonJS规范,上述代码直接在浏览器中打开是会报错的。这就需要用到webpack了

安装配置webpack

npm init
npm install webpack --save
npm install webpack -g

在项目根目录下新建webpack.config.js:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

module.exports = {
    context: __dirname,
    devtool: debug ? "inline-sourcemap" : null,
    entry: "./js/scripts.js",
    output: {
        path: __dirname + "/js",
        filename: "scripts.min.js"
    },
    plugins: debug ? [] : [
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
    ]
};

在根目录下下执行webpack,得到scripts.min.js

/******/ (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__) {

    /**
     * Created by Administrator on 2016/6/29 0029.
     */
    __webpack_require__(1);
    __webpack_require__(2);


/***/ },
/* 1 */
/***/ function(module, exports) {

    /**
     * Created by Administrator on 2016/6/29 0029.
     */
    // module #1
    console.log("module1 stuff");

/***/ },
/* 2 */
/***/ function(module, exports) {

    /**
     * Created by Administrator on 2016/6/29 0029.
     */
    // module #2
    console.log("module2 stuff");

/***/ }
/******/ ]);
....

也可以设置NODE_ENV=production,然后在webpack,这样得到的就是压缩后的,此时将index.html中的scripts.js改成scripts.min.js,就可以在浏览器中输出预期的内容了。

引入jquery,并使用

npm install jquery -S

修改module1.js

var $ = require('jquery'); // 这里的$只在当前module下有效
$('h1').html('new text');

我们可以像在nodejs中使用包一样了,是不是很嗨皮?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 无意中看到zhangwnag大佬分享的webpack教程感觉受益匪浅,特此分享以备自己日后查看,也希望更多的人看到...
    小小字符阅读 12,549评论 7 35
  • GitChat技术杂谈 前言 本文较长,为了节省你的阅读时间,在文前列写作思路如下: 什么是 webpack,它要...
    萧玄辞阅读 14,336评论 7 110
  • 写在开头 先说说为什么要写这篇文章, 最初的原因是组里的小朋友们看了webpack文档后, 表情都是这样的: (摘...
    Lefter阅读 10,691评论 4 31
  • 在现在的前端开发中,前后端分离、模块化开发、版本控制、文件合并与压缩、mock数据等等一些原本后端的思想开始...
    Charlot阅读 10,846评论 1 32
  • 构建一个小项目——FlyBird,学习webpack和react。(本文成文于2017/2/25) 从webpac...
    布蕾布蕾阅读 17,005评论 31 98