├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.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 (http://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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 osdevisnot 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 | # react-app-rewire-create-react-library 2 | Use create-react-app to build react libraries. 3 | 4 | This gives you ability to reuse most of CRA setup for building your libraries. 5 | 6 | ## Installation 7 | ``` 8 | yarn add --dev react-app-rewire-create-react-library 9 | 10 | # or use npm if you don't have yarn yet 11 | 12 | npm install --save-dev react-app-rewire-create-react-library 13 | ``` 14 | 15 | ## Usage 16 | In the `config-overrides.js` you created for [react-app-rewired](https://github.com/timarney/react-app-rewired) add this code: 17 | 18 | ``` 19 | const rewireCreateReactLibrary = require('react-app-rewire-create-react-library'); 20 | 21 | module.exports = function override(config, env) { 22 | config = rewireCreateReactLibrary(config, env); 23 | return config; 24 | }; 25 | ``` 26 | 27 | In `package.json`, add a separate npm script to build library 28 | 29 | ``` 30 | { 31 | "scripts": { 32 | ... 33 | "build-library": "react-app-rewired build --library", 34 | ... 35 | } 36 | } 37 | ``` 38 | 39 | And you can now use CRA to build your library 💪 40 | 41 | ## Conventions 42 | 43 | The library name will be read from `name` property in your `package.json`. For scoped NPM packages, the scope name will be omited from library name. 44 | 45 | The webpack entry file is read from `module` property in `package.json` and output file & path are read from `main` property in `package.json`. Why? [read here](https://github.com/dherman/defense-of-dot-js/blob/master/proposal.md#typical-usage) 46 | 47 | All the dependencies in your `package.json` will ba added to as commonjs externals to webpack config. 48 | 49 | This package will be activated only when a `--library` flag is detected in npm script. This eliminated the need to maintain separate `config-overrides.js` for app and library builds. 50 | 51 | ## Caveats 52 | 53 | Setting `main` file to be outside of `build` folder would cause CRA to throw error when reporting file sizes. To avoid this, always set `"main"` to be inside `build` folder. For example: 54 | ``` 55 | # GOOD: uses build directory for output 56 | "main": "build/my-library.js", 57 | 58 | # BAD: uses dist directory for output 59 | "main": "dist/my-library.js", 60 | ``` 61 | 62 | `public` folder always gets copied over to build even for library builds. This is currently handled and defaulted inside CRA, there is no way to override this at the moment. However, you can use `.npmignore` to avoid publishing these files to NPM: 63 | ``` 64 | # .npmignore 65 | 66 | ** 67 | !build/*.js 68 | !build/*.map 69 | ``` 70 | 71 | ## License 72 | Licensed under the MIT License, Copyright @ 2017 osdevisnot. See LICENSE.md for more information. 73 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | const rewireCreateReactLibrary = (config, env) => { 4 | if (process.env.npm_lifecycle_script.includes('library')) { 5 | /** 6 | * Determine Library Name based on package name 7 | * basename will omit scope name from npm scoped packages 8 | */ 9 | const libraryName = path.basename(process.env.npm_package_name); 10 | /** 11 | * Read the entry and output filename from package.json's module and main properties 12 | * Why? Read here: https://github.com/dherman/defense-of-dot-js/blob/master/proposal.md#typical-usage 13 | */ 14 | const entryFile = process.env.npm_package_module; 15 | const outFile = path.basename(process.env.npm_package_main); 16 | const outDir = process.env.npm_package_main.replace(outFile, ''); 17 | /** 18 | * add library configurations to webpack config 19 | */ 20 | config.output.library = libraryName; 21 | config.output.libraryTarget = 'commonjs'; 22 | /** 23 | * Change the webpack entry and output path 24 | */ 25 | config.entry = { [libraryName]: path.resolve(entryFile) }; 26 | config.output.filename = outFile; 27 | config.output.path = path.resolve(outDir); 28 | /** 29 | * Add all package dependencies as externals as commonjs externals 30 | */ 31 | let externals = {}; 32 | Object.keys(process.env).forEach(key => { 33 | if (key.includes('npm_package_dependencies_')) { 34 | let pkgName = key.replace('npm_package_dependencies_', ''); 35 | pkgName = pkgName.replace(/_/g, '-'); 36 | // below if condition addresses scoped packages : eg: @storybook/react 37 | if (pkgName.startsWith('-')) { 38 | const scopeName = pkgName.substr(1, pkgName.indexOf('-', 1) - 1); 39 | const remainingPackageName = pkgName.substr(pkgName.indexOf('-', 1) + 1, pkgName.length); 40 | pkgName = `@${scopeName}/${remainingPackageName}`; 41 | } 42 | externals[pkgName] = `commonjs ${pkgName}`; 43 | } 44 | }); 45 | config.externals = externals; 46 | /** 47 | * Clear all plugins from CRA webpack config 48 | */ 49 | config.plugins = []; 50 | } 51 | return config; 52 | }; 53 | 54 | module.exports = rewireCreateReactLibrary; 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-app-rewire-create-react-library", 3 | "version": "0.1.1", 4 | "description": "Use create-react-app to build react libraries", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/osdevisnot/react-app-rewire-create-react-library.git" 12 | }, 13 | "keywords": [], 14 | "author": "osdevisnot ", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/osdevisnot/react-app-rewire-create-react-library/issues" 18 | }, 19 | "homepage": "https://github.com/osdevisnot/react-app-rewire-create-react-library#readme" 20 | } 21 | --------------------------------------------------------------------------------