SlideShare ist ein Scribd-Unternehmen logo
1 von 43
@sebpertus#DevoxxFR
Ecmascript Modules aka ESM
It’s about time !!!
@sebpertus#DevoxxFR
Sebastien Pertus
OSS Lover
Node.JS & .Net Core advocate
SQL Server man
Develops on Windows
@sebpertus
(Suivez moi, on a un concours de grosse kékétte, et je suis loin derrière !!)
@sebpertus#DevoxxFR
ES6 / ES7 / ES2015 / ES2016
" ES7 is ES2016"
" ES6 is ES2015 "
" Too long for twitter … "
@sebpertus#DevoxxFR
Where we are today
@sebpertus#DevoxxFR
ES6 : Where we are today
96%
On updated browsers
http://kangax.github.io/compat-table/es6/
@sebpertus#DevoxxFR
Les Modules
CommonJS / AMD / ESM ?
@sebpertus#DevoxxFR
Modules ?
• No Html files in Node to load Javascript files
• Implementing a way to load files as « modules » in a fair way
• 2009 : Projet ServerJS was born (later changed its name to CommonJS)
• They defined a first version of « what could be a module »
@sebpertus#DevoxxFR
NodeJS
class person {
getFullName() { return `${this.lastName} ${this.firstName}`; }
}
exports.person = person;
const people = require("./person");
var p = new people.person();
@sebpertus#DevoxxFR
Browsers
• AMD (Asynchronous Module definition) is format defined for browser
modules loader
• Implemented by framework like require.js
<script src="lib/require.js"></script>
define(function () {
return { getHello: function () { return 'Hello World';} };
});
define(function (require) {
var messages = require('./messages');
var m = messages.getHello();
});
@sebpertus#DevoxxFR
ESM : EcamScript Modules
• Landed in ES6
export class person {
getFullName() { return `${this.lastName} ${this.firstName}`; }
}
import * as people from './person';
var p = new people.person();
@sebpertus#DevoxxFR
Demo
Writing EcmaScripts Modules
@sebpertus#DevoxxFR
Modules in browsers
@sebpertus#DevoxxFR
Modules in Browser
• Safari 10.1
• Chrome canary 60
• Firefox 54
• Edge 16
With FLAGS
https://jakearchibald.com/2017/es-modules-in-browsers/
@sebpertus#DevoxxFR
In browser, don’t forget extensions
https://medium.com/webpack/the-state-of-javascript-modules-4636d1774358
@sebpertus#DevoxxFR
ESM. In browsers
Support
@sebpertus#DevoxxFR
ESM. In browsers. Scripts vs Modules
@sebpertus#DevoxxFR
ESM. In browsers. Rember that !
• Modules are deferred. Run after document loaded
• "import" and "export" statements can only appears at the top-level
• Modules are strict mode by design (think "use strict" )
@sebpertus#DevoxxFR
ESM. In browsers. Defer by default
@sebpertus#DevoxxFR
ESM. In browsers. Defer by default
@sebpertus#DevoxxFR
Demos
Demo 02 : Writing EcmaScripts
Modules for browsers.
Demo 03 : Creating an Express
application and modules
Demo 04 : Using bundler to
handle non ESM browsers
@sebpertus#DevoxxFR
ESM in Node.JS
… The Michael Jackson Script !
@sebpertus#DevoxxFR
NodeJS modules : CommonJS
• Synchronous
• Evaluated during runtime
@sebpertus#DevoxxFR
ESM vs CJS
export class person {
getFullName() { return `${this.lastName} ${this.firstName}`; }
}
import * as people from './person';
var p = new people.person();
class person {
getFullName() { return `${this.lastName} ${this.firstName}`; }
}
exports.person = person;
const people = require("./person");
var p = new people.person();
@sebpertus#DevoxxFR
ESM modules in NodeJS
• https://github.com/nodejs/node-eps/blob/master/002-es-
modules.md
• Implement interoperability for EcmaScript 6 modules (ESM) and
Node’s existing module system (CJS)
• Allow a common module syntax for Browser (ES6 spec) and Server
side
@sebpertus#DevoxxFR
EF modules vs NodeJS modules
• CommonJS is a dynamic loader modules system
• Synchronous
• Evaluated during runtime
• ES is a static loader modules system
• Asynchronous
• Evaluated during parse time
@sebpertus#DevoxxFR
Example
console.log('entry eval');
require('middle');
console.log('entry done);
export {};
console.log('middle eval');
require('right');
console.log('middle done');
entry eval
middle eval
right eval
right done
middle done
entry done
export {};
console.log('right eval');
console.log('right done');
entry eval
entry done
middle eval
middle done
right eval
right done
sync async
@sebpertus#DevoxxFR
ESM vs CJS. Solutions evaluated ?
• Does Node.js want to ship a synchronous or asynchronous module
loader?
• Returning a promise (async)
• Returning an object (sync)
• Both ?
• Multiple Solutions investigated:
• Use CJS (require) for actuals .js files / Use ESM (import) with a newly file .mjs
• Use a double parsing time
• Config file
• Obiwan Kenobi
@sebpertus#DevoxxFR
@sebpertus#DevoxxFR
Detectiong CJS / ES Modules
• https://github.com/nodejs/node/wiki/ES6-Module-Detection-in-Node
• https://github.com/dherman/defense-of-dot-js/blob/master/proposal.md
@sebpertus#DevoxxFR
References
• http://codeheroics.github.io/talks/nodemjs/
• https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-
node-js-42c958b890c
• https://hackernoon.com/node-js-tc-39-and-modules-a1118aecf95e
• https://medium.com/webpack/the-state-of-javascript-modules-
4636d1774358
• https://github.com/nodejs/node-eps/blob/master/002-es6-modules.md
• https://github.com/nodejs/node-eps/issues/57 (mjs tradeoff)
• http://2ality.com/2017/01/babel-esm-spec-mode.html
• http://2ality.com/2017/05/es-module-specifiers.html
@sebpertus#DevoxxFR
Michael Jackson Script
Code compiler must know if it’s a CJS or ES module
In certain circumstance an import * from ‘foo’ could be
a CJS module or an ES module (same syntax !)
Node.js will need to have some kind of mechanism for
identifying in advance what kind of file is being loaded
foo.js will treat foo.js as CommonJS
bar.mjs will treat bar.mjs as an ESM Module.
console.log(`Module load ${Date()}`)
@sebpertus#DevoxxFR
Hey Let’s go !!!
• We are Ok ?
• OK DEMO TIME, DUDE !!!!
• Well, at least, I made demos from here, last year…
@sebpertus#DevoxxFR
But…
• Suddenly….
• The January 26 ….
• Comes in the game …
https://github.com/nodejs/node/pull/18392
@sebpertus#DevoxxFR
ESM: NodeJS Package mode
package.json
{
"mode": "esm"
}
@sebpertus#DevoxxFR
ESM: NodeJS Package mode
• Builds on the « In Defense on JS » proposal
• https://github.com/dherman/defense-of-dot-js/blob/master/proposal.md
• A directory containing a package.json with this flag will be considered as an ESM
• Any « .js » file will be considered as an ESM (otherwise as a CommonJS module)
• Benefits
• Enables « .js » to be used as ESM
• Transparent interop
• Easy upgrade path
• Cons
• Resolver performance
• If forget to mention « mode:esm » => Unexpected token export will be raised
@sebpertus#DevoxxFR
@sebpertus#DevoxxFR
ES6 : Modules (ESM)
• Node.js 8.7+ : Implementation with flags
@sebpertus#DevoxxFR
ESM: Interoperability
• ES modules can import CJS modules
• BUT They have only a default export (value of module.exports)
• ES modules does not allow using « require() »
• Path resolution does not work the same
• ESM are asynchronous
• CJS modules can’t require() ES modules
• You start with ESM, you are stick with it !
• ESM in older Node.JS version
• Are you serious ????
• Use @std/esm package
@sebpertus#DevoxxFR
https://medium.com/web-on-the-edge/es-modules-in-node-today-32cff914e4b
ES Modules in Node Today
with polyfil @std/esm
@std/esm
Works from Node.js 4
Use *.mjs modules file extension
@sebpertus#DevoxxFR
@sebpertus#DevoxxFR
Demo : ESM Modules in Node (nighty build)
Demo 05 : Complete solution
and migrate to *.mjs
Demo 06 : future
implementation of dynamic
import()
@sebpertus#DevoxxFR
Références
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
https://www.contentful.com/blog/2017/04/04/es6-modules-support-lands-in-browsers-is-it-time-to-rethink-bundling/
http://2ality.com/2011/03/first-look-at-upcoming-javascript.html
https://medium.com/dev-channel/es6-modules-in-chrome-canary-m60-ba588dfb8ab7
http://2ality.com/2014/09/es6-modules-final.html
https://jakearchibald.com/2017/es-modules-in-browsers/
https://blog.mariusschulz.com/2016/06/12/bundling-and-tree-shaking-with-rollup-and-ecmascript-2015-modules
https://medium.com/the-node-js-collection/the-current-state-of-implementation-and-planning-for-esmodules-
a4ecb2aac07a
http://2ality.com/2017/01/import-operator.html
@sebpertus#DevoxxFR
Grab the code
https://github.com/Mimetis/ESM
Get the readme file and play the session with
your organization / meetup groups 
@sebpertus

Weitere ähnliche Inhalte

Was ist angesagt?

T4T Training day - NodeJS
T4T Training day - NodeJST4T Training day - NodeJS
T4T Training day - NodeJS
Tim Sommer
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
Prasoon Kumar
 
OSGi Community Event 2010 - Rapid Bundle Development with Bndtools for Eclipse
OSGi Community Event 2010 - Rapid Bundle Development with Bndtools for EclipseOSGi Community Event 2010 - Rapid Bundle Development with Bndtools for Eclipse
OSGi Community Event 2010 - Rapid Bundle Development with Bndtools for Eclipse
mfrancis
 
Webconf nodejs-production-architecture
Webconf nodejs-production-architectureWebconf nodejs-production-architecture
Webconf nodejs-production-architecture
Ben Lin
 

Was ist angesagt? (20)

Tampering with JavaScript
Tampering with JavaScriptTampering with JavaScript
Tampering with JavaScript
 
Node.js in a heterogeneous system
Node.js in a heterogeneous systemNode.js in a heterogeneous system
Node.js in a heterogeneous system
 
NodeJS Concurrency
NodeJS ConcurrencyNodeJS Concurrency
NodeJS Concurrency
 
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime WebGeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime Web
 
Create your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and PackerCreate your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and Packer
 
T4T Training day - NodeJS
T4T Training day - NodeJST4T Training day - NodeJS
T4T Training day - NodeJS
 
Cis222 2
Cis222 2Cis222 2
Cis222 2
 
The SPDY Protocol
The SPDY ProtocolThe SPDY Protocol
The SPDY Protocol
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs
 
Lighting fast rails with zeus
Lighting fast rails with zeusLighting fast rails with zeus
Lighting fast rails with zeus
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Cialug August 2021
Cialug August 2021Cialug August 2021
Cialug August 2021
 
Building an ActionScript Game Server with over 15,000 Concurrent Connections
Building an ActionScript Game Server with over 15,000 Concurrent ConnectionsBuilding an ActionScript Game Server with over 15,000 Concurrent Connections
Building an ActionScript Game Server with over 15,000 Concurrent Connections
 
Packer, where DevOps begins
Packer, where DevOps beginsPacker, where DevOps begins
Packer, where DevOps begins
 
OSGi Community Event 2010 - Rapid Bundle Development with Bndtools for Eclipse
OSGi Community Event 2010 - Rapid Bundle Development with Bndtools for EclipseOSGi Community Event 2010 - Rapid Bundle Development with Bndtools for Eclipse
OSGi Community Event 2010 - Rapid Bundle Development with Bndtools for Eclipse
 
Webconf nodejs-production-architecture
Webconf nodejs-production-architectureWebconf nodejs-production-architecture
Webconf nodejs-production-architecture
 
Ansible @ WebElement 2015
Ansible @ WebElement 2015Ansible @ WebElement 2015
Ansible @ WebElement 2015
 
A Introduction of Packer
A Introduction of PackerA Introduction of Packer
A Introduction of Packer
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
A slightly advanced introduction to node.js
A slightly advanced introduction to node.jsA slightly advanced introduction to node.js
A slightly advanced introduction to node.js
 

Ähnlich wie EcamScript (ESM) It's about time !

Ähnlich wie EcamScript (ESM) It's about time ! (20)

Future of NodeJS
Future of NodeJSFuture of NodeJS
Future of NodeJS
 
Discovering OpenBSD on AWS
Discovering OpenBSD on AWSDiscovering OpenBSD on AWS
Discovering OpenBSD on AWS
 
Node.js: The What, The How and The When
Node.js: The What, The How and The WhenNode.js: The What, The How and The When
Node.js: The What, The How and The When
 
mesos-devoxx14
mesos-devoxx14mesos-devoxx14
mesos-devoxx14
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
How we dockerized a startup
How we dockerized a startupHow we dockerized a startup
How we dockerized a startup
 
Going Offline with JS
Going Offline with JSGoing Offline with JS
Going Offline with JS
 
A nodejs application
A nodejs applicationA nodejs application
A nodejs application
 
Apache Mesos at Twitter (Texas LinuxFest 2014)
Apache Mesos at Twitter (Texas LinuxFest 2014)Apache Mesos at Twitter (Texas LinuxFest 2014)
Apache Mesos at Twitter (Texas LinuxFest 2014)
 
Webpack Encore - Asset Management for the rest of us
Webpack Encore - Asset Management for the rest of usWebpack Encore - Asset Management for the rest of us
Webpack Encore - Asset Management for the rest of us
 
D-DAY 2015 Conteneurisé une startup
D-DAY 2015 Conteneurisé une startup  D-DAY 2015 Conteneurisé une startup
D-DAY 2015 Conteneurisé une startup
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
Build your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectBuild your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto project
 
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
SenchaCon 2016: Building a Faceted Catalog of Video Game Assets Using Ext JS ...
 
Gianluca Varisco - DevOoops (Increase awareness around DevOps infra security)
Gianluca Varisco - DevOoops (Increase awareness around DevOps infra security)Gianluca Varisco - DevOoops (Increase awareness around DevOps infra security)
Gianluca Varisco - DevOoops (Increase awareness around DevOps infra security)
 
mjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profilingmjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profiling
 
.NET7.pptx
.NET7.pptx.NET7.pptx
.NET7.pptx
 
Getting started with developing Nodejs
Getting started with developing NodejsGetting started with developing Nodejs
Getting started with developing Nodejs
 
Dcjq node.js presentation
Dcjq node.js presentationDcjq node.js presentation
Dcjq node.js presentation
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

EcamScript (ESM) It's about time !

Hinweis der Redaktion

  1. export class Person { firstName: string; lastName: string; constructor(fn: string, ln: string, public age = 27) { this.firstName = fn; this.lastName = ln; } getFullName() { return `Full name : ${this.firstName} ${this.lastName} ${this.age} ` } } export class Kid { } export class adzdazddadadzad { } export class people { kid = () => new Kid(); person = (fn, ln, age) => new Person(fn, ln, age); az = () => new Kid() } var pp = new people(); export default pp;