├── .babelrc.js ├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmrc ├── .nvmrc ├── LICENSE ├── README.md ├── jest.config.js ├── package-lock.json ├── package.json ├── rollup.config.js ├── src ├── constants.js └── index.js └── test └── index.spec.js /.babelrc.js: -------------------------------------------------------------------------------- 1 | const { NODE_ENV } = process.env; 2 | 3 | module.exports = { 4 | presets: [ 5 | [ 6 | '@babel/preset-env', 7 | { 8 | modules: NODE_ENV === 'test' ? 'auto' : false 9 | } 10 | ] 11 | ], 12 | plugins: [ 13 | '@babel/plugin-proposal-object-rest-spread' 14 | ] 15 | }; 16 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | /dist/** 2 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | browser: true, 4 | node: true, 5 | jest: true 6 | }, 7 | parserOptions: { 8 | ecmaVersion: 9, 9 | sourceType: 'module', 10 | allowImportExportEverywhere: true 11 | }, 12 | extends: [ 13 | 'eslint:recommended' 14 | ], 15 | rules: { 16 | // Possible Errors 17 | 'no-empty': ['error', { 18 | 'allowEmptyCatch': true 19 | }], 20 | 'no-use-before-define': ['error', { 21 | 'functions': false 22 | }], 23 | // Stylistic Issues 24 | 'indent': ['warn', 2, { 25 | 'SwitchCase': 1, 26 | 'ignoredNodes': ['TemplateLiteral'] 27 | }], 28 | 'quotes': ['warn', 'single', { 29 | 'allowTemplateLiterals': true 30 | }], 31 | 'no-multiple-empty-lines': ['warn', { 32 | 'max': 1 33 | }], 34 | 'space-infix-ops': ['warn', { 35 | 'int32Hint': false 36 | }], 37 | 'semi': ['warn', 'always'], 38 | 'no-trailing-spaces': ['warn'], 39 | 'comma-spacing': ['warn'], 40 | 'comma-style': ['warn'], 41 | 'operator-linebreak': ['warn', 'before'], 42 | 'brace-style': ['warn'], 43 | 'keyword-spacing': ['warn'], 44 | 'object-curly-spacing': ['warn', 'always'], 45 | 'space-before-blocks': ['warn', 'always'], 46 | 'spaced-comment': ['warn', 'always'], 47 | 'space-before-function-paren': ['warn', { 48 | 'anonymous': 'always', 49 | 'named': 'never', 50 | 'asyncArrow': 'always' 51 | }], 52 | 'padded-blocks': ['warn', 'never'], 53 | 'comma-dangle': ['warn', 'never'], 54 | // Best Practices 55 | 'curly': ['warn'], 56 | 'eqeqeq': ['error', 'always', { 57 | 'null': 'ignore' 58 | }], 59 | 'no-multi-spaces': ['warn', { 60 | 'ignoreEOLComments': true, 61 | 'exceptions': { 62 | 'Property': false 63 | } 64 | }] 65 | } 66 | }; 67 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: build 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [16.x] 20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 21 | 22 | steps: 23 | - uses: actions/checkout@v2 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v1 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | 29 | - name: build, test, lint 30 | run: | 31 | npm install 32 | npm run test:coverage --if-present 33 | npm run lint --if-present 34 | npm run build --if-present 35 | 36 | - name: Coveralls 37 | uses: coverallsapp/github-action@master 38 | with: 39 | github-token: ${{ secrets.GITHUB_TOKEN }} 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | # production 64 | dist 65 | lib 66 | 67 | # platform specific 68 | .DS_Store 69 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | save-prefix='~' -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 16 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 George Raptis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![build](https://github.com/georapbox/rollup-library-starter-kit/workflows/build/badge.svg) 2 | 3 | # rollup-library-starter-kit 4 | 5 | Rollup starter kit for creating libraries (Input: ES6, Output: UMD, CommonJS, ESM) 6 | 7 | ## Features 8 | 9 | - Rollup 2.x.x 10 | - Babel 7 11 | - ES6 as a source 12 | - Exports in UMD, CommonJS, ESM formats 13 | - ES6 test setup with [Jest](https://jestjs.io/) 14 | - Linting with [ESLint](https://eslint.org/) 15 | - Basic [Travis](https://travis-ci.org/) configuration 16 | 17 | ## Getting started 18 | 19 | ### 1. Setup the library's name 20 | 21 | - Open `rollup.config.js` and change the value of `LIBRARY_NAME` variable with your library's name. 22 | - Open `package.json` and change the following properties with your library's equivalent 23 | - `name` 24 | - `version` 25 | - `description` 26 | - `main` 27 | - `module` 28 | - `browser` 29 | - `repository` 30 | - `author` 31 | - `license` 32 | - `bugs` 33 | - `homepage` 34 | 35 | ### 2. Install dependencies 36 | 37 | - Run `npm install` to install the library's dependencies. 38 | 39 | ### 3. Build for development 40 | 41 | - Having all the dependencies installed run `npm run dev`. This command will generate `UMD` (unminified), `CommonJS` and `ESM` modules under the `dist` folder. It will also watch for changes in source files to recompile. 42 | 43 | ### 4. Build for production 44 | 45 | - Having all the dependencies installed run `npm run build`. This command will generate the same modules as above and one extra minified `UMD` bundle for usage in browser. 46 | 47 | ## Scripts 48 | 49 | - `npm run build` - Produces production version of library modules under `dist` folder. 50 | - `npm run dev` - Produces a development version of library and runs a watcher to watch for changes. 51 | - `npm run test` - Runs the tests. 52 | - `npm run test:watch` - Runs the tests in watch mode for development. 53 | - `npm run test:coverage` - Runs the tests and provides with test coverage information. 54 | - `npm run lint` - Lints the source code with ESlint. 55 | - `npm run prepare` - Run both BEFORE the package is packed and published, on local npm install without any arguments, and when installing git dependencies. 56 | - `npm run clean` - Deletes `dist` and `coverage` folders. 57 | 58 | ## Misc 59 | 60 | - By default all source code is located under the `src` folder. 61 | - Be default `dist` folder is excluded from source control but included for npm. You can change this behavior by not excluding this folder inside the `.gitignore` file. 62 | - The starter kit assumes that all tests are located under `test` folder with `.spec.js` extension. 63 | 64 | ## License 65 | 66 | [The MIT License (MIT)](https://georapbox.mit-license.org/@2019) 67 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | coverageReporters: ['json', 'lcov', 'text', 'clover', 'html'] 3 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-library-starter-kit", 3 | "version": "1.0.0", 4 | "description": "Rollup starter kit for creating libraries", 5 | "main": "dist/Library.cjs.min.js", 6 | "module": "dist/Library.esm.min.js", 7 | "unpkg": "dist/Library.umd.min.js", 8 | "files": [ 9 | "src/", 10 | "dist/" 11 | ], 12 | "scripts": { 13 | "build": "rollup -c --environment BUILD:production", 14 | "dev": "rollup -c -w", 15 | "lint": "eslint src/**/*.js", 16 | "test": "jest --config jest.config.js", 17 | "test:watch": "npm run test -- --watch", 18 | "test:coverage": "npm run test -- --coverage", 19 | "clean": "rimraf dist coverage", 20 | "prepare": "npm-run-all clean lint test build" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/georapbox/rollup-library-starter-kit.git" 25 | }, 26 | "keywords": [], 27 | "author": "George Raptis ", 28 | "license": "MIT", 29 | "bugs": { 30 | "url": "https://github.com/georapbox/rollup-library-starter-kit/issues" 31 | }, 32 | "homepage": "https://github.com/georapbox/rollup-library-starter-kit#readme", 33 | "devDependencies": { 34 | "@babel/core": "~7.17.9", 35 | "@babel/plugin-proposal-object-rest-spread": "~7.17.3", 36 | "@babel/preset-env": "~7.16.11", 37 | "@babel/register": "~7.17.7", 38 | "cross-env": "~7.0.3", 39 | "eslint": "~8.14.0", 40 | "jest": "~28.0.1", 41 | "npm-run-all": "~4.1.5", 42 | "rimraf": "~3.0.2", 43 | "rollup": "~2.70.2", 44 | "@rollup/plugin-babel": "~5.3.1", 45 | "@rollup/plugin-commonjs": "~22.0.0", 46 | "@rollup/plugin-node-resolve": "~13.2.1", 47 | "rollup-plugin-terser": "~7.0.2" 48 | }, 49 | "dependencies": {}, 50 | "browserslist": "> 0.5%, last 2 versions, Firefox ESR, not dead", 51 | "engines": { 52 | "node": ">=16.0.0", 53 | "npm": ">=8.0.0" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | // import resolve from '@rollup/plugin-node-resolve'; 2 | // import commonjs from '@rollup/plugin-commonjs'; 3 | import babel from '@rollup/plugin-babel'; 4 | import { terser } from 'rollup-plugin-terser'; 5 | import pkg from './package.json'; 6 | 7 | const LIBRARY_NAME = 'Library'; // Change with your library's name 8 | const EXTERNAL = []; // Indicate which modules should be treated as external 9 | const GLOBALS = {}; // https://rollupjs.org/guide/en/#outputglobals 10 | 11 | const banner = `/*! 12 | * ${pkg.name} 13 | * ${pkg.description} 14 | * 15 | * @version v${pkg.version} 16 | * @author ${pkg.author} 17 | * @homepage ${pkg.homepage} 18 | * @repository ${pkg.repository.url} 19 | * @license ${pkg.license} 20 | */`; 21 | 22 | const makeConfig = (env = 'development') => { 23 | let bundleSuffix = ''; 24 | 25 | if (env === 'production') { 26 | bundleSuffix = 'min.'; 27 | } 28 | 29 | const config = { 30 | input: 'src/index.js', 31 | external: EXTERNAL, 32 | output: [ 33 | { 34 | banner, 35 | name: LIBRARY_NAME, 36 | file: `dist/${LIBRARY_NAME}.umd.${bundleSuffix}js`, // UMD 37 | format: 'umd', 38 | exports: 'auto', 39 | globals: GLOBALS 40 | }, 41 | { 42 | banner, 43 | file: `dist/${LIBRARY_NAME}.cjs.${bundleSuffix}js`, // CommonJS 44 | format: 'cjs', 45 | // We use `default` here as we are only exporting one thing using `export default`. 46 | // https://rollupjs.org/guide/en/#outputexports 47 | exports: 'default', 48 | globals: GLOBALS 49 | }, 50 | { 51 | banner, 52 | file: `dist/${LIBRARY_NAME}.esm.${bundleSuffix}js`, // ESM 53 | format: 'es', 54 | exports: 'auto', 55 | globals: GLOBALS 56 | } 57 | ], 58 | plugins: [ 59 | // Uncomment the following 2 lines if your library has external dependencies 60 | // resolve(), // teach Rollup how to find external modules 61 | // commonjs(), // so Rollup can convert external modules to an ES module 62 | babel({ 63 | babelHelpers: 'bundled', 64 | exclude: ['node_modules/**'] 65 | }) 66 | ] 67 | }; 68 | 69 | if (env === 'production') { 70 | config.plugins.push(terser({ 71 | output: { 72 | comments: /^!/ 73 | } 74 | })); 75 | } 76 | 77 | return config; 78 | }; 79 | 80 | export default commandLineArgs => { 81 | const configs = [ 82 | makeConfig() 83 | ]; 84 | 85 | // Production 86 | if (commandLineArgs.environment === 'BUILD:production') { 87 | configs.push(makeConfig('production')); 88 | } 89 | 90 | return configs; 91 | }; 92 | -------------------------------------------------------------------------------- /src/constants.js: -------------------------------------------------------------------------------- 1 | export const LIB_NAME = 'Library'; 2 | export const LIB_VERSION = '1.0.0'; 3 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { LIB_NAME, LIB_VERSION } from './constants'; 2 | 3 | class Library { 4 | constructor() { 5 | this._name = LIB_NAME; 6 | this.version = LIB_VERSION; 7 | } 8 | 9 | name() { 10 | return this._name; 11 | } 12 | } 13 | 14 | export default Library; 15 | -------------------------------------------------------------------------------- /test/index.spec.js: -------------------------------------------------------------------------------- 1 | import Library from '../src'; 2 | 3 | let library; 4 | 5 | // Replace with actual tests 6 | describe('Library.src.js', () => { 7 | beforeEach(() => library = new Library()); 8 | 9 | it('should get the library\'s version', () => { 10 | expect(library.version).toEqual('1.0.0'); 11 | }); 12 | 13 | it('should get the library\'s name', () => { 14 | const spy = jest.spyOn(library, 'name'); 15 | const name = library.name(); 16 | 17 | expect(name).toEqual('Library'); 18 | 19 | expect(spy).toHaveBeenCalled(); 20 | 21 | spy.mockRestore(); 22 | }); 23 | }); 24 | --------------------------------------------------------------------------------