├── .babelrc ├── .gitignore ├── README.md ├── dist └── add.min.js ├── lib ├── add.js └── add.test.js ├── package-lock.json ├── package.json └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | node_modules/ 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # npm Module Starter 2 | 3 | This is a simple example of a JavaScript module, using Webpack 4 and Babel to generate a browser compatible UMD version. 4 | 5 | It also uses Jest as unit testing. 6 | 7 | ## Installation 8 | 9 | ```sh 10 | git clone https://github.com/codeKonami/npm-module-starter.git 11 | cd npm-module-starter 12 | npm install 13 | npm run test # to launch the tests 14 | npm run build # to build the distro 15 | ``` 16 | -------------------------------------------------------------------------------- /dist/add.min.js: -------------------------------------------------------------------------------- 1 | !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.add=t():e.add=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t){e.exports=function(e,t){return e+t}}])}); -------------------------------------------------------------------------------- /lib/add.js: -------------------------------------------------------------------------------- 1 | const add = (a, b) => { 2 | return a + b; 3 | }; 4 | 5 | module.exports = add; 6 | -------------------------------------------------------------------------------- /lib/add.test.js: -------------------------------------------------------------------------------- 1 | import add from './add'; 2 | 3 | test('should return 10 when value (3, 7)', () => { 4 | expect(add(3, 7)).toBe(10); 5 | }); 6 | 7 | test('should return 25 when value (20, 5)', () => { 8 | expect(add(20, 5)).toBe(25); 9 | }); 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "add", 3 | "version": "1.0.0", 4 | "description": "Just an add function", 5 | "main": "lib/add.js", 6 | "scripts": { 7 | "test": "jest", 8 | "build": "webpack" 9 | }, 10 | "license": "MIT", 11 | "devDependencies": { 12 | "@babel/core": "^7.7.2", 13 | "@babel/preset-env": "^7.7.1", 14 | "babel-loader": "^8.0.6", 15 | "jest": "^24.9.0", 16 | "uglifyjs-webpack-plugin": "^2.2.0", 17 | "webpack": "^4.41.2", 18 | "webpack-cli": "^3.3.10" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); 4 | 5 | module.exports = { 6 | mode: 'production', 7 | entry: './lib/add.js', 8 | output: { 9 | path: path.resolve(__dirname, 'dist'), 10 | filename: 'add.min.js', 11 | libraryTarget: 'umd', 12 | library: 'add', 13 | }, 14 | module: { 15 | rules: [ 16 | { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" } 17 | ] 18 | }, 19 | plugins: [ 20 | new UglifyJsPlugin(), 21 | ] 22 | } 23 | --------------------------------------------------------------------------------