去搞清楚webpack作了什么以前,我以为首先要思考一下咱们为何须要webpack,它究竟解决了什么痛点。想一想咱们平常搬砖的场景:
1.开发的时候须要一个开发环境,要是咱们修改一下代码保存以后浏览器就自动展示最新的代码那就行了(热更新服务)
2.本地写代码的时候,要是调后端的接口不跨域就行了(代理服务)
3.为了跟上时代,要是能用上什么ES678N等等新东西就行了(翻译服务)
4.项目要上线了,要是能一键压缩代码啊图片什么的就行了(压缩打包服务)
5.咱们平时的静态资源都是放到CDN上的,要是能自动帮我把这些搞好的静态资源怼到CDN去就行了(自动上传服务)javascript
- 若是与输入相关的需求,找entry(好比多页面就有多个入口)
- 若是与输出相关的需求,找output(好比你须要定义输出文件的路径、名字等等)
- 若是与模块寻址相关的需求,找resolve(好比定义别名alias)
- 若是与转译相关的需求,找loader(好比处理sass处理es678N)
- 若是与构建流程相关的需求,找plugin(好比我须要在打包完成后,将打包好的文件复制到某个目录,而后提交到git上)
webpack打包出来的什么
webpack搞了不少东西,但最终产出的无非就是通过重重服务处理过的代码,那么这些代码是怎样的呢?
首先咱们先来看看入口文件index.js:java
console.log('index') const one = require('./module/one.js') const two = require('./module/two.js') one() two()
嗯,很简单,没什么特别,引入了两个模块,最后执行了它们一下。其中one.js和two.js的代码也很简单,就是导出了个函数:node
// one.js module.exports = function () { console.log('one') }
// two.js module.exports = function () { console.log('two') }
好了,就是这么简单的代码,放到webpack打包出来的是什么呢?webpack
/******/ (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); /******/ }) /************************************************************************/ /