npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unplugin-monorepo

v0.1.0

Published

[![npm](https://img.shields.io/npm/v/unplugin-monorepo?color=91B2D4&label=)](https://npmjs.com/package/unplugin-monorepo)

Downloads

4

Readme

unplugin-monorepo

npm

Require the least configuration for support bundling of local packages within a monorepo.

Inspired by vite-ts-monorepo-rfc

Installation

npm i unplugin-monorepo -D
// vite.config.ts
import { viteMonorepo } from 'unplugin-monorepo/vite';

export default defineConfig({
  plugins: [
    viteMonorepo({ /* options */ }),
  ],
});

Currently, only VITE is supported

// nuxt.config.js
export default defineNuxtConfig({
  modules: [
    ['unplugin-monorepo/nuxt', { /* options */ }],
  ],
});

This module works for both Nuxt 2 and Nuxt Vite

Usage

First, install and import unplugin-monorepo as described above.

Next, let's take the simplest monorepo as an example. The directory structure is as follows:

monorepo
├── app # Application
└── lib # Library
    └── src
        └── index.ts

In this structure, app depends on lib.

{
  "name": "app",
  "devDependencies": {
    "lib": "workspace:*"
  }
}

Configuring Sub-Projects

When registering unplugin-monorepo, the bundler will prioritize reading the file corresponding to the bundler.sourceDir field of the sub-project during the bundling process. Therefore, you need to configure the bundler.sourceDir field in the package.json file of the sub-project and point to the source file path.

For example, in the following example, when referencing a lib package, it will read the ./src/index.ts file to build:

{
  "name": "lib",
  "bundler": {
    "sourceDir": "src"
  }
}

TypeScript Projects

In TypeScript projects, you need to use TypeScript's Project Reference feature, which can help you use source code development in combination with unplugin-monorepo.

Project reference provides the following capabilities:

  • Allows TypeScript to correctly recognize the types of other sub-projects without needing to build the sub-projects.
  • When navigating code in VS Code, VS Code can automatically jump to the source code files of the corresponding modules.

Based on the above example, where app references the lib sub-project, we need to configure composite and references in app's tsconfig.json, pointing to the relative directory of lib:

app/tsconfig.json:

{
  "compilerOptions": {
    "composite": true
  },
  "references": [
    {
      "path": "../lib"
    }
  ]
}

Additionally, you need to configure rootDir in the lib sub-project:

lib/tsconfig.json:

{
  "compilerOptions": {
    "rootDir": "src"
  }
}

After adding the above configuration, the project reference is configured. You can restart VS Code to see the effect of the configuration.

The above is just a simple example. In actual monorepo projects, there may be more complex dependencies, and you need to add complete references configurations to make the above features work correctly.

If the above configuration doesn't solve your problem, feel free to open an ISSUE

Options Configuration

export interface Options {
  /**
   * package.json special meta key
   * @zh 读取 `package.json` 自定义字段,用于配置源代码文件对应的解析字段。
   * @default 'bundler'
   */
  packageMetaKey?: string
  /**
   * package.json special meta default value
   * @zh 读取 `package.json` 自定义字段 `packageMetaKey` 后,解析字段值的默认值。
   * */
  packageMetaDefaultValue?: {
    sourceDir: string
  }
}

Examples

There are examples in the playgrounds of this repository, which you can refer to.

Credits

  • vite-ts-monorepo-rfc is the main inspiration for this project. Before implementing this solution, I only used the conditions field in vite.config.ts, but after recognizing the pain points mentioned in the RFC, I decided to develop this plugin.
  • @rsbuild/plugin-source-build provided inspiration for configuring TypeScript Project Reference in this project.
  • vite Most of the utility functions are derived from Vite.

License

Made with 💙

Published under MIT License.