package-proxy
Version:
Proxify your npm package for easier imports
170 lines (116 loc) • 3.96 kB
Markdown
<!-- @format -->
<h2 align="center">package-proxy</h2>
<div align="center">
[](https://www.npmjs.com/package/package-proxy)
[](https://circleci.com/gh/sl-julienamblard/package-proxy)
[](https://codecov.io/gh/sl-julienamblard/package-proxy)
[](https://www.npmjs.com/package/package-proxy)
[](https://www.npmjs.com/package/package-proxy)
[](https://github.com/sl-julienamblard/package-proxy/blob/packages/package-proxy/LICENSE)
</div>
1. [Install](
1. [The problem](
1. [The solution](
1. [How to use](
<a id="install"></a>
<h2 align="center">Install</h2>
_Install with npm:_
```bash
npm i package-proxy --save-dev
```
_Install with yarn:_
```bash
yarn add package-proxy --dev
```
<a id="problem"></a>
- <h4>Barrel file and Tree Shaking</h4>
In most package, all module are exports in one barrel file like this
```bash
import { foo, bar, baz } from "packageName"
```
**This can be a dangerous practice** because most of bundler (**including webpack**) will import every module in the bundled flie even if you only import one module.
This can lead to a huge increasing of bundle size.
[=> Read more about barrel file and tree shaking](https://lecstor.com/code-tree-webpack/)
- <h4>Awefull import path</h4>
On another side, if you need to expose a lot of module and want to get rid of barrel file,
you will need to let the user do import like this:
```bash
import foo from "packageName/lib/foo"
import bar from "packageName/lib/path/to/file/bar"
```
<a id="solution"></a>
<h3>The solution</h3>
This package help you to create proxy to transform import like this:
```bash
import foo from "packageName/lib/foo"
import bar from "packageName/lib/path/to/file/bar"
```
into this:
```bash
import foo from "packageName/foo"
import bar from "packageName/bar"
```
with one config file.
More precisely, it will create a `package.json` file like this:
```json
{
"name": "packageName/foo",
"private": true,
"main": "../lib/path/to/module/foo/index.js",
"module": "../esm/path/to/module/foo/index.js",
"types": "../lib/path/to/module/foo/index.d.ts"
}
```
<a id="usage"></a>
<h3>How to use</h3>
<a id="config"></a>
In your root folder, create a `pproxy.json` file
```json
{
"packageName": "yourPackageName",
"proxify": [
{
"src": "folderToProxify"
}
]
}
```
//on continue demain j'ne ai ma claque
<a id="CLI"></a>
| command | description |
| --------- | ---------------------- |
| **write** | create the proxy files |
> This is an optional command. Avoiding it will also trigger the **write** command
```bash
proxify write
```
| command | description |
| --------- | ---------------------- |
| **clean** | clean the proxy files. |
```bash
proxify clean
```
| option | description |
| ------------------------- | --------------------------- |
| **-c, --config \<path\>** | custom the config file path |
```bash
proxify create --config path/to/config.json
```
| option | description |
| -------------- | ----------- |
| **-h, --help** | Show help |
```bash
proxify --help
```
| option | description |
| ----------------- | -------------------- |
| **-v, --version** | Show package version |
```bash
proxify --version
```