The welcome
Library is a minimal (getting started) javascript library that will welcome you from two modules.
The story (or blog) over this repository can be found here:
This repository will build a library:
- exposed as the global variable
welcome
- saved in the file
prefix-welcome.js
- aggregating two modules (
foo
andbar
) - testing a method
The tool used are:
npm
for the module dependencyWebPack
as builder.- Jest as test runner
The output from the browser or from Node will be
Welcome from the foo package !
A warm Welcome from the bar package !
- Install Node
- Clone this repository
git clone https://github.com/gerardnico/welcomejs
- Execute a
npm install
- Execute a
npm run test
to execute the test with Jest throughnpm
npm run test
- Execute a
npm run build
to build the libraryprefix-welcome.js
in the directorydist
npm run build
- Run the examples given in the usage section below.
See the file welcomeLibInNode.js
var welcome = require("../dist/prefix-welcome");
console.log(welcome.foo());
console.log(welcome.bar());
- Run it
C:\welcome\example>node welcomeLibInNode.js
- Output:
Welcome from the foo package !
A warm Welcome from the bar package !
See the file welcomeLibInBrowser.html
<html>
<!--
If published in Node
<script src="https://unpkg.com/myLisbrary"></script>
Otherwised locally
-->
<script src="../dist/prefix-welcome.js"></script>
<body>
</body>
<script>
// Global variable
var welcomeFoo = welcome.foo();
// Property in the window object
var welcomeBar = window.welcome.bar();
document.body.innerHTML = "<h2>"+welcomeFoo+"</h2><h2>"+welcomeBar+"</h2>";
</script>
</html>
- Run it:
Note on thenpm
configuration file package.json
:
Babel
has been added to be able to use theimport
statement with thejest
test framework.jest
is a node process and actually understand onlyrequire
Webpack 2
understands natively theimport
statement.
package-lock.json
should be versionned. npm create it. It's a lockfile that is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.