Skip to content
Draft
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
19 changes: 19 additions & 0 deletions playground/conditional-export-typings/package.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {defineConfig} from '@sanity/pkg-utils'

export default defineConfig({
tsconfig: 'tsconfig.dist.json',
bundles: [
{
source: './src/index.ts',
require: './dist/index.node.cjs',
import: './dist/index.node.js',
runtime: 'node',
},
{
source: './src/middleware/index.ts',
require: './dist/middleware/index.node.cjs',
import: './dist/middleware/index.node.js',
runtime: 'node',
},
],
})
78 changes: 78 additions & 0 deletions playground/conditional-export-typings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"name": "conditional-export-typings",
"version": "0.0.0-development",
"private": true,
"license": "MIT",
"sideEffects": false,
"type": "module",
"imports": {
"#env": {
"api-extractor": {
"browser": "./dist/__tmp__/src/env.browser.ts",
"node": "./dist/__tmp__/src/env.node.ts",
"default": "./dist/__tmp__/src/env.default.ts"
},
"browser": "./src/env.browser.ts",
"node": "./src/env.node.ts",
"default": "./src/env.default.ts"
},
"#url": {
"api-extractor": {
"browser": "./dist/__tmp__/src/middleware/url.browser.ts",
"node": "./dist/__tmp__/src/middleware/url.node.ts",
"default": "./dist/__tmp__/src/middleware/url.default.ts"
},
"browser": "./src/middleware/url.browser.ts",
"node": "./src/middleware/url.node.ts",
"default": "./src/middleware/url.default.ts"
}
},
"exports": {
".": {
"source": "./src/index.ts",
"browser": {
"source": "./src/index.ts",
"import": "./dist/index.browser.js",
"require": "./dist/index.browser.cjs"
},
"node": {
"import": "./dist/index.node.js",
"require": "./dist/index.node.cjs"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./middleware": {
"source": "./src/middleware/index.ts",
"browser": {
"source": "./src/middleware/index.ts",
"import": "./dist/middleware/index.browser.js",
"require": "./dist/middleware/index.browser.cjs"
},
"node": {
"import": "./dist/middleware/index.node.js",
"require": "./dist/middleware/index.node.cjs"
},
"import": "./dist/middleware/index.js",
"require": "./dist/middleware/index.cjs",
"default": "./dist/middleware/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"middleware": [
"./dist/middleware.d.ts"
]
}
},
"files": [
"dist"
],
"scripts": {
"build": "pkg build --strict --check --clean"
}
}
4 changes: 4 additions & 0 deletions playground/conditional-export-typings/src/env.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type {Env} from './types'

/** @public */
export const env = 'browser' satisfies Env
4 changes: 4 additions & 0 deletions playground/conditional-export-typings/src/env.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type {Env} from './types'

/** @public */
export const env: Env = 'default'
4 changes: 4 additions & 0 deletions playground/conditional-export-typings/src/env.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type {Env} from './types'

