Giter VIP home page Giter VIP logo

pkg-utils's Introduction

@sanity/pkg-utils

Simple utilities for modern npm packages.

npm install @sanity/pkg-utils -D

npm version

Basic usage

# Initialize a new package
pnpx @sanity/pkg-utils@latest init my-package

# In a Node.js package directory with `package.json` present

# Check the package
pkg-utils check

# Build the package
pkg-utils build

# Watch the package
pkg-utils watch

Run pkg-utils -h for more information on CLI usage.

Configuration

@sanity/pkg-utils reads most of its configuration from package.json. But sometimes you need more control. You may then add a configuration file named package.config.ts (or .js, .cjs, or .mjs).

// package.config.ts

import {defineConfig} from '@sanity/pkg-utils'

export default defineConfig({
  extract: {
    rules: {
      // do not require internal members to be prefixed with `_`
      'ae-internal-missing-underscore': 'off',
    },
  },

  // the path to the tsconfig file for distributed builds
  tsconfig: 'tsconfig.dist.json',
})

Options

bundles

  • Type: PkgBundle[]
  • Default: undefined

An array of entry points to bundle. This is useful if you want to bundle something that should not be exported by the package, e.g. CLI scripts or Node.js workers.

define

  • Type: Record<string, string | number | boolean | null | undefined>
  • Default: {}

An object defining globals within the package.

dist

  • Type: string
  • Default: './dist'

The path to the directory to which bundle and chunk files should be written.

exports

  • Type: PkgConfigProperty<PkgExports>
  • Default: the value of "exports" in package.json

Override or modify the value of the exports before it’s parsed internally.

extract

  • Type:
    {
      rules?: {
        'ae-forgotten-export'?: PkgRuleLevel
        'ae-incompatible-release-tags'?: PkgRuleLevel
        'ae-internal-missing-underscore'?: PkgRuleLevel
        'ae-missing-release-tag'?: PkgRuleLevel
      }
    }
  • Default: undefined

Configure the level of reporting of API Extractor (which is used to bundle the type definitions, as well as lint the TSDoc of the package).

external

  • Type: string[]
  • Default: []

Packages to exclude from bundles.

jsx

  • Type: 'transform' | 'preserve' | 'automatic'
  • Default: 'automatic'

Strategy for bundling JSX.

jsxFactory

  • Type: string
  • Default: 'createElement'

The name of the function that creates JSX elements.

jsxFragment

  • Type: string
  • Default: 'Fragment'

The name of JSX fragment elements.

jsxImportSource

  • Type: string
  • Default: 'react'

The name of the library from which to import JSX factory and fragment names.

legacyExports

  • Type: boolean
  • Default: false

Build package with support for legacy exports (writes root <export>.js files). Use this if you need to support older bundlers.

minify

  • Type: boolean
  • Default: false

Whether to minify the bundled JavaScript.

rollup.plugins

  • Type: PkgConfigProperty<RollupPlugin[]>
  • Default: []

Rollup plugins to load when bundling.

runtime

  • Type: '*' | 'browser' | 'node'
  • Default: '*'

Default runtime of package exports

sourcemap

  • Type: boolean
  • Default: true

Whether to include source map files.

src

  • Type: string
  • Default: './src'

The path to the directory in which source code is located.

tsconfig

  • Type: string
  • Default: 'tsconfig.json'

The path to the TypeScript configuration file.

License

MIT

pkg-utils's People

Contributors

bjoerge avatar ecospark[bot] avatar largis21 avatar mariuslundgard avatar renovate[bot] avatar rexxars avatar semantic-release-bot avatar snorrees avatar stipsan avatar vitorrd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pkg-utils's Issues

"watch" doesn't work on Windows

Dependency globby only accepts forward slashes as parameters. As a result, Windows paths will always fail. This currently makes it impossible to use plugin-kit's link-watch on Windows,
The solution is to change src/node/globFiles.ts from:

import globby from 'globby'

export function globFiles(patterns: string[]): Promise<string[]> {
  return globby(patterns)
}

To:

import path from 'path'
import globby from 'globby'

