2.2 基于webpack进行开发

Hello world

文件目录

hello
--- hello.js
--- index.js
--- index.html

hello.js

module.exports = "hello world!";

index.js

 echo -e var "hello = require(\"hello\");\nconsole.log(hello);" > index.js
var text = require("./hello");
console.log(text);

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="./bundle.js"></script>

</body>
</html>

打包

webpack ./index.js bundle.js

将index.js作为项目的入口文件进行构建,并将结果输出到bundle.js

bundle.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);
/******/ })
/************************************************************************/
/*********/       // webpackBootstrap 传入的参数是一个数组
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

/* __webpack_require__等同于require,参数直接去传入该模块在modules列表中的索引值*/
    var text = __webpack_require__(1); 
    console.log(text);

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

    module.exports = "hello world!";


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

创建 index.css

div {
  width: 100px;
  height: 100px;
  background-color: red;
}

在 index.js 中添加,注意引用 loader 时,现在不支持缩写可能回报的异常

ERROR in ./index.js
Module not found: Error: Can't resolve 'style' in '/home/grandkai/Workspace/hello'
BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders.
                 You need to specify 'style-loader' instead of 'style',
                 see https://webpack.js.org/guides/migrating/#automatic-loader-module-name-extension-removed
 @ ./index.js 2:0-39

var hello = require("./hello");
require("style-loader!css-loader!./index.css");
console.log(hello);
document.body.appendChild(document.createElement("div"));

执行打包

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

相关阅读更多精彩内容

  • 无意中看到zhangwnag大佬分享的webpack教程感觉受益匪浅,特此分享以备自己日后查看,也希望更多的人看到...
    小小字符阅读 8,388评论 7 35
  • webpack 介绍 webpack 是什么 为什么引入新的打包工具 webpack 核心思想 webpack 安...
    yxsGert阅读 6,685评论 2 71
  • 前言 WebPack 是什么? WebPack 是什么,WebPack 可以看做是模块打包机:它做的事情是,分析你...
    Promise__阅读 1,238评论 3 12
  • 1.被搁在路中间的斑马线 像极了和你吵架 故意横穿马路那次。 那时候的我好可爱啊。 现在的我也好可爱呀,嘻嘻。 突...
    温养阅读 542评论 0 0
  • 原作者:anthony 译者:励定洲 你知道网站的导航会影响到你的转化率吗?一些研究发现在注册页简化导航可以显著增...
    定Ding阅读 842评论 4 8

友情链接更多精彩内容