packlets
Original:🇺🇸 English
Translated
Learn the rules of packlets for managing a JavaScript/TypeScript project. Use this skill whenever a user mentions packlets or when working in a project with packlets (src/packlets) directory.
1installs
Sourcedtinth/agent-skills
Added on
NPX Install
npx skill4agent add dtinth/agent-skills packletsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Packlets are a nice way to keep your JavaScript codebase somewhat loosely-coupled, without having to separate things into different npm packages.
You follow these 5 rules:
- Packlets live in and the entry point is
./src/packlets/<name>/.index.ts - Files outside a packlet can only import from packlet’s entry point, .
index.ts - Files inside a packlet may not import its own entry point, .
index.ts - Packlets can import other packlets but cannot create circular dependencies.
- Packlets can only import other packlets and npm packages.
About packlets
Motivation
When building a large application, it's a good idea to organize source files into modules, so that their dependencies can be managed. For example, suppose an application's source files can be grouped as follows:
- - the logging system
src/logging/*.ts - - the data model
src/data-model/*.ts - - the report engine
src/reports/*.ts - - other arbitrary files such as startup code and the main application
src/*.ts
Using file folders is helpful, but it's not very strict. Files under can easily import files from , creating a confusing circular import. They can also import arbitrary application files. Also, there is no clear distinction between which files are the "public API" for versus its private implementation details.
src/logging/src/reportssrc/loggingAll these problems can be solved by reorganizing the project into NPM packages (or Rush projects). Something like this:
- - the logging system
@my-app/logging - - the data model
@my-app/data-model - - the report engine
@my-app/reports - - other arbitrary files such as startup code and the main application
@my-app/application
However, separating code in this way has some downsides. The projects need to build separately, which has some tooling costs (for example, "watch mode" now needs to consider multiple projects). In a large monorepo, the library may attract other consumers, before the API has been fully worked out.
Packlets provide a lightweight alternative that offers many of the same benefits of packages, but without the file. It's a great way to prototype your project organization before later graduating your packlets into proper NPM packages.
package.json5 rules for packlets
With packlets, our folders would be reorganized as follows:
- - the logging system
src/packlets/logging/*.ts - - the data model
src/packlets/data-model/*.ts - - the report engine
src/packlets/reports/*.ts - - other arbitrary files such as startup code and the main application
src/*.ts
The packlets-tutorial sample project illustrates this layout in full detail.
The basic design can be summarized in 5 rules:
-
A "packlet" is defined to be a folder path. The index.ts file will have the exported APIs. The
./src/packlets/<packlet-name>/index.tsname must consist of lower case words separated by hyphens, similar to an NPM package name.<packlet-name>Example file paths:src/packlets/controls src/packlets/logger src/packlets/my-long-nameNOTE: Thecannot be nested deeper in the tree. Like with NPM packages,packletsis a flat namespace.src/packlets -
Files outside the packlet folder can only import the packlet root index.ts:src/app/App.tsts
// Okay import { MainReport } from '../packlets/reports'; // Error: The import statement does not use the packlet's entry point (@rushstack/packlets/mechanics) import { MainReport } from '../packlets/reports/index'; // Error: The import statement does not use the packlet's entry point (@rushstack/packlets/mechanics) import { MainReport } from '../packlets/reports/MainReport'; -
Files inside a packlet folder should import their siblings directly, not via their own index.ts (which might create a circular reference):src/packlets/logging/Logger.tsts
// Okay import { MessageType } from "./MessageType"; // Error: Files under a packlet folder must not import from their own index.ts file (@rushstack/packlets/mechanics) import { MessageType } from "."; // Error: Files under a packlet folder must not import from their own index.ts file (@rushstack/packlets/mechanics) import { MessageType } from "./index"; -
Packlets may reference other packlets, but not in a way that would introduce a circular dependency:src/packlets/data-model/DataModel.tsts
// Okay import { Logger } from '../../packlets/logging';src/packlets/logging/Logger.tsts// Error: Packlet imports create a circular reference: (@rushstack/packlets/circular-deps) // "logging" is referenced by src/packlets/data-model/DataModel.ts // "data-model" is referenced by src/packlets/logging/Logger.ts import { DataModel } from '../../packlets/data-model'; -
Other source files are allowed outside the src/packlets folder. They may import a packlet, but packlets must only import from other packlets or NPM packages.src/app/App.tsts
// Okay import { MainReport } from '../packlets/reports';src/packlets/data-model/ExampleModel.tsts// Error: A local project file cannot be imported. A packlet's dependencies must be // NPM packages and/or other packlets. (@rushstack/packlets/mechanics) import { App } from '../../app/App';