@sanity/pkg-utils 4.0.0 → 4.1.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/dist/_chunks/{build-xtvcN4rn.cjs → build-N7HnlT8k.cjs} +5 -4
- package/dist/_chunks/{build-xtvcN4rn.cjs.map → build-N7HnlT8k.cjs.map} +1 -1
- package/dist/_chunks/{buildAction-lOl6hAY8.cjs → buildAction-yqNgk-aq.cjs} +2 -2
- package/dist/_chunks/{buildAction-lOl6hAY8.cjs.map → buildAction-yqNgk-aq.cjs.map} +1 -1
- package/dist/_chunks/{check-5On74q2h.cjs → check-j5YR3hUi.cjs} +4 -3
- package/dist/_chunks/check-j5YR3hUi.cjs.map +1 -0
- package/dist/_chunks/{checkAction-RYG9-Wqo.cjs → checkAction-AD9RJftD.cjs} +2 -2
- package/dist/_chunks/{checkAction-RYG9-Wqo.cjs.map → checkAction-AD9RJftD.cjs.map} +1 -1
- package/dist/_chunks/{consoleSpy-bV4ZI2h8.cjs → consoleSpy-HqQIfX7k.cjs} +112 -73
- package/dist/_chunks/consoleSpy-HqQIfX7k.cjs.map +1 -0
- package/dist/_chunks/{index-YSy1xob6.cjs → index-0-iA8gVL.cjs} +4 -3
- package/dist/_chunks/{index-YSy1xob6.cjs.map → index-0-iA8gVL.cjs.map} +1 -1
- package/dist/_chunks/{init-tT7f7xkO.cjs → init-9DSt45GN.cjs} +3 -2
- package/dist/_chunks/{init-tT7f7xkO.cjs.map → init-9DSt45GN.cjs.map} +1 -1
- package/dist/_chunks/{initAction-zkJJWu4U.cjs → initAction-sChm6v2i.cjs} +2 -2
- package/dist/_chunks/{initAction-zkJJWu4U.cjs.map → initAction-sChm6v2i.cjs.map} +1 -1
- package/dist/_chunks/{logger-c-JGqYo_.cjs → logger-L4V-Be7d.cjs} +2 -2
- package/dist/_chunks/logger-L4V-Be7d.cjs.map +1 -0
- package/dist/_chunks/{watch-Mo2KRBJf.cjs → watch-3unutrcJ.cjs} +5 -4
- package/dist/_chunks/{watch-Mo2KRBJf.cjs.map → watch-3unutrcJ.cjs.map} +1 -1
- package/dist/_chunks/{watchAction-7JsrPTEX.cjs → watchAction-0OCDBHAy.cjs} +2 -2
- package/dist/_chunks/{watchAction-7JsrPTEX.cjs.map → watchAction-0OCDBHAy.cjs.map} +1 -1
- package/dist/cli.cjs +5 -5
- package/dist/index.cjs +7 -7
- package/dist/index.d.ts +1 -1
- package/dist/index.js +108 -70
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
- package/src/node/core/defaults.ts +3 -6
- package/src/node/core/pkg/types.ts +1 -1
- package/src/node/core/pkg/validatePkg.ts +1 -1
- package/src/node/resolveBrowserTarget.ts +10 -28
- package/src/node/resolveBuildContext.ts +13 -2
- package/src/node/resolveNodeTarget.ts +2 -8
- package/dist/_chunks/check-5On74q2h.cjs.map +0 -1
- package/dist/_chunks/consoleSpy-bV4ZI2h8.cjs.map +0 -1
- package/dist/_chunks/logger-c-JGqYo_.cjs.map +0 -1
- package/src/node/resolveBrowserslistVersions.ts +0 -60
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import path$1, { relative, resolve, dirname } from 'path';
|
|
|
3
3
|
import pkgUp from 'pkg-up';
|
|
4
4
|
import findConfig from 'find-config';
|
|
5
5
|
import fs$1, { statSync } from 'fs';
|
|
6
|
+
import config from '@sanity/browserslist-config';
|
|
6
7
|
import fs, { writeFile, readFile, readdir, lstat } from 'fs/promises';
|
|
7
8
|
import { z, ZodError } from 'zod';
|
|
8
9
|
import chalk from 'chalk';
|
|
@@ -101,12 +102,7 @@ function resolveConfigProperty(prop, initialValue) {
|
|
|
101
102
|
return prop;
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
const DEFAULT_BROWSERSLIST_QUERY =
|
|
105
|
-
"> 0.2% and supports es6-module and supports es6-module-dynamic-import",
|
|
106
|
-
"not dead",
|
|
107
|
-
"not IE 11",
|
|
108
|
-
"maintained node versions"
|
|
109
|
-
];
|
|
105
|
+
const DEFAULT_BROWSERSLIST_QUERY = config;
|
|
110
106
|
|
|
111
107
|
function isRecord(value) {
|
|
112
108
|
return Boolean(value) && !Array.isArray(value) && typeof value === "object";
|
|
@@ -156,7 +152,7 @@ const pkgSchema = z.object({
|
|
|
156
152
|
])
|
|
157
153
|
)
|
|
158
154
|
),
|
|
159
|
-
browserslist: z.optional(z.array(z.string()))
|
|
155
|
+
browserslist: z.optional(z.union([z.string(), z.array(z.string())]))
|
|
160
156
|
});
|
|
161
157
|
function validatePkg(input) {
|
|
162
158
|
return pkgSchema.parse(input);
|
|
@@ -517,6 +513,99 @@ function createLogger() {
|
|
|
517
513
|
};
|
|
518
514
|
}
|
|
519
515
|
|
|
516
|
+
// convert the browserslist field in package.json to
|
|
517
|
+
// esbuild compatible array of browsers
|
|
518
|
+
function browserslistToEsbuild(browserslistConfig, options = {}) {
|
|
519
|
+
if (!browserslistConfig) {
|
|
520
|
+
// the path from where the script is run
|
|
521
|
+
const path = process.cwd();
|
|
522
|
+
|
|
523
|
+
// read config if none is passed
|
|
524
|
+
browserslistConfig = browserslist.loadConfig({ path, ...options });
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const SUPPORTED_ESBUILD_TARGETS = [
|
|
528
|
+
'es',
|
|
529
|
+
'chrome',
|
|
530
|
+
'edge',
|
|
531
|
+
'firefox',
|
|
532
|
+
'ios',
|
|
533
|
+
'node',
|
|
534
|
+
'safari',
|
|
535
|
+
'opera',
|
|
536
|
+
'ie',
|
|
537
|
+
];
|
|
538
|
+
|
|
539
|
+
// https://github.com/eBay/browserslist-config/issues/16#issuecomment-863870093
|
|
540
|
+
const UNSUPPORTED = ['android 4'];
|
|
541
|
+
|
|
542
|
+
const replaces = {
|
|
543
|
+
ios_saf: 'ios',
|
|
544
|
+
android: 'chrome',
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
const separator = ' ';
|
|
548
|
+
|
|
549
|
+
return (
|
|
550
|
+
browserslist(browserslistConfig, options)
|
|
551
|
+
// filter out the unsupported ones
|
|
552
|
+
.filter((b) => !UNSUPPORTED.some((u) => b.startsWith(u)))
|
|
553
|
+
// replaces safari TP with latest safari version
|
|
554
|
+
.map((b) => {
|
|
555
|
+
if (b === 'safari TP') {
|
|
556
|
+
return browserslist('last 1 safari version')[0]
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
return b
|
|
560
|
+
})
|
|
561
|
+
// transform into ['chrome', '88']
|
|
562
|
+
.map((b) => b.split(separator))
|
|
563
|
+
// replace the similar browser
|
|
564
|
+
.map((b) => {
|
|
565
|
+
if (replaces[b[0]]) {
|
|
566
|
+
b[0] = replaces[b[0]];
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return b
|
|
570
|
+
})
|
|
571
|
+
// 11.0-12.0 --> 11.0
|
|
572
|
+
.map((b) => {
|
|
573
|
+
if (b[1].includes('-')) {
|
|
574
|
+
b[1] = b[1].slice(0, b[1].indexOf('-'));
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
return b
|
|
578
|
+
})
|
|
579
|
+
// 11.0 --> 11
|
|
580
|
+
.map((b) => {
|
|
581
|
+
if (b[1].endsWith('.0')) {
|
|
582
|
+
b[1] = b[1].slice(0, -2);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return b
|
|
586
|
+
})
|
|
587
|
+
// removes invalid versions that will break esbuild
|
|
588
|
+
// https://github.com/evanw/esbuild/blob/35c0d65b9d4f29a26176404d2890d1b499634e9f/compat-table/src/caniuse.ts#L119-L122
|
|
589
|
+
.filter((b) => /^\d+(\.\d+)*$/.test(b[1]))
|
|
590
|
+
// only get the targets supported by esbuild
|
|
591
|
+
.filter((b) => SUPPORTED_ESBUILD_TARGETS.includes(b[0]))
|
|
592
|
+
// only get the oldest version, assuming that the older version
|
|
593
|
+
// is last in the array
|
|
594
|
+
.reduce((acc, b) => {
|
|
595
|
+
const existingIndex = acc.findIndex((br) => br[0] === b[0]);
|
|
596
|
+
|
|
597
|
+
if (existingIndex !== -1) {
|
|
598
|
+
acc[existingIndex][1] = b[1];
|
|
599
|
+
} else {
|
|
600
|
+
acc.push(b);
|
|
601
|
+
}
|
|
602
|
+
return acc
|
|
603
|
+
}, [])
|
|
604
|
+
// remove separator
|
|
605
|
+
.map((b) => b.join(''))
|
|
606
|
+
)
|
|
607
|
+
}
|
|
608
|
+
|
|
520
609
|
function pathContains(containerPath, itemPath) {
|
|
521
610
|
return !path$1.relative(containerPath, itemPath).startsWith("..");
|
|
522
611
|
}
|
|
@@ -544,65 +633,10 @@ function findCommonDirPath(filePaths) {
|
|
|
544
633
|
return ret;
|
|
545
634
|
}
|
|
546
635
|
|
|
547
|
-
function sortAsNumber(a, b) {
|
|
548
|
-
const aVersion = Number(a);
|
|
549
|
-
const bVersion = Number(b);
|
|
550
|
-
return aVersion - bVersion;
|
|
551
|
-
}
|
|
552
|
-
function resolveBrowserslistVersions(query) {
|
|
553
|
-
const result = browserslist(query);
|
|
554
|
-
const entries = result.map((key) => {
|
|
555
|
-
const [name, version] = key.split(" ");
|
|
556
|
-
return { name, version: version.split("-")[0].split(".")[0] };
|
|
557
|
-
});
|
|
558
|
-
const andChr = entries.filter((b) => b.name === "and_chr");
|
|
559
|
-
const andFf = entries.filter((b) => b.name === "and_ff");
|
|
560
|
-
const chrome = entries.filter((b) => b.name === "chrome");
|
|
561
|
-
const edge = entries.filter((b) => b.name === "edge");
|
|
562
|
-
const ff = entries.filter((b) => b.name === "firefox");
|
|
563
|
-
const iosSaf = entries.filter((b) => b.name === "ios_saf");
|
|
564
|
-
const saf = entries.filter((b) => b.name === "safari");
|
|
565
|
-
const opera = entries.filter((b) => b.name === "opera");
|
|
566
|
-
const node = entries.filter((b) => b.name === "node");
|
|
567
|
-
const versions = {
|
|
568
|
-
chrome: andChr.concat(chrome).map((b) => b.version),
|
|
569
|
-
ff: andFf.concat(ff).map((b) => b.version),
|
|
570
|
-
edge: edge.map((b) => b.version),
|
|
571
|
-
iosSaf: iosSaf.map((b) => b.version),
|
|
572
|
-
saf: saf.map((b) => b.version),
|
|
573
|
-
opera: opera.map((b) => b.version),
|
|
574
|
-
node: node.map((b) => b.version)
|
|
575
|
-
};
|
|
576
|
-
versions.chrome.sort(sortAsNumber);
|
|
577
|
-
versions.ff.sort(sortAsNumber);
|
|
578
|
-
versions.edge.sort(sortAsNumber);
|
|
579
|
-
versions.iosSaf.sort(sortAsNumber);
|
|
580
|
-
versions.saf.sort(sortAsNumber);
|
|
581
|
-
versions.opera.sort(sortAsNumber);
|
|
582
|
-
versions.node.sort(sortAsNumber);
|
|
583
|
-
return versions;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
636
|
function resolveBrowserTarget(versions) {
|
|
587
|
-
const target =
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
}
|
|
591
|
-
if (versions.ff.length) {
|
|
592
|
-
target.push(`firefox${versions.ff[0]}`);
|
|
593
|
-
}
|
|
594
|
-
if (versions.edge.length) {
|
|
595
|
-
target.push(`edge${versions.edge[0]}`);
|
|
596
|
-
}
|
|
597
|
-
if (versions.iosSaf.length) {
|
|
598
|
-
target.push(`ios${versions.iosSaf[0]}`);
|
|
599
|
-
}
|
|
600
|
-
if (versions.saf.length) {
|
|
601
|
-
target.push(`safari${versions.saf[0]}`);
|
|
602
|
-
}
|
|
603
|
-
if (versions.opera.length) {
|
|
604
|
-
target.push(`opera${versions.opera[0]}`);
|
|
605
|
-
}
|
|
637
|
+
const target = versions.filter(
|
|
638
|
+
(version) => version.startsWith("chrome") || version.startsWith("edge") || version.startsWith("firefox") || version.startsWith("ios") || version.startsWith("safari") || version.startsWith("opera")
|
|
639
|
+
);
|
|
606
640
|
if (target.length === 0) {
|
|
607
641
|
return void 0;
|
|
608
642
|
}
|
|
@@ -610,10 +644,7 @@ function resolveBrowserTarget(versions) {
|
|
|
610
644
|
}
|
|
611
645
|
|
|
612
646
|
function resolveNodeTarget(versions) {
|
|
613
|
-
const target =
|
|
614
|
-
if (versions.node.length) {
|
|
615
|
-
target.push(`node${versions.node[0]}`);
|
|
616
|
-
}
|
|
647
|
+
const target = versions.filter((version) => version.startsWith("node"));
|
|
617
648
|
if (target.length === 0) {
|
|
618
649
|
return void 0;
|
|
619
650
|
}
|
|
@@ -632,7 +663,14 @@ async function resolveBuildContext(options) {
|
|
|
632
663
|
tsconfig: tsconfigPath
|
|
633
664
|
} = options;
|
|
634
665
|
const tsconfig = await loadTSConfig({ cwd, tsconfigPath });
|
|
635
|
-
|
|
666
|
+
let browserslist = pkg.browserslist;
|
|
667
|
+
if (!browserslist) {
|
|
668
|
+
logger.warn(
|
|
669
|
+
'Could not detect a `browserslist` property in `package.json`, using default configuration. Add `"browserslist": "extends @sanity/browserslist-config"` to silence this warning.'
|
|
670
|
+
);
|
|
671
|
+
browserslist = DEFAULT_BROWSERSLIST_QUERY;
|
|
672
|
+
}
|
|
673
|
+
const targetVersions = browserslistToEsbuild(browserslist);
|
|
636
674
|
const nodeTarget = resolveNodeTarget(targetVersions);
|
|
637
675
|
const webTarget = resolveBrowserTarget(targetVersions);
|
|
638
676
|
if (!nodeTarget) {
|