Last active
February 11, 2024 23:14
-
-
Save mendozammatias/325e9f3b8ae0cf91bfbf1f11987bd975 to your computer and use it in GitHub Desktop.
Part 1 - Javascript module types (CJS, AMD, UMD, ESM, WTF?)
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
// data/names.js | |
define('names', [], function() { | |
return ({ | |
john: 'John Doe', | |
foo: 'Foo Bar', | |
test: 'Test User', | |
homer: 'Homer Jay Simpson' | |
}) | |
}); | |
// lib/greetings.js | |
define('greetings',['../data/names'], function(names) { | |
return ({ | |
sayHelloTo: function(user) { | |
return `Hello ${names[user]} !`; | |
}, | |
sayGoodbyeTo: function(user) { | |
return `Goodbye ${names[user]} !`; | |
} | |
}) | |
}); | |
// index.js | |
define('index',['./lib/greetings'], function(greetings) { | |
console.info(greetings.sayHelloTo('foo')); | |
console.info(greetings.sayGoodbyeTo('foo')); | |
}); |
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
// data/names.js | |
const names = { | |
john: 'John Doe', | |
foo: 'Foo Bar', | |
test: 'Test User', | |
homer: 'Homer Jay Simpson' | |
}; | |
module.exports = names | |
// lib/greetings.js | |
const names = require('../data/names'); | |
module.exports.sayHelloTo = function(user) { | |
return `Hello ${names[user]} !`; | |
} | |
module.exports.sayGoodbyeTo = function(user) { | |
return `Goodbye ${names[user]} !`; | |
} | |
// index.js | |
const greetings = require('./lib/greetings') | |
console.info(greetings.sayHelloTo('john')); | |
console.info(greetings.sayGoodbyeTo('john')); |
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
// data/names.js | |
export const names = { | |
john: "John Doe", | |
foo: "Foo Bar", | |
test: "Test User", | |
homer: "Homer Jay Simpson" | |
}; | |
// lib/greetings.js | |
import { names } from "../data/names"; | |
export const sayHelloTo = function(user) { | |
return `Hello ${names[user]} !`; | |
}; | |
export const sayGoodbyeTo = function(user) { | |
return `Goodbye ${names[user]} !`; | |
}; | |
// index.js | |
import {sayHelloTo, sayGoodbyeTo} from "./lib/greetings"; | |
console.info(sayHelloTo("homer")); | |
console.info(sayGoodbyeTo("homer")); |
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
// data/names.js | |
(function(root, factory) { | |
if (typeof define === "function" && define.amd) { | |
define([], factory); | |
} else if (typeof exports === "object") { | |
module.exports = factory(); | |
} else { | |
root.returnExports = factory(root); | |
} | |
})(this, function() { | |
return { | |
john: "John Doe", | |
foo: "Foo Bar", | |
test: "Test User", | |
homer: "Homer Jay Simpson" | |
}; | |
}); | |
// lib/greetings.js | |
(function(root, factory) { | |
if (typeof define === "function" && define.amd) { | |
define(["../data/names"], factory); | |
} else if (typeof exports === "object") { | |
module.exports = factory(require("../data/names")); | |
} else { | |
root.returnExports = factory(root.names); | |
} | |
})(this, function(names) { | |
function sayHelloTo(user) { | |
return `Hello ${names[user]} !`; | |
} | |
function sayGoodbyeTo(user) { | |
return `Hello ${names[user]} !`; | |
} | |
return { | |
sayHelloTo, | |
sayGoodbyeTo | |
}; | |
}); | |
// index.js | |
(function(root, factory) { | |
if (typeof define === "function" && define.amd) { | |
define(["./lib/greetings"], factory); | |
} else if (typeof exports === "object") { | |
module.exports = factory(require("./lib/greetings")); | |
} else { | |
root.returnExports = factory(root.greetings); | |
} | |
})(this, function(greetings) { | |
console.info(greetings.sayHelloTo("test")); | |
console.info(greetings.sayGoodbyeTo("test")); | |
return null; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment