🎁

preconstruct

GitHub

Exporting Multiple Entrypoints

Note: this tutorial builds upon the Getting Started tutorial so if you haven't gone through it, you should do that first.

A repository with the end result of this tutorial is available at https://github.com/preconstruct/a-random-number-with-create

Curious why you would want multiple entrypoints in a package? Read the When should I use multiple entrypoints? guide.

We're going to create another package from a-random-number called a-random-number-with-create and add another entrypoint called create so that people can import a-random-number-with-create/create to create their own random numbers and still import a-random-number-with-create to get a random number.

The first thing we're going to do is create our entrypoint directory.

mkdir create

Then we can set what the entrypoints for our package should be.

"preconstruct": {
"entrypoints": [
".",
"create"
]
}
What does "." mean here?

"." is referring to the current directory so in this context it means that the current directory should be an entrypoint. So consumers should be able to import a-random-number (".") and a-random-number/create ("create")

If we run yarn preconstruct fix now, it'll ask us a question and fix any problems with our project.

🎁 ? a-random-number-with-create A package.json file does not exist for this entrypoint, would you like to create one automatically? Yes
🎁 error a-random-number-with-create/create no source file was provided, please create a file at src/index or specify a custom source file with the preconstruct source option

Now preconstruct is complaining that there isn't a source file so we need to create one. We're going to create one at src/create.js

export let createRandom = () => Math.random();

We're also going to change src/index.js to use our new createRandom function

import { createRandom } from "./create";
export let random = createRandom();

Since the source file for our create entrypoint isn't at the default of src/index.js relative to the entrypoint, we need to use the source option in the package.json in the create directory.

{
"main": "dist/a-random-number-with-create.cjs.js",
"module": "dist/a-random-number-with-create.esm.js",
"preconstruct": {
"source": "../src/random"
}
}

Now if we run yarn preconstruct build, everything will work and after publishing the package, people can import a-random-number-with-create/create.