export function globFiles(patterns: string[]): Promise<string[]> {
  return globby(patterns.map(pattern => pattern.split(path.sep).join(path.posix.sep))
}

If no one can get to this soon, I'll make a PR when time permits.

Peer dependency error for `typescript` when initializing a new package

If you find a security vulnerability, do NOT open an issue. Email [email protected] instead.

Describe the bug

When using npm to initialize a new package:

npx @sanity/pkg-utils@latest init <pkg-name>

The installation will throw a peer dependency error:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/typescript
npm ERR!   dev typescript@"^5.4.3" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer typescript@"5.4.2" from @sanity/[email protected]
npm ERR! node_modules/@sanity/pkg-utils
npm ERR!   dev @sanity/pkg-utils@"^5.1.7" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Which versions of Node.js / npm are you running?

Node v20.12.0
NPM v10.5.0

Additional context

@sanity/pkg-utils specifies an exact version of typescript as a peer dependency:

"typescript": "5.4.2"

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (config:recommended). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.


Using a curated preset maintained by


Sanity: The Composable Content Cloud

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • chore(deps): update dependency styled-components to ^6.1.9
  • chore(deps): lock file maintenance
  • πŸ” Create all pending approval PRs at once πŸ”

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/browserslist.yml
  • actions/checkout v4
  • pnpm/action-setup v3
  • actions/setup-node v4
  • actions/create-github-app-token v1
  • peter-evans/create-pull-request v6@6d6857d36972b65feb161a90e484f2984215f83e
.github/workflows/format-if-needed.yml
  • actions/checkout v4
  • pnpm/action-setup v3
  • actions/setup-node v4
  • actions/create-github-app-token v1
  • peter-evans/create-pull-request v6@6d6857d36972b65feb161a90e484f2984215f83e
.github/workflows/main.yml
  • actions/checkout v4
  • pnpm/action-setup v3
  • actions/setup-node v4
  • actions/upload-artifact v3
  • actions/checkout v4
  • pnpm/action-setup v3
  • actions/setup-node v4
  • actions/download-artifact v3
  • actions/upload-artifact v3
  • actions/checkout v4
  • pnpm/action-setup v3
  • actions/setup-node v4
  • actions/download-artifact v3
.github/workflows/release-please.yml
  • actions/create-github-app-token v1
  • google-github-actions/release-please-action v4
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
npm
package.json
  • @babel/core ^7.24.5
  • @babel/types ^7.24.5
  • @microsoft/api-extractor 7.43.1
  • @microsoft/tsdoc-config ^0.16.2
  • @optimize-lodash/rollup-plugin 4.0.4
  • @rollup/plugin-alias ^5.1.0
  • @rollup/plugin-babel ^6.0.4
  • @rollup/plugin-commonjs ^25.0.7
  • @rollup/plugin-json ^6.1.0
  • @rollup/plugin-node-resolve ^15.2.3
  • @rollup/plugin-replace ^5.0.5
  • @rollup/plugin-terser ^0.4.4
  • @sanity/browserslist-config ^1.0.3
  • browserslist ^4.23.0
  • cac ^6.7.14
  • chalk ^4.1.2
  • chokidar ^3.6.0
  • esbuild ^0.20.2
  • esbuild-register ^3.5.0
  • find-config ^1.0.0
  • get-latest-version ^5.1.0
  • git-url-parse ^14.0.0
  • jsonc-parser ^3.2.1
  • mkdirp ^3.0.1
  • outdent ^0.8.0
  • parse-git-config ^3.0.0
  • pkg-up ^3.1.0
  • prettier ^3.2.5
  • pretty-bytes ^5.6.0
  • prompts ^2.4.2
  • recast 0.23.6
  • rimraf ^4.4.1
  • rollup ^4.17.2
  • rollup-plugin-esbuild ^6.1.1
  • rxjs ^7.8.1
  • treeify ^1.1.0
  • uuid ^9.0.1
  • zod 3.23.5
  • zod-validation-error 3.2.0
  • @commitlint/cli ^19.3.0
  • @commitlint/config-conventional ^19.2.2
  • @sanity/prettier-config ^1.0.2
  • @types/babel__core ^7.20.5
  • @types/cpx ^1.5.5
  • @types/find-config ^1.0.4
  • @types/git-url-parse ^9.0.3
  • @types/parse-git-config ^3.0.4
  • @types/prompts ^2.4.9
  • @types/rimraf ^3.0.2
  • @types/treeify ^1.0.3
  • @types/uuid ^9.0.8
  • @typescript-eslint/eslint-plugin ^7.8.0
  • @typescript-eslint/parser ^7.8.0
  • browserslist-to-esbuild 2.1.1
  • commitizen ^4.3.0
  • cpx ^1.5.0
  • cz-conventional-changelog ^3.3.0
  • eslint ^8.57.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-import ^2.29.1
  • eslint-plugin-prettier ^5.1.3
  • eslint-plugin-simple-import-sort 12.1.0
  • npm-run-all2 ^6.1.2
  • outdent ^0.8.0
  • rollup-plugin-visualizer ^5.12.0
  • ts-node ^10.9.2
  • typescript 5.4.5
  • vitest ^1.5.3
  • vitest-github-actions-reporter ^0.11.1
  • typescript 5.4.x
  • node >=18.17.0
  • pnpm 9.0.6
playground/babel-plugin/package.json
  • @babel/core ^7.24.5
  • @babel/plugin-proposal-object-rest-spread ^7.20.7
playground/browser-bundle/package.json
playground/custom-dist/package.json
playground/default-export/package.json
playground/dummy-commonjs/package.json
playground/dummy-lodash-es/package.json
  • lodash ^4.17.21
  • lodash-es ^4.17.21
  • @types/lodash ^4.17.0
  • @types/lodash-es ^4.17.12
playground/dummy-lodash/package.json
  • lodash ^4.17.21
  • @types/lodash ^4.17.0
playground/dummy-module/package.json
playground/dummy-side-effects/package.json
playground/extra-bundles/package.json
playground/js/package.json
playground/multi-export/package.json
  • @sanity/visual-editing ^1.8.16
  • @sveltejs/adapter-auto ^3.2.0
  • @sveltejs/kit ^2.5.7
  • @sveltejs/package ^2.3.1
  • @sveltejs/vite-plugin-svelte ^3.1.0
  • svelte ^4.2.15
  • vite ^5.2.10
  • @sveltejs/kit ^2.0.0
  • svelte ^4.0.0
playground/multi-exports-commonjs/package.json
playground/node-api/package.json
playground/reference-cjs-legacy/package.json
playground/reference-cjs-only/package.json
playground/reference-cjs/package.json
  • @tsconfig/node-lts 20.1.3
  • typescript 5.4.5
playground/reference-esm-legacy/package.json
playground/reference-esm-only/package.json
  • @tsconfig/node-lts 20.1.3
  • typescript 5.4.5
playground/reference-esm/package.json
  • @tsconfig/node-lts 20.1.3
  • typescript 5.4.5
playground/runtime-astro/package.json
  • astro ^4.7.0
playground/runtime-next-js/package.json
  • @types/react ^18.3.1
  • next ^14.2.3
  • react ^18.3.1
  • react-dom ^18.3.1
playground/runtime-node-commonjs/package.json
playground/runtime-node-module/package.json
playground/runtime-svelte/package.json
  • @fontsource/fira-mono ^5.0.13
  • @neoconfetti/svelte ^1.0.0
  • @sveltejs/adapter-auto ^3.2.0
  • @sveltejs/kit ^2.5.7
  • @sveltejs/vite-plugin-svelte ^3.1.0
  • @types/eslint ^8.56.10
  • @typescript-eslint/eslint-plugin ^7.8.0
  • @typescript-eslint/parser ^7.8.0
  • eslint ^9.0.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-svelte ^2.38.0
  • prettier ^3.2.5
  • prettier-plugin-svelte ^3.2.3
  • svelte ^4.2.15
  • svelte-check ^3.7.0
  • tslib ^2.6.2
  • typescript 5.4.5
  • vite ^5.2.10
playground/runtime-vite/package.json
  • typescript 5.4.5
  • vite ^5.2.10
playground/runtime-webpack-v3/package.json
  • webpack ^3.12.0
playground/sanity/package.json
playground/styled-components-module/package.json
  • @rollup/plugin-commonjs ^25.0.7
  • @rollup/plugin-node-resolve ^15.2.3
  • @rollup/plugin-swc ^0.3.0
  • @swc/plugin-styled-components ^2.0.2
  • @types/react ^18.3.1
  • babel-plugin-styled-components ^2.1.4
  • react ^18.3.1
  • rollup ^4.17.2
  • rollup-plugin-esbuild ^6.1.1
  • styled-components ^6.1.8
playground/ts-bundler/package.json
playground/ts-node16/package.json
playground/ts/package.json
playground/use-client-directive/package.json
  • @types/react ^18.3.1
  • react ^18.3.1
  • react ^18.2.0
playground/vision/package.json

  • Check this box to trigger a request for Renovate to run again on this repository

Export package src folder into the output directory

Hi everyone,

We are currently updating our Sanity Studio plugin to V3 and I would like to ask if it is possible to also copy or output the whole src folder into the outDir so that we can import specific files from this src/schemas folder into another plugin or project:

Screen Shot 2023-02-22 at 2 54 09 PM

Currently when we build the plugin, it only outputs index files into the target output directory lib

image

This is the relevant config from the package.json:

image

This is the package.config.ts:

Screen Shot 2023-02-22 at 3 11 00 PM

This is the tsconfig.json:

image

This is the tsconfig.lib.json:

image

This is the tsconfig.settings.json:

image

I would really appreciate your help on this. Thanks a lot! :)