/** @public */
export const env = 'node' satisfies Env
1 change: 1 addition & 0 deletions playground/conditional-export-typings/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {env} from '#env'
1 change: 1 addition & 0 deletions playground/conditional-export-typings/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {env} from './env'
2 changes: 2 additions & 0 deletions playground/conditional-export-typings/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {env} from '../env'
export {parse} from '#url'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @public */
export function parse(unsafe: string) {
return new URL(unsafe)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @public */
export function parse(unsafe: string, origin?: string) {
return new URL(unsafe, origin)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as url from 'node:url'

/** @public */
export function parse(unsafe: string) {
return url.parse(unsafe, true)
}
1 change: 1 addition & 0 deletions playground/conditional-export-typings/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Env = 'browser' | 'node' | 'default'
9 changes: 9 additions & 0 deletions playground/conditional-export-typings/tsconfig.dist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@sanity/pkg-utils/tsconfig/strictest.json",
"include": ["./src"],
"compilerOptions": {
"rootDir": ".",
"outDir": "./dist",
"emitDeclarationOnly": true
}
}
8 changes: 8 additions & 0 deletions playground/conditional-export-typings/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@sanity/pkg-utils/tsconfig/strictest.json",
"include": ["./package.config.ts", "./src"],
"compilerOptions": {
"rootDir": ".",
"noEmit": true
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions src/node/core/pkg/loadPkgWithReporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,27 @@ export async function loadPkgWithReporting(options: {
}

if (exp.node) {
if (exp.import && exp.node.import && !assertOrder('node', 'import', keys)) {
if (
exp.import &&
typeof exp.node !== 'string' &&
exp.node.import &&
!assertOrder('node', 'import', keys)
) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node\` property should come before the \`import\` property`,
)
}

if (exp.node.module) {
if (typeof exp.node !== 'string' && exp.node.module) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node.module\` condition shouldn't be used as it's not well supported in all bundlers. A better strategy is to refactor the codebase to no longer be vulnerable to the "dual package hazard"`,
)
}

if (
typeof exp.node !== 'string' &&
!exp.node.source &&
exp.node.import &&
(exp.node.require || exp.require) &&
Expand All @@ -79,12 +85,22 @@ export async function loadPkgWithReporting(options: {
)
}

if (exp.require && exp.node.require && exp.require === exp.node.require) {
if (
typeof exp.node !== 'string' &&
exp.require &&
exp.node.require &&
exp.require === exp.node.require
) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node.require\` property isn't necessary as it's identical to \`require\``,
)
} else if (exp.require && exp.node.require && !assertOrder('node', 'require', keys)) {
} else if (
typeof exp.node !== 'string' &&
exp.require &&
exp.node.require &&
!assertOrder('node', 'require', keys)
) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node\` property should come before the \`require\` property`,
Expand Down
29 changes: 18 additions & 11 deletions src/node/core/pkg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ export interface PackageJSON {
dependencies?: Record<string, string | undefined>
devDependencies?: Record<string, string | undefined>
peerDependencies?: Record<string, string | undefined>
imports?:
| Partial<Record<`#${string}`, string | Record<string, string | Record<string, string>>>>
| undefined
exports?: Record<
string,
| `./${string}.json`
| `./${string}.css`
| {
source?: string
types?: string
browser?: {
source: string
import?: string
require?: string
}
node?: {
source?: string
module?: string
import?: string
require?: string
}
browser?:
| {
source?: string
import?: string
require?: string
}
| string
node?:
| {
source?: string
module?: string
import?: string
require?: string
}
| string
module?: string
import?: string
require?: string
Expand Down
32 changes: 22 additions & 10 deletions src/node/core/pkg/validatePkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const pkgSchema = z.object({
browser: z.optional(z.record(z.string())),
module: z.optional(z.string()),
types: z.optional(z.string()),
imports: z.optional(
z.record(
z.custom<`#${string}`>((val) => typeof val === 'string' && val.startsWith('#')),
z.record(z.union([z.record(z.string()), z.string()])),
),
),
exports: z.optional(
z.record(
z.union([
Expand All @@ -25,18 +31,24 @@ const pkgSchema = z.object({
types: z.optional(z.string()),
source: z.optional(z.string()),
browser: z.optional(
z.object({
source: z.string(),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
z.union([
z.object({
source: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
z.string(),
]),
),
node: z.optional(
z.object({
source: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
z.union([
z.object({
source: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
z.string(),
]),
),
import: z.optional(z.string()),
require: z.optional(z.string()),
Expand Down
4 changes: 4 additions & 0 deletions src/node/resolveBuildTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
exportPath: exp._path,
sourcePath: exp.source,
targetPaths: getTargetPaths(pkg.type, exp),
runtime: '*',
})
}

Expand All @@ -59,6 +60,7 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
exportPath: exp._path,
sourcePath: exp.browser.source,
targetPaths: getTargetPaths(pkg.type, exp.browser),
runtime: 'browser',
})
}

Expand All @@ -68,6 +70,7 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
exportPath: exp._path,
sourcePath: exp.node.source,
targetPaths: getTargetPaths(pkg.type, exp.node),
runtime: 'node',
})
}
}
Expand All @@ -86,6 +89,7 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
exportPath,
sourcePath: bundle.source,
targetPaths: getTargetPaths(pkg.type, bundle),
runtime: bundle.runtime || '*',
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/node/resolveWatchTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function resolveWatchTasks(ctx: BuildContext): WatchTask[] {
exportPath: exp._path,
sourcePath: exp.source,
targetPaths: getTargetPaths(pkg.type, exp),
runtime: '*',
})
}

Expand All @@ -56,6 +57,7 @@ export function resolveWatchTasks(ctx: BuildContext): WatchTask[] {
exportPath: exp._path,
sourcePath: exp.browser.source,
targetPaths: getTargetPaths(pkg.type, exp.browser),
runtime: 'browser',
})
}

Expand All @@ -65,6 +67,7 @@ export function resolveWatchTasks(ctx: BuildContext): WatchTask[] {
exportPath: exp._path,
sourcePath: exp.node.source,
targetPaths: getTargetPaths(pkg.type, exp.node),
runtime: 'node',
})
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/node/tasks/dts/buildTemporaryImportsPackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from 'node:fs/promises'
import path from 'node:path'

import type {BuildContext} from '../../core'

export async function buildTemporaryImportsPackageJson(
ctx: Pick<BuildContext, 'pkg'>,
tmpPath: string,
): Promise<void> {
// eslint-disable-next-line no-console
console.log(ctx, tmpPath)
// eslint-disable-next-line no-debugger
debugger

const {imports} = ctx.pkg

await fs.writeFile(path.join(tmpPath, 'package.json'), JSON.stringify({imports}, null, 2))
// eslint-disable-next-line no-debugger
debugger
}
Loading
Loading