@sanity/pkg-utils 1.1.0 → 1.3.1

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/dist/cli.cjs CHANGED
@@ -1,14 +1,16 @@
1
1
  'use strict';
2
2
 
3
3
  var cac = require('cac');
4
- var build = require('./_build-f5ddd6ac.cjs');
4
+ var build = require('./_build-5bf997de.cjs');
5
5
  require('path');
6
6
  require('chalk');
7
7
  require('fs');
8
8
  require('module');
9
9
  require('esbuild');
10
10
  require('process');
11
+ require('find-config');
11
12
  require('pkg-up');
13
+ require('browserslist');
12
14
  require('util');
13
15
  require('rimraf');
14
16
  require('typescript');
package/dist/cli.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cli.cjs","sources":["../src/cli.ts"],"sourcesContent":["import cac from 'cac'\nimport {build} from './build'\n\nconst cli = cac()\n\ncli\n .command('build', 'Build package')\n .option('--extract', 'Extract API reference')\n .option('--tsconfig [tsconfig]', '[string] tsconfig.json')\n .action(async (options) => {\n await build({\n cwd: process.cwd(),\n extract: options.extract ? Boolean(options.extract) : undefined,\n tsconfig: options.tsconfig,\n })\n })\n\n// Display help message when `-h` or `--help` appears\ncli.help()\n\n// Display version number when `-v` or `--version` appears\n// It's also used in help message\ncli.version('0.0.0')\n\ncli.parse()\n"],"names":["cac","build"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAM,GAAG,GAAGA,uBAAG,EAAE,CAAC;AAClB,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC,MAAM,CAAC,WAAW,EAAE,uBAAuB,CAAC,CAAC,MAAM,CAAC,uBAAuB,EAAE,wBAAwB,CAAC,CAAC,MAAM,CAAC,OAAO,OAAO,KAAK;AACvK,EAAE,MAAMC,WAAK,CAAC;AACd,IAAI,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;AACtB,IAAI,OAAO,EAAE,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;AAChE,IAAI,QAAQ,EAAE,OAAO,CAAC,QAAQ;AAC9B,GAAG,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AACH,GAAG,CAAC,IAAI,EAAE,CAAC;AACX,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACrB,GAAG,CAAC,KAAK,EAAE;;"}
1
+ {"version":3,"file":"cli.cjs","sources":["../src/cli.ts"],"sourcesContent":["import cac from 'cac'\nimport {build} from './build'\n\nconst cli = cac()\n\ncli\n .command('build', 'Build package')\n .option('--extract', 'Extract API reference')\n .option('--tsconfig [tsconfig]', '[string] tsconfig.json')\n .action(async (options) => {\n await build({\n cwd: process.cwd(),\n extract: options.extract ? Boolean(options.extract) : undefined,\n tsconfig: options.tsconfig,\n })\n })\n\n// Display help message when `-h` or `--help` appears\ncli.help()\n\n// Display version number when `-v` or `--version` appears\n// It's also used in help message\ncli.version('0.0.0')\n\ncli.parse()\n"],"names":["cac","build"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAM,GAAG,GAAGA,uBAAG,EAAE,CAAC;AAClB,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC,MAAM,CAAC,WAAW,EAAE,uBAAuB,CAAC,CAAC,MAAM,CAAC,uBAAuB,EAAE,wBAAwB,CAAC,CAAC,MAAM,CAAC,OAAO,OAAO,KAAK;AACvK,EAAE,MAAMC,WAAK,CAAC;AACd,IAAI,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;AACtB,IAAI,OAAO,EAAE,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;AAChE,IAAI,QAAQ,EAAE,OAAO,CAAC,QAAQ;AAC9B,GAAG,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AACH,GAAG,CAAC,IAAI,EAAE,CAAC;AACX,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACrB,GAAG,CAAC,KAAK,EAAE;;"}
package/dist/index.cjs CHANGED
@@ -1,13 +1,15 @@
1
1
  'use strict';
2
2
 
3
- var build = require('./_build-f5ddd6ac.cjs');
3
+ var build = require('./_build-5bf997de.cjs');
4
4
  require('path');
5
5
  require('chalk');
6
6
  require('fs');
7
7
  require('module');
8
8
  require('esbuild');
9
9
  require('process');
10
+ require('find-config');
10
11
  require('pkg-up');
12
+ require('browserslist');
11
13
  require('util');
12
14
  require('rimraf');
13
15
  require('typescript');
@@ -27,6 +29,7 @@ function defineConfig(configOptions) {
27
29
  return configOptions;
28
30
  }
29
31
 
32
+ exports._DEFAULTS = build._DEFAULTS;
30
33
  exports._isRecord = build._isRecord;
31
34
  exports._loadConfig = build._loadConfig;
32
35
  exports._loadPkg = build._loadPkg;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/core/config/defineConfig.ts"],"sourcesContent":["import {PkgConfigOptions} from './types'\n\n/**\n * @public\n */\nexport function defineConfig(configOptions: PkgConfigOptions): PkgConfigOptions {\n return configOptions\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAO,SAAS,YAAY,CAAC,aAAa,EAAE;AAC5C,EAAE,OAAO,aAAa,CAAC;AACvB;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/core/config/defineConfig.ts"],"sourcesContent":["import {PkgConfigOptions} from './types'\n\n/**\n * @public\n */\nexport function defineConfig(configOptions: PkgConfigOptions): PkgConfigOptions {\n return configOptions\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAO,SAAS,YAAY,CAAC,aAAa,EAAE;AAC5C,EAAE,OAAO,aAAa,CAAC;AACvB;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -20,6 +20,10 @@ export declare interface _BuildContext {
20
20
  dist: string;
21
21
  pkg: _PackageJSON;
22
22
  src: string;
23
+ target: {
24
+ node: string[];
25
+ web: string[];
26
+ };
23
27
  tsconfig?: string;
24
28
  }
25
29
 
@@ -41,6 +45,13 @@ export declare type ConfigProperty<T> = ConfigPropertyResolver<T> | T;
41
45
  */
42
46
  export declare type ConfigPropertyResolver<T> = (prev: T) => T;
43
47
 
48
+ /**
49
+ * @internal
50
+ */
51
+ export declare const _DEFAULTS: {
52
+ browserslist: string[];
53
+ };
54
+
44
55
  /**
45
56
  * @public
46
57
  */
@@ -89,13 +100,14 @@ export declare interface _PackageJSON {
89
100
  source?: string;
90
101
  module?: string;
91
102
  types?: string;
103
+ browserslist?: string[];
92
104
  }
93
105
 
94
106
  /**
95
107
  * @public
96
108
  */
97
109
  export declare interface PackageSubPathExportEntry {
98
- runtime: 'node' | 'web';
110
+ runtime?: 'node' | 'web';
99
111
  source: string;
100
112
  require?: string;
101
113
  default?: string;
@@ -122,6 +134,10 @@ export declare interface PkgConfigOptions {
122
134
  * Packages to exclude from the bundles.
123
135
  */
124
136
  external?: ConfigProperty<string[]>;
137
+ /**
138
+ * Default runtime of package exports
139
+ */
140
+ runtime?: 'node' | 'web';
125
141
  /**
126
142
  * Directory of source files.
127
143
  */
@@ -131,6 +147,6 @@ export declare interface PkgConfigOptions {
131
147
  /**
132
148
  * @internal
133
149
  */
134
- export declare function _resolveConfigProperty<T>(prop: ConfigProperty<T>, initialValue: T): T;
150
+ export declare function _resolveConfigProperty<T>(prop: ConfigProperty<T> | undefined, initialValue: T): T;
135
151
 
136
152
  export { }
package/dist/index.js CHANGED
@@ -4,7 +4,9 @@ import require$$1 from 'fs';
4
4
  import require$$2 from 'module';
5
5
  import require$$3 from 'esbuild';
6
6
  import require$$4 from 'process';
7
+ import findConfig from 'find-config';
7
8
  import pkgUp from 'pkg-up';
9
+ import browserslist from 'browserslist';
8
10
  import { promisify } from 'util';
9
11
  import rimrafCallback from 'rimraf';
10
12
  import ts from 'typescript';
@@ -20,6 +22,15 @@ import json from '@rollup/plugin-json';
20
22
  import { nodeResolve } from '@rollup/plugin-node-resolve';
21
23
  import esbuild from 'rollup-plugin-esbuild';
22
24
 
25
+ const _DEFAULTS = {
26
+ browserslist: [
27
+ "> 0.2% and supports es6-module and supports es6-module-dynamic-import",
28
+ "not dead",
29
+ "not IE 11",
30
+ "maintained node versions"
31
+ ]
32
+ };
33
+
23
34
  function _isRecord(value) {
24
35
  return Boolean(value) && !Array.isArray(value) && typeof value === "object";
25
36
  }
@@ -4894,17 +4905,32 @@ async function _loadConfig(options) {
4894
4905
  if (!pkgPath)
4895
4906
  return void 0;
4896
4907
  const root = path.dirname(pkgPath);
4908
+ const configFile = _findConfigFile(root);
4909
+ if (!configFile) {
4910
+ return void 0;
4911
+ }
4897
4912
  try {
4898
- const { unregister } = node.register({ extensions: [".js", ".ts"] });
4899
- const mod = require(path.resolve(root, "package.config.ts"));
4913
+ const { unregister } = configFile.transform ? node.register({ extensions: [".js", ".ts"] }) : { unregister: () => void 0 };
4914
+ const mod = require(configFile.path);
4900
4915
  unregister();
4901
- return mod.default;
4916
+ return (mod == null ? void 0 : mod.default) || mod || void 0;
4902
4917
  } catch (_) {
4903
4918
  return void 0;
4904
4919
  }
4905
4920
  }
4921
+ function _findConfigFile(cwd) {
4922
+ const ts = findConfig("package.config.ts", { cwd });
4923
+ if (ts)
4924
+ return { path: ts, transform: true };
4925
+ const cjs = findConfig("package.config.cjs", { cwd });
4926
+ if (cjs)
4927
+ return { path: cjs, transform: false };
4928
+ return null;
4929
+ }
4906
4930
 
4907
4931
  function _resolveConfigProperty(prop, initialValue) {
4932
+ if (!prop)
4933
+ return initialValue;
4908
4934
  if (typeof prop === "function") {
4909
4935
  return prop(initialValue);
4910
4936
  }
@@ -4924,7 +4950,7 @@ async function _loadPkg(options) {
4924
4950
  function _parseExports(pkg) {
4925
4951
  const rootExport = {
4926
4952
  path: ".",
4927
- runtime: "web",
4953
+ runtime: void 0,
4928
4954
  source: pkg.source || "index.js",
4929
4955
  require: pkg.main,
4930
4956
  default: pkg.module,
@@ -4939,7 +4965,6 @@ function _parseExports(pkg) {
4939
4965
  } else {
4940
4966
  extraExports.push({
4941
4967
  path: exportPath,
4942
- runtime: "web",
4943
4968
  source: exportEntry.source,
4944
4969
  ...exportEntry
4945
4970
  });
@@ -4952,8 +4977,58 @@ function _parseExports(pkg) {
4952
4977
  return [rootExport, ...extraExports];
4953
4978
  }
4954
4979
 
4980
+ function _sortAsNumber(a, b) {
4981
+ const aVersion = Number(a);
4982
+ const bVersion = Number(b);
4983
+ return aVersion - bVersion;
4984
+ }
4985
+ function _parseBrowserslistVersions(query) {
4986
+ const result = browserslist(query);
4987
+ const entries = result.map((key) => {
4988
+ const [name, version] = key.split(" ");
4989
+ return { name, version: version.split("-")[0].split(".")[0] };
4990
+ });
4991
+ const andChr = entries.filter((b) => b.name === "and_chr");
4992
+ const andFf = entries.filter((b) => b.name === "and_ff");
4993
+ const chrome = entries.filter((b) => b.name === "chrome");
4994
+ const edge = entries.filter((b) => b.name === "edge");
4995
+ const ff = entries.filter((b) => b.name === "firefox");
4996
+ const iosSaf = entries.filter((b) => b.name === "ios_saf");
4997
+ const saf = entries.filter((b) => b.name === "safari");
4998
+ const opera = entries.filter((b) => b.name === "opera");
4999
+ const node = entries.filter((b) => b.name === "node");
5000
+ const versions = {
5001
+ chrome: andChr.concat(chrome).map((b) => b.version),
5002
+ ff: andFf.concat(ff).map((b) => b.version),
5003
+ edge: edge.map((b) => b.version),
5004
+ iosSaf: iosSaf.map((b) => b.version),
5005
+ saf: saf.map((b) => b.version),
5006
+ opera: opera.map((b) => b.version),
5007
+ node: node.map((b) => b.version)
5008
+ };
5009
+ versions.chrome.sort(_sortAsNumber);
5010
+ versions.ff.sort(_sortAsNumber);
5011
+ versions.edge.sort(_sortAsNumber);
5012
+ versions.iosSaf.sort(_sortAsNumber);
5013
+ versions.saf.sort(_sortAsNumber);
5014
+ versions.opera.sort(_sortAsNumber);
5015
+ versions.node.sort(_sortAsNumber);
5016
+ return versions;
5017
+ }
5018
+
5019
+ function _parseNodeTarget(versions) {
5020
+ const target = [];
5021
+ if (versions.node.length) {
5022
+ target.push(`node${versions.node[0]}`);
5023
+ }
5024
+ if (target.length === 0) {
5025
+ return void 0;
5026
+ }
5027
+ return target;
5028
+ }
5029
+
4955
5030
  function _parseTasks(ctx) {
4956
- const { pkg } = ctx;
5031
+ const { config, pkg } = ctx;
4957
5032
  const entries = Object.entries(ctx.exports || {}).map(
4958
5033
  ([path2, entry]) => ({ path: path2, ...entry })
4959
5034
  );
@@ -4974,6 +5049,7 @@ function _parseTasks(ctx) {
4974
5049
  }
4975
5050
  }
4976
5051
  for (const entry of entries) {
5052
+ const runtime = entry.runtime || (config == null ? void 0 : config.runtime) || "web";
4977
5053
  const format = [];
4978
5054
  const target = [];
4979
5055
  if (entry.require) {
@@ -4982,20 +5058,20 @@ function _parseTasks(ctx) {
4982
5058
  if (entry.default) {
4983
5059
  format.push("esm");
4984
5060
  }
4985
- if (entry.runtime === "node") {
4986
- target.push("node12");
5061
+ if (runtime === "node") {
5062
+ target.push(...ctx.target.node);
4987
5063
  }
4988
- if (entry.runtime === "web") {
4989
- target.push("chrome104");
5064
+ if (runtime === "web") {
5065
+ target.push(...ctx.target.web);
4990
5066
  }
4991
5067
  for (const f of format) {
4992
- const buildId = `${entry.runtime}:${f}:${target.join(",")}`;
5068
+ const buildId = `${runtime}:${f}:${target.join(",")}`;
4993
5069
  if (!rollupTasks[buildId]) {
4994
5070
  rollupTasks[buildId] = {
4995
5071
  type: "rollup",
4996
5072
  buildId,
4997
5073
  entries: [entry],
4998
- runtime: entry.runtime,
5074
+ runtime,
4999
5075
  format: f,
5000
5076
  target
5001
5077
  };
@@ -5014,6 +5090,32 @@ function _parseTasks(ctx) {
5014
5090
  return tasks;
5015
5091
  }
5016
5092
 
5093
+ function _parseWebTarget(versions) {
5094
+ const target = [];
5095
+ if (versions.chrome.length) {
5096
+ target.push(`chrome${versions.chrome[0]}`);
5097
+ }
5098
+ if (versions.ff.length) {
5099
+ target.push(`firefox${versions.ff[0]}`);
5100
+ }
5101
+ if (versions.edge.length) {
5102
+ target.push(`edge${versions.edge[0]}`);
5103
+ }
5104
+ if (versions.iosSaf.length) {
5105
+ target.push(`ios${versions.iosSaf[0]}`);
5106
+ }
5107
+ if (versions.saf.length) {
5108
+ target.push(`safari${versions.saf[0]}`);
5109
+ }
5110
+ if (versions.opera.length) {
5111
+ target.push(`opera${versions.opera[0]}`);
5112
+ }
5113
+ if (target.length === 0) {
5114
+ return void 0;
5115
+ }
5116
+ return target;
5117
+ }
5118
+
5017
5119
  async function _loadTSConfig(options) {
5018
5120
  const { cwd, tsconfigPath } = options;
5019
5121
  const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
@@ -5368,14 +5470,23 @@ async function build(options) {
5368
5470
  const { path: exportPath, ...exportEntry } = x;
5369
5471
  return { ...acc, [exportPath]: exportEntry };
5370
5472
  }, {});
5371
- const exports = (config == null ? void 0 : config.exports) ? _resolveConfigProperty(config.exports, parsedExports) : parsedExports;
5372
- const initialExternal = [
5473
+ const exports = _resolveConfigProperty(config == null ? void 0 : config.exports, parsedExports);
5474
+ const parsedExternal = [
5373
5475
  ...pkg.dependencies ? Object.keys(pkg.dependencies) : [],
5374
5476
  ...pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []
5375
5477
  ];
5376
- const external = (config == null ? void 0 : config.external) ? _resolveConfigProperty(config.external, initialExternal) : initialExternal;
5478
+ const external = _resolveConfigProperty(config == null ? void 0 : config.external, parsedExternal);
5479
+ const targetVersions = _parseBrowserslistVersions(pkg.browserslist || _DEFAULTS.browserslist);
5377
5480
  const srcPath = path.resolve(cwd, (config == null ? void 0 : config.src) || "src");
5378
5481
  const distPath = path.resolve(cwd, (config == null ? void 0 : config.dist) || "dist");
5482
+ const nodeTarget = _parseNodeTarget(targetVersions);
5483
+ const webTarget = _parseWebTarget(targetVersions);
5484
+ if (!nodeTarget) {
5485
+ throw new Error("no matching `node` target");
5486
+ }
5487
+ if (!webTarget) {
5488
+ throw new Error("no matching `web` target");
5489
+ }
5379
5490
  const context = {
5380
5491
  config,
5381
5492
  cwd,
@@ -5386,6 +5497,7 @@ async function build(options) {
5386
5497
  dist: path.relative(cwd, distPath),
5387
5498
  pkg,
5388
5499
  src: path.relative(cwd, srcPath),
5500
+ target: { node: nodeTarget, web: webTarget },
5389
5501
  tsconfig
5390
5502
  };
5391
5503
  console.log(chalk.gray("cwd "), context.cwd);
@@ -5410,5 +5522,5 @@ async function build(options) {
5410
5522
  }
5411
5523
  }
5412
5524
 
5413
- export { _isRecord, _loadConfig, _loadPkg, _parseExports, _resolveConfigProperty, build, defineConfig };
5525
+ export { _DEFAULTS, _isRecord, _loadConfig, _loadPkg, _parseExports, _resolveConfigProperty, build, defineConfig };
5414
5526
  //# sourceMappingURL=index.js.map