Can't initialize new package with `pkg-utils init`

If you find a security vulnerability, do NOT open an issue. Email [email protected] instead.

Describe the bug

Initialization prompt will error with:

error Cannot find package 'prettier-plugin-packagejson' imported from [.]/noop.js
([.] is the absolute path to the current working directory)

To Reproduce

Steps to reproduce the behavior:

  1. Run npx @sanity/pkg-utils@latest init [path] and answer prompts

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Which versions of Sanity are you using?

Run sanity versions in the terminal and copy-paste the result here.

What operating system are you using?

Which versions of Node.js / npm are you running?

[email protected]
[email protected]

Additional context

Add any other context about the problem here.

Security issue?

Any security issues should be submitted directly to [email protected]. In order to determine whether you are dealing with a security issue, ask yourself these two questions:

  • Can I access something that's not mine, or something I shouldn't have access to?
  • Can I disable something for other people? If the answer to either of those two questions are "yes", then you're probably dealing with a security issue. Note that even if you answer "no" to both questions, you may still be dealing with a security issue, so if you're unsure, just email us at [[email protected]](mailto:[email protected].

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Unformatted errors when running pkg watch

Currently, errors that may happen during doExtract when running pkg watch --strict (not sure if --strict matters) are currently logged as-is without any sort of formatting, which leads to output like this:

[error] DtsError: encountered 2 errors when extracting types
    at doExtract (./node_modules/@sanity/pkg-utils/dist/_chunks/index-c12a2784.cjs:2156:2098) {
  messages: [
    ExtractorMessage {
      category: 'Extractor',
      messageId: 'ae-forgotten-export',
      text: '(…)',
      sourceFilePath: './src/types.ts',
      sourceFileLine: 23,
      sourceFileColumn: 1,
      properties: {},
      _handled: true,
      _logLevel: 'error'
    },
    ExtractorMessage {
      category: 'Extractor',
      messageId: 'ae-forgotten-export',
      text: '(…)',
      sourceFilePath: './types.ts',
      sourceFileLine: 139,
      sourceFileColumn: 1,
      properties: {},
      _handled: true,
      _logLevel: 'error'
    }
  ]
}

ERROR: "pkg:watch" exited with 1.

Not a super big deal, but it would be nice if these errors could be formatted the same way as they are when error occurs during pkg build:

[error] 1638ms
[error] encountered 2 errors when extracting types


types.ts:23:1 - error ae-forgotten-export
(The error message)

types.ts:139:1 - error ae-forgotten-export
(The error message)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.