惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Security Archives - TechRepublic
Security Archives - TechRepublic
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
U
Unit 42
L
LangChain Blog
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
V2EX - 技术
V2EX - 技术
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
N
News | PayPal Newsroom
量子位
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
S
Security @ Cisco Blogs
Y
Y Combinator Blog
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
P
Privacy International News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
Last Week in AI
Last Week in AI
AI
AI
Recorded Future
Recorded Future
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
P
Privacy & Cybersecurity Law Blog

博客园 - 匡匡

Vue 父子组件通信方式 Vue: 组件扩展 WebClient 指定出口 IP IIS8 下 JS, CSS 等静态文件出现 500 错误 使用 ffmpeg 转换 mov 视频 使用 ildasm 和 ilasm 修改程序集的的引用信息 2020-01-08 工作日记:无题 .Net Core 程序集管理说明(加载) .NET CORE 动态加载 DLL 的问题 ASP.NET 后台 COOKIE 的设置 使用 sql server 默认跟踪分析执行的 SQL 语句 Nginx深入详解之upstream分配方式 使用 HttpWebRequest 类做 POST 请求没有应反 使用 pdf.js 查看发票时,显示不了台头和印章的解决办法 Flex 布局里 input 宽度最小 150px 的问题, 浏览器 BUG? 使用像素单位设置 EXCEL 列宽或行高 sweetalert 快速显示两个提示, 第二个显示不出的问题 加权轮询和加权随机算法 在 Docker 中部署 ASP.NET CORE 应用
webpack 里的 import, exports 实现原理
匡匡 · 2019-07-25 · via 博客园 - 匡匡

在使用 webpack 对脚本进行打包, 在开发中, 每个文件中都会使用 import 语句来导入一些功能,又会使用 export 语句导出一些功能,为了研究 import 和 export 原理,研究了 webpack 打包后的代码,其实原理也是非常简单:

webpack 对所有输入文件都打包到一个文件中:

在最终的输出文件中,webpack 会定义一个 Object 对象,这个对象中放入了所有的输入文件的内容, 以文件名为 key, 文件内容(字符串)作为值,如:

var modules= {};

modules['index'] = "源代码";

modules['hello'] = '源代码';

当然,webpack 会对源代码进行一定的修改, 会把 export 语句修改为 exports.变量 = 值, 这种方法, exports 是一个参数, 大概是这样子的:

modules['index'] = (function(exports) {

  // 代码,放在一个私有域中。

  // 通过 exports 对象,把需要公开的变量,函数,类公开出去。

  exports.xx = xxx;

});

这就大概是一个源文件最终的结果,变成了一个函数,那么这个 exports 到底是个什么对象?其实 exports 就是一个 Object 对象,里面没有任何其实内容,全部是函数中设置的内容,作用只是把对象传递到 import 语句:

先来看看 import 的语法: import xxx from 'filename'

import 语句, 最终大概会变成这样:

var cmp1 = require('filename');

require 函数大概的实现:

function require(id) {

if(installedModules[id]) {

  return installedModules[id].exports; // 返回 exports 对象

}

var module = installedModules[id] = {

      i: moduleId,

      l: false,

      exports: {} // 定义 exports 对象

 };

modules[id](module.exports); // 把 exports 参数传递到函数中

return module.exports; // 最后返回

}

不管 import xxx 语句中的 xxx 怎么设置, 最终都会变成一个变量,引用模块的 exports 对象,然后通过这个变量来访问变量,函数等:

import { a, b } from 'filename'

console.log(a);

console.log(b);

最终编译的结果:

var cmp1 = require('filename');

console.log(cmp1.a);

console.log(cmp1.b);

最后, 文件传统执行入口文件的代码 require('index');