Skip to content

⚙️ How Bundlers Optimize @avensio/jsonld-schema

@avensio/jsonld-schema is a modular ESM library designed for tree-shaking and lazy loading.

🧩 Library Structure

text
dist/
├─ classes/
│  ├─ Thing.d.ts
│  ├─ WebPage.d.ts
│  └─ ...
└─ validation/
   ├─ classes/
   │  ├─ Order.validator.js
   │  ├─ WebPage.validator.js
   │  └─ ...
   ├─ properties/
   │  ├─ name.validator.js
   │  ├─ url.validator.js
   │  └─ ...
   └─ core.js

Each validator is emitted as a standalone ES module, while classes are exposed as granular TypeScript declarations. There is no internal bundling, ensuring:

  • ✅ Clean modular imports
  • ✅ Full tree-shaking support
  • Lazy loading via dynamic import()
  • ✅ Automatic deduplication by bundlers

⚙️ Tree-Shaking in Consumer Projects

When a validator is imported:

ts
import { validateOrder } from '@avensio/jsonld-schema/validation/classes/Order.validator'

Bundlers such as Rollup, Vite, or Webpack include only the modules that validateOrder depends on.

  • Shared modules (e.g. validateName, validateUrl) are bundled once
  • Unused validators are excluded

Example: Importing validateOrder may include ~30 related class validators and ~100 property validators. Importing validateOrganization later reuses shared dependencies instead of duplicating them.

🌐 Lazy Loading in the Browser

In browser contexts (e.g. Nuxt DevTools or JSON-LD Viewer):

  • Validators are loaded on demand via import()
  • Each module is cached after the first load
  • Shared validators are not re-fetched

Result: minimal network requests and fast validation.

Rollup / Vite

js
export default {
  treeshake: true,
  output: {
    format: 'esm'
  }
}

Bundlers automatically deduplicate imports; no manual configuration required.

✅ Summary

  • Each validator is a standalone ES module
  • Class definitions are available as granular TypeScript-only exports
  • Bundlers automatically remove unused code
  • Dynamic imports enable efficient on-demand loading
  • Works in both Node and browser environments

Built with VitePress – Released under the MIT License.