@sanity/pkg-utils 1.1.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.1.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.",
@@ -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
 
@@ -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
  })