Skip to content

Commit

Permalink
add ch10 requirejs + umd + cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Apr 28, 2023
1 parent 3d17925 commit 2d5e742
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ch10/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Code in app.js. baseURL set to the lib folder
// containing jquery.color.
define(["https://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"],
function(colorPlugin, _) {
// Here we've loaded the jQuery color plugin and Lodash from a CDN
// None of these will be accessible in the global scope, but we
// can easily reference them below.

// Pseudorandomize an array of colors, selecting the first
// item in the shuffled array
var shuffleColor = _.first(_.shuffle(["#AAA", "#FFF", "#111", "#F16"]));
console.log(shuffleColor);

// Animate the background color of any elements with the class
// "item" on the page using the shuffled color
$(".item").animate({ "backgroundColor": shuffleColor });

// What we return can be used by other modules
return function() {};
});
7 changes: 7 additions & 0 deletions ch10/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function () {
return {
doSomething: function () {
console.log('Bar: Doing something else...');
}
};
});
7 changes: 7 additions & 0 deletions ch10/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function () {
return {
doSomething: function () {
console.log('Foo: Doing something...');
}
};
});
7 changes: 7 additions & 0 deletions ch10/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(["./templates", "text!./template.md", "css!./template.css"],
function (templates, template) {
console.log(templates);
console.log(template);
// Use the templates and template content here
}
);
13 changes: 13 additions & 0 deletions ch10/node/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mathUtils = require('./math');

const result1 = mathUtils.add(5, 3);
console.log('Addition:', result1);

const result2 = mathUtils.subtract(10, 7);
console.log('Subtraction:', result2);

/*
››› node main.js
Addition: 8
Subtraction: 3
*/
12 changes: 12 additions & 0 deletions ch10/node/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function add(a, b) {
return a + b;
}

function subtract(a, b) {
return a - b;
}

module.exports = {
add,
subtract
};
10 changes: 10 additions & 0 deletions ch10/node/umd_main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
define(["node/umd_math"], function (mathUtils) {
const outputElement = document.getElementById('output');

const result1 = mathUtils.add(5, 3);
outputElement.innerHTML += 'Addition: ' + result1 + '<br>';

const result2 = mathUtils.subtract(10, 7);
outputElement.innerHTML += 'Subtraction: ' + result2 + '<br>';
});

25 changes: 25 additions & 0 deletions ch10/node/umd_math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD module
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node.js/CommonJS module
module.exports = factory();
} else {
// Global variable (browser)
root.mathUtils = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
var mathUtils = {};

mathUtils.add = function (a, b) {
return a + b;
};

mathUtils.subtract = function (a, b) {
return a - b;
};

return mathUtils;
}));

29 changes: 29 additions & 0 deletions ch10/requirejs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AMD/RequireJS Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
</head>
<body>
<h1>AMD/RequireJS Example</h1>
<button id="button">Do Stuff</button>

<script>
require.config({
paths: {
foo: './foo',
bar: './bar'
}
});

require(['foo', 'bar'], function (foo, bar) {
document.getElementById('button').addEventListener('click', function () {
foo.doSomething();
bar.doSomething();
});
});
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions ch10/requirejs_jquery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>RequireJS and jQuery Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script>
// Configure RequireJS to load the main module
require(["app"], function(app) {
// The main module will be executed here
});
</script>
</head>
<body>
<div class="item">Animated Item</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions ch10/requirejs_plugins.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AMD/RequireJS Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
</head>
<body>
<script>
require.config({
paths: {
text: "https://cdnjs.cloudflare.com/ajax/libs/require-text/2.0.12/text.min",
css: "https://cdnjs.cloudflare.com/ajax/libs/require-css/0.1.10/css.min",
main: "./main"
}
});

require(["main"]);
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions ch10/template.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: yellow;
}
1 change: 1 addition & 0 deletions ch10/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a template
7 changes: 7 additions & 0 deletions ch10/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function () {
return {
template1: "<div>Template 1</div>",
template2: "<div>Template 2</div>",
template3: "<div>Template 3</div>"
};
});
16 changes: 16 additions & 0 deletions ch10/umd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>UMD Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script>
// Configure RequireJS to load the main module
require(["node/umd_main"], function () {
// The main module will be executed here
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>

0 comments on commit 2d5e742

Please sign in to comment.