Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
127 slides
-
Then, Title of the talk - Author Name Slide 1:
Talk Begins Slide 2: -
Now, Author Name Slide 1: Title of the talk Slide
2: Slide 3: : Talk Begins Slide 8: : : -
What did you use to make the presentation? Which VSCode
theme is that? Which font family is that? -
What did you use to make the presentation? - Keynote
Which VSCode theme is that? - Material Which font family is that? - Operator mono -
What is a package? A package is a namespace that
organises set of related files/modules that are responsible of doing some task. In node.js every package has a package.json which has the metadata about that package -
What is a module? A module is anything that can
be loaded with require/import in a Node.js program. import React from 'react'; const webpack = require('webpack'); -
The simplest answer is we all are lazy and so
we don’t want to write the same thing over and over again. Hence, we create packages that can be published as modules so that other people can use them directly. -
So now since we know the “Why” part of it
let’s address the “How” part of it -
// file1.js const makeMessage = (message) => { return `Message:
${message}`; } // file2.js const showMessage = () => { const message = makeMessage('Calling makeMessage’) // makeMessage is undefined console.log(message); } -
// file2.js const makeMessage = (message) => { return `Message:
${message}`; } const showMessage = () => { const message = makeMessage('Calling makeMessage') console.log(message); } -
This was just a small piece of code, but imagine
if you have big massive application and you’re writing everything in one file. -
Issues 1. Messy Code 2. Fucked up Dependency management. 3.
Refactoring becomes painful. -
What are the different types of JS module systems? Various
JS Module Systems + -
1. CommonJS 2. AMD: Async Module Definition 3. UMD: Universal
Module Definition 4. ECMAScript Harmony (ES6) Various JS Module Systems + -
CommonJS Various JS Module Systems + // File log.js function
log(){ console.log('Example of CJS module system'); } // expose log to other modules module.exports = { log }; // File index.js var logModule = require('./log'); logModule.log(); • No static analyzing, as you get an object, so property lookup is at runtime. • You always get a copy of an object, so you won’t get live changes from the module itself. • No tree shaking, because when you import you get an object. -
CommonJS Various JS Module Systems + //------ lib.cjs.js ------ let
counter = 3; const incCounter = () => { counter++; } module.exports = { counter, incCounter } //------ main.js ------ const lib = require('./lib.cjs'); // The imported value `counter` is not live console.log(lib.counter); // 3 lib.incCounter(); console.log(lib.counter); // 3 -
CommonJS Various JS Module Systems + // File log.js function
log(){ console.log('Example of CJS module system'); } // expose log to other modules module.exports = { log } // File index.js var logModule = require('./log'); logModule.log(); • No static analyzing, as you get an object, so property lookup is at runtime. • You always get a copy of an object, so you won’t get live changes from the module itself. • No tree shaking, because when you import you get an object. -
AMD: Async Module Definition Various JS Module Systems + //
File log.js define(['logModule'], function(){ // export (expose) foo for other modules return { log: function(){ console.log('Example of AMD module system'); } }; }); // File index.js require(['log'], function (logModule) { logModule.log(); }); • Implemented by RequireJs • Complex Syntax • Used for dynamic loading of modules -
UMD: Universal Module Definition Various JS Module Systems + //
File log.js (function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports"], factory); } else if (typeof exports !== "undefined") { factory(exports); } else { var mod = { exports: {} }; factory(mod.exports); global.log = mod.exports; } })(this, function (exports) { "use strict"; function log() { console.log("Example of UMD module system"); } // expose log to other modules exports.log = log; }); • Combination of CommonJs + AMD (that is, Syntax of CommonJs + async loading of AMD) -
ECMAScript Harmony (ES6) Various JS Module Systems + // File
log.js const log = () => { console.log('Example of ES module system'); } export default log // File index.js import log from "./log" log(); • Much cleaner. • Import via “import” and export via “export” • Static analyzing — You can determine imports and exports at compile time (statically) . This opens up doors for Tree Shaking. • When you import, you get back actual value, so any change is a live changes in the module itself. -
ECMAScript Harmony (ES6) Various JS Module Systems + //------ lib.es.js
------ export default class Counter { static counter = 3; } export const showCounter = () => { console.log('counter', Counter.counter); } //------ main.js ------ import Counter, { showCounter } from './lib'; // The `Counter.counter` is live console.log(showCounter()) // 3 Counter.counter++ console.log(showCounter()) // 4 -
All of these module systems have one thing in common:
they allow you to import and export stuff Various JS Module Systems + -
Various JS Module Systems + Target 500 million internet users
in India ~ 52.4% users are on 3G and slower networks -
export default class ecmascript { showMessage = () => {
console.log('Welcome to the world of ES'); }; } ES RAW -
export default class ecmascript { constructor() { this.showMessage = ()
=> { console.log('Welcome to the world of ES'); }; } } ES (“modules”: false) -
export default class ecmascript { constructor() { this.showMessage = ()
=> { console.log('Welcome to the world of ES'); }; } } ES CJS exports.__esModule = true; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function”); } } var ecmascript = function ecmascript() { _classCallCheck(this, ecmascript); this.showMessage = function () { console.log('Welcome to the world of ES'); }; }; exports.default = ecmascript; (“modules”: false) (“modules”: “commonjs”) -
export default class ecmascript { constructor() { this.showMessage = ()
=> { console.log('Welcome to the world of ES'); }; } } ES CJS exports.__esModule = true; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function”); } } var ecmascript = function ecmascript() { _classCallCheck(this, ecmascript); this.showMessage = function () { console.log('Welcome to the world of ES'); }; }; exports.default = ecmascript; # Type Size (Bytes) 1 RAW 112 2 ES 146 3 CJS 398 (“modules”: false) (“modules”: “commonjs”) -
Considering the fact that you’re building your code as ES
target but still what about unused code? -
Tree Shaking •It is a term commonly used in the
context of JavaScript for live code inclusion(import whatever is required). " •It relies on the static structure of ES module syntax, that is, “import” and “export”. -
Tree Shakeable ✅ // File shakebake.js const shake = ()
=> console.log('shake'); const bake = () => console.log('bake'); //can be tree shaken as we export as es modules export { shake, bake }; // File index.js import { shake } from './shakebake.js' // only "shake" is included in the output -
Tree Shakeable ✅ // File shakebake.js const shake = ()
=> console.log('shake'); const bake = () => console.log('bake'); //can be tree shaken as we export as es modules export { shake, bake }; // File index.js import { shake } from './shakebake.js' // only "shake" is included in the output Non Tree Shakeable ❌ // File shakebake.js const shake = () => console.log('shake'); const bake = () => console.log('bake'); //cannot be tree shaken as we have exported an object export default { shake, bake }; // File index.js import { shake } from './shakebake.js' // both "shake" and "bake" are included in the output -
Various JS Module Systems + CommonJS ESM Target Comparison #
CommonJS ESM Tree Shaking ❌ ✅ Async Module Loading ❌ ✅ Static/Compile- time analyzing ❌ ✅ Import/require gives actual value of the module? ❌ ✅ -
Various JS Module Systems + CommonJS ESM Target There’s more
in ESM! • <script type=“module”> ➡ deferred by default. • Even if we add the <script type=“module”/> multiple times on the page it gets executed just once. • Global variables is global to that module (lexical scope) and not to the window scope. • You can optimize the delivery of your modules further by using <link rel=“modulepreload”> PS: This is still anexperimental feature. -
<html> <head> <script type="module" src="//unpkg.com/something.mjs"></script> <script type="module" src="//unpkg.com/something.mjs"></script> <script type="module"
src="//unpkg.com/something.mjs"></script> <script type="module" src="//unpkg.com/something.mjs"></script> <!-- The above gets executed just once --> <script src="//unpkg.com/something.js"></script> <script src="//unpkg.com/something.js"></script> <script src="//unpkg.com/something.js"></script> <!-- The above gets executed thrice --> </head> </html> -
Various JS Module Systems + CommonJS ESM Target What Else
in ESM? • <script type=“module”> ➡ deferred by default. • Even if we add the <script type=“module”/> multiple times on the page it gets executed just once. • Global variables are global to that module (lexical scope) and not to the window scope. • You can optimize the delivery of your modules further by using <link rel=“modulepreload”> PS: This is still anexperimental feature. -
<html> <head> <script type="module"> const testModule = "This is a
module" </script> <script type="module"> console.log({ testModule }) // testModule is undefined </script> </head> </html> Scope -
Various JS Module Systems + CommonJS ESM Target What Else
in ESM? • <script type=“module”> ➡ deferred by default. • Even if we add the <script type=“module”/> multiple times on the page it gets executed just once. • Global variables are global to that module (lexical scope) and not to the window scope. • You can optimize the delivery of your modules further by using <link rel=“modulepreload”> PS: This is still anexperimental feature. -
Various JS Module Systems + CommonJS ESM Target Advice: CJS/ESM?
ESM should be targeted primarily and then if there’s a use case for CJS then go for it as the secondary target. -
Shall we just compile or bundle the source? Various JS
Module Systems CommonJS Target Bundle/ Compile ESM + -
Compiling: It is a process of transforming the source from
one module system to another as per the language specs. Tools: Babel PS: Babel is a compiler and not transpiler Bundling: It is a technique most commonly used in today’s “module-based” development where common functionalities can be extracted into a flat bundle at build time. Tools: Webpack/Rollup Various JS Module Systems CommonJS Target Bundle/ Compile ESM + -
Various JS Module Systems CommonJS Target Bundle/ Compile ESM +
Component Library: material-ui, blueprint, semantic-ui etc. Core Package: react, react-dom etc. Core Packages Component Library -
The best approach is publishing different versions of a module
for various use cases. -
A good example is package.json which contains two entry point
versions: “main” and “module” { "name": "ui-kit", "version": "0.0.1", "main": "dist/index.js", "module": "dist/index.mjs", } -
“main” is the standard field used by node.js ecosystem and
therefore it should point to a CJS file. “module” field, on the other hand, points to an ES module file. -
✅ Checklist Manifesto? Thanks Gant for letting me steal this
“line” from your slide 3 -
Component Library 1. Consumer Application should be able to include
the specific components in your application. import { Button } from 'ui-kit' module: "dist/index.mjs" // ---> The module field in package.json -
Component Library 1. Consumer Application should be able to include
the specific components in your application. import { Button } from 'ui-kit' module: "dist/index.mjs" // ---> The module field in package.json 2. if the consumer application use legacy bundling tools, the author should add support for CJS module system as well. import { Button } from 'ui-kit' main: "dist/index.js" // ---> The main field in package.json -
Component Library 1. Consumer Application should be able to include
the specific components in your application. import { Button } from 'ui-kit' module: "dist/index.mjs" // ---> The module field in package.json 2. if the consumer application use legacy bundling tools, the author should add support for CJS module system as well. import { Button } from 'ui-kit' main: "dist/index.js" // ---> The main field in package.json 3. If the consumer doesn't have ^webpack-4 then no sideEffects benefit. import Button from 'ui-kit/es/Button' import Button from 'ui-kit/cjs/Button' -
Component Library 1. Consumer Application should be able to include
the specific components in your application. import { Button } from 'ui-kit' module: "dist/index.mjs" // ---> The module field in package.json 2. if the consumer application use legacy bundling tools, the author should add support for CJS module system as well. import { Button } from 'ui-kit' main: "dist/index.js" // ---> The main field in package.json 3. If the consumer doesn't have ^webpack-4 then no sideEffects benefit. import Button from 'ui-kit/es/Button' import Button from 'ui-kit/cjs/Button' 4. Consumer application should be able to directly consume the package i.e 'ui-kit' with <script type="module"> i.e no tree shaking, no side effects in the browser. import Button from "https://unpkg.com/ui-kit/es/Button/index.mjs" -
Core Packages 1. Consumer Application should be able to include
the default and named imports in the application. import api, { get, post } from 'api' module: "dist/index.mjs" // ---> The module field in package.json -
Core Packages 1. Consumer Application should be able to include
the default and named imports in the application. import api, { get, post } from 'api' module: "dist/index.mjs" // ---> The module field in package.json 2. if the consumer application use legacy bundling tools, the author should add support for CJS module system as well. import api, { get, post } from 'api' main: "dist/index.js" // ---> The main field in package.json -
Core Packages 1. Consumer Application should be able to include
the default and named imports in the application. import api, { get, post } from 'api' module: "dist/index.mjs" // ---> The module field in package.json 2. if the consumer application use legacy bundling tools, the author should add support for CJS module system as well. import api, { get, post } from 'api' main: "dist/index.js" // ---> The main field in package.json 3. Consumer application should be able to directly consume the package i.e 'ui-kit' with <script type="module"> i.e no tree shaking, no side effects in the browser. import api, { get, post } from “https://unpkg.com/api/dist/index.mjs” import api, { get, post } from “https://unpkg.com/api/dist/index.js” -
Component Library ui-kit dist index.js index.mjs cjs Button Text es
Button Text api dist index.js index.mjs Core Packages -
Various JS Module Systems Core Packages CommonJS Target Bundle/ Compile
ESM Component Library + Webpack “sideEffects” Flag -
Various JS Module Systems Core Packages CommonJS Target Bundle/ Compile
ESM + Advice: Bundle/Compile? Component Library: 1. Babelify the source and target CJS and ES. 2. Create “index.mjs” with re-exports. Core Packages: 1. Bundle the source with Rollup and target CJS and ES. Component Library -
How to Publish? Various JS Module Systems Core Packages Target
Bundle/ Compile + CommonJS ESM Publish Component Library -
Various JS Module Systems Core Packages Target Bundle/ Compile +
CommonJS ESM Publish NPM publish It’s as simple as that? > npm version patch -m "Upgrade to %s for reasons” > npm publish Component Library -
Various JS Module Systems Core Packages Target Bundle/ Compile Component
Library + CommonJS ESM Publish NPM publish Hmm, so what’s new in that? -
Everyone was tired of checking whether the object was
null over and over. So one of you implemented “isEmpty” and thought to publish the package as npm module. -
Now someone from team identified that this “isEmpty” doesn’t checks
for empty arrays. So they want to make changes to this package. After making changes they publish a minor version of the module to npm. > npm version minor -m "Upgrade to %s for reasons” > npm publish -
The problem comes if someone wants to use this, they
need to update the package version to the latest. “0.30.0” ➡ “0.30.1” -
What if you were a large team and there was
some critical bugfix in one of the package and you want people to upgrade the package version to the latest at any cost? +7+77……+7 -
What if you were a large team and there was
some critical bugfix in one of the package and you want people to upgrade the package version to the latest at any cost? -
What is inside your node_modules? { "name": "my-package", "files":
[ "folder1", "file1" ] } -
What If we can build the package locally and refer
it(symlink) from node_modules? -
ui-kit dist src package.json api dist src package.json my-app build
src package.json Repo1 Repo2 Repo3 -
ui-kit dist src package.json api dist src package.json my-app build
src package.json Repo1 Repo2 Repo3 ui-kit dist src package.json api dist src package.json my-app build src package.json package.json Repo -
Left Hand Side It becomes cumbersome if we want to
refer(symlink) “ui-kit “ and “api” inside “my-app”. -
ui-kit dist src package.json api dist src package.json my-app build
src package.json Repo1 Repo2 Repo3 ui-kit dist src package.json api dist src package.json my-app build src package.json package.json Repo -
Right Hand Side It becomes super easy if we want
to refer(symlink) “ui-kit “ and “api” inside “my-app”, because they are now within same repo and under one hierarchy -
ui-kit dist src package.json api dist src package.json my-app build
src package.json Repo1 Repo2 Repo3 ui-kit dist src package.json api dist src package.json my-app build src package.json package.json Repo -
Monorepos? Various JS Module Systems Core Package Target Bundle/ Compile
Component Library + CommonJS ESM Publish Monorepos NPM publish -
Lerna is a tool that optimizes our workflow around managing
npm packages. It is created by awesome people who built babel. -
You can perform operations like build multiple packages, clean multiple
packages, publish multiple packages for all the packages in your monorepo with simple commands. -
Lerna creates symlinks between the packages that refer each other
inside your monorepo. This process is called “bootstrapping”. -
+ Publish Monorepos NPM publish Various JS Module Systems Target
CommonJS ESM Core Package Component Library Bundle/ Compile Advice: npm publish/monorepos? Well, If you’re publishing something to be used globally you have to publish it to npm. If you’re building something that is private within your team/org give monorepos a try. -
Various JS Module Systems + Let’s Reiterate Target CommonJS ESM
Core Package Component Library Bundle/ Compile -
+ Publish Monorepos NPM publish Let’s Reiterate Various JS Module
Systems Target CommonJS ESM Core Package Component Library Bundle/ Compile