-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from okbug/study/webpack
webpack分析
- Loading branch information
Showing
14 changed files
with
3,816 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
/node_modules | ||
../node_modules | ||
./node_modules | ||
**/node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<script src="./main.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// (() => { | ||
// var __webpack_modules__ = { | ||
// "./src/title.js": (module) => { | ||
// module.exports = "title"; | ||
// }, | ||
// }; | ||
// var __webpack_module_cache__ = {}; | ||
|
||
// function __webpack_require__(moduleId) { | ||
// var cachedModule = __webpack_module_cache__[moduleId]; | ||
// if (cachedModule !== undefined) { | ||
// } | ||
// var module = (__webpack_module_cache__[moduleId] = { | ||
// exports: {}, | ||
// }); | ||
// __webpack_modules__[moduleId](module, module.exports, __webpack_require__); | ||
// return module.exports; | ||
// } | ||
// var __webpack_exports__ = {}; | ||
// (() => { | ||
// const title = __webpack_require__("./src/title.js"); | ||
// console.log(title); | ||
// })(); | ||
// })(); | ||
|
||
var modules = { | ||
// 每一个moduleId都是一个函数,里面是原模块中的源代码 | ||
title: (module) => { | ||
module.exports = "title"; | ||
}, | ||
}; | ||
var cache = {}; | ||
function require(moduleId /* title */) { | ||
// 核心模块加载器 | ||
if (cache[moduleId]) { | ||
// 如果前面已经加载过了,就直接返回 | ||
return cache[moduleId].exports; | ||
} | ||
|
||
// 没有缓存就开始创建一个新的 | ||
var module = (cache[moduleId] = { | ||
exports: {}, | ||
}); | ||
|
||
// 执行modules中的那个函数 | ||
modules[moduleId](module, module.exports, require); | ||
return module.exports; | ||
} | ||
(() => { | ||
// 这里是入口文件 | ||
let title = require("title" /* moduleId */); | ||
|
||
console.log(title); // title | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>webpack分析</title> | ||
<script defer src="main.js"></script></head> | ||
<body> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function require() {} | ||
// 上面代码和 1.sync/main.js 中一样 | ||
|
||
// commonjs要加载esmodule需要添加下面这几个方法 | ||
|
||
require.r = function (exports) { | ||
// 如果有Symbol就用 | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
}; | ||
|
||
require.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); | ||
|
||
require.d = (exports, definition) => { | ||
for (var key in definition) { | ||
if (require.o(definition, key) && !require.o(exports, key)) { | ||
Object.defineProperty(exports, key, {enumerable: true, get: definition[key]}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/1.sync => webpack同步加载commonjs | ||
|
||
/2.cjs2esm => commonjs加载esmodule | ||
|
||
/3.esm2esm => esm 加载esm |
Oops, something went wrong.