@sanity/pkg-utils 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/pkg-utils",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "author": "Sanity.io <hello@sanity.io>",
5
5
  "license": "MIT",
6
6
  "description": "Simple utilities for modern npm packages.",
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "scripts": {
35
- "build": "node -r esbuild-register src/cli.ts build --extract --tsconfig tsconfig.dist.json",
35
+ "build": "node -r esbuild-register src/cli build --extract --tsconfig tsconfig.dist.json",
36
36
  "format": "prettier --write --cache --ignore-unknown .",
37
37
  "lint": "eslint . --ext .cjs,.js,.ts",
38
38
  "prebuild": "rimraf dist",
@@ -52,6 +52,7 @@
52
52
  "chalk": "^4.1.2",
53
53
  "esbuild": "^0.15.5",
54
54
  "esbuild-register": "^3.3.3",
55
+ "find-config": "^1.0.0",
55
56
  "jsonc-parser": "^3.1.0",
56
57
  "mkdirp": "^1.0.4",
57
58
  "pkg-up": "^3.1.0",
@@ -64,6 +65,7 @@
64
65
  "@commitlint/config-conventional": "^17.0.3",
65
66
  "@sanity/semantic-release-preset": "^2.0.0",
66
67
  "@types/cpx": "^1.5.2",
68
+ "@types/find-config": "^1.0.1",
67
69
  "@types/mkdirp": "^1.0.2",
68
70
  "@types/node": "^18.7.13",
69
71
  "@types/rimraf": "^3.0.2",
@@ -1,13 +1,13 @@
1
1
  import path from 'path'
2
- import {PackageSubPathExportEntry} from '../core'
2
+ import {PackageSubPathExportEntry, _DEFAULTS} from '../core'
3
3
  import {_BuildContext} from './_types'
4
- import {_RollupTask, _BuildTask, _DtsTask} from './tasks'
4
+ import {_BuildTask, _DtsTask, _RollupTask} from './tasks'
5
5
 
6
6
  /**
7
7
  * @internal
8
8
  */
9
9
  export function _parseTasks(ctx: _BuildContext): _BuildTask[] {
10
- const {pkg} = ctx
10
+ const {config, pkg} = ctx
11
11
 
12
12
  const entries = Object.entries(ctx.exports || {}).map(
13
13
  ([path, entry]) => ({path, ...entry} as PackageSubPathExportEntry & {path: string})
@@ -35,6 +35,8 @@ export function _parseTasks(ctx: _BuildContext): _BuildTask[] {
35
35
 
36
36
  // Parse `rollup` tasks
37
37
  for (const entry of entries) {
38
+ const runtime = entry.runtime || config?.runtime || 'web'
39
+
38
40
  const format: ('commonjs' | 'esm')[] = []
39
41
  const target: string[] = []
40
42
 
@@ -46,23 +48,23 @@ export function _parseTasks(ctx: _BuildContext): _BuildTask[] {
46
48
  format.push('esm')
47
49
  }
48
50
 
49
- if (entry.runtime === 'node') {
50
- target.push('node12')
51
+ if (runtime === 'node') {
52
+ target.push(...(config?.target?.node || _DEFAULTS.target.node))
51
53
  }
52
54
 
53
- if (entry.runtime === 'web') {
54
- target.push('chrome104')
55
+ if (runtime === 'web') {
56
+ target.push(...(config?.target?.web || _DEFAULTS.target.web))
55
57
  }
56
58
 
57
59
  for (const f of format) {
58
- const buildId = `${entry.runtime}:${f}:${target.join(',')}`
60
+ const buildId = `${runtime}:${f}:${target.join(',')}`
59
61
 
60
62
  if (!rollupTasks[buildId]) {
61
63
  rollupTasks[buildId] = {
62
64
  type: 'rollup',
63
65
  buildId,
64
66
  entries: [entry],
65
- runtime: entry.runtime,
67
+ runtime,
66
68
  format: f,
67
69
  target,
68
70
  }
@@ -19,11 +19,6 @@ export async function build(options: {
19
19
  const pkg = await _loadPkg({cwd})
20
20
  const config = await _loadConfig({cwd})
21
21
 
22
- // if (!config) {
23
- // console.log('no configuration found')
24
- // return
25
- // }
26
-
27
22
  const parsedExports = _parseExports(pkg).reduce<PackageExports>((acc, x) => {
28
23
  const {path: exportPath, ...exportEntry} = x
29
24
 
@@ -25,6 +25,7 @@ export async function _buildTypes(options: {
25
25
 
26
26
  const compilerOptions: ts.CompilerOptions = {
27
27
  ...config.options,
28
+ declaration: true,
28
29
  emitDeclarationOnly: true,
29
30
  outDir,
30
31
  }
@@ -13,7 +13,6 @@ import {_createTSDocConfig} from './_createTSDocConfig'
13
13
  export async function _extractTypes(options: {
14
14
  cwd: string
15
15
  distPath: string
16
- // exportId: string
17
16
  exportPath: string
18
17
  files: _BuildFile[]
19
18
  projectPath: string
@@ -27,16 +27,15 @@ export interface _DtsTask {
27
27
  export async function _dtsTask(ctx: _BuildContext, task: _DtsTask): Promise<void> {
28
28
  const {cwd, dist, files, tsconfig} = ctx
29
29
  const exportPath = task.exportPath === '.' ? './index' : task.exportPath
30
+ const distPath = path.resolve(cwd, dist)
31
+ const tmpPath = path.resolve(cwd, dist, 'tmp')
32
+ const tsconfigPath = path.resolve(cwd, tsconfig || 'tsconfig.json')
30
33
 
31
34
  console.log('=================================================================================')
32
35
  console.log(chalk.gray('task '), 'd.ts')
33
36
  console.log(chalk.gray('source '), path.join('', task.source))
34
37
  console.log(chalk.gray('export '), path.join(dist, `${exportPath}.d.ts`))
35
-
36
- const distPath = path.resolve(cwd, dist)
37
- const tmpPath = path.resolve(cwd, dist, 'tmp')
38
-
39
- const tsconfigPath = path.resolve(cwd, tsconfig || 'tsconfig.json')
38
+ console.log(chalk.gray('tsconfig '), path.relative(cwd, tsconfigPath))
40
39
 
41
40
  const types = await _buildTypes({
42
41
  cwd,
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @internal
3
+ */
4
+ export const _DEFAULTS = {
5
+ target: {
6
+ node: ['node12'],
7
+ web: ['chrome58', 'edge16', 'firefox54', 'opera44', 'safari11'],
8
+ },
9
+ }
@@ -1,5 +1,6 @@
1
1
  import path from 'path'
2
2
  import {register} from 'esbuild-register/dist/node'
3
+ import findConfig from 'find-config'
3
4
  import pkgUp from 'pkg-up'
4
5
  import {PkgConfigOptions} from './types'
5
6
 
@@ -15,16 +16,40 @@ export async function _loadConfig(options: {cwd: string}): Promise<PkgConfigOpti
15
16
 
16
17
  const root = path.dirname(pkgPath)
17
18
 
19
+ const configFile = _findConfigFile(root)
20
+
21
+ if (!configFile) {
22
+ return undefined
23
+ }
24
+
18
25
  try {
19
- const {unregister} = register({extensions: ['.js', '.ts']})
26
+ const {unregister} = configFile.transform
27
+ ? register({extensions: ['.js', '.ts']})
28
+ : {unregister: () => undefined}
20
29
 
21
30
  // eslint-disable-next-line @typescript-eslint/no-var-requires
22
- const mod = require(path.resolve(root, 'package.config.ts'))
31
+ const mod = require(configFile.path)
23
32
 
24
33
  unregister()
25
34
 
26
- return mod.default
35
+ return mod?.default || mod || undefined
27
36
  } catch (_) {
28
37
  return undefined
29
38
  }
30
39
  }
40
+
41
+ // Looks for:
42
+ // 1. package.config.ts
43
+ // 2. package.config.cjs
44
+ //
45
+ function _findConfigFile(cwd: string): {path: string; transform: boolean} | null {
46
+ const ts = findConfig('package.config.ts', {cwd})
47
+
48
+ if (ts) return {path: ts, transform: true}
49
+
50
+ const cjs = findConfig('package.config.cjs', {cwd})
51
+
52
+ if (cjs) return {path: cjs, transform: false}
53
+
54
+ return null
55
+ }
@@ -2,7 +2,7 @@
2
2
  * @public
3
3
  */
4
4
  export interface PackageSubPathExportEntry {
5
- runtime: 'node' | 'web'
5
+ runtime?: 'node' | 'web'
6
6
 
7
7
  // paths
8
8
  source: string
@@ -41,8 +41,16 @@ export interface PkgConfigOptions {
41
41
  * Packages to exclude from the bundles.
42
42
  */
43
43
  external?: ConfigProperty<string[]>
44
+ /**
45
+ * Default runtime of package exports
46
+ */
47
+ runtime?: 'node' | 'web'
44
48
  /**
45
49
  * Directory of source files.
46
50
  */
47
51
  src?: string
52
+ target?: {
53
+ node?: string[]
54
+ web?: string[]
55
+ }
48
56
  }
package/src/core/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './_defaults'
1
2
  export * from './_isRecord'
2
3
  export * from './config'
3
4
  export * from './pkg'
@@ -8,7 +8,7 @@ import {_PackageJSON} from './_types'
8
8
  export function _parseExports(pkg: _PackageJSON): (PackageSubPathExportEntry & {path: string})[] {
9
9
  const rootExport: PackageSubPathExportEntry & {path: string} = {
10
10
  path: '.',
11
- runtime: 'web',
11
+ runtime: undefined,
12
12
 
13
13
  source: pkg.source || 'index.js',
14
14
  require: pkg.main,
@@ -26,7 +26,6 @@ export function _parseExports(pkg: _PackageJSON): (PackageSubPathExportEntry & {
26
26
  } else {
27
27
  extraExports.push({
28
28
  path: exportPath,
29
- runtime: 'web',
30
29
  source: exportEntry.source,
31
30
  ...exportEntry,
32
31
  })