Javascript implementation is available in a following repo
Boilerplate to create an npm module development environment with example code (custom hook that track window dimensions). Typescript compiler will create export type definitions together with the javascript file. The package can be used with both Typescript and Javascript projects.
Our setup will consist of two separate projects:
- NPM Module Project
- React Host Project (to debug new features)
NPM module will be connected to the React Host Project using npm link
. Project separation will enable us to keep our module logic encapsulated and more maintainable.
Build module project to src/index.js
-> index.js
.
npm run build
Run development will compile a file every time that any changes in /src
folder will occur.
npm run develop
Deploy your project to NPM. The script will trigger npm publish
.
npm run deploy
-
Clone template project.
git clone https://github.com/Halo-Lab/react-hook-module-template-typescript
-
Pick a module name and change folder name in
package.json
accordingly. Make sure you are using a unique one so you can publish your package to npm. Provide additional information about your module by changing description, author, license and keywords. Describe basic functionality of your module inREADME.md
file.project folder
/my-new-module
package.json
"name": "my-new-module", "description": "NPM module template description", "author": "John Smith", "license": "ISC", "keywords": ["react", "hooks", "screen size"]
-
Install dependencies
npm i
-
An initial build command will compile all files from
/src
folder to a singleindex.js
file.npm run build
-
Our module is ready for development. Now we are ready to connect our module to React Host Project. Create a new React Project in the different folder and change
App.js
file with example code. Main concept is to keep two projects in separate folders. So after the development process Module Project will include module logic exclusively.Be sure that you're not creating new project inside Module folder.
Navigate to your main Project Folder and create a new React Project.
cd .. npx create-react-app host_react_app
App.js
import React from 'react'; import useResizing from 'my-new-module'; function App() { const screenSize = useResizing(); return ( <div className="App"> {`${screenSize.width}px ${screenSize.height}px`} </div> ); } export default App;
-
To connect our module open Module Project folder and create a symlink. Npm link will add our package to the global
/lib/node_modules/my-new-module
folder and create a link from it. You can find more information about npm link functionality in the official documentation.cd my-new-module npm link
-
Now open React Host Project and add your locally developed module. That will create a symbolic link from globally-installed
my-new-module
to currentnode_modules/
foldercd host_react_app npm link my-new-module
-
Start React host and Module projects. Any changes inside module's
/src
folder will trigger both projects to rebuild.React host project
cd host_react_app npm run start
Module project
cd my-new-module npm run develop
-
Change remote's Git URL to your own repository.
git remote set-url origin https://github.com/USERNAME/my-new-module.git
-
Develop your custom module logic inside
/src
folder.
Have fun! ✌️