@afterpack/integration-utils 0.2.1 → 0.2.2
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/index.cjs +828 -788
- package/dist/index.d.cts +19 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.js +827 -789
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -763,418 +763,6 @@ function warnIfPublicPath(artifactPath, logger = DEFAULT_LOGGER) {
|
|
|
763
763
|
return true;
|
|
764
764
|
}
|
|
765
765
|
|
|
766
|
-
//#endregion
|
|
767
|
-
//#region src/source-map.ts
|
|
768
|
-
const SOURCE_MAPPING_URL_RE = /\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/gm;
|
|
769
|
-
const SOURCE_MAPPING_URL_LINE_RE = /^[ \t]*\/\/[#@]\s*sourceMappingURL=\S+[ \t]*\r?\n?/gm;
|
|
770
|
-
const CSS_SOURCE_MAPPING_URL_RE = /[ \t]*\/\*[#@]\s*sourceMappingURL=[^\s*]+\s*\*\/\s*$/;
|
|
771
|
-
function withSourceMappingURL(code, url) {
|
|
772
|
-
const stripped = code.replace(SOURCE_MAPPING_URL_LINE_RE, "");
|
|
773
|
-
if (url == null) return stripped;
|
|
774
|
-
const separator = stripped.length > 0 && !stripped.endsWith("\n") ? "\n" : "";
|
|
775
|
-
return `${stripped}${separator}//# sourceMappingURL=${url}\n`;
|
|
776
|
-
}
|
|
777
|
-
function withoutCssSourceMappingURL(css) {
|
|
778
|
-
return css.replace(CSS_SOURCE_MAPPING_URL_RE, "");
|
|
779
|
-
}
|
|
780
|
-
function extractSourceMappingURL(code) {
|
|
781
|
-
let last = null;
|
|
782
|
-
for (const match of code.matchAll(SOURCE_MAPPING_URL_RE)) last = match[1];
|
|
783
|
-
return last;
|
|
784
|
-
}
|
|
785
|
-
function isRemoteSourceMappingURL(url) {
|
|
786
|
-
return /^https?:\/\//i.test(url) || url.startsWith("//");
|
|
787
|
-
}
|
|
788
|
-
function decodeDataUri(url) {
|
|
789
|
-
if (!url.startsWith("data:")) return null;
|
|
790
|
-
const comma = url.indexOf(",");
|
|
791
|
-
if (comma === -1) return null;
|
|
792
|
-
const meta = url.slice(5, comma);
|
|
793
|
-
const payload = url.slice(comma + 1);
|
|
794
|
-
try {
|
|
795
|
-
if (/;base64/i.test(meta)) return Buffer.from(payload, "base64").toString("utf8");
|
|
796
|
-
return decodeURIComponent(payload);
|
|
797
|
-
} catch {
|
|
798
|
-
return null;
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
function discoverInputSourceMap(jsPath, code) {
|
|
802
|
-
const adjacent = `${jsPath}.map`;
|
|
803
|
-
if ((0, node_fs.existsSync)(adjacent)) try {
|
|
804
|
-
return (0, node_fs.readFileSync)(adjacent, "utf8");
|
|
805
|
-
} catch {}
|
|
806
|
-
let source = code;
|
|
807
|
-
if (source == null) try {
|
|
808
|
-
source = (0, node_fs.readFileSync)(jsPath, "utf8");
|
|
809
|
-
} catch {
|
|
810
|
-
return null;
|
|
811
|
-
}
|
|
812
|
-
const url = extractSourceMappingURL(source);
|
|
813
|
-
if (url == null) return null;
|
|
814
|
-
if (url.startsWith("data:")) return decodeDataUri(url);
|
|
815
|
-
if (isRemoteSourceMappingURL(url)) return null;
|
|
816
|
-
let mapPath;
|
|
817
|
-
try {
|
|
818
|
-
mapPath = (0, node_path.isAbsolute)(url) ? url : (0, node_path.resolve)((0, node_path.dirname)(jsPath), decodeURIComponent(url));
|
|
819
|
-
} catch {
|
|
820
|
-
return null;
|
|
821
|
-
}
|
|
822
|
-
if (!(0, node_fs.existsSync)(mapPath)) return null;
|
|
823
|
-
try {
|
|
824
|
-
return (0, node_fs.readFileSync)(mapPath, "utf8");
|
|
825
|
-
} catch {
|
|
826
|
-
return null;
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
//#endregion
|
|
831
|
-
//#region src/bundle.ts
|
|
832
|
-
const BUNDLE_JS_RE = /\.[cm]?js$/;
|
|
833
|
-
const CSS_MAP_RE = /\.css\.map$/;
|
|
834
|
-
function collectBundleJs(bundle, outDir) {
|
|
835
|
-
const files = [];
|
|
836
|
-
const inputs = new Map();
|
|
837
|
-
const entries = new Map();
|
|
838
|
-
for (const [fileName, entry] of Object.entries(bundle)) {
|
|
839
|
-
if (!BUNDLE_JS_RE.test(fileName)) continue;
|
|
840
|
-
const source = entry.type === "chunk" ? entry.code : typeof entry.source === "string" ? entry.source : void 0;
|
|
841
|
-
if (source === void 0) continue;
|
|
842
|
-
const filePath = (0, node_path.resolve)(outDir, fileName);
|
|
843
|
-
files.push(filePath);
|
|
844
|
-
entries.set(filePath, entry);
|
|
845
|
-
inputs.set(filePath, {
|
|
846
|
-
source,
|
|
847
|
-
sourceMap: entry.map ? entry.map.toString() : null
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
return {
|
|
851
|
-
files,
|
|
852
|
-
inputs,
|
|
853
|
-
entries
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
function dataUri(json) {
|
|
857
|
-
return `data:application/json;charset=utf-8;base64,${Buffer.from(json, "utf8").toString("base64")}`;
|
|
858
|
-
}
|
|
859
|
-
function applyBundleOutput(bundle, entry, out, policy) {
|
|
860
|
-
if (entry.type !== "chunk") {
|
|
861
|
-
entry.source = out.code;
|
|
862
|
-
return;
|
|
863
|
-
}
|
|
864
|
-
const mapName = entry.sourcemapFileName ?? null;
|
|
865
|
-
const inlineMap = mapName == null && entry.map != null;
|
|
866
|
-
const emitMap = policy.sourceMap && out.sourceMap != null;
|
|
867
|
-
let url = null;
|
|
868
|
-
if (emitMap && out.sourceMap != null) {
|
|
869
|
-
if (mapName != null && bundle[mapName]) {
|
|
870
|
-
bundle[mapName].source = out.sourceMap;
|
|
871
|
-
url = (0, node_path.basename)(mapName);
|
|
872
|
-
} else if (inlineMap) url = dataUri(out.sourceMap);
|
|
873
|
-
} else {
|
|
874
|
-
if (mapName != null) delete bundle[mapName];
|
|
875
|
-
entry.map = null;
|
|
876
|
-
}
|
|
877
|
-
entry.code = withSourceMappingURL(out.code, policy.emitSourceMappingURL ? url : null);
|
|
878
|
-
}
|
|
879
|
-
function stripBundleCssSourceMaps(bundle) {
|
|
880
|
-
for (const [fileName, entry] of Object.entries(bundle)) {
|
|
881
|
-
if (CSS_MAP_RE.test(fileName)) {
|
|
882
|
-
delete bundle[fileName];
|
|
883
|
-
continue;
|
|
884
|
-
}
|
|
885
|
-
if (entry.type !== "asset" || !fileName.endsWith(".css") || entry.source === void 0) continue;
|
|
886
|
-
const css = typeof entry.source === "string" ? entry.source : Buffer.from(entry.source).toString("utf8");
|
|
887
|
-
const stripped = withoutCssSourceMappingURL(css);
|
|
888
|
-
if (stripped !== css) entry.source = stripped;
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
//#endregion
|
|
893
|
-
//#region src/glob.ts
|
|
894
|
-
function normalize(value) {
|
|
895
|
-
return value.includes("\\") ? value.replaceAll("\\", "/") : value;
|
|
896
|
-
}
|
|
897
|
-
function segments(value) {
|
|
898
|
-
return value.split("/").filter((part) => part !== "");
|
|
899
|
-
}
|
|
900
|
-
function isAnchored(pattern) {
|
|
901
|
-
if (pattern.startsWith("/")) return true;
|
|
902
|
-
return pattern.length >= 2 && pattern[1] === ":" && /^[A-Za-z]$/.test(pattern[0]);
|
|
903
|
-
}
|
|
904
|
-
function segmentMatches(pattern, seg) {
|
|
905
|
-
if (!pattern.includes("*") && !pattern.includes("?")) return pattern === seg;
|
|
906
|
-
const p = [...pattern];
|
|
907
|
-
const s = [...seg];
|
|
908
|
-
let pi = 0;
|
|
909
|
-
let si = 0;
|
|
910
|
-
let star = -1;
|
|
911
|
-
let starSi = 0;
|
|
912
|
-
while (si < s.length) if (pi < p.length && p[pi] === "*") {
|
|
913
|
-
star = pi;
|
|
914
|
-
starSi = si;
|
|
915
|
-
pi += 1;
|
|
916
|
-
} else if (pi < p.length && (p[pi] === "?" || p[pi] === s[si])) {
|
|
917
|
-
pi += 1;
|
|
918
|
-
si += 1;
|
|
919
|
-
} else if (star >= 0) {
|
|
920
|
-
pi = star + 1;
|
|
921
|
-
starSi += 1;
|
|
922
|
-
si = starSi;
|
|
923
|
-
} else return false;
|
|
924
|
-
return p.slice(pi).every((c) => c === "*");
|
|
925
|
-
}
|
|
926
|
-
function segmentsMatch(pattern, path) {
|
|
927
|
-
let pi = 0;
|
|
928
|
-
let si = 0;
|
|
929
|
-
let star = -1;
|
|
930
|
-
let starSi = 0;
|
|
931
|
-
while (si < path.length) if (pi < pattern.length && pattern[pi] === "**") {
|
|
932
|
-
star = pi;
|
|
933
|
-
starSi = si;
|
|
934
|
-
pi += 1;
|
|
935
|
-
} else if (pi < pattern.length && segmentMatches(pattern[pi], path[si])) {
|
|
936
|
-
pi += 1;
|
|
937
|
-
si += 1;
|
|
938
|
-
} else if (star >= 0) {
|
|
939
|
-
pi = star + 1;
|
|
940
|
-
starSi += 1;
|
|
941
|
-
si = starSi;
|
|
942
|
-
} else return false;
|
|
943
|
-
return pattern.slice(pi).every((s) => s === "**");
|
|
944
|
-
}
|
|
945
|
-
function matchesPath(pattern, path) {
|
|
946
|
-
const pat = normalize(pattern);
|
|
947
|
-
const parts = segments(pat);
|
|
948
|
-
if (parts.length === 0) return false;
|
|
949
|
-
const segs = segments(normalize(path));
|
|
950
|
-
if (isAnchored(pat)) return segmentsMatch(parts, segs);
|
|
951
|
-
for (let i = 0; i <= segs.length; i++) if (segmentsMatch(parts, segs.slice(i))) return true;
|
|
952
|
-
return false;
|
|
953
|
-
}
|
|
954
|
-
function reachesInto(pattern, dir) {
|
|
955
|
-
const pat = normalize(pattern);
|
|
956
|
-
const parts = segments(pat);
|
|
957
|
-
if (parts.length === 0) return false;
|
|
958
|
-
if (!isAnchored(pat)) return true;
|
|
959
|
-
for (let k = 1; k <= parts.length; k++) if (matchesPath(`/${parts.slice(0, k).join("/")}`, dir)) return true;
|
|
960
|
-
return false;
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
//#endregion
|
|
964
|
-
//#region src/collect.ts
|
|
965
|
-
const BACKUP_RE = /\.backup\.[0-9a-f]{8}\.[cm]?js$/;
|
|
966
|
-
const NODE_MODULES = "node_modules";
|
|
967
|
-
function isCollectableJs(name) {
|
|
968
|
-
if (BACKUP_RE.test(name)) return false;
|
|
969
|
-
return name.endsWith(".js") || name.endsWith(".mjs") || name.endsWith(".cjs");
|
|
970
|
-
}
|
|
971
|
-
function statOrNull(path) {
|
|
972
|
-
try {
|
|
973
|
-
return (0, node_fs.statSync)(path);
|
|
974
|
-
} catch {
|
|
975
|
-
return null;
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
function collectJsFiles(target, options = {}) {
|
|
979
|
-
const stat = statOrNull(target);
|
|
980
|
-
if (!stat) return [];
|
|
981
|
-
if (!stat.isDirectory()) return isCollectableJs((0, node_path.basename)(target)) ? [target] : [];
|
|
982
|
-
return walkJs(target, options.include ?? [], false, options.onNestedProject);
|
|
983
|
-
}
|
|
984
|
-
function entriesOf(dir) {
|
|
985
|
-
try {
|
|
986
|
-
return (0, node_fs.readdirSync)(dir, { withFileTypes: true });
|
|
987
|
-
} catch {
|
|
988
|
-
return [];
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
function kindOf(entry, full) {
|
|
992
|
-
if (entry.isDirectory()) return "dir";
|
|
993
|
-
if (entry.isFile()) return "file";
|
|
994
|
-
const stat = statOrNull(full);
|
|
995
|
-
if (!stat) return null;
|
|
996
|
-
return stat.isDirectory() ? "dir" : "file";
|
|
997
|
-
}
|
|
998
|
-
function isNestedProject(dir) {
|
|
999
|
-
return (0, node_fs.existsSync)((0, node_path.join)(dir, "package.json"));
|
|
1000
|
-
}
|
|
1001
|
-
function walkJs(dir, include, readmitted, onNestedProject) {
|
|
1002
|
-
const out = [];
|
|
1003
|
-
for (const entry of entriesOf(dir)) {
|
|
1004
|
-
const name = entry.name;
|
|
1005
|
-
const full = (0, node_path.join)(dir, name);
|
|
1006
|
-
const kind = kindOf(entry, full);
|
|
1007
|
-
if (kind === null) continue;
|
|
1008
|
-
if (kind === "dir") {
|
|
1009
|
-
const skipped = name === NODE_MODULES;
|
|
1010
|
-
if (!skipped && !readmitted && isNestedProject(full)) {
|
|
1011
|
-
onNestedProject?.(full);
|
|
1012
|
-
continue;
|
|
1013
|
-
}
|
|
1014
|
-
if (skipped && !include.some((pattern) => reachesInto(pattern, full))) continue;
|
|
1015
|
-
out.push(...walkJs(full, include, readmitted || skipped, onNestedProject));
|
|
1016
|
-
} else if (isCollectableJs(name)) {
|
|
1017
|
-
if (readmitted && !include.some((pattern) => matchesPath(pattern, full))) continue;
|
|
1018
|
-
out.push(full);
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
return out;
|
|
1022
|
-
}
|
|
1023
|
-
const SOURCE_MAP_RE = /\.(?:[cm]?js|css)\.map$/;
|
|
1024
|
-
function collectFiles(dir, accept) {
|
|
1025
|
-
if (!(0, node_fs.existsSync)(dir)) return [];
|
|
1026
|
-
const out = [];
|
|
1027
|
-
for (const entry of entriesOf(dir)) {
|
|
1028
|
-
const name = entry.name;
|
|
1029
|
-
const full = (0, node_path.join)(dir, name);
|
|
1030
|
-
const kind = kindOf(entry, full);
|
|
1031
|
-
if (kind === null) continue;
|
|
1032
|
-
if (kind === "dir") out.push(...collectFiles(full, accept));
|
|
1033
|
-
else if (accept(name)) out.push(full);
|
|
1034
|
-
}
|
|
1035
|
-
return out;
|
|
1036
|
-
}
|
|
1037
|
-
function collectSourceMaps(dir) {
|
|
1038
|
-
return collectFiles(dir, (name) => SOURCE_MAP_RE.test(name));
|
|
1039
|
-
}
|
|
1040
|
-
function collectStylesheets(dir) {
|
|
1041
|
-
return collectFiles(dir, (name) => name.endsWith(".css"));
|
|
1042
|
-
}
|
|
1043
|
-
|
|
1044
|
-
//#endregion
|
|
1045
|
-
//#region src/notices.ts
|
|
1046
|
-
const NOTICE_MESSAGE_LIMIT = 500;
|
|
1047
|
-
const NOTICE_LIMIT = 5;
|
|
1048
|
-
const NOTICE_CODE_LIMIT = 64;
|
|
1049
|
-
const NOTICE_HOSTS = new Set(["afterpack.dev", "www.afterpack.dev"]);
|
|
1050
|
-
const ESC = 27;
|
|
1051
|
-
const BEL = 7;
|
|
1052
|
-
const C1_CSI = 155;
|
|
1053
|
-
const C1_OSC = 157;
|
|
1054
|
-
function isControl(code) {
|
|
1055
|
-
return code < 32 || code >= 127 && code <= 159;
|
|
1056
|
-
}
|
|
1057
|
-
function isInvisibleFormat(code) {
|
|
1058
|
-
return code === 1564 || code >= 8203 && code <= 8207 || code >= 8234 && code <= 8238 || code >= 8288 && code <= 8297 || code === 65279;
|
|
1059
|
-
}
|
|
1060
|
-
function skipCsi(text, from) {
|
|
1061
|
-
let i = from;
|
|
1062
|
-
while (i < text.length) {
|
|
1063
|
-
const c = text.charCodeAt(i);
|
|
1064
|
-
i += 1;
|
|
1065
|
-
if (c >= 64 && c <= 126) return i;
|
|
1066
|
-
}
|
|
1067
|
-
return i;
|
|
1068
|
-
}
|
|
1069
|
-
function skipOsc(text, from) {
|
|
1070
|
-
let i = from;
|
|
1071
|
-
while (i < text.length) {
|
|
1072
|
-
const c = text.charCodeAt(i);
|
|
1073
|
-
if (c === BEL) return i + 1;
|
|
1074
|
-
if (c === ESC) return text.charCodeAt(i + 1) === 92 ? i + 2 : i + 1;
|
|
1075
|
-
if (c === 156) return i + 1;
|
|
1076
|
-
i += 1;
|
|
1077
|
-
}
|
|
1078
|
-
return i;
|
|
1079
|
-
}
|
|
1080
|
-
function stripTerminalControls(text) {
|
|
1081
|
-
let out = "";
|
|
1082
|
-
let i = 0;
|
|
1083
|
-
while (i < text.length) {
|
|
1084
|
-
const c = text.charCodeAt(i);
|
|
1085
|
-
if (c === ESC) {
|
|
1086
|
-
const next = text.charCodeAt(i + 1);
|
|
1087
|
-
if (next === 91) i = skipCsi(text, i + 2);
|
|
1088
|
-
else if (next === 93) i = skipOsc(text, i + 2);
|
|
1089
|
-
else i += Number.isNaN(next) ? 1 : 2;
|
|
1090
|
-
continue;
|
|
1091
|
-
}
|
|
1092
|
-
if (c === C1_CSI) {
|
|
1093
|
-
i = skipCsi(text, i + 1);
|
|
1094
|
-
continue;
|
|
1095
|
-
}
|
|
1096
|
-
if (c === C1_OSC) {
|
|
1097
|
-
i = skipOsc(text, i + 1);
|
|
1098
|
-
continue;
|
|
1099
|
-
}
|
|
1100
|
-
if (isControl(c)) {
|
|
1101
|
-
out += c === 9 || c === 10 || c === 13 ? " " : "";
|
|
1102
|
-
i += 1;
|
|
1103
|
-
continue;
|
|
1104
|
-
}
|
|
1105
|
-
if (c === 8232 || c === 8233) {
|
|
1106
|
-
out += " ";
|
|
1107
|
-
i += 1;
|
|
1108
|
-
continue;
|
|
1109
|
-
}
|
|
1110
|
-
if (isInvisibleFormat(c)) {
|
|
1111
|
-
i += 1;
|
|
1112
|
-
continue;
|
|
1113
|
-
}
|
|
1114
|
-
out += text[i];
|
|
1115
|
-
i += 1;
|
|
1116
|
-
}
|
|
1117
|
-
return out;
|
|
1118
|
-
}
|
|
1119
|
-
function sanitizeServerText(value, limit = NOTICE_MESSAGE_LIMIT) {
|
|
1120
|
-
if (typeof value !== "string") return "";
|
|
1121
|
-
const clean = stripTerminalControls(value).replace(/\s+/g, " ").trim();
|
|
1122
|
-
if (clean.length <= limit) return clean;
|
|
1123
|
-
const chars = Array.from(clean);
|
|
1124
|
-
return chars.length > limit ? chars.slice(0, limit).join("") : clean;
|
|
1125
|
-
}
|
|
1126
|
-
function sanitizeNoticeUrl(value) {
|
|
1127
|
-
if (typeof value !== "string") return null;
|
|
1128
|
-
let url;
|
|
1129
|
-
try {
|
|
1130
|
-
url = new URL(value.trim());
|
|
1131
|
-
} catch {
|
|
1132
|
-
return null;
|
|
1133
|
-
}
|
|
1134
|
-
if (url.protocol !== "https:") return null;
|
|
1135
|
-
if (!NOTICE_HOSTS.has(url.hostname) || url.port !== "") return null;
|
|
1136
|
-
if (url.username !== "" || url.password !== "") return null;
|
|
1137
|
-
const href = url.href;
|
|
1138
|
-
return href.length <= NOTICE_MESSAGE_LIMIT ? href : null;
|
|
1139
|
-
}
|
|
1140
|
-
function noticeSeverity(value) {
|
|
1141
|
-
if (value === "info") return "info";
|
|
1142
|
-
if (value === "error" || value === "critical") return "error";
|
|
1143
|
-
return "warning";
|
|
1144
|
-
}
|
|
1145
|
-
function sanitizeNotices(raw) {
|
|
1146
|
-
if (!Array.isArray(raw)) return [];
|
|
1147
|
-
const out = [];
|
|
1148
|
-
for (const entry of raw) {
|
|
1149
|
-
if (out.length >= NOTICE_LIMIT) break;
|
|
1150
|
-
if (typeof entry !== "object" || entry === null) continue;
|
|
1151
|
-
const n = entry;
|
|
1152
|
-
const message = sanitizeServerText(n.message);
|
|
1153
|
-
if (message === "") continue;
|
|
1154
|
-
out.push({
|
|
1155
|
-
severity: noticeSeverity(n.severity),
|
|
1156
|
-
code: sanitizeServerText(n.code, NOTICE_CODE_LIMIT),
|
|
1157
|
-
message,
|
|
1158
|
-
url: sanitizeNoticeUrl(n.url)
|
|
1159
|
-
});
|
|
1160
|
-
}
|
|
1161
|
-
return out;
|
|
1162
|
-
}
|
|
1163
|
-
function formatNotice(notice) {
|
|
1164
|
-
const head = notice.severity === "info" ? "AfterPack notice" : `AfterPack ${notice.severity}`;
|
|
1165
|
-
const tail = notice.url ? ` ${notice.url}` : "";
|
|
1166
|
-
return `${head}: ${notice.message}${tail}`;
|
|
1167
|
-
}
|
|
1168
|
-
function reportNotices(raw, logger, prefix = (m) => m) {
|
|
1169
|
-
const notices = sanitizeNotices(raw);
|
|
1170
|
-
for (const notice of notices) {
|
|
1171
|
-
const line = prefix(formatNotice(notice));
|
|
1172
|
-
if (notice.severity === "info" || !logger.warn) logger.log(line);
|
|
1173
|
-
else logger.warn(line);
|
|
1174
|
-
}
|
|
1175
|
-
return notices;
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
766
|
//#endregion
|
|
1179
767
|
//#region src/registry.ts
|
|
1180
768
|
const PRESET_VALUES = [
|
|
@@ -1184,6 +772,20 @@ const PRESET_VALUES = [
|
|
|
1184
772
|
"hard",
|
|
1185
773
|
"extreme"
|
|
1186
774
|
];
|
|
775
|
+
const SOURCE_TYPE_VALUES = [
|
|
776
|
+
"auto",
|
|
777
|
+
"module",
|
|
778
|
+
"script"
|
|
779
|
+
];
|
|
780
|
+
const MODULE_OUTPUT_FORMATS = new Set([
|
|
781
|
+
"es",
|
|
782
|
+
"esm",
|
|
783
|
+
"esmodule",
|
|
784
|
+
"module"
|
|
785
|
+
]);
|
|
786
|
+
function sourceTypeForOutputFormat(format) {
|
|
787
|
+
return typeof format === "string" && MODULE_OUTPUT_FORMATS.has(format.toLowerCase()) ? "module" : void 0;
|
|
788
|
+
}
|
|
1187
789
|
const TRANSFORM_KIND_VALUES = [
|
|
1188
790
|
"stringEncoding",
|
|
1189
791
|
"controlFlowFlatten",
|
|
@@ -1233,6 +835,18 @@ const CONFIG_KEYS = [
|
|
|
1233
835
|
},
|
|
1234
836
|
default: "light"
|
|
1235
837
|
},
|
|
838
|
+
{
|
|
839
|
+
path: "sourceType",
|
|
840
|
+
shape: "scalar",
|
|
841
|
+
scope: "program",
|
|
842
|
+
tier: "free",
|
|
843
|
+
surface: "engine",
|
|
844
|
+
item: {
|
|
845
|
+
kind: "enum",
|
|
846
|
+
values: SOURCE_TYPE_VALUES
|
|
847
|
+
},
|
|
848
|
+
default: "auto"
|
|
849
|
+
},
|
|
1236
850
|
{
|
|
1237
851
|
path: "complexity",
|
|
1238
852
|
shape: "scalar",
|
|
@@ -1536,438 +1150,855 @@ const CONFIG_KEYS = [
|
|
|
1536
1150
|
},
|
|
1537
1151
|
default: "detected from the environment"
|
|
1538
1152
|
},
|
|
1539
|
-
{
|
|
1540
|
-
path: "directives.enabled",
|
|
1541
|
-
shape: "scalar",
|
|
1542
|
-
scope: "program",
|
|
1543
|
-
tier: "free",
|
|
1544
|
-
surface: "build",
|
|
1545
|
-
item: { kind: "boolean" },
|
|
1546
|
-
default: "true"
|
|
1153
|
+
{
|
|
1154
|
+
path: "directives.enabled",
|
|
1155
|
+
shape: "scalar",
|
|
1156
|
+
scope: "program",
|
|
1157
|
+
tier: "free",
|
|
1158
|
+
surface: "build",
|
|
1159
|
+
item: { kind: "boolean" },
|
|
1160
|
+
default: "true"
|
|
1161
|
+
},
|
|
1162
|
+
{
|
|
1163
|
+
path: "diagnostics.level",
|
|
1164
|
+
shape: "scalar",
|
|
1165
|
+
scope: "program",
|
|
1166
|
+
tier: "free",
|
|
1167
|
+
surface: "build",
|
|
1168
|
+
item: {
|
|
1169
|
+
kind: "enum",
|
|
1170
|
+
values: [
|
|
1171
|
+
"summary",
|
|
1172
|
+
"all",
|
|
1173
|
+
"none"
|
|
1174
|
+
]
|
|
1175
|
+
},
|
|
1176
|
+
default: "summary"
|
|
1177
|
+
},
|
|
1178
|
+
{
|
|
1179
|
+
path: "diagnostics.format",
|
|
1180
|
+
shape: "scalar",
|
|
1181
|
+
scope: "program",
|
|
1182
|
+
tier: "free",
|
|
1183
|
+
surface: "cli",
|
|
1184
|
+
item: {
|
|
1185
|
+
kind: "enum",
|
|
1186
|
+
values: ["text", "json"]
|
|
1187
|
+
},
|
|
1188
|
+
default: "text"
|
|
1189
|
+
},
|
|
1190
|
+
{
|
|
1191
|
+
path: "telemetry.enabled",
|
|
1192
|
+
shape: "scalar",
|
|
1193
|
+
scope: "program",
|
|
1194
|
+
tier: "free",
|
|
1195
|
+
surface: "build",
|
|
1196
|
+
item: { kind: "boolean" },
|
|
1197
|
+
default: "true"
|
|
1198
|
+
},
|
|
1199
|
+
{
|
|
1200
|
+
path: "allowUnobfuscated",
|
|
1201
|
+
shape: "scalar",
|
|
1202
|
+
scope: "program",
|
|
1203
|
+
tier: "free",
|
|
1204
|
+
surface: "build",
|
|
1205
|
+
item: { kind: "boolean" },
|
|
1206
|
+
default: "false"
|
|
1207
|
+
},
|
|
1208
|
+
{
|
|
1209
|
+
path: "key",
|
|
1210
|
+
shape: "scalar",
|
|
1211
|
+
scope: "program",
|
|
1212
|
+
tier: "pro",
|
|
1213
|
+
surface: "build",
|
|
1214
|
+
item: { kind: "string" },
|
|
1215
|
+
default: "unset; builds run locally with basic protection"
|
|
1216
|
+
}
|
|
1217
|
+
];
|
|
1218
|
+
const EMPTY_CONFIG = {};
|
|
1219
|
+
const CONFIG_BY_PATH = new Map(CONFIG_KEYS.map((k) => [k.path, k]));
|
|
1220
|
+
const CONFIG_PREFIXES = new Set(CONFIG_KEYS.flatMap((k) => {
|
|
1221
|
+
const parts = k.path.split(".");
|
|
1222
|
+
return parts.slice(0, -1).map((_, i) => parts.slice(0, i + 1).join("."));
|
|
1223
|
+
}));
|
|
1224
|
+
const CONFIG_CHILDREN = new Map([...CONFIG_PREFIXES].map((prefix) => [prefix, CONFIG_KEYS.filter((k) => k.path.startsWith(`${prefix}.`)).map((k) => k.path)]));
|
|
1225
|
+
const BY_NORMALIZED = new Map(CONFIG_KEYS.map((k) => [normalizeKey(k.path), k]));
|
|
1226
|
+
const CHILDREN_BY_NORMALIZED = new Map([...CONFIG_CHILDREN].map(([prefix, children]) => [normalizeKey(prefix), children]));
|
|
1227
|
+
const PREFIX_BY_NORMALIZED = new Map([...CONFIG_PREFIXES].map((prefix) => [normalizeKey(prefix), prefix]));
|
|
1228
|
+
function normalizeKey(key) {
|
|
1229
|
+
return key.replace(/[-_.]/g, "").toLowerCase();
|
|
1230
|
+
}
|
|
1231
|
+
function groupBooleanKey(path) {
|
|
1232
|
+
if (!CONFIG_PREFIXES.has(path)) return void 0;
|
|
1233
|
+
return CONFIG_BY_PATH.get(`${path}.enabled`);
|
|
1234
|
+
}
|
|
1235
|
+
function nearestName(name, candidates) {
|
|
1236
|
+
const target = normalizeKey(name);
|
|
1237
|
+
let best;
|
|
1238
|
+
for (const candidate of candidates) {
|
|
1239
|
+
const distance = editDistance(target, normalizeKey(candidate));
|
|
1240
|
+
if (distance > Math.max(2, Math.floor(target.length / 4))) continue;
|
|
1241
|
+
if (!best || distance < best.distance) best = {
|
|
1242
|
+
name: candidate,
|
|
1243
|
+
distance
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
return best?.name;
|
|
1247
|
+
}
|
|
1248
|
+
function suggestKey(key) {
|
|
1249
|
+
const exact = BY_NORMALIZED.get(normalizeKey(key));
|
|
1250
|
+
if (exact) return exact.path;
|
|
1251
|
+
const near = nearestName(key, CONFIG_KEYS.map((k) => k.path));
|
|
1252
|
+
if (near) return near;
|
|
1253
|
+
const tail = normalizeKey(key);
|
|
1254
|
+
const owners = CONFIG_KEYS.filter((k) => normalizeKey(k.path.slice(k.path.lastIndexOf(".") + 1)) === tail);
|
|
1255
|
+
return owners.length === 1 ? owners[0].path : void 0;
|
|
1256
|
+
}
|
|
1257
|
+
function canonicalSpelling(name) {
|
|
1258
|
+
const norm$1 = normalizeKey(name);
|
|
1259
|
+
const key = BY_NORMALIZED.get(norm$1);
|
|
1260
|
+
if (key) return { key: key.path };
|
|
1261
|
+
const group$1 = CHILDREN_BY_NORMALIZED.get(norm$1);
|
|
1262
|
+
return group$1 && group$1.length > 0 ? { group: group$1 } : void 0;
|
|
1263
|
+
}
|
|
1264
|
+
function editDistance(a, b) {
|
|
1265
|
+
const prev = new Array(b.length + 1);
|
|
1266
|
+
const next = new Array(b.length + 1);
|
|
1267
|
+
for (let j = 0; j <= b.length; j++) prev[j] = j;
|
|
1268
|
+
for (let i = 1; i <= a.length; i++) {
|
|
1269
|
+
next[0] = i;
|
|
1270
|
+
for (let j = 1; j <= b.length; j++) next[j] = Math.min(prev[j] + 1, next[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
1271
|
+
for (let j = 0; j <= b.length; j++) prev[j] = next[j];
|
|
1272
|
+
}
|
|
1273
|
+
return prev[b.length];
|
|
1274
|
+
}
|
|
1275
|
+
const DOCS_URL = "https://www.afterpack.dev/docs/config";
|
|
1276
|
+
function unknownKeyMessage(path, surface) {
|
|
1277
|
+
const group$1 = CHILDREN_BY_NORMALIZED.get(normalizeKey(path));
|
|
1278
|
+
if (group$1 && group$1.length > 0) {
|
|
1279
|
+
const list = group$1.map((child) => `\`${child}\``).join(", ");
|
|
1280
|
+
const tail$1 = group$1.length === 1 ? `did you mean ${list}?` : `its keys are ${list}`;
|
|
1281
|
+
const canonical = PREFIX_BY_NORMALIZED.get(normalizeKey(path));
|
|
1282
|
+
const enabled = canonical ? groupBooleanKey(canonical) : void 0;
|
|
1283
|
+
const rule = enabled ? `; a boolean here means \`${enabled.path}\`` : "";
|
|
1284
|
+
return `\`${path}\` is a group of configuration keys, not a key itself (${surface}) — ${tail$1}${rule}`;
|
|
1285
|
+
}
|
|
1286
|
+
const suggestion = suggestKey(path);
|
|
1287
|
+
const tail = suggestion ? ` — did you mean \`${suggestion}\`?` : ` — see ${DOCS_URL}`;
|
|
1288
|
+
return `unknown configuration key \`${path}\` (${surface})${tail}`;
|
|
1289
|
+
}
|
|
1290
|
+
function isPlainObject(value) {
|
|
1291
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1292
|
+
}
|
|
1293
|
+
function describe(item) {
|
|
1294
|
+
switch (item.kind) {
|
|
1295
|
+
case "boolean": return "true or false";
|
|
1296
|
+
case "number": return item.integer ? `an integer >= ${item.min}` : `a number >= ${item.min}`;
|
|
1297
|
+
case "unbounded": return `a number >= ${item.min}, or the string "${item.unlimited}"`;
|
|
1298
|
+
case "string": return "a string";
|
|
1299
|
+
case "enum": return `one of: ${item.values.join(", ")}`;
|
|
1300
|
+
case "seed": return "a number, any string, or \"git\"";
|
|
1301
|
+
case "reserved": return "a name, or { \"glob\": \"…\", \"names\": [\"…\"] }";
|
|
1302
|
+
case "region": return "an object with numeric `start` and `end`";
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
function misdirectedHint(key, value, spell) {
|
|
1306
|
+
const other = misdirectedTo(key, value);
|
|
1307
|
+
if (!other) return "";
|
|
1308
|
+
const written = spell(other.path, String(value));
|
|
1309
|
+
return ` — \`${String(value)}\` is a \`${other.path}\` value; write \`${written}\` instead`;
|
|
1310
|
+
}
|
|
1311
|
+
const isFiniteNumber = (v) => typeof v === "number" && Number.isFinite(v);
|
|
1312
|
+
const isTransformKinds = (v) => Array.isArray(v) && v.every((k) => TRANSFORM_KIND_VALUES.includes(k));
|
|
1313
|
+
const KINDS = `a list of: ${TRANSFORM_KIND_VALUES.join(", ")}`;
|
|
1314
|
+
const isNonNegative = (v) => isFiniteNumber(v) && v >= 0;
|
|
1315
|
+
function group(fields) {
|
|
1316
|
+
return {
|
|
1317
|
+
is: isPlainObject,
|
|
1318
|
+
want: "an object",
|
|
1319
|
+
fields
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
const REGION_FIELDS = {
|
|
1323
|
+
start: {
|
|
1324
|
+
required: true,
|
|
1325
|
+
is: isFiniteNumber,
|
|
1326
|
+
want: "a number"
|
|
1327
|
+
},
|
|
1328
|
+
end: {
|
|
1329
|
+
required: true,
|
|
1330
|
+
is: isFiniteNumber,
|
|
1331
|
+
want: "a number"
|
|
1547
1332
|
},
|
|
1548
|
-
{
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
scope: "program",
|
|
1552
|
-
tier: "free",
|
|
1553
|
-
surface: "build",
|
|
1554
|
-
item: {
|
|
1555
|
-
kind: "enum",
|
|
1556
|
-
values: [
|
|
1557
|
-
"summary",
|
|
1558
|
-
"all",
|
|
1559
|
-
"none"
|
|
1560
|
-
]
|
|
1561
|
-
},
|
|
1562
|
-
default: "summary"
|
|
1333
|
+
complexity: {
|
|
1334
|
+
is: isNonNegative,
|
|
1335
|
+
want: "a number >= 0"
|
|
1563
1336
|
},
|
|
1564
|
-
{
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1337
|
+
inflation: group({ max: {
|
|
1338
|
+
is: isNonNegative,
|
|
1339
|
+
want: "a number >= 0"
|
|
1340
|
+
} }),
|
|
1341
|
+
strings: group({ encode: {
|
|
1342
|
+
is: (v) => typeof v === "boolean",
|
|
1343
|
+
want: "true or false"
|
|
1344
|
+
} }),
|
|
1345
|
+
transforms: group({
|
|
1346
|
+
only: {
|
|
1347
|
+
is: isTransformKinds,
|
|
1348
|
+
want: KINDS
|
|
1573
1349
|
},
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
{
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
tier: "free",
|
|
1590
|
-
surface: "build",
|
|
1591
|
-
item: { kind: "boolean" },
|
|
1592
|
-
default: "false"
|
|
1350
|
+
deny: {
|
|
1351
|
+
is: isTransformKinds,
|
|
1352
|
+
want: KINDS
|
|
1353
|
+
}
|
|
1354
|
+
}),
|
|
1355
|
+
label: {
|
|
1356
|
+
is: (v) => typeof v === "string",
|
|
1357
|
+
want: "a string"
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
const RESERVED_FIELDS = {
|
|
1361
|
+
glob: {
|
|
1362
|
+
required: true,
|
|
1363
|
+
is: (v) => typeof v === "string",
|
|
1364
|
+
want: "a string"
|
|
1593
1365
|
},
|
|
1594
|
-
{
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1366
|
+
names: {
|
|
1367
|
+
required: true,
|
|
1368
|
+
is: (v) => Array.isArray(v) && v.every((n) => typeof n === "string"),
|
|
1369
|
+
want: "a list of names"
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
function leafNames(spec, prefix) {
|
|
1373
|
+
return Object.entries(spec).flatMap(([name, field]) => field.fields ? leafNames(field.fields, `${prefix}${name}.`) : [prefix + name]);
|
|
1374
|
+
}
|
|
1375
|
+
function checkFields(spec, value, prefix = "") {
|
|
1376
|
+
for (const name of Object.keys(value)) {
|
|
1377
|
+
if (name in spec) continue;
|
|
1378
|
+
const near = nearestName(prefix + name, Object.keys(spec).map((n) => prefix + n));
|
|
1379
|
+
return `item has an unknown field \`${prefix}${name}\`` + (near ? ` — did you mean \`${near}\`?` : "") + ` (allowed: ${leafNames(spec, prefix).join(", ")})`;
|
|
1380
|
+
}
|
|
1381
|
+
for (const [name, field] of Object.entries(spec)) {
|
|
1382
|
+
const present = value[name];
|
|
1383
|
+
if (present === void 0) {
|
|
1384
|
+
if (field.required) return `item is missing \`${prefix}${name}\` (${field.want})`;
|
|
1385
|
+
continue;
|
|
1386
|
+
}
|
|
1387
|
+
if (!field.is(present)) return `item's \`${prefix}${name}\` must be ${field.want}, got ${JSON.stringify(present)}`;
|
|
1388
|
+
if (field.fields) {
|
|
1389
|
+
const nested = checkFields(field.fields, present, `${prefix}${name}.`);
|
|
1390
|
+
if (nested) return nested;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
return void 0;
|
|
1394
|
+
}
|
|
1395
|
+
function expected(item, value) {
|
|
1396
|
+
return `expected ${describe(item)}, got ${JSON.stringify(value)}`;
|
|
1397
|
+
}
|
|
1398
|
+
function checkItem(item, value) {
|
|
1399
|
+
switch (item.kind) {
|
|
1400
|
+
case "boolean": return typeof value === "boolean" ? void 0 : expected(item, value);
|
|
1401
|
+
case "number": {
|
|
1402
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return expected(item, value);
|
|
1403
|
+
if (value < item.min) return expected(item, value);
|
|
1404
|
+
if (item.integer && !Number.isInteger(value)) return expected(item, value);
|
|
1405
|
+
return void 0;
|
|
1406
|
+
}
|
|
1407
|
+
case "unbounded": {
|
|
1408
|
+
if (value === item.unlimited) return void 0;
|
|
1409
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= item.min) return void 0;
|
|
1410
|
+
return `${expected(item, value)} — there is no \`Infinity\` in JSON; write "${item.unlimited}"`;
|
|
1411
|
+
}
|
|
1412
|
+
case "string": return typeof value === "string" ? void 0 : expected(item, value);
|
|
1413
|
+
case "enum": return typeof value === "string" && item.values.includes(value) ? void 0 : expected(item, value);
|
|
1414
|
+
case "seed": return typeof value === "number" || typeof value === "string" ? void 0 : expected(item, value);
|
|
1415
|
+
case "reserved": {
|
|
1416
|
+
if (typeof value === "string") return void 0;
|
|
1417
|
+
if (!isPlainObject(value)) return expected(item, value);
|
|
1418
|
+
return checkFields(RESERVED_FIELDS, value);
|
|
1419
|
+
}
|
|
1420
|
+
case "region": {
|
|
1421
|
+
if (!isPlainObject(value)) return expected(item, value);
|
|
1422
|
+
return checkFields(REGION_FIELDS, value);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
function checkValue(key, value) {
|
|
1427
|
+
if (key.shape === "scalar") {
|
|
1428
|
+
const failure = checkItem(key.item, value);
|
|
1429
|
+
return failure ? `\`${key.path}\`: ${failure}` : void 0;
|
|
1430
|
+
}
|
|
1431
|
+
if (!Array.isArray(value)) return `\`${key.path}\`: expected a list of values, got ${JSON.stringify(value)}`;
|
|
1432
|
+
for (const entry of value) {
|
|
1433
|
+
const failure = checkItem(key.item, entry);
|
|
1434
|
+
if (failure) return `\`${key.path}\`: ${failure}`;
|
|
1435
|
+
}
|
|
1436
|
+
return void 0;
|
|
1437
|
+
}
|
|
1438
|
+
function misdirectedTo(key, value) {
|
|
1439
|
+
if (key.namedValuesOf === void 0 || typeof value !== "string") return void 0;
|
|
1440
|
+
const other = CONFIG_BY_PATH.get(key.namedValuesOf);
|
|
1441
|
+
if (!other || other.shape !== "scalar") return void 0;
|
|
1442
|
+
return checkItem(other.item, value) === void 0 ? other : void 0;
|
|
1443
|
+
}
|
|
1444
|
+
function validateConfig(input, surface) {
|
|
1445
|
+
const issues = [];
|
|
1446
|
+
const config = {};
|
|
1447
|
+
if (input === void 0 || input === null) return {
|
|
1448
|
+
config,
|
|
1449
|
+
issues
|
|
1450
|
+
};
|
|
1451
|
+
if (!isPlainObject(input)) {
|
|
1452
|
+
issues.push({
|
|
1453
|
+
path: "",
|
|
1454
|
+
message: `configuration (${surface}) must be an object`
|
|
1455
|
+
});
|
|
1456
|
+
return {
|
|
1457
|
+
config,
|
|
1458
|
+
issues
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
walk(input, "", config, issues, surface);
|
|
1462
|
+
return {
|
|
1463
|
+
config,
|
|
1464
|
+
issues
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
function walk(source, prefix, target, issues, surface) {
|
|
1468
|
+
for (const [name, value] of Object.entries(source)) {
|
|
1469
|
+
if (value === void 0) continue;
|
|
1470
|
+
const path = prefix ? `${prefix}.${name}` : name;
|
|
1471
|
+
const key = CONFIG_BY_PATH.get(path);
|
|
1472
|
+
if (key) {
|
|
1473
|
+
const failure = checkValue(key, value);
|
|
1474
|
+
if (failure) {
|
|
1475
|
+
const hint = misdirectedHint(key, value, (p, v) => `"${p}": ${JSON.stringify(v)}`);
|
|
1476
|
+
issues.push({
|
|
1477
|
+
path,
|
|
1478
|
+
message: `${failure} (${surface})${hint}`
|
|
1479
|
+
});
|
|
1480
|
+
} else target[name] = value;
|
|
1481
|
+
continue;
|
|
1482
|
+
}
|
|
1483
|
+
if (CONFIG_PREFIXES.has(path)) {
|
|
1484
|
+
if (isPlainObject(value)) {
|
|
1485
|
+
const nested = {};
|
|
1486
|
+
walk(value, path, nested, issues, surface);
|
|
1487
|
+
if (Object.keys(nested).length > 0) target[name] = nested;
|
|
1488
|
+
continue;
|
|
1489
|
+
}
|
|
1490
|
+
const enabled = groupBooleanKey(path);
|
|
1491
|
+
if (enabled && typeof value === "boolean") {
|
|
1492
|
+
target[name] = { enabled: value };
|
|
1493
|
+
continue;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
issues.push({
|
|
1497
|
+
path,
|
|
1498
|
+
message: unknownKeyMessage(path, surface)
|
|
1499
|
+
});
|
|
1602
1500
|
}
|
|
1603
|
-
];
|
|
1604
|
-
const EMPTY_CONFIG = {};
|
|
1605
|
-
const CONFIG_BY_PATH = new Map(CONFIG_KEYS.map((k) => [k.path, k]));
|
|
1606
|
-
const CONFIG_PREFIXES = new Set(CONFIG_KEYS.flatMap((k) => {
|
|
1607
|
-
const parts = k.path.split(".");
|
|
1608
|
-
return parts.slice(0, -1).map((_, i) => parts.slice(0, i + 1).join("."));
|
|
1609
|
-
}));
|
|
1610
|
-
const CONFIG_CHILDREN = new Map([...CONFIG_PREFIXES].map((prefix) => [prefix, CONFIG_KEYS.filter((k) => k.path.startsWith(`${prefix}.`)).map((k) => k.path)]));
|
|
1611
|
-
const BY_NORMALIZED = new Map(CONFIG_KEYS.map((k) => [normalizeKey(k.path), k]));
|
|
1612
|
-
const CHILDREN_BY_NORMALIZED = new Map([...CONFIG_CHILDREN].map(([prefix, children]) => [normalizeKey(prefix), children]));
|
|
1613
|
-
const PREFIX_BY_NORMALIZED = new Map([...CONFIG_PREFIXES].map((prefix) => [normalizeKey(prefix), prefix]));
|
|
1614
|
-
function normalizeKey(key) {
|
|
1615
|
-
return key.replace(/[-_.]/g, "").toLowerCase();
|
|
1616
|
-
}
|
|
1617
|
-
function groupBooleanKey(path) {
|
|
1618
|
-
if (!CONFIG_PREFIXES.has(path)) return void 0;
|
|
1619
|
-
return CONFIG_BY_PATH.get(`${path}.enabled`);
|
|
1620
1501
|
}
|
|
1621
|
-
function
|
|
1622
|
-
const
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
const
|
|
1626
|
-
|
|
1627
|
-
if (!best || distance < best.distance) best = {
|
|
1628
|
-
name: candidate,
|
|
1629
|
-
distance
|
|
1630
|
-
};
|
|
1502
|
+
function deepMerge(base, override) {
|
|
1503
|
+
const out = { ...base };
|
|
1504
|
+
for (const [name, value] of Object.entries(override)) {
|
|
1505
|
+
if (value === void 0) continue;
|
|
1506
|
+
const existing = out[name];
|
|
1507
|
+
out[name] = isPlainObject(value) && isPlainObject(existing) ? deepMerge(existing, value) : value;
|
|
1631
1508
|
}
|
|
1632
|
-
return
|
|
1633
|
-
}
|
|
1634
|
-
function suggestKey(key) {
|
|
1635
|
-
const exact = BY_NORMALIZED.get(normalizeKey(key));
|
|
1636
|
-
if (exact) return exact.path;
|
|
1637
|
-
const near = nearestName(key, CONFIG_KEYS.map((k) => k.path));
|
|
1638
|
-
if (near) return near;
|
|
1639
|
-
const tail = normalizeKey(key);
|
|
1640
|
-
const owners = CONFIG_KEYS.filter((k) => normalizeKey(k.path.slice(k.path.lastIndexOf(".") + 1)) === tail);
|
|
1641
|
-
return owners.length === 1 ? owners[0].path : void 0;
|
|
1509
|
+
return out;
|
|
1642
1510
|
}
|
|
1643
|
-
function
|
|
1644
|
-
|
|
1645
|
-
const key = BY_NORMALIZED.get(norm$1);
|
|
1646
|
-
if (key) return { key: key.path };
|
|
1647
|
-
const group$1 = CHILDREN_BY_NORMALIZED.get(norm$1);
|
|
1648
|
-
return group$1 && group$1.length > 0 ? { group: group$1 } : void 0;
|
|
1511
|
+
function mergeInto(base, override) {
|
|
1512
|
+
return deepMerge(base, override);
|
|
1649
1513
|
}
|
|
1650
|
-
function
|
|
1651
|
-
|
|
1652
|
-
const next = new Array(b.length + 1);
|
|
1653
|
-
for (let j = 0; j <= b.length; j++) prev[j] = j;
|
|
1654
|
-
for (let i = 1; i <= a.length; i++) {
|
|
1655
|
-
next[0] = i;
|
|
1656
|
-
for (let j = 1; j <= b.length; j++) next[j] = Math.min(prev[j] + 1, next[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
1657
|
-
for (let j = 0; j <= b.length; j++) prev[j] = next[j];
|
|
1658
|
-
}
|
|
1659
|
-
return prev[b.length];
|
|
1514
|
+
function mergeConfig(base, override) {
|
|
1515
|
+
return deepMerge(base, override);
|
|
1660
1516
|
}
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
const
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
const tail$1 = group$1.length === 1 ? `did you mean ${list}?` : `its keys are ${list}`;
|
|
1667
|
-
const canonical = PREFIX_BY_NORMALIZED.get(normalizeKey(path));
|
|
1668
|
-
const enabled = canonical ? groupBooleanKey(canonical) : void 0;
|
|
1669
|
-
const rule = enabled ? `; a boolean here means \`${enabled.path}\`` : "";
|
|
1670
|
-
return `\`${path}\` is a group of configuration keys, not a key itself (${surface}) — ${tail$1}${rule}`;
|
|
1517
|
+
function getPath(config, path) {
|
|
1518
|
+
let current = config;
|
|
1519
|
+
for (const part of path.split(".")) {
|
|
1520
|
+
if (!isPlainObject(current)) return void 0;
|
|
1521
|
+
current = current[part];
|
|
1671
1522
|
}
|
|
1672
|
-
|
|
1673
|
-
const tail = suggestion ? ` — did you mean \`${suggestion}\`?` : ` — see ${DOCS_URL}`;
|
|
1674
|
-
return `unknown configuration key \`${path}\` (${surface})${tail}`;
|
|
1523
|
+
return current;
|
|
1675
1524
|
}
|
|
1676
|
-
function
|
|
1677
|
-
return
|
|
1525
|
+
function nest(path, value) {
|
|
1526
|
+
return path.split(".").reduceRight((inner, segment) => ({ [segment]: inner }), value);
|
|
1678
1527
|
}
|
|
1679
|
-
function
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
case "enum": return `one of: ${item.values.join(", ")}`;
|
|
1686
|
-
case "seed": return "a number, any string, or \"git\"";
|
|
1687
|
-
case "reserved": return "a name, or { \"glob\": \"…\", \"names\": [\"…\"] }";
|
|
1688
|
-
case "region": return "an object with numeric `start` and `end`";
|
|
1528
|
+
function setPath(target, path, value) {
|
|
1529
|
+
const parts = path.split(".");
|
|
1530
|
+
let node = target;
|
|
1531
|
+
for (const part of parts.slice(0, -1)) {
|
|
1532
|
+
if (!isPlainObject(node[part])) node[part] = {};
|
|
1533
|
+
node = node[part];
|
|
1689
1534
|
}
|
|
1535
|
+
node[parts[parts.length - 1]] = value;
|
|
1690
1536
|
}
|
|
1691
|
-
function
|
|
1692
|
-
const
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1537
|
+
function toEngineConfig(config) {
|
|
1538
|
+
const out = {};
|
|
1539
|
+
for (const key of CONFIG_KEYS) {
|
|
1540
|
+
if (key.surface !== "engine") continue;
|
|
1541
|
+
const value = getPath(config, key.path);
|
|
1542
|
+
if (value === void 0) continue;
|
|
1543
|
+
setPath(out, key.path, value);
|
|
1544
|
+
}
|
|
1545
|
+
return out;
|
|
1696
1546
|
}
|
|
1697
|
-
|
|
1698
|
-
const
|
|
1699
|
-
const
|
|
1700
|
-
const isNonNegative = (v) => isFiniteNumber(v) && v >= 0;
|
|
1701
|
-
function group(fields) {
|
|
1547
|
+
function toPluginOptions(config) {
|
|
1548
|
+
const read = (path) => getPath(config, path);
|
|
1549
|
+
const set = (container) => Object.values(container).some((v) => v !== void 0) ? container : void 0;
|
|
1702
1550
|
return {
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1551
|
+
artifactOptions: {
|
|
1552
|
+
protectionMap: set({ enabled: read("protectionMap.enabled") }),
|
|
1553
|
+
sourceMap: set({
|
|
1554
|
+
enabled: read("sourceMap.enabled"),
|
|
1555
|
+
emitUrl: read("sourceMap.emitUrl")
|
|
1556
|
+
}),
|
|
1557
|
+
build: set({
|
|
1558
|
+
backup: read("build.backup"),
|
|
1559
|
+
mode: read("build.mode")
|
|
1560
|
+
}),
|
|
1561
|
+
allowUnobfuscated: read("allowUnobfuscated"),
|
|
1562
|
+
telemetry: set({ enabled: read("telemetry.enabled") })
|
|
1563
|
+
},
|
|
1564
|
+
seed: read("seed"),
|
|
1565
|
+
preset: read("preset"),
|
|
1566
|
+
complexity: read("complexity"),
|
|
1567
|
+
regions: read("regions"),
|
|
1568
|
+
directives: {
|
|
1569
|
+
enabled: read("directives.enabled") ?? DIRECTIVES_ENABLED_DEFAULT,
|
|
1570
|
+
explicit: read("directives.enabled") !== void 0
|
|
1571
|
+
},
|
|
1572
|
+
build: set({ autorun: read("build.autorun") }),
|
|
1573
|
+
diagnostics: set({
|
|
1574
|
+
level: read("diagnostics.level"),
|
|
1575
|
+
format: read("diagnostics.format")
|
|
1576
|
+
}),
|
|
1577
|
+
paths: set({ include: read("paths.include") }),
|
|
1578
|
+
key: read("key")
|
|
1706
1579
|
};
|
|
1707
1580
|
}
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
}
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
}
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1581
|
+
|
|
1582
|
+
//#endregion
|
|
1583
|
+
//#region src/source-map.ts
|
|
1584
|
+
const SOURCE_MAPPING_URL_RE = /\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/gm;
|
|
1585
|
+
const SOURCE_MAPPING_URL_LINE_RE = /^[ \t]*\/\/[#@]\s*sourceMappingURL=\S+[ \t]*\r?\n?/gm;
|
|
1586
|
+
const CSS_SOURCE_MAPPING_URL_RE = /[ \t]*\/\*[#@]\s*sourceMappingURL=[^\s*]+\s*\*\/\s*$/;
|
|
1587
|
+
function withSourceMappingURL(code, url) {
|
|
1588
|
+
const stripped = code.replace(SOURCE_MAPPING_URL_LINE_RE, "");
|
|
1589
|
+
if (url == null) return stripped;
|
|
1590
|
+
const separator = stripped.length > 0 && !stripped.endsWith("\n") ? "\n" : "";
|
|
1591
|
+
return `${stripped}${separator}//# sourceMappingURL=${url}\n`;
|
|
1592
|
+
}
|
|
1593
|
+
function withoutCssSourceMappingURL(css) {
|
|
1594
|
+
return css.replace(CSS_SOURCE_MAPPING_URL_RE, "");
|
|
1595
|
+
}
|
|
1596
|
+
function extractSourceMappingURL(code) {
|
|
1597
|
+
let last = null;
|
|
1598
|
+
for (const match of code.matchAll(SOURCE_MAPPING_URL_RE)) last = match[1];
|
|
1599
|
+
return last;
|
|
1600
|
+
}
|
|
1601
|
+
function isRemoteSourceMappingURL(url) {
|
|
1602
|
+
return /^https?:\/\//i.test(url) || url.startsWith("//");
|
|
1603
|
+
}
|
|
1604
|
+
function decodeDataUri(url) {
|
|
1605
|
+
if (!url.startsWith("data:")) return null;
|
|
1606
|
+
const comma = url.indexOf(",");
|
|
1607
|
+
if (comma === -1) return null;
|
|
1608
|
+
const meta = url.slice(5, comma);
|
|
1609
|
+
const payload = url.slice(comma + 1);
|
|
1610
|
+
try {
|
|
1611
|
+
if (/;base64/i.test(meta)) return Buffer.from(payload, "base64").toString("utf8");
|
|
1612
|
+
return decodeURIComponent(payload);
|
|
1613
|
+
} catch {
|
|
1614
|
+
return null;
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
function discoverInputSourceMap(jsPath, code) {
|
|
1618
|
+
const adjacent = `${jsPath}.map`;
|
|
1619
|
+
if ((0, node_fs.existsSync)(adjacent)) try {
|
|
1620
|
+
return (0, node_fs.readFileSync)(adjacent, "utf8");
|
|
1621
|
+
} catch {}
|
|
1622
|
+
let source = code;
|
|
1623
|
+
if (source == null) try {
|
|
1624
|
+
source = (0, node_fs.readFileSync)(jsPath, "utf8");
|
|
1625
|
+
} catch {
|
|
1626
|
+
return null;
|
|
1627
|
+
}
|
|
1628
|
+
const url = extractSourceMappingURL(source);
|
|
1629
|
+
if (url == null) return null;
|
|
1630
|
+
if (url.startsWith("data:")) return decodeDataUri(url);
|
|
1631
|
+
if (isRemoteSourceMappingURL(url)) return null;
|
|
1632
|
+
let mapPath;
|
|
1633
|
+
try {
|
|
1634
|
+
mapPath = (0, node_path.isAbsolute)(url) ? url : (0, node_path.resolve)((0, node_path.dirname)(jsPath), decodeURIComponent(url));
|
|
1635
|
+
} catch {
|
|
1636
|
+
return null;
|
|
1637
|
+
}
|
|
1638
|
+
if (!(0, node_fs.existsSync)(mapPath)) return null;
|
|
1639
|
+
try {
|
|
1640
|
+
return (0, node_fs.readFileSync)(mapPath, "utf8");
|
|
1641
|
+
} catch {
|
|
1642
|
+
return null;
|
|
1744
1643
|
}
|
|
1745
|
-
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
//#endregion
|
|
1647
|
+
//#region src/bundle.ts
|
|
1648
|
+
const BUNDLE_JS_RE = /\.[cm]?js$/;
|
|
1649
|
+
const CSS_MAP_RE = /\.css\.map$/;
|
|
1650
|
+
function collectBundleJs(bundle, outDir) {
|
|
1651
|
+
const files = [];
|
|
1652
|
+
const inputs = new Map();
|
|
1653
|
+
const entries = new Map();
|
|
1654
|
+
for (const [fileName, entry] of Object.entries(bundle)) {
|
|
1655
|
+
if (!BUNDLE_JS_RE.test(fileName)) continue;
|
|
1656
|
+
const source = entry.type === "chunk" ? entry.code : typeof entry.source === "string" ? entry.source : void 0;
|
|
1657
|
+
if (source === void 0) continue;
|
|
1658
|
+
const filePath = (0, node_path.resolve)(outDir, fileName);
|
|
1659
|
+
files.push(filePath);
|
|
1660
|
+
entries.set(filePath, entry);
|
|
1661
|
+
inputs.set(filePath, {
|
|
1662
|
+
source,
|
|
1663
|
+
sourceMap: entry.map ? entry.map.toString() : null
|
|
1664
|
+
});
|
|
1756
1665
|
}
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1666
|
+
return {
|
|
1667
|
+
files,
|
|
1668
|
+
inputs,
|
|
1669
|
+
entries
|
|
1670
|
+
};
|
|
1760
1671
|
}
|
|
1761
|
-
function
|
|
1762
|
-
for (const
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1672
|
+
function sourceTypeForBundle(format, entries) {
|
|
1673
|
+
for (const entry of entries) if (entry.type !== "chunk") return void 0;
|
|
1674
|
+
return sourceTypeForOutputFormat(format);
|
|
1675
|
+
}
|
|
1676
|
+
function dataUri(json) {
|
|
1677
|
+
return `data:application/json;charset=utf-8;base64,${Buffer.from(json, "utf8").toString("base64")}`;
|
|
1678
|
+
}
|
|
1679
|
+
function applyBundleOutput(bundle, entry, out, policy) {
|
|
1680
|
+
if (entry.type !== "chunk") {
|
|
1681
|
+
entry.source = out.code;
|
|
1682
|
+
return;
|
|
1766
1683
|
}
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1684
|
+
const mapName = entry.sourcemapFileName ?? null;
|
|
1685
|
+
const inlineMap = mapName == null && entry.map != null;
|
|
1686
|
+
const emitMap = policy.sourceMap && out.sourceMap != null;
|
|
1687
|
+
let url = null;
|
|
1688
|
+
if (emitMap && out.sourceMap != null) {
|
|
1689
|
+
if (mapName != null && bundle[mapName]) {
|
|
1690
|
+
bundle[mapName].source = out.sourceMap;
|
|
1691
|
+
url = (0, node_path.basename)(mapName);
|
|
1692
|
+
} else if (inlineMap) url = dataUri(out.sourceMap);
|
|
1693
|
+
} else {
|
|
1694
|
+
if (mapName != null) delete bundle[mapName];
|
|
1695
|
+
entry.map = null;
|
|
1696
|
+
}
|
|
1697
|
+
entry.code = withSourceMappingURL(out.code, policy.emitSourceMappingURL ? url : null);
|
|
1698
|
+
}
|
|
1699
|
+
function stripBundleCssSourceMaps(bundle) {
|
|
1700
|
+
for (const [fileName, entry] of Object.entries(bundle)) {
|
|
1701
|
+
if (CSS_MAP_RE.test(fileName)) {
|
|
1702
|
+
delete bundle[fileName];
|
|
1771
1703
|
continue;
|
|
1772
1704
|
}
|
|
1773
|
-
if (
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
}
|
|
1705
|
+
if (entry.type !== "asset" || !fileName.endsWith(".css") || entry.source === void 0) continue;
|
|
1706
|
+
const css = typeof entry.source === "string" ? entry.source : Buffer.from(entry.source).toString("utf8");
|
|
1707
|
+
const stripped = withoutCssSourceMappingURL(css);
|
|
1708
|
+
if (stripped !== css) entry.source = stripped;
|
|
1778
1709
|
}
|
|
1779
|
-
return void 0;
|
|
1780
1710
|
}
|
|
1781
|
-
|
|
1782
|
-
|
|
1711
|
+
|
|
1712
|
+
//#endregion
|
|
1713
|
+
//#region src/glob.ts
|
|
1714
|
+
function normalize(value) {
|
|
1715
|
+
return value.includes("\\") ? value.replaceAll("\\", "/") : value;
|
|
1783
1716
|
}
|
|
1784
|
-
function
|
|
1785
|
-
|
|
1786
|
-
case "boolean": return typeof value === "boolean" ? void 0 : expected(item, value);
|
|
1787
|
-
case "number": {
|
|
1788
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return expected(item, value);
|
|
1789
|
-
if (value < item.min) return expected(item, value);
|
|
1790
|
-
if (item.integer && !Number.isInteger(value)) return expected(item, value);
|
|
1791
|
-
return void 0;
|
|
1792
|
-
}
|
|
1793
|
-
case "unbounded": {
|
|
1794
|
-
if (value === item.unlimited) return void 0;
|
|
1795
|
-
if (typeof value === "number" && Number.isFinite(value) && value >= item.min) return void 0;
|
|
1796
|
-
return `${expected(item, value)} — there is no \`Infinity\` in JSON; write "${item.unlimited}"`;
|
|
1797
|
-
}
|
|
1798
|
-
case "string": return typeof value === "string" ? void 0 : expected(item, value);
|
|
1799
|
-
case "enum": return typeof value === "string" && item.values.includes(value) ? void 0 : expected(item, value);
|
|
1800
|
-
case "seed": return typeof value === "number" || typeof value === "string" ? void 0 : expected(item, value);
|
|
1801
|
-
case "reserved": {
|
|
1802
|
-
if (typeof value === "string") return void 0;
|
|
1803
|
-
if (!isPlainObject(value)) return expected(item, value);
|
|
1804
|
-
return checkFields(RESERVED_FIELDS, value);
|
|
1805
|
-
}
|
|
1806
|
-
case "region": {
|
|
1807
|
-
if (!isPlainObject(value)) return expected(item, value);
|
|
1808
|
-
return checkFields(REGION_FIELDS, value);
|
|
1809
|
-
}
|
|
1810
|
-
}
|
|
1717
|
+
function segments(value) {
|
|
1718
|
+
return value.split("/").filter((part) => part !== "");
|
|
1811
1719
|
}
|
|
1812
|
-
function
|
|
1813
|
-
if (
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
if (!
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1720
|
+
function isAnchored(pattern) {
|
|
1721
|
+
if (pattern.startsWith("/")) return true;
|
|
1722
|
+
return pattern.length >= 2 && pattern[1] === ":" && /^[A-Za-z]$/.test(pattern[0]);
|
|
1723
|
+
}
|
|
1724
|
+
function segmentMatches(pattern, seg) {
|
|
1725
|
+
if (!pattern.includes("*") && !pattern.includes("?")) return pattern === seg;
|
|
1726
|
+
const p = [...pattern];
|
|
1727
|
+
const s = [...seg];
|
|
1728
|
+
let pi = 0;
|
|
1729
|
+
let si = 0;
|
|
1730
|
+
let star = -1;
|
|
1731
|
+
let starSi = 0;
|
|
1732
|
+
while (si < s.length) if (pi < p.length && p[pi] === "*") {
|
|
1733
|
+
star = pi;
|
|
1734
|
+
starSi = si;
|
|
1735
|
+
pi += 1;
|
|
1736
|
+
} else if (pi < p.length && (p[pi] === "?" || p[pi] === s[si])) {
|
|
1737
|
+
pi += 1;
|
|
1738
|
+
si += 1;
|
|
1739
|
+
} else if (star >= 0) {
|
|
1740
|
+
pi = star + 1;
|
|
1741
|
+
starSi += 1;
|
|
1742
|
+
si = starSi;
|
|
1743
|
+
} else return false;
|
|
1744
|
+
return p.slice(pi).every((c) => c === "*");
|
|
1745
|
+
}
|
|
1746
|
+
function segmentsMatch(pattern, path) {
|
|
1747
|
+
let pi = 0;
|
|
1748
|
+
let si = 0;
|
|
1749
|
+
let star = -1;
|
|
1750
|
+
let starSi = 0;
|
|
1751
|
+
while (si < path.length) if (pi < pattern.length && pattern[pi] === "**") {
|
|
1752
|
+
star = pi;
|
|
1753
|
+
starSi = si;
|
|
1754
|
+
pi += 1;
|
|
1755
|
+
} else if (pi < pattern.length && segmentMatches(pattern[pi], path[si])) {
|
|
1756
|
+
pi += 1;
|
|
1757
|
+
si += 1;
|
|
1758
|
+
} else if (star >= 0) {
|
|
1759
|
+
pi = star + 1;
|
|
1760
|
+
starSi += 1;
|
|
1761
|
+
si = starSi;
|
|
1762
|
+
} else return false;
|
|
1763
|
+
return pattern.slice(pi).every((s) => s === "**");
|
|
1764
|
+
}
|
|
1765
|
+
function matchesPath(pattern, path) {
|
|
1766
|
+
const pat = normalize(pattern);
|
|
1767
|
+
const parts = segments(pat);
|
|
1768
|
+
if (parts.length === 0) return false;
|
|
1769
|
+
const segs = segments(normalize(path));
|
|
1770
|
+
if (isAnchored(pat)) return segmentsMatch(parts, segs);
|
|
1771
|
+
for (let i = 0; i <= segs.length; i++) if (segmentsMatch(parts, segs.slice(i))) return true;
|
|
1772
|
+
return false;
|
|
1773
|
+
}
|
|
1774
|
+
function reachesInto(pattern, dir) {
|
|
1775
|
+
const pat = normalize(pattern);
|
|
1776
|
+
const parts = segments(pat);
|
|
1777
|
+
if (parts.length === 0) return false;
|
|
1778
|
+
if (!isAnchored(pat)) return true;
|
|
1779
|
+
for (let k = 1; k <= parts.length; k++) if (matchesPath(`/${parts.slice(0, k).join("/")}`, dir)) return true;
|
|
1780
|
+
return false;
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
//#endregion
|
|
1784
|
+
//#region src/collect.ts
|
|
1785
|
+
const BACKUP_RE = /\.backup\.[0-9a-f]{8}\.[cm]?js$/;
|
|
1786
|
+
const NODE_MODULES = "node_modules";
|
|
1787
|
+
function isCollectableJs(name) {
|
|
1788
|
+
if (BACKUP_RE.test(name)) return false;
|
|
1789
|
+
return name.endsWith(".js") || name.endsWith(".mjs") || name.endsWith(".cjs");
|
|
1790
|
+
}
|
|
1791
|
+
function statOrNull(path) {
|
|
1792
|
+
try {
|
|
1793
|
+
return (0, node_fs.statSync)(path);
|
|
1794
|
+
} catch {
|
|
1795
|
+
return null;
|
|
1821
1796
|
}
|
|
1822
|
-
return void 0;
|
|
1823
1797
|
}
|
|
1824
|
-
function
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
if (!
|
|
1828
|
-
return
|
|
1798
|
+
function collectJsFiles(target, options = {}) {
|
|
1799
|
+
const stat = statOrNull(target);
|
|
1800
|
+
if (!stat) return [];
|
|
1801
|
+
if (!stat.isDirectory()) return isCollectableJs((0, node_path.basename)(target)) ? [target] : [];
|
|
1802
|
+
return walkJs(target, options.include ?? [], false, options.onNestedProject);
|
|
1829
1803
|
}
|
|
1830
|
-
function
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
issues
|
|
1836
|
-
};
|
|
1837
|
-
if (!isPlainObject(input)) {
|
|
1838
|
-
issues.push({
|
|
1839
|
-
path: "",
|
|
1840
|
-
message: `configuration (${surface}) must be an object`
|
|
1841
|
-
});
|
|
1842
|
-
return {
|
|
1843
|
-
config,
|
|
1844
|
-
issues
|
|
1845
|
-
};
|
|
1804
|
+
function entriesOf(dir) {
|
|
1805
|
+
try {
|
|
1806
|
+
return (0, node_fs.readdirSync)(dir, { withFileTypes: true });
|
|
1807
|
+
} catch {
|
|
1808
|
+
return [];
|
|
1846
1809
|
}
|
|
1847
|
-
walk(input, "", config, issues, surface);
|
|
1848
|
-
return {
|
|
1849
|
-
config,
|
|
1850
|
-
issues
|
|
1851
|
-
};
|
|
1852
1810
|
}
|
|
1853
|
-
function
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
if (
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
continue;
|
|
1875
|
-
}
|
|
1876
|
-
const enabled = groupBooleanKey(path);
|
|
1877
|
-
if (enabled && typeof value === "boolean") {
|
|
1878
|
-
target[name] = { enabled: value };
|
|
1811
|
+
function kindOf(entry, full) {
|
|
1812
|
+
if (entry.isDirectory()) return "dir";
|
|
1813
|
+
if (entry.isFile()) return "file";
|
|
1814
|
+
const stat = statOrNull(full);
|
|
1815
|
+
if (!stat) return null;
|
|
1816
|
+
return stat.isDirectory() ? "dir" : "file";
|
|
1817
|
+
}
|
|
1818
|
+
function isNestedProject(dir) {
|
|
1819
|
+
return (0, node_fs.existsSync)((0, node_path.join)(dir, "package.json"));
|
|
1820
|
+
}
|
|
1821
|
+
function walkJs(dir, include, readmitted, onNestedProject) {
|
|
1822
|
+
const out = [];
|
|
1823
|
+
for (const entry of entriesOf(dir)) {
|
|
1824
|
+
const name = entry.name;
|
|
1825
|
+
const full = (0, node_path.join)(dir, name);
|
|
1826
|
+
const kind = kindOf(entry, full);
|
|
1827
|
+
if (kind === null) continue;
|
|
1828
|
+
if (kind === "dir") {
|
|
1829
|
+
const skipped = name === NODE_MODULES;
|
|
1830
|
+
if (!skipped && !readmitted && isNestedProject(full)) {
|
|
1831
|
+
onNestedProject?.(full);
|
|
1879
1832
|
continue;
|
|
1880
1833
|
}
|
|
1834
|
+
if (skipped && !include.some((pattern) => reachesInto(pattern, full))) continue;
|
|
1835
|
+
out.push(...walkJs(full, include, readmitted || skipped, onNestedProject));
|
|
1836
|
+
} else if (isCollectableJs(name)) {
|
|
1837
|
+
if (readmitted && !include.some((pattern) => matchesPath(pattern, full))) continue;
|
|
1838
|
+
out.push(full);
|
|
1881
1839
|
}
|
|
1882
|
-
issues.push({
|
|
1883
|
-
path,
|
|
1884
|
-
message: unknownKeyMessage(path, surface)
|
|
1885
|
-
});
|
|
1886
1840
|
}
|
|
1841
|
+
return out;
|
|
1887
1842
|
}
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1843
|
+
const SOURCE_MAP_RE = /\.(?:[cm]?js|css)\.map$/;
|
|
1844
|
+
function collectFiles(dir, accept) {
|
|
1845
|
+
if (!(0, node_fs.existsSync)(dir)) return [];
|
|
1846
|
+
const out = [];
|
|
1847
|
+
for (const entry of entriesOf(dir)) {
|
|
1848
|
+
const name = entry.name;
|
|
1849
|
+
const full = (0, node_path.join)(dir, name);
|
|
1850
|
+
const kind = kindOf(entry, full);
|
|
1851
|
+
if (kind === null) continue;
|
|
1852
|
+
if (kind === "dir") out.push(...collectFiles(full, accept));
|
|
1853
|
+
else if (accept(name)) out.push(full);
|
|
1894
1854
|
}
|
|
1895
1855
|
return out;
|
|
1896
1856
|
}
|
|
1897
|
-
function
|
|
1898
|
-
return
|
|
1857
|
+
function collectSourceMaps(dir) {
|
|
1858
|
+
return collectFiles(dir, (name) => SOURCE_MAP_RE.test(name));
|
|
1899
1859
|
}
|
|
1900
|
-
function
|
|
1901
|
-
return
|
|
1860
|
+
function collectStylesheets(dir) {
|
|
1861
|
+
return collectFiles(dir, (name) => name.endsWith(".css"));
|
|
1902
1862
|
}
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1863
|
+
|
|
1864
|
+
//#endregion
|
|
1865
|
+
//#region src/notices.ts
|
|
1866
|
+
const NOTICE_MESSAGE_LIMIT = 500;
|
|
1867
|
+
const NOTICE_LIMIT = 5;
|
|
1868
|
+
const NOTICE_CODE_LIMIT = 64;
|
|
1869
|
+
const NOTICE_HOSTS = new Set(["afterpack.dev", "www.afterpack.dev"]);
|
|
1870
|
+
const ESC = 27;
|
|
1871
|
+
const BEL = 7;
|
|
1872
|
+
const C1_CSI = 155;
|
|
1873
|
+
const C1_OSC = 157;
|
|
1874
|
+
function isControl(code) {
|
|
1875
|
+
return code < 32 || code >= 127 && code <= 159;
|
|
1876
|
+
}
|
|
1877
|
+
function isInvisibleFormat(code) {
|
|
1878
|
+
return code === 1564 || code >= 8203 && code <= 8207 || code >= 8234 && code <= 8238 || code >= 8288 && code <= 8297 || code === 65279;
|
|
1879
|
+
}
|
|
1880
|
+
function skipCsi(text, from) {
|
|
1881
|
+
let i = from;
|
|
1882
|
+
while (i < text.length) {
|
|
1883
|
+
const c = text.charCodeAt(i);
|
|
1884
|
+
i += 1;
|
|
1885
|
+
if (c >= 64 && c <= 126) return i;
|
|
1908
1886
|
}
|
|
1909
|
-
return
|
|
1887
|
+
return i;
|
|
1910
1888
|
}
|
|
1911
|
-
function
|
|
1912
|
-
|
|
1889
|
+
function skipOsc(text, from) {
|
|
1890
|
+
let i = from;
|
|
1891
|
+
while (i < text.length) {
|
|
1892
|
+
const c = text.charCodeAt(i);
|
|
1893
|
+
if (c === BEL) return i + 1;
|
|
1894
|
+
if (c === ESC) return text.charCodeAt(i + 1) === 92 ? i + 2 : i + 1;
|
|
1895
|
+
if (c === 156) return i + 1;
|
|
1896
|
+
i += 1;
|
|
1897
|
+
}
|
|
1898
|
+
return i;
|
|
1913
1899
|
}
|
|
1914
|
-
function
|
|
1915
|
-
|
|
1916
|
-
let
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1900
|
+
function stripTerminalControls(text) {
|
|
1901
|
+
let out = "";
|
|
1902
|
+
let i = 0;
|
|
1903
|
+
while (i < text.length) {
|
|
1904
|
+
const c = text.charCodeAt(i);
|
|
1905
|
+
if (c === ESC) {
|
|
1906
|
+
const next = text.charCodeAt(i + 1);
|
|
1907
|
+
if (next === 91) i = skipCsi(text, i + 2);
|
|
1908
|
+
else if (next === 93) i = skipOsc(text, i + 2);
|
|
1909
|
+
else i += Number.isNaN(next) ? 1 : 2;
|
|
1910
|
+
continue;
|
|
1911
|
+
}
|
|
1912
|
+
if (c === C1_CSI) {
|
|
1913
|
+
i = skipCsi(text, i + 1);
|
|
1914
|
+
continue;
|
|
1915
|
+
}
|
|
1916
|
+
if (c === C1_OSC) {
|
|
1917
|
+
i = skipOsc(text, i + 1);
|
|
1918
|
+
continue;
|
|
1919
|
+
}
|
|
1920
|
+
if (isControl(c)) {
|
|
1921
|
+
out += c === 9 || c === 10 || c === 13 ? " " : "";
|
|
1922
|
+
i += 1;
|
|
1923
|
+
continue;
|
|
1924
|
+
}
|
|
1925
|
+
if (c === 8232 || c === 8233) {
|
|
1926
|
+
out += " ";
|
|
1927
|
+
i += 1;
|
|
1928
|
+
continue;
|
|
1929
|
+
}
|
|
1930
|
+
if (isInvisibleFormat(c)) {
|
|
1931
|
+
i += 1;
|
|
1932
|
+
continue;
|
|
1933
|
+
}
|
|
1934
|
+
out += text[i];
|
|
1935
|
+
i += 1;
|
|
1920
1936
|
}
|
|
1921
|
-
|
|
1937
|
+
return out;
|
|
1922
1938
|
}
|
|
1923
|
-
function
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1939
|
+
function sanitizeServerText(value, limit = NOTICE_MESSAGE_LIMIT) {
|
|
1940
|
+
if (typeof value !== "string") return "";
|
|
1941
|
+
const clean = stripTerminalControls(value).replace(/\s+/g, " ").trim();
|
|
1942
|
+
if (clean.length <= limit) return clean;
|
|
1943
|
+
const chars = Array.from(clean);
|
|
1944
|
+
return chars.length > limit ? chars.slice(0, limit).join("") : clean;
|
|
1945
|
+
}
|
|
1946
|
+
function sanitizeNoticeUrl(value) {
|
|
1947
|
+
if (typeof value !== "string") return null;
|
|
1948
|
+
let url;
|
|
1949
|
+
try {
|
|
1950
|
+
url = new URL(value.trim());
|
|
1951
|
+
} catch {
|
|
1952
|
+
return null;
|
|
1953
|
+
}
|
|
1954
|
+
if (url.protocol !== "https:") return null;
|
|
1955
|
+
if (!NOTICE_HOSTS.has(url.hostname) || url.port !== "") return null;
|
|
1956
|
+
if (url.username !== "" || url.password !== "") return null;
|
|
1957
|
+
const href = url.href;
|
|
1958
|
+
return href.length <= NOTICE_MESSAGE_LIMIT ? href : null;
|
|
1959
|
+
}
|
|
1960
|
+
function noticeSeverity(value) {
|
|
1961
|
+
if (value === "info") return "info";
|
|
1962
|
+
if (value === "error" || value === "critical") return "error";
|
|
1963
|
+
return "warning";
|
|
1964
|
+
}
|
|
1965
|
+
function sanitizeNotices(raw) {
|
|
1966
|
+
if (!Array.isArray(raw)) return [];
|
|
1967
|
+
const out = [];
|
|
1968
|
+
for (const entry of raw) {
|
|
1969
|
+
if (out.length >= NOTICE_LIMIT) break;
|
|
1970
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
1971
|
+
const n = entry;
|
|
1972
|
+
const message = sanitizeServerText(n.message);
|
|
1973
|
+
if (message === "") continue;
|
|
1974
|
+
out.push({
|
|
1975
|
+
severity: noticeSeverity(n.severity),
|
|
1976
|
+
code: sanitizeServerText(n.code, NOTICE_CODE_LIMIT),
|
|
1977
|
+
message,
|
|
1978
|
+
url: sanitizeNoticeUrl(n.url)
|
|
1979
|
+
});
|
|
1930
1980
|
}
|
|
1931
1981
|
return out;
|
|
1932
1982
|
}
|
|
1933
|
-
function
|
|
1934
|
-
const
|
|
1935
|
-
const
|
|
1936
|
-
return {
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
}),
|
|
1947
|
-
allowUnobfuscated: read("allowUnobfuscated"),
|
|
1948
|
-
telemetry: set({ enabled: read("telemetry.enabled") })
|
|
1949
|
-
},
|
|
1950
|
-
seed: read("seed"),
|
|
1951
|
-
preset: read("preset"),
|
|
1952
|
-
complexity: read("complexity"),
|
|
1953
|
-
regions: read("regions"),
|
|
1954
|
-
directives: {
|
|
1955
|
-
enabled: read("directives.enabled") ?? DIRECTIVES_ENABLED_DEFAULT,
|
|
1956
|
-
explicit: read("directives.enabled") !== void 0
|
|
1957
|
-
},
|
|
1958
|
-
build: set({ autorun: read("build.autorun") }),
|
|
1959
|
-
diagnostics: set({
|
|
1960
|
-
level: read("diagnostics.level"),
|
|
1961
|
-
format: read("diagnostics.format")
|
|
1962
|
-
}),
|
|
1963
|
-
paths: set({ include: read("paths.include") }),
|
|
1964
|
-
key: read("key")
|
|
1965
|
-
};
|
|
1983
|
+
function formatNotice(notice) {
|
|
1984
|
+
const head = notice.severity === "info" ? "AfterPack notice" : `AfterPack ${notice.severity}`;
|
|
1985
|
+
const tail = notice.url ? ` ${notice.url}` : "";
|
|
1986
|
+
return `${head}: ${notice.message}${tail}`;
|
|
1987
|
+
}
|
|
1988
|
+
function reportNotices(raw, logger, prefix = (m) => m) {
|
|
1989
|
+
const notices = sanitizeNotices(raw);
|
|
1990
|
+
for (const notice of notices) {
|
|
1991
|
+
const line = prefix(formatNotice(notice));
|
|
1992
|
+
if (notice.severity === "info" || !logger.warn) logger.log(line);
|
|
1993
|
+
else logger.warn(line);
|
|
1994
|
+
}
|
|
1995
|
+
return notices;
|
|
1966
1996
|
}
|
|
1967
1997
|
|
|
1968
1998
|
//#endregion
|
|
1969
1999
|
//#region src/compat.ts
|
|
1970
2000
|
const MIN_CORE_VERSION = "0.2.0";
|
|
2001
|
+
const SOURCE_TYPE_MIN_CORE_VERSION = "0.2.3";
|
|
1971
2002
|
const CORE_PACKAGE = "@afterpack/core";
|
|
1972
2003
|
const VERSION_PATTERN$1 = /^\d{1,9}\.\d{1,9}\.\d{1,9}(?:[-+][0-9A-Za-z.+-]{1,64})?$/;
|
|
1973
2004
|
const PACKAGE_NAME_PATTERN = /^(?:@[a-z0-9][a-z0-9._-]{0,63}\/)?[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
@@ -2039,6 +2070,9 @@ function isBelowVersion(version, minimum) {
|
|
|
2039
2070
|
for (let i = 0; i < 3; i++) if (have[i] !== need[i]) return have[i] < need[i];
|
|
2040
2071
|
return false;
|
|
2041
2072
|
}
|
|
2073
|
+
function coreSupportsSourceType(coreVersion) {
|
|
2074
|
+
return releaseTriple(coreVersion) !== null && !isBelowVersion(coreVersion, SOURCE_TYPE_MIN_CORE_VERSION);
|
|
2075
|
+
}
|
|
2042
2076
|
const CLI_PACKAGE = "afterpack";
|
|
2043
2077
|
function updateCommand(identity, minVersion) {
|
|
2044
2078
|
const core = `${CORE_PACKAGE}@${safeVersionString(minVersion) ?? "latest"}`;
|
|
@@ -2715,6 +2749,7 @@ function buildEngineConfig(options) {
|
|
|
2715
2749
|
if (options.complexity !== void 0) defaults.complexity = options.complexity;
|
|
2716
2750
|
if (options.preset !== void 0) defaults.preset = options.preset;
|
|
2717
2751
|
else if (options.complexity === void 0) defaults.preset = DEFAULT_PRESET;
|
|
2752
|
+
if (options.sourceType !== void 0) defaults.sourceType = options.sourceType;
|
|
2718
2753
|
const config = mergeInto({
|
|
2719
2754
|
...defaults,
|
|
2720
2755
|
sourceMap,
|
|
@@ -4256,11 +4291,14 @@ async function runObfuscationPass(options) {
|
|
|
4256
4291
|
env,
|
|
4257
4292
|
cwd
|
|
4258
4293
|
});
|
|
4294
|
+
const localEngineVersion = await readEngineVersion(engine);
|
|
4295
|
+
const derivedSourceType = options.sourceType !== void 0 && coreSupportsSourceType(localEngineVersion ?? options.client?.coreVersion) ? options.sourceType : void 0;
|
|
4259
4296
|
const engineConfig = buildEngineConfig({
|
|
4260
4297
|
policy,
|
|
4261
4298
|
seed,
|
|
4262
4299
|
preset: options.preset,
|
|
4263
4300
|
complexity: options.complexity,
|
|
4301
|
+
sourceType: derivedSourceType,
|
|
4264
4302
|
regions: capture.regions,
|
|
4265
4303
|
engine: options.engineConfig,
|
|
4266
4304
|
renameGlobals: capture.renameGlobals
|
|
@@ -4298,7 +4336,7 @@ async function runObfuscationPass(options) {
|
|
|
4298
4336
|
if (collected.unknownFiles > 0 && batch.source === "cloud") logger.warn(prefix("engine diagnostics are not returned on the Pro Cloud path — this build's diagnostics were recorded server-side."));
|
|
4299
4337
|
const telemetry = options.telemetry;
|
|
4300
4338
|
const telemetryEnabled = telemetry != null && resolveTelemetryEnabled(artifactOptions.telemetry?.enabled, env);
|
|
4301
|
-
const engineVersion = (batch.source === "cloud" ? safeVersionString(batch.engineVersion) : null) ??
|
|
4339
|
+
const engineVersion = (batch.source === "cloud" ? safeVersionString(batch.engineVersion) : null) ?? localEngineVersion;
|
|
4302
4340
|
if (telemetry && telemetryEnabled) {
|
|
4303
4341
|
const facts = {
|
|
4304
4342
|
label,
|
|
@@ -4658,6 +4696,8 @@ exports.sanitizeNotices = sanitizeNotices;
|
|
|
4658
4696
|
exports.scanDirectives = scanDirectives;
|
|
4659
4697
|
exports.serverAddsInfo = serverAddsInfo;
|
|
4660
4698
|
exports.sha256Of = sha256Of;
|
|
4699
|
+
exports.sourceTypeForBundle = sourceTypeForBundle;
|
|
4700
|
+
exports.sourceTypeForOutputFormat = sourceTypeForOutputFormat;
|
|
4661
4701
|
exports.stripBundleCssSourceMaps = stripBundleCssSourceMaps;
|
|
4662
4702
|
exports.toEngineConfig = toEngineConfig;
|
|
4663
4703
|
exports.toPluginOptions = toPluginOptions;
|