# @cscart/tools

> build services and tools

Latest version **0.6.1** (published 2024-04-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @cscart/tools
pnpm add @cscart/tools
yarn add @cscart/tools
bun add @cscart/tools
```

Provides the commands `install-esm`, `build-modules`.

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.1 |
| Published | 2024-04-15 |
| First published | 2023-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 32 |
| Unpacked size | 37.3 KB |
| Known vulnerabilities | 0 (+12 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | m4n1ac, saunterer |

## Links

- npm: https://www.npmjs.com/package/@cscart/tools
- npm.io page: https://npm.io/package/@cscart/tools

## Dependencies (32)

- [tar](https://npm.io/package/tar.md) ^6.2.0
- [rollup](https://npm.io/package/rollup.md) ^4.9.1
- [cssnano](https://npm.io/package/cssnano.md) ^6.1.0
- [@babel/core](https://npm.io/package/@babel/core.md) ^7.23.6
- [autoprefixer](https://npm.io/package/autoprefixer.md) ^10.4.18
- [postcss-nested](https://npm.io/package/postcss-nested.md) ^6.0.1
- [@babel/template](https://npm.io/package/@babel/template.md) ^7.22.15
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.23.6
- [@babel/preset-env](https://npm.io/package/@babel/preset-env.md) ^7.23.6
- [rollup-plugin-dts](https://npm.io/package/rollup-plugin-dts.md) ^6.1.0
- [rollup-plugin-copy](https://npm.io/package/rollup-plugin-copy.md) ^3.5.0
- [@babel/preset-react](https://npm.io/package/@babel/preset-react.md) ^7.23.3
- [@rollup/plugin-html](https://npm.io/package/@rollup/plugin-html.md) ^1.0.3
- [babel-preset-minify](https://npm.io/package/babel-preset-minify.md) ^0.5.2
- [rollup-plugin-serve](https://npm.io/package/rollup-plugin-serve.md) ^2.0.2
- [@rollup/plugin-babel](https://npm.io/package/@rollup/plugin-babel.md) ^6.0.4
- [@vitejs/plugin-react](https://npm.io/package/@vitejs/plugin-react.md) ^4.2.1
- [rollup-plugin-dotenv](https://npm.io/package/rollup-plugin-dotenv.md) ^0.5.0
- [@rollup/plugin-terser](https://npm.io/package/@rollup/plugin-terser.md) ^0.4.4
- [rollup-plugin-postcss](https://npm.io/package/rollup-plugin-postcss.md) ^4.0.2
- [@rollup/plugin-commonjs](https://npm.io/package/@rollup/plugin-commonjs.md) ^25.0.7
- [@web/rollup-plugin-copy](https://npm.io/package/@web/rollup-plugin-copy.md) ^0.5.1
- [@babel/preset-typescript](https://npm.io/package/@babel/preset-typescript.md) ^7.23.3
- [rollup-plugin-livereload](https://npm.io/package/rollup-plugin-livereload.md) ^2.0.5
- [rollup-plugin-visualizer](https://npm.io/package/rollup-plugin-visualizer.md) ^5.9.2
- [@rollup/plugin-typescript](https://npm.io/package/@rollup/plugin-typescript.md) ^11.1.5
- [rollup-plugin-shebang-bin](https://npm.io/package/rollup-plugin-shebang-bin.md) ^0.0.6
- [@rollup/plugin-node-resolve](https://npm.io/package/@rollup/plugin-node-resolve.md) ^15.2.3
- [@babel/plugin-proposal-decorators](https://npm.io/package/@babel/plugin-proposal-decorators.md) ^7.23.7
- [@babel/plugin-proposal-class-properties](https://npm.io/package/@babel/plugin-proposal-class-properties.md) ^7.18.6
- [babel-plugin-transform-typescript-metadata](https://npm.io/package/babel-plugin-transform-typescript-metadata.md) ^0.3.2
- [babel-plugin-tsconfig-paths-module-resolver](https://npm.io/package/babel-plugin-tsconfig-paths-module-resolver.md) ^1.0.4

## Recent versions

- 0.6.1 (latest) — 2024-04-15
- 0.6.1-canary (canary) — 2024-04-15
- 0.5.14 — 2024-03-07
- 0.5.14-canary.3 — 2024-03-07
- 0.5.14-canary.2 — 2024-03-07
- 0.5.13-canary.1 — 2024-03-04
- 0.5.13 — 2024-02-28
- 0.5.12 — 2024-01-23
- 0.5.11 — 2024-01-22
- 0.5.10 — 2024-01-22
- 0.5.9 — 2024-01-18
- 0.5.8 — 2023-12-28
- 0.5.7 — 2023-12-28
- 0.5.6 — 2023-12-28
- 0.5.5 — 2023-12-27
- … 8 more at https://npm.io/package/@cscart/tools/versions

## README

# Build tools for cs-cart enterprise extensions

`@cscart/tools` is a bunch of tools and presets for develop libraries, add-ons and packages for `@cscart`

## Install

1. `yarn add -D @cscart/tools`
2. create `cscart.config.js` at the root of your project
3. Install required dependencies (like `@cscart/tools-gql`)
4. `yarn build-modules` or `yarn build-modules --watch`

## Production mode

Add `NODE_ENV=production` before `yarn build-modules`

`package.json`
```
...
"scripts": {
  "build": "NODE_ENV=production yarn build-modules"
},
```

## Create library with @cscart/tools

### Purposes and positioning

Library is a bunch of reusable components and functions unioned by a single purpose or role. Such as UI library or form table. 

With this tool you can create a preconfigured library available to publish to NPM. In addition you may use this configuration with your add-on for @cscart and share you code via npm package. 

### How to share and consume library code

edit `cscart.config.js`:

```JS
import { libraryPlugin } from '@cscart/tools/library-plugin'

const cscartConfig = {
  rollup: () => ({
    destinationPath: 'dist/',
    ...
    entries: [
      libraryPlugin,
    ],
  }),
}
```

`libraryPlugin` is a configuration for handle your root src scripts as library entry points, so it looks like:

```JS
const libraryPlugin = {
  input: ['src/*.ts'],
  ...
}
```

### Why

It may be inconvenient to place library entrypoints at top level of src directory but this is a side effect of esm imports. 
For example `@cscart/ui` emits multiple separate entries like Button, Checkbox and etc available for imports like 
```TS
import Button from '@cscart/ui/Button' 
```

without path aliases or import rewrites like `package.json` exports which works only in `nodejs` environment. 

Sharing code for reusing by other developers is a good practice. It allows to reduce develop time and bundle size, so you should keep your code accessible.

## Create your library

1. Create properly defined `package.json`

```JSON
 {
  "name": "@vendor-name/package-name",
  "version": "0.0.1",
  ...
  "type": "module",
  "scripts" : {
    "build": "NODE_ENV=production yarn build-modules"
  },
  "devDependencies": {
    ...
  },
}
```

// TODO: add auto file generating with init binary

2. create `cscart.config.js`

```JS
import { libraryPlugin } from '@cscart/tools/library-plugin'

const cscartConfig = {
  rollup: () => ({
    destinationPath: 'dist/',
    externals: [ 'react' ],
    entries: [
      libraryPlugin,
    ],
  }),
}

export default cscartConfig
```

3. create `src` folder
4. Configure `typescript`
5. Create some sharable components or utils at `src` like `src/sanitizeHtml.ts`
6. `yarn build` 
7. cd to `dist` folder: `cd ./dist`
8. `npm login`
9. `npm publish`

## Install-esm

`install-esm` is a command to install directly npm package without it`s dependencies. It very handy for packages and libraries with external or bundled dependencies. And only acceptable for using in projects not libraries. 

For example `yarn install-esm @cscart/ui ./public` brings a fully functioning package as ESM library available stright in a browser runtime. In case when `./public` is a folder with static files emitted along with a bundle. 

### How to consume such libraries

Variant 1: with projectPlugin

1. Edit `cscart.config.js`:

```JS
import { projectPlugin } from '@cscart/tools/project-plugin'

export default {
  rollup: ({isProduction}) => ({
    externals: [
      '@cscart/ui',
    ],
    destinationPath: 'dist/',
    entries: [
      ...
      // 
      projectPlugin({
        htmlPath: 'src/index.html'
      }),
    ],
  }),
}
```

## Manage extensions priorities

create `arrange.js` next to `cscart.config.js`: 

```JS
export default [
  {
    shell: /^@cscart\/backoffice\/features\/product_features\//,
    after: /^@cscart\/backoffice\/features\/product\//,
  },
]
```

Where `shell` describes items top be moved by rules `before` and `after`. 
If there are many items found for `shell` and `before` then a chunk of `shell`
will be placed before 

### Rule before 

Initial importmap: 

```
{
  "b": "b",
  "b": "b",
  "a": "a",
  "b": "b",
  "a": "a",
}
```

Rule: 

```
{
  shell: a,
  before: b,
},
```


Result: 

```
{
  "a": "a",
  "a": "a",
  "b": "b",
  "b": "b",
  "b": "b",
}
```

So items will be arranged so that the last item of group a will be before the first item of group b

### Rule after 

Initial importmap: 

```
{
  "b": "b",
  "b": "b",
  "b": "b",
  "a": "a",
  "a": "a",
}
```

Rule: 

```
{
  shell: a,
  after: b,
},
```


Result: 

```
{
  "a": "a",
  "a": "a",
  "b": "b",
  "b": "b",
  "b": "b",
}
```

So items will be arranged so that the first item of group a will be after the last item of group b

Note that order of moved groups may not be preserved. Specify rule if items are meant to be ordered;

Now all imports from `@cscart/ui` package in your project will treated as external and will not be included to your bundle. Moreover all imports like `import Button from '@cscart/ui/Button'` will generate corresponding chunk for [importmap](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap). 
This allows you to write esm imports without transpilation and share heavy dependencies like `@cscart/ui` and `@cscart/core` as native prebuilt ES modules. 

All generated importmaps can be collected and inserted to available `index.html` due to projectPlugin build process.

### FAQ

Q: I haave a heavy third-party library such as @mui, how can I share it for reusing it by other add-ons and libraries? 

A: The best way is not to share such dependencies. You never know who and how will use this. But it heavily loads your filesystem and result project size. Instead of this you should consume functions and components of the library and rely on tree-shaking due to build process. Many of ESM libraries not to meant to use as native ESM at browser runtime. There is necessity to bundle this libraries. So other developers should too. 
Example: 
Your project: 
```TS
import Button from '@mui/Button'
```

Some third-party add-on for your project: 

```TS
import Checkbox from '@mui/Checkbox'
```

Conslusion: consume only necessary components. Create reusable code and share it with libraryPlugin.

---
_Source: https://npm.io/package/@cscart/tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
