webpack2打包ES6 Module(import export)

54 阅读2分钟

webpack.config.js

"use strict";

const path = require("path");
module.exports = {
	entry: "./bin/test/src/index.js",
	output: {
		path: path.resolve(__dirname, "dist"),
		filename: "bundle.js"
	}
};

mathUtils模块mathUtils.js

"use strict";

export default function sum(a, b) {
    return a + b;
}
export function multiply(a, b) {
    return a * b;
}

circle模块circle.js

"use strict";

export function area(radius) {
    return Math.PI * radius * radius;
}
export var radius = 10;

index.js导入circle模块和mathUtils模块

"use strict";

import { area, radius } from "./circle.js";
import sum, {multiply} from "./mathUtils.js";

console.log("圆面积" + area(4));
console.log(radius);
console.log(sum(5, 10));
console.log(multiply(5, 10));

入口index.js通过webpack打包生成的文件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;
/******/
/******/ 	// identity function for calling harmony imports with the correct context
/******/ 	__webpack_require__.i = function(value) { return value; };
/******/
/******/ 	// 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 = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* harmony export (immutable) */ __webpack_exports__["a"] = area;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return radius; });


function area(radius) {
    return Math.PI * radius * radius;
}
var radius = 10;

/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* harmony export (immutable) */ __webpack_exports__["a"] = sum;
/* harmony export (immutable) */ __webpack_exports__["b"] = multiply;


function sum(a, b) {
    return a + b;
}

function multiply(a, b) {
    return a * b;
}

/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__circle_js__ = __webpack_require__(0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__mathUtils_js__ = __webpack_require__(1);

console.log("圆面积" + __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__circle_js__["a" /* area */])(4));
console.log(__WEBPACK_IMPORTED_MODULE_0__circle_js__["b" /* radius */]);
console.log(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1__mathUtils_js__["a" /* default */])(5, 10));
console.log(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1__mathUtils_js__["b" /* multiply */])(5, 10));

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

circle模块(moduleId 0)、mathUtils(moduleId 1)模块和index模块(moduleId 2)分别封装到函数,这3个函数组成一个数组,这个数组作为参数值传递给立即执行函数。立即执行函数通过__webpack_require__(2) 执行index.js模块代码。index模块函数调用__webpack_require__(0) 和__webpack_require__(1) 执行circle模块和mathUtils模块代码,且导入circle模块和mathUtils模块的module.exports对外的接口。 import是实时获取导入模块的最新值,通过Object.defineProperty实现的。我们看circle模块的radius为例。index在导入circle模块时,circle模块对外提供的接口对象module.exports的属性radius是如下代码定义的。

__webpack_require__.d = function(exports, name, getter) {
 	if(!__webpack_require__.o(exports, name)) {
		Object.defineProperty(exports, name, {
			configurable: false,
 			enumerable: true,
 			get: getter
 		});
       }
};

function(module, __webpack_exports__, __webpack_require__) {
      __webpack_require__.d(__webpack_exports__, "b", function() { return radius; });
     
     var radius = 10;
}

通过Object.defineProperty的get函数方法能够获取到最新的radius值。