Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use type PackageJson from type-fest #117

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
'node_modules',
'pnpm-lock.yaml',
],
words: ['parseable', 'opencollective'],
flagWords: banWords,
dictionaries: ['dictionary'],
dictionaryDefinitions: [
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
AutoExternal,
Format,
LibConfig,
PkgJson,
PackageJson,
RslibConfig,
RslibConfigAsyncFn,
RslibConfigExport,
Expand Down Expand Up @@ -89,7 +89,7 @@ export async function loadConfig({

export const composeAutoExternalConfig = (options: {
autoExternal: AutoExternal;
pkgJson?: PkgJson;
pkgJson?: PackageJson;
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
}): RsbuildConfig => {
const { autoExternal, pkgJson, userExternals } = options;
Expand Down Expand Up @@ -242,7 +242,7 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
const composeAutoExtensionConfig = (
config: LibConfig,
autoExtension: boolean,
pkgJson?: PkgJson,
pkgJson?: PackageJson,
): {
config: RsbuildConfig;
jsExtension: string;
Expand Down
239 changes: 239 additions & 0 deletions packages/core/src/types/external/typeFest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/**
* The following code is modified based on
* https://github.com/sindresorhus/type-fest/blob/986fab/source/package-json.d.ts
*
* MIT OR CC0-1.0 Licensed
* https://github.com/sindresorhus/type-fest/blob/main/license-cc0
*/

/**
* Matches a JSON object.
*
* This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
*
* @category JSON
*/
type JsonObject = { [Key in string]: JsonValue } & {
[Key in string]?: JsonValue | undefined;
};

/**
* Matches a JSON array.
*
* @category JSON
*/
type JsonArray = JsonValue[] | readonly JsonValue[];

/**
* Matches any valid JSON primitive value.
*
* @category JSON
*/
type JsonPrimitive = string | number | boolean | null;

/**
* Matches any valid JSON value.
*
* @category JSON
*/
type JsonValue = JsonPrimitive | JsonObject | JsonArray;

type Scripts = Partial<Record<string, string>>;

type Dependency = Partial<Record<string, string>>;

/**
* A mapping of conditions and the paths to which they resolve.
*/
type ExportConditions = {
// eslint-disable-line @typescript-eslint/consistent-indexed-object-style
[condition: string]: Exports;
};

/**
* Entry points of a module, optionally with conditions and subpath exports.
*/
type Exports =
| null
| string
| Array<string | ExportConditions>
| ExportConditions;

/**
* Import map entries of a module, optionally with conditions and subpath imports.
*/
type Imports = {
[key: `#${string}`]: Exports;
};

interface NonStandardEntryPoints {
/**
An ECMAScript module ID that is the primary entry point to the program.
*/
module?: string;

/**
Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.

[Read more.](https://webpack.js.org/guides/tree-shaking/)
*/
sideEffects?: boolean | string[];
}

/**
* Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Containing standard npm properties.
*/
interface PackageJsonStandard {
/**
The name of the package.
*/
name?: string;

/**
Package version, parseable by [`node-semver`](https://github.com/npm/node-semver).
*/
version?: string;

/**
Package description, listed in `npm search`.
*/
description?: string;

/**
Keywords associated with package, listed in `npm search`.
*/
keywords?: string[];

/**
The licenses for the package.
*/
licenses?: Array<{
type?: string;
url?: string;
}>;

/**
The files included in the package.
*/
files?: string[];

/**
Resolution algorithm for importing ".js" files from the package's scope.

[Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
*/
type?: 'module' | 'commonjs';

/**
The module ID that is the primary entry point to the program.
*/
main?: string;

/**
Subpath exports to define entry points of the package.

[Read more.](https://nodejs.org/api/packages.html#subpath-exports)
*/
exports?: Exports;

/**
Subpath imports to define internal package import maps that only apply to import specifiers from within the package itself.

[Read more.](https://nodejs.org/api/packages.html#subpath-imports)
*/
imports?: Imports;

/**
The executable files that should be installed into the `PATH`.
*/
bin?: string | Partial<Record<string, string>>;

/**
Script commands that are run at various times in the lifecycle of the package. The key is the lifecycle event, and the value is the command to run at that point.
*/
scripts?: Scripts;

/**
Is used to set configuration parameters used in package scripts that persist across upgrades.
*/
config?: JsonObject;

/**
The dependencies of the package.
*/
dependencies?: Dependency;

/**
Additional tooling dependencies that are not required for the package to work. Usually test, build, or documentation tooling.
*/
devDependencies?: Dependency;

/**
Dependencies that will usually be required by the package user directly or via another dependency.
*/
peerDependencies?: Dependency;

/**
Engines that this package runs on.
*/
engines?: {
[EngineName in 'npm' | 'node' | string]?: string;
};

/**
@deprecated
*/
engineStrict?: boolean;

/**
If set to `true`, a warning will be shown if package is installed locally. Useful if the package is primarily a command-line application that should be installed globally.

@deprecated
*/
preferGlobal?: boolean;

/**
If set to `true`, then npm will refuse to publish it.
*/
private?: boolean;

/**
A set of config values that will be used at publish-time. It's especially handy to set the tag, registry or access, to ensure that a given package is not tagged with 'latest', published to the global public registry or that a scoped module is private by default.
*/
publishConfig?: PublishConfig;
}

type PublishConfig = {
/**
Additional, less common properties from the [npm docs on `publishConfig`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#publishconfig).
*/
[additionalProperties: string]: JsonValue | undefined;

/**
When publishing scoped packages, the access level defaults to restricted. If you want your scoped package to be publicly viewable (and installable) set `--access=public`. The only valid values for access are public and restricted. Unscoped packages always have an access level of public.
*/
access?: 'public' | 'restricted';

/**
The base URL of the npm registry.

Default: `'https://registry.npmjs.org/'`
*/
registry?: string;

/**
The tag to publish the package under.

Default: `'latest'`
*/
tag?: string;
};

/**
* Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Also includes types for fields used by other popular projects, like TypeScript and Yarn.
*
* @category File
*/
export type PackageJson = JsonObject &
PackageJsonStandard &
NonStandardEntryPoints;
2 changes: 1 addition & 1 deletion packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type * from './config';
export type * from './utils';
export type * from './external/typeFest';
6 changes: 0 additions & 6 deletions packages/core/src/types/utils.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/src/utils/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Format, PkgJson } from '../types';
import type { Format, PackageJson } from '../types';
import { logger } from './logger';

export const getDefaultExtension = (options: {
format: Format;
pkgJson?: PkgJson;
pkgJson?: PackageJson;
autoExtension: boolean;
}): {
jsExtension: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs';
import fsP from 'node:fs/promises';
import path from 'node:path';
import color from 'picocolors';
import type { PkgJson } from '../types';
import type { PackageJson } from '../types';
import { logger } from './logger';

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ export async function calcLongestCommonPath(
return lca;
}

export const readPackageJson = (rootPath: string): undefined | PkgJson => {
export const readPackageJson = (rootPath: string): undefined | PackageJson => {
const pkgJsonPath = path.join(rootPath, './package.json');

if (!fs.existsSync(pkgJsonPath)) {
Expand Down