Skip to content

Latest commit

 

History

History
323 lines (225 loc) · 14.6 KB

FAQs.md

File metadata and controls

323 lines (225 loc) · 14.6 KB

FAQs

Do you have a guide to working with a create-typescript-app repository?

Yes! See Contributing to a create-typescript-app Repository. It'll walk you through the common activities you'll need to contribute to a repository scaffolded with create-typescript-app.

Can I use (insert tool here) with this template?

Yes! After you set up a repository, you can substitute in any tools you'd like.

If you think the tool would be broadly useful to most consumers of this template, feel free to file a feature request to add it in.

Can I create a GitHub action?

Yes! If you want to read the GitHub Actions documentation in detail. Here we'll outline the steps required to migrate a CTA app to a GitHub Action:

  1. GitHub Actions store built output on a GitHub branch rather than in a published package on npm. As a consequence we should:

    • delete .github/workflows/release.yaml and .github/workflows/post-release.yaml.

    • update .github/workflows/build.yaml to ensure dist is up to date:

      .github/workflows/build.yaml
      jobs:
        build:
          permissions:
            contents: read
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v4
            - uses: ./.github/actions/prepare
            - run: pnpm build
      
            - name: Verify dist is up to date
              run: |
                changes=$(git status --porcelain --untracked-files=all --ignored -- dist ':!*.d.ts')
                if [ -n "$changes" ]; then
                  echo "$changes"
                  echo "::error::dist/ is out of date. Run 'pnpm build', then commit the files listed above. Files marked !! are gitignored and need 'git add --force'."
                  exit 1
                fi
      
      name: Build
      
      on:
        pull_request: ~
        push:
          branches:
            - main
    • GitHub Actions run without installing package dependencies. Replace tsdown with ncc to build source files and dependencies into a single JS file. Delete tsdown.config.ts then execute the following commands:

    pnpm remove tsdown
    pnpm add @vercel/ncc -D
    • Now we need to update the build script in our package.json:
    -"build": "tsdown",
    +"build": "rm -rf dist && ncc build src/index.ts -o dist --license licenses.txt",
    • Our build now emits to the dist directory; so we'll want to avoid linting that directory by adding the following to .eslintignore and our .prettierignore:
    +dist
    • Rather than having to remember to compile each time, we'll update our pre-commit hook in .husky/pre-commit to build for us on each commit:
    +pnpm run build
    +git add dist
    npx pretty-quick --staged
  2. Create an action.yaml metadata file.

It's worth reading the GitHub Actions documentation.

How can I bundle build output?

By default, create-typescript-app configures tsdown with unbundle: true: every file under src/ is built to a matching file under dist/. That keeps output readable and lets consumers deep-import individual files, which suits the many small packages this template is used for.

Pass --bundle to instead bundle everything reachable from src/index.ts into a single dist/index.mjs:

npx create-typescript-app --bundle

That emits a tsdown.config.ts with src/index.ts as the only entry and no unbundle setting, so tsdown uses its default bundled output. Dependencies are still left external; see tsdown > Dependencies if you'd like to inline any.

Re-running create-typescript-app in a repository keeps whatever the existing tsdown.config.ts does: bundling stays on unless it contains unbundle: true. To switch an existing repository back, add unbundle: true to its tsdown.config.ts and re-run create-typescript-app.

With the Vite block, an existing vite.config.ts is read the same way: bundling stays on unless build.rolldownOptions.output contains preserveModules: true.

How can I add dual CommonJS / ECMAScript Modules emit?

First, I'd suggest reading TypeScript Handbook > Modules - Introduction to understand how CommonJS (CJS) and ECMAScript (ESM) came to be.

Then:

  1. In tsdown.config.ts, set the tsdown format option to ["cjs", "esm"]

  2. Add a package.json "exports" entry like:

    {
    	"exports": {
    		".": {
    			"types": {
    				"import": "./dist/index.d.mts",
    				"require": "./dist/index.d.cts"
    			},
    			"import": "./dist/index.mjs",
    			"require": "./dist/index.cjs"
    		}
    	}
    }

    Every path inside "exports" has to start with ./. Node.js refuses to load the package with an ERR_INVALID_PACKAGE_TARGET error otherwise.

  3. Add package.json "main", "module", and "types" entries pointing to the same files:

    {
    	"main": "./dist/index.cjs",
    	"module": "./dist/index.mjs",
    	"types": "./dist/index.d.cts"
    }

    "exports" is ignored by TypeScript's older "moduleResolution": "node10" and by other legacy resolvers. Without these three entries they find nothing at all for the package.

That should be it!

To be safe, consider checking with arethetypeswrong:

  1. Run pnpm build
  2. Run npm pack
  3. Upload that generated .tgz file to arethetypeswrong.github.io

Why doesn't create-typescript-app have an option to dual emit CJS and ESM?

Dual CJS/ESM emit is a stopgap solution while the JavaScript ecosystem migrates towards full ESM support in most-to-all popular user packages. Most packages newly created with create-typescript-app should target just ESM by default.

Some packages published with create-typescript legitimately need dual CJS/ESM output because they're used by frameworks that don't yet fully support ESM. That's reasonable.

Unless you know a package needs to support a CJS consumer, please strongly consider keeping it ESM-only (the create-typescript-app default). ESM-only packages have a smaller footprint by virtue of including fewer files.

What about eslint-config-prettier?

eslint-config-prettier is an ESLint plugin that serves only to turn off all rules that are unnecessary or might conflict with formatters such as Prettier. None of the ESLint configs enabled by this repository's tooling leave any rules enabled that would need to be disabled. Using eslint-config-prettier would be redundant.

What determines which preset a tool goes into?

The four presets correspond to what have seemed to be the most common user needs of template consumers:

  1. Minimal: Developers who just want the barest of starting templates.
    • They may be very new to TypeScript tooling, or they may have made an informed decision that the additional tooling isn't worth the complexity and/or time investment.
    • Tooling in this preset is only what would be essential for a small TypeScript package that can be built, formatted, linted, and released.
  2. Common: The common case of users who want the minimal tooling along with common repository management.
    • Tooling added in this preset should be essential for a TypeScript repository that additionally automates useful GitHub tasks: contributor recognition, release management, and testing.
  3. Everything: Power users (including this repository) who want as much of the latest and greatest safety checks and standardization as possible.

Note that you can always customize exactly which preset you use per CLI.

Why does this package include so many tools?

This repository is meant to serve as a starter that includes all the general tooling a modern TypeScript/Node repository could possibly need. Each of the included tools exists for a good reason and provides real value.

If you don't want to use any particular tool, you can always remove it manually.

Why tabs?

This repository template configures useTabs: true in the root-level prettier.config.ts. It does so because tabs have been phrased by the community as generally better for accessibility:

Note that those points on tabs over spaces have generally been made by accessibility-experienced individuals rather than accessibility-focused organizations. If you know of any accessibility organization that's published more formal recommendations or research, please do file an issue here for this FAQ entry to be updated.

You can adjust the tab size that GitHub uses to display files from your account settings page (the default is 8 spaces).

If you really want spaces in your project you can always remove the "useTabs": true.

How can I use bin?

The --bin option allows you to create a package.json bin value to include for npx-style running. An example of this would be "bin/index.js". A starter file will be created at that path that imports the built entry point; edit it to run your CLI.

If you'd like an example of what that looks like, take a look at the CTA source code!

How do I publish a new package for the first time?

The release.yaml workflow publishes to npm with trusted publishing, so it doesn't need an npm token. But npm only lets you add a trusted publisher to a package that already exists. So you'll need to publish the first version yourself:

  1. Pull the latest main branch, in case the Release workflow already bumped the version
  2. Run pnpm build
  3. Run npm publish, logging in with npm login first if needed
  4. Go to https://www.npmjs.com/package/<your-package>/access and add a GitHub Actions trusted publisher with:
    • Organization or user: your GitHub username or organization
    • Repository: your repository's name
    • Workflow filename: release.yaml
    • Allowed actions: npm publish

You can also do step 4 from the command line with npm trust github --file release.yaml --allow-publish (npm 11.15.0 or later).

From then on, the release.yaml workflow will publish new versions for you.

How do I release a beta version?

release-it can release prerelease versions with its --preRelease option. To have the Release workflow release betas:

  1. In .github/workflows/release.yaml, add this alongside the uses: JoshuaKGoldberg/release-it-action line:

    with:
      release-it-args: --preRelease=beta
  2. In .release-it.json, change the after:git:release hook to npm publish --tag beta, since npm won't publish a prerelease without a tag

Each release will then be the next beta, such as 2.0.0-beta.0 and then 2.0.0-beta.1. The version still follows your commits: from 1.2.0, a breaking change releases 2.0.0-beta.0 and a fix releases 1.2.1-beta.0.

To go back to stable releases, undo both changes. The next release will then drop the -beta suffix, such as going from 2.0.0-beta.1 to 2.0.0.

How do I release a new major version?

Add a ! before the colon in the PR title, such as feat!: drop support for Node.js 20. The Release workflow will then bump the major version, including from 0.x to 1.0.0.

To put several breaking changes into one major version, see How do I release several changes at once?.

How do I release several changes at once?

The release.yaml workflow runs on every push to main. It releases a new version if there's been a feat, fix, or perf commit, or a breaking change marked with !, since the last release. PRs are squash merged, so that usually means one release per PR.

To batch several PRs into one release:

  1. Disable the Release workflow from your repository's Actions tab, or with gh workflow disable release.yaml
  2. Merge all but the last PR
  3. Re-enable the workflow, or run gh workflow enable release.yaml
  4. Merge the last PR

The release from that last merge will include every change since the previous release.

How do I release changes that didn't trigger a release?

Commits typed as build, chore, ci, docs, refactor, style, or test don't trigger a release.

If a change that didn't trigger a release does need to reach users, push a fix commit to main:

git commit --allow-empty --message "fix: release recent dependency updates"
git push

If a Renovate-bumped devDependency regularly changes your build output, you can add a rule to .github/renovate.json to title its updates as fix:

{
	"packageRules": [
		{
			"matchPackageNames": ["your-dev-dependency"],
			"semanticCommitType": "fix"
		}
	]
}

Why do I get "The inferred type of ... cannot be named without a reference to ..." (TS2742)?

TypeScript needs to write out a type for each export in its .d.ts output. If that type comes from a package you don't depend on directly, TypeScript can't refer to it, because pnpm only lets your package import its own dependencies.

For example, express() returns a type from @types/express-serve-static-core, which @types/express depends on but a consuming package might not. This applies to every file in src, even test helpers that aren't part of your package's public API.

The usual fix is to give the export a type annotation that only uses packages you depend on:

import express from "express";

export const app: express.Express = express();

See microsoft/TypeScript#47663 (comment) for other workarounds.