Skip to content

Source dependencies

One core benefit of a monorepo, often overlooked or underutilized, is the ability to leverage shared workspaces as source-level dependencies. When code is colocated within a monorepo, there should be no need to rebuild across workspace boundaries to incorporate changes from dependent workspaces.

However, some monorepo implementations have difficulties when they introduce TypeScript or other languages and features that require compilation or transpilation before sharing code across Workspaces. In fact, some notable projects and teams have opted to remove TypeScript due to these challenges alone.

Fortunately, with oneRepo, such concerns are avoidable, as it provides support for using source-level dependencies out of the box.

To achieve a workflow that eliminates the need for pre-compilation, each Workspace’s package.json entry fields should point to the uncompiled source files, typically found in the src directory.

./modules/my-module/package.json
{
"name": "my-module",
"type": "module",
"main": "./src/index.ts"
}

For workspaces that are publishable to external registries, the publishConfig space in the package.json can be used to define overrides for the published package, specifying the compiled entrypoints (e.g., within the dist or build directories) and other publish-only configurations.

./modules/my-module/package.json
{
"name": "my-module",
"type": "module",
"main": "./src/index.ts",
"publishConfig": {
"main": "./dist/index.js"
}
}

When using the publishConfig, oneRepo’s integrated change management tools automatically adjust the package.json file during the publishing process. This ensures that the publicly available version of your package includes all necessary entry points pointing to the correctly built resources, rather than internal, unbuilt source dependencies.

Use the built-in one graph verify command with a custom JSON schema to ensure all shared workspaces conform to using fields correctly.

graph-schema.ts
import type { GraphSchemaValidators } from 'onerepo';
export default {
'modules/*': {
'package.json': 'package.json': (workspace: graph.Workspace, graph: graph.Graph) => ({
type: 'object',
properties: {
main: {
type: 'string',
const: './src/index.ts',
errorMessage: '`main` field must be set to `./src/index.ts`',
},
publishConfig: {
type: 'object',
properties: {
main: {
type: 'string',
const: './dist/index.js',
errorMessage: '`publishConfig.main` must be set to `./dist/index.js`',
},
typings: {
type: 'string',
const: './dist/src/index.d.ts',
errorMessage: '`publishConfig.typings` must be set to `./dist/src/index.d.ts`',
},
},
required: ['main', 'typings'],
},
},
required: ['publishConfig'],
})
}
} satisfies GraphSchemaValidators;