Share Code in a Monorepo
A Stencil project in a monorepo often needs code that lives outside it, such as design tokens, utility functions, or types shared with an app. Stencil supports two ways to use that code:
- Import it as a package. The shared code is its own workspace package, with its own build, and the Stencil project lists it as a dependency.
- Import its source. The Stencil project imports the shared
.tsfiles directly, with a relative path or atsconfig.jsonpath alias.
Both ways bundle the shared code into loader-bundle and standalone. They differ in what the collection and dist/types output contain, and so in what you publish.
Import the shared code as a package
Use this way when the shared code is useful on its own, or when other packages in the monorepo already depend on it.
-
Give the shared code its own
package.json, with a build that outputs JavaScript and.d.tsfiles. -
Add the shared package to the Stencil project's
dependencies. For example, with pnpm workspaces:package.json{
"dependencies": {
"@acme/shared": "workspace:*"
}
} -
Import it by its package name:
import { formatGreeting } from '@acme/shared';
The collection and dist/types output keep import ... from '@acme/shared'. Publish @acme/shared along with your component library, so that consumers can install it.
Import the shared source directly
Use this way when the shared code has no build of its own, or you don't want to publish it as a separate package. Take this layout:
my-monorepo/
├── shared/
│ └── format.ts
└── packages/
└── components/
├── src/
├── stencil.config.ts
└── tsconfig.json
-
In the Stencil project's
tsconfig.json, setrootDirto a folder that contains both projects:packages/components/tsconfig.json{
"compilerOptions": {
"rootDir": "../..",
"paths": {
"@shared/*": ["../../shared/*"]
}
},
"include": ["src"]
}The shared folder doesn't need to be in
include. TypeScript compiles the shared files that your components import.The
pathsalias is optional. Without it, import the files with a relative path, such as../../../../../shared/format. -
Import the shared code:
import { formatGreeting } from '@shared/format'; -
To rebuild in watch mode when a shared file changes, add the shared folder to
watchExternalDirs:stencil.config.tsexport const config: Config = {
watchExternalDirs: ['../../shared'],
};
If rootDir doesn't contain the shared folder, the build fails with TypeScript error TS6059, which says that a file is not under rootDir.
Stencil copies the shared modules into the collection and dist/types output, under an _external/ folder. The folders inside _external/ repeat each file's path relative to rootDir, so shared/format.ts becomes dist/collection/_external/shared/format.js and dist/types/_external/shared/format.d.ts. Stencil rewrites the imports that point at these files, so the output works without the shared folder. Files in srcDir keep their usual place in the output.
Component styles work the same way. A styleUrls entry that points into the shared folder is copied to dist/collection/_external/.