Skip to content

Commit 14e8d44

Browse files
committed
Documentation "How to consume the npm packages?"
1 parent a62089c commit 14e8d44

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,81 @@ You can use HTML5 attributes like `type="email"`, `required`, [`minlength`](http
322322

323323
In the last case you will have to manage translations yourself (see SignUp example).
324324

325+
## How to consume the npm packages?
326+
327+
### ESNext (currently ES2018) + ES modules
328+
329+
Files inside [`lib/`](https://unpkg.com/react-form-with-constraints@latest/lib/) (package.json `"module": "lib/index.js"`).
330+
331+
A recent browser or Node.js is required or you will need to transpile the react-form-with-constraints source code using Babel (or TypeScript tsc).
332+
333+
Several advantages:
334+
- The combine use of `"sideEffects": false` with `"module": ...` generates a smaller bundle thanks to [tree shaking](https://webpack.js.org/guides/tree-shaking/)
335+
- You can transpile react-form-with-constraints source code with your Babel's [preset-env](https://babeljs.io/docs/en/babel-preset-env) and [Browserslist](https://github.com/browserslist/browserslist) configuration
336+
337+
For this to work, do not exclude `node_modules` from your webpack configuration, example:
338+
339+
```JS
340+
// webpack.config.js
341+
module: {
342+
rules: [
343+
{
344+
test: /\.(js|jsx?)$/,
345+
346+
// See [Enable babel-preset-env for node_modules that target newer Node versions](https://github.com/facebook/create-react-app/issues/1125)
347+
// See [Create React App 2.0: "You can now use packages written for latest Node versions without breaking the build"](https://reactjs.org/blog/2018/10/01/create-react-app-v2.html)
348+
// See ["If you have to exclude node_modules/, how do you get babel to polyfill/transform code in 3rd party code?"](https://github.com/webpack/webpack/issues/6544#issuecomment-417108242)
349+
// See [Compile dependencies with babel-preset-env](https://github.com/facebook/create-react-app/pull/3776)
350+
//exclude: /node_modules/,
351+
exclude: /\/core-js/,
352+
353+
loader: 'babel-loader',
354+
options: {
355+
// See https://github.com/facebook/create-react-app/blob/v2.1.0/packages/react-scripts/config/webpack.config.dev.js#L284
356+
compact: false
357+
}
358+
}
359+
]
360+
}
361+
```
362+
363+
You probably want to configure Babel with `sourceType: 'unambiguous'`:
364+
365+
```JS
366+
// babel.config.js
367+
module.exports = {
368+
// See https://github.com/facebook/create-react-app/blob/v2.1.0/packages/babel-preset-react-app/dependencies.js#L64
369+
// See [Add Babel config sourceType: 'unambiguous' for dependencies](https://github.com/facebook/create-react-app/pull/5052)
370+
sourceType: 'unambiguous',
371+
372+
presets: [
373+
[
374+
'@babel/preset-env',
375+
{
376+
useBuiltIns: 'usage'
377+
}
378+
],
379+
'@babel/preset-react'
380+
],
381+
plugins: [
382+
'@babel/plugin-proposal-class-properties',
383+
'@babel/plugin-proposal-object-rest-spread'
384+
]
385+
};
386+
```
387+
388+
### ES5 + CommonJS
389+
390+
Classic ES5 transpilation, files inside [`lib-es5/`](https://unpkg.com/react-form-with-constraints@latest/lib-es5/) (package.json `"main": "lib-es5/index.js"`).
391+
No tree shaking.
392+
393+
### UMD (Universal Module Definition) + ES5
394+
395+
Files inside [`dist/`](https://unpkg.com/react-form-with-constraints@latest/dist/).
396+
Typical use is with `<script src="react-form-with-constraints.production.min.js">` inside your index.html.
397+
398+
A good use case is [CodePen](https://codepen.io/tkrotoff/pen/BRGdqL), files are generated by [Rollup](packages/react-form-with-constraints/rollup.config.js).
399+
325400
## Notes
326401

327402
- A [`type="hidden"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden#Validation), [`readonly`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-readonly) or [`disabled`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-disabled) input won't trigger any HTML5 form constraint validation like [`required`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-required), see https://codepen.io/tkrotoff/pen/gdjVNv

0 commit comments

Comments
 (0)