preface

Recently, the Monorepo project used Lerna as the tool, so the team was thinking about how to transform, the final overall technology selection is PNPM + Changeset + Turborepo. Accordingly, there is a need to support the capabilities of Lerna previously used in the context of this selection.

Of particular interest is the need to publish the Package to private Registry. Because Changeset is selected, the final command to execute the publish will be:

pnpm changeset publish
Copy the code

At this time, it involves a question: where should the private Registry be configured in the project? Before we rush to find the answer, let’s take a look at the four postures for configuring private Registry.

1 Global registry

We can set Global Registry by setting NPM or PNPM config, for example:

# npm
npm config set registry=http://localhost:2000

# or pnpm
pnpm config set registry=http://localhost:2000
Copy the code

This allows the configuration to be read at the code level through process.env.npm_config_registry.

2 .npmrc

Either NPM or PNPM reads the configuration from the.npMRc file of the project by default, so when we need to smuggle the package publication with Registry, we can set it like this:

registry = http://localhost:2000
Copy the code

3 –registry

When performing NPM publish or PNPM publish, we can also tell the corresponding package management tool what Registry is to publish the package by carrying — Registry Option, for example:

# npm
npm publish --registry=http://localhost:2000

# or pnpm
pnpm publish --registry=http://localhost:2000
Copy the code

4 PublishConfig

PublishConfig refers to the publishconfig. registry that we can tell NPM or PNPM to publish in the project package.json, for example:

{..."publishConfig": {
    "registry": "http://localhost:2000"}... }Copy the code

5 Changeset Publish principle

Having seen the four postures for setting up private Registry, we return to the question at the beginning of this article. How do I let PNPM Changeset Publish know to publish to the specified private Registry? If you think one of these four options is ok, you may need to step in some holes.

First, we need to know that the essence of the PNPM Changeset publish command is to perform Changeset publish. Therefore, it is likely that not all of the above four ways to set up Registry will work because Changeset has a publish mechanism of its own. And it does three things:

1. First, get the Package Info, which gets it from the specified Registry. For example, if you were getting Package Info for rollup, it would look like this:

npm info rollup --registry="https://registry.npmjs.org/" --json
Copy the code

2. Second, compare the version field in package.json with the version field in package.json to determine whether the current version is released

3. Finally, if the current version is not published, publish based on the package management tool currently in use, construct a Registry address that it thinks should be published, and rewrite the configuration on env like this:

// packages/cli/src/commands/publish/npm-utils
const envOverride = {
  npm_config_registry: getCorrectRegistry()
};
let { code, stdout, stderr } = await spawn(
  // Dynamic package management tools, such as PNPM, NPM, and YARN
  publishTool.name,
  ["publish", opts.cwd, "--json". publishFlags], {env: Object.assign({}, process.env, envOverride)
  }
);
Copy the code

As you can see, the whole Changeset publish process is very simple and easy to understand. Also, it’s important to note that all that Registry retrieves is done by a function called getCorrectRegistry(), which is defined like this:

// packages/cli/src/commands/publish/npm-utils.ts
function getCorrectRegistry(packageJson? : PackageJSON) :string {
  constregistry = packageJson? .publishConfig? .registry ?? process.env.npm_config_registry;return! registry || registry ==="https://registry.yarnpkg.com"
    ? "https://registry.npmjs.org"
    : registry;
}
Copy the code

With that in mind, I think you can see why not all of the four ways to set up Registry are likely to work.

Because Registry configuration is only supported in Changeset using publishConfig or env, So if you try other ways will publish to 2 https://registry.yarnpkg.com or https://registry.npmjs.org, and in the first step for Package Info when probably will fail.

conclusion

I think the article is short, but the knowledge conveyed is quite interesting. As for PNPM + Changeset + Turborepo, at least for now, my experience is still very smooth, whether it is dependency installation, multi-package task execution, Version Or Publish, etc. So, if you’re interested, you can try it.

Finally, if there is any improper expression or mistake in the article, please make an Issue

give a like

If you get something from this post, please give me a “like”. This will be my motivation to keep sharing. Thank you

My name is Wu Liu. I like innovation and source Code manipulation, and FOCUS on source Code (Vue 3, Vite), front-end engineering, cross-end and other technology learning and sharing. Welcome to follow my wechat official account Code Center or GitHub.