layout | theme | background | class | highlighter | lineNumbers | info |
---|---|---|---|---|---|---|
center |
seriph |
text-center |
shiki |
false |
## 从ES modules 看 JavaScript 的发展和演化
|
Press Space for next page
- 🎨 1. the history (1989~2015)
- 💻 2. esm 兼容性
- ✏️ 3. esm 使用
- 📤 4. 展望
<style> h1 { background-color: #2B90B6; background-image: linear-gradient(45deg, #4EC5D4 10%, #146b8c 20%); background-size: 100%; -webkit-background-clip: text; -moz-background-clip: text; -webkit-text-fill-color: transparent; -moz-text-fill-color: transparent; } </style>
- regex
- try...catch
- ...
- classes
- module
- optional type annotations & static types
- generators & iterators
- destructuring assignment
- ...
- commonjs
var foo = require("foo");
// ...
module.exports = {
bar: function(){
// ...
}
}
- strict mode
- getters & setters
- JSON
- ...
foo = 0;
console.log(foo); // ?
var foo = 0;
console.log(window.foo); // ?
function foo(){
this.bar = "bar";
}
foo(); //?
var foo = 0;
foo.bar = "bar"; //?
var mod = (function mod(){
var local = '....';
var foo = function foo(){ return local;}
//...
return {foo};// exported mod
}());
"use strict";
foo = 0;
console.log(foo);
- es modules
- classes
- generators & iterators
- destructuring assignment
- ...
var $ = (function(){
var jquery = function(){};
//...
return jquery;
}());
var $ = require('jquery');
module.exports = $;
import $ from 'jquery';
export default $;
工具 | 支持情况 |
---|---|
babel | ✅ |
webpack | ✅ |
eslint | ✅ |
rollup | ✅ |
Typescript | ✅(ts v4.5 beta) |
mocha | ✅ |
jest | 🚧 |
{
"presets": ["@babel/preset-env"],
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
}
}
<script>alert('stop!!!!!');</script>
<div>hello!</div>
<script type="module">alert('stop!!!!!');</script>
<div>hello!</div>
- 使用
"type": "module"
或*.mjs
- 使用
.js/.mjs
(e.g.import foo from "./foo.js"
) - 使用
pkg.exports
(dual-mode) - 在 esm 中引入 cjs ✅
- 在 cjs 中引入 esm ❌
Q: esm 相关?
- import()
- import.meta
Q: esm的未来?
Q: The TC39 Process?
- 🤷🏻 Stage 0: Strawperson
- 💡 Stage 1: Proposal
- ✍🏻 Stage 2: Draft
- 📝 Stage 3: Candidate - almost final
- 🎖 Stage 4: Formal standard
- https://jakearchibald.com/2017/es-modules-in-browsers/
- https://caniuse.com/
- https://nodejs.org/
- https://www.typescriptlang.org/docs/
- https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#esm-nodejs
- https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77
- https://gils-blog.tayar.org/posts/using-jsm-esm-in-nodejs-a-practical-guide-part-1/
- https://gils-blog.tayar.org/posts/using-jsm-esm-in-nodejs-a-practical-guide-part-2/
- https://tc39.es/process-document/