標籤:for ble cti config turn exports ejs window return
webpack、gulp、grunt是前端打包功能工具;因為已經學習了gulp,而最近發現webpack很火,於是著手學習webpack。本篇是webpack學習筆記系列的第一篇,歡迎指教。
我是從慕課網以及官網文檔相結合的方式學習的,從官方文檔學到的第一個知識點是在使用webpack打包過程中,即使沒有webpack.config,js這個檔案也是可以的。
首先是全域安裝webpack,cmd(如果是window系統,在任意位置)執行命令:
npm install --g webpack或cnpm install --g webpack
然後利用cd命令開啟學習webpack所在檔案地址,執行本地安裝命令,全域安裝是為了在window任意位置都可以使用webpack命令;而本地安裝則是在當前檔案夾下應用webpack.
本地安裝命令:
npm install --save-dev webpack或cnpm install --save-dev webpack
具體代碼html代碼:
<html> <head> <title>Getting Started</title> <meta charset="utf-8"> </head> <body> <script src="./dist/bundle.js"></script> </body> </html>
js代碼:
function test(){var div=document.createElement("div");div.innerHTML="這是一個測試資料";document.body.appendChild(div);}test();
不用webpack.config.js檔案,執行
webpack src/index.js dist/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] = {/******/ i: moduleId,/******/ l: false,/******/ exports: {}/******/ };/******//******/ // Execute the module function/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);/******//******/ // Flag the module as loaded/******/ module.l = 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;/******//******/ // define getter function for harmony exports/******/ __webpack_require__.d = function(exports, name, getter) {/******/ if(!__webpack_require__.o(exports, name)) {/******/ Object.defineProperty(exports, name, {/******/ configurable: false,/******/ enumerable: true,/******/ get: getter/******/ });/******/ }/******/ };/******//******/ // getDefaultExport function for compatibility with non-harmony modules/******/ __webpack_require__.n = function(module) {/******/ var getter = module && module.__esModule ?/******/ function getDefault() { return module[‘default‘]; } :/******/ function getModuleExports() { return module; };/******/ __webpack_require__.d(getter, ‘a‘, getter);/******/ return getter;/******/ };/******//******/ // Object.prototype.hasOwnProperty.call/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };/******//******/ // __webpack_public_path__/******/ __webpack_require__.p = "";/******//******/ // Load entry module and return exports/******/ return __webpack_require__(__webpack_require__.s = 0);/******/ })/************************************************************************//******/ ([/* 0 *//***/ (function(module, exports) {function test(){var div=document.createElement("div");div.innerHTML="這是一個測試資料";document.body.appendChild(div);}test();/***/ })/******/ ]);
完畢!
總結:本文是學習webpack的第一篇筆記,主要講了怎麼不使用webpack.config,js下如果打包js,檔案。注意:本文預設學習者已經安裝了nodejs會用npm和cnpm.
webpack學習筆記一