2.2 基于webpack進(jìn)行開發(fā)

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作為項(xiàng)目的入口文件進(jìn)行構(gòu)建,并將結(jié)果輸出到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 傳入的參數(shù)是一個(gè)數(shù)組
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

/* __webpack_require__等同于require,參數(shù)直接去傳入該模塊在modules列表中的索引值*/
    var text = __webpack_require__(1); 
    console.log(text);

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

    module.exports = "hello world!";


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

創(chuàng)建 index.css

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

在 index.js 中添加,注意引用 loader 時(shí),現(xiàn)在不支持縮寫可能回報(bào)的異常

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"));

執(zhí)行打包

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 無意中看到zhangwnag大佬分享的webpack教程感覺受益匪淺,特此分享以備自己日后查看,也希望更多的人看到...
    小小字符閱讀 8,373評(píng)論 7 35
  • webpack 介紹 webpack 是什么 為什么引入新的打包工具 webpack 核心思想 webpack 安...
    yxsGert閱讀 6,673評(píng)論 2 71
  • 前言 WebPack 是什么? WebPack 是什么,WebPack 可以看做是模塊打包機(jī):它做的事情是,分析你...
    Promise__閱讀 1,231評(píng)論 3 12
  • 1.被擱在路中間的斑馬線 像極了和你吵架 故意橫穿馬路那次。 那時(shí)候的我好可愛啊。 現(xiàn)在的我也好可愛呀,嘻嘻。 突...
    溫養(yǎng)閱讀 536評(píng)論 0 0
  • 原作者:anthony 譯者:勵(lì)定洲 你知道網(wǎng)站的導(dǎo)航會(huì)影響到你的轉(zhuǎn)化率嗎?一些研究發(fā)現(xiàn)在注冊(cè)頁(yè)簡(jiǎn)化導(dǎo)航可以顯著增...
    定Ding閱讀 841評(píng)論 4 8

友情鏈接更多精彩內(nèi)容