lm-web-controls 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +1 -0
- package/dist/index.js +1 -0
- package/leadmetrics-lm-web-controls-1.0.0.tgz +0 -0
- package/lm-web-controls-1.0.0.tgz +0 -0
- package/notes.txt +5 -0
- package/package.json +47 -0
- package/readme.md +221 -0
- package/rollup.config.mjs +34 -0
- package/src/blocks/email/index.tsx +10 -0
- package/src/index.ts +1 -0
- package/src/types/leadmetrics-lm-web-controls.d.ts +1 -0
- package/tsconfig.json +21 -0
package/dist/index.es.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t from"react";var e=function(){return t.createElement("a",{href:"mailto:test@test.com",target:"_blank"},"test@test.com")};export{e as Email};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("react"));exports.Email=function(){return t.default.createElement("a",{href:"mailto:test@test.com",target:"_blank"},"test@test.com")};
|
|
Binary file
|
|
Binary file
|
package/notes.txt
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lm-web-controls",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "leadmetrics-lm-web-controls",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.es.js",
|
|
7
|
+
"types": "types/leadmetrics-lm-web-controls.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
10
|
+
"build": "npx rollup -c"
|
|
11
|
+
},
|
|
12
|
+
"author": "Leadmetrics",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"react": "18.2.0",
|
|
16
|
+
"react-dom": "18.2.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@babel/cli": "7.21.0",
|
|
20
|
+
"@babel/core": "7.21.4",
|
|
21
|
+
"@babel/preset-env": "7.21.4",
|
|
22
|
+
"@babel/preset-react": "7.18.6",
|
|
23
|
+
"@babel/preset-typescript": "7.21.4",
|
|
24
|
+
"@rollup/plugin-alias": "5.0.0",
|
|
25
|
+
"@types/react": "18.0.34",
|
|
26
|
+
"babel-loader": "9.1.2",
|
|
27
|
+
"react": "18.2.0",
|
|
28
|
+
"react-dom": "18.2.0",
|
|
29
|
+
"typescript": "5.0.4"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@rollup/plugin-node-resolve": "15.1.0",
|
|
33
|
+
"@syncfusion/ej2-react-buttons": "21.1.37",
|
|
34
|
+
"@syncfusion/ej2-react-calendars": "21.1.41",
|
|
35
|
+
"@syncfusion/ej2-react-dropdowns": "21.1.41",
|
|
36
|
+
"@syncfusion/ej2-react-grids": "21.1.41",
|
|
37
|
+
"@syncfusion/ej2-react-inputs": "22.1.34",
|
|
38
|
+
"acorn": "8.9.0",
|
|
39
|
+
"acorn-walk": "8.2.0",
|
|
40
|
+
"commander": "10.0.1",
|
|
41
|
+
"rollup-plugin-babel": "4.4.0",
|
|
42
|
+
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
43
|
+
"rollup-plugin-terser": "7.0.2",
|
|
44
|
+
"rollup-plugin-typescript2": "0.34.1",
|
|
45
|
+
"ts-loader": "9.4.3"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# How to create a Custom NPM Module with React Components?
|
|
2
|
+
|
|
3
|
+
This repo is aimed to describe the process of creating a custom NPM package/module with React.JS components. It will guide you through the steps of setting up your development environment, writing and exporting your components, and publishing your package to the NPM registry[Optional]. Additionally, it will provide tips and best practices for maintaining and updating your package.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
**Step 1:** Make a directory and initialize npm.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
mkdir custom-npm-package
|
|
11
|
+
npm init
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Alternatively, you can use the following command to quickly generate a minimal boilerplate:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm init -y
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Add the necessary fields. After the successfull walkthrough a package.json will created and looks like below.
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
{
|
|
24
|
+
"name": "gittuts",
|
|
25
|
+
"version": "1.0.0",
|
|
26
|
+
"description": "",
|
|
27
|
+
"main": "index.js",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
30
|
+
},
|
|
31
|
+
"author": "",
|
|
32
|
+
"license": "ISC"
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Step 2:** Create a src folder in the root add a index.js/index.ts in it also create a component folder and creates some components in it.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
src
|
|
40
|
+
├── components
|
|
41
|
+
│ ├── Component1
|
|
42
|
+
│ │ └── index.tsx
|
|
43
|
+
│ ├── Component2
|
|
44
|
+
│ │ └── index.tsx
|
|
45
|
+
│ └── Component3
|
|
46
|
+
│ └── index.tsx
|
|
47
|
+
└── index.ts
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Step 3:** Now, Add react and react-dom as peer-dependencies and dev-dependencies.
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
yarn add react react-dom --dev
|
|
54
|
+
|
|
55
|
+
or
|
|
56
|
+
|
|
57
|
+
npm install react react-dom --dev
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The final package.json may looks like below.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
{
|
|
64
|
+
"name": "custom-npm-package",
|
|
65
|
+
"version": "1.0.0",
|
|
66
|
+
"description": "react custom npm module",
|
|
67
|
+
"main": "index.js",
|
|
68
|
+
"scripts": {
|
|
69
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
70
|
+
"build": "npx babel src --out-file index.js --extensions .ts,.tsx"
|
|
71
|
+
},
|
|
72
|
+
"author": "Anandhu",
|
|
73
|
+
"license": "ISC",
|
|
74
|
+
"peerDependencies": {
|
|
75
|
+
"react": "^18.2.0",
|
|
76
|
+
"react-dom": "^18.2.0"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@types/react": "^18.0.34",
|
|
80
|
+
"react": "^18.2.0",
|
|
81
|
+
"react-dom": "^18.2.0",
|
|
82
|
+
"typescript": "^5.0.4"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Since it is Typescript. I've also installed typescript, typescript react with it.
|
|
88
|
+
|
|
89
|
+
**Step 4:** Its time to bundle the code. Install [rollup.js](https://rollupjs.org/)
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
npm install --save react rollup
|
|
93
|
+
```
|
|
94
|
+
You will also need to install the Rollup plugins for Babel, CommonJS, and Node Resolve by running
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
npm install --save-dev @rollup/plugin-babel @rollup/plugin-commonjs @rollup/plugin-node-resolve rollup-plugin-terser
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Step 5:** Configure Rollup
|
|
102
|
+
|
|
103
|
+
Create a `rollup.config.mjs` or `rollup.config.js` file in the root of your project. In this file, configure Rollup to bundle your React component by specifying the input file, output format, and plugins to use. Here is an example configuration:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
import babel from 'rollup-plugin-babel';
|
|
107
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
108
|
+
import external from 'rollup-plugin-peer-deps-external';
|
|
109
|
+
import terser from '@rollup/plugin-terser';
|
|
110
|
+
import typescript from "rollup-plugin-typescript2"; // For Typescript
|
|
111
|
+
|
|
112
|
+
export default [
|
|
113
|
+
{
|
|
114
|
+
input: './src/index.ts',
|
|
115
|
+
output: [
|
|
116
|
+
{
|
|
117
|
+
file: 'dist/index.js',
|
|
118
|
+
format: 'cjs',
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
file: 'dist/index.es.js',
|
|
122
|
+
format: 'es',
|
|
123
|
+
exports: 'named',
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
plugins: [
|
|
127
|
+
babel({
|
|
128
|
+
exclude: 'node_modules/**',
|
|
129
|
+
presets: ['@babel/preset-react']
|
|
130
|
+
}),
|
|
131
|
+
external({
|
|
132
|
+
includeDependencies: true
|
|
133
|
+
}),
|
|
134
|
+
resolve(),
|
|
135
|
+
terser(),
|
|
136
|
+
typescript({ useTsconfigDeclarationDir: true }),
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
To our package.json, let's add this as build command so that we won't have to type it repeatedly. Under "scripts" in package.json add the following.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
"scripts": {
|
|
147
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
148
|
+
"build": "npx rollup -c"
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
now we can simply use ```npm run build``` or ```yarn build``` to bundle the code.
|
|
153
|
+
|
|
154
|
+
If it compiled without any error you may see a new folder `dist` generated in the root folder with transpiled Javascript Code.
|
|
155
|
+
|
|
156
|
+

|
|
157
|
+
|
|
158
|
+
_image: bundled output_
|
|
159
|
+
|
|
160
|
+
Note: that if you're using a build tool like Webpack, you may not need to use npx babel at all. Instead, you can use a TypeScript loader like ts-loader or awesome-typescript-loader that will transpile and transform your TypeScript files as part of the build process.
|
|
161
|
+
|
|
162
|
+
**Step 6:** Lets pack it so we can use/test it in other projects. Run the following command from the root folder.
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
npm pack
|
|
166
|
+
```
|
|
167
|
+
This will generate a .tgz file that we can use to install the package locally in other projects.
|
|
168
|
+
|
|
169
|
+
Alternatively, you can link the package for testing purpose(Recommended).
|
|
170
|
+
|
|
171
|
+
Using `npm link` in your package's root directory, create a global symlink of your package. A shortcut that directs your system to another directory or file is known as a "symlink," short for symbolic link.
|
|
172
|
+
|
|
173
|
+
Now, create an another application and tell the application to use the global symlink with `npm link your-package-name`. This way, we could save a lot of time.
|
|
174
|
+
|
|
175
|
+
**Step 7:** Navigate to your other project's directory and install your package using npm install <path to tarball file>. For example:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
npm i 'path_to_the_packed_file'
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**Step 8:** In your host project's code you can import and use your component as follows:
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
import logo from "./logo.svg";
|
|
185
|
+
import "./App.css";
|
|
186
|
+
import TextInput from "my_npm_package"; // Importing from the package
|
|
187
|
+
import { useState } from "react";
|
|
188
|
+
|
|
189
|
+
function App() {
|
|
190
|
+
const [value, setValue] = useState('');
|
|
191
|
+
console.log(value)
|
|
192
|
+
|
|
193
|
+
function handleChange(e) {
|
|
194
|
+
setValue(e.target.value);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return (
|
|
198
|
+
<div className="App">
|
|
199
|
+
<header className="App-header">
|
|
200
|
+
<img src={logo} className="App-logo" alt="logo" />
|
|
201
|
+
<p>Custom npm module.</p>
|
|
202
|
+
<TextInput label="Name" value={value} onChange={handleChange} />
|
|
203
|
+
</header>
|
|
204
|
+
</div>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export default App;
|
|
209
|
+
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Additionally we can use packages like [storybook.js](https://storybook.js.org/) to build UI components and pages in isolation and by doing so we could see the changes that happens to our components without the need of installing it in a host application. You could find the documention for implementing story book [here](https://storybook.js.org/docs/react/get-started/install/).
|
|
213
|
+
|
|
214
|
+
**Step 9:** Publishing our package to NPM Registry
|
|
215
|
+
|
|
216
|
+
To publish a package to the npm registry, we need to follow these steps:
|
|
217
|
+
|
|
218
|
+
- Create an npm account on [npmjs.com](https://www.npmjs.com/signup).
|
|
219
|
+
- Sign in to npm from your terminal using the ```npm login``` command.
|
|
220
|
+
- Prepare your package for publishing.
|
|
221
|
+
- Publish the package from the terminal using the ```npm publish``` command.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import babel from 'rollup-plugin-babel';
|
|
2
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
3
|
+
import external from 'rollup-plugin-peer-deps-external';
|
|
4
|
+
import { terser } from 'rollup-plugin-terser';
|
|
5
|
+
import typescript from "rollup-plugin-typescript2";
|
|
6
|
+
|
|
7
|
+
export default [
|
|
8
|
+
{
|
|
9
|
+
input: './src/index.ts',
|
|
10
|
+
output: [
|
|
11
|
+
{
|
|
12
|
+
file: 'dist/index.js',
|
|
13
|
+
format: 'cjs',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
file: 'dist/index.es.js',
|
|
17
|
+
format: 'es',
|
|
18
|
+
exports: 'named',
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
plugins: [
|
|
22
|
+
babel({
|
|
23
|
+
exclude: 'node_modules/**',
|
|
24
|
+
presets: ['@babel/preset-react']
|
|
25
|
+
}),
|
|
26
|
+
external({
|
|
27
|
+
includeDependencies: true
|
|
28
|
+
}),
|
|
29
|
+
resolve(),
|
|
30
|
+
terser(),
|
|
31
|
+
typescript({ useTsconfigDeclarationDir: true }),
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
];
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './blocks/email'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import React, { Component, ChangeEvent } from "react";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"declaration": true,
|
|
4
|
+
"declarationDir": "build",
|
|
5
|
+
"module": "esnext",
|
|
6
|
+
"target": "es5",
|
|
7
|
+
"lib": ["es6", "dom", "es2016", "es2017"],
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"jsx": "react",
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"esModuleInterop": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*"],
|
|
15
|
+
"exclude": [
|
|
16
|
+
"node_modules",
|
|
17
|
+
"build",
|
|
18
|
+
"src/**/*.stories.tsx",
|
|
19
|
+
"src/**/*.test.tsx"
|
|
20
|
+
]
|
|
21
|
+
}
|