@sanity/pkg-utils 7.4.0-canary.2 → 7.4.1-canary.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-cjs/findConfigFile.cjs +24 -0
- package/dist/_chunks-cjs/findConfigFile.cjs.map +1 -0
- package/dist/_chunks-cjs/index.cjs +231 -1910
- package/dist/_chunks-cjs/index.cjs.map +1 -1
- package/dist/_chunks-cjs/index2.cjs +781 -0
- package/dist/_chunks-cjs/index2.cjs.map +1 -0
- package/dist/_chunks-cjs/index3.cjs +50 -0
- package/dist/_chunks-cjs/index3.cjs.map +1 -0
- package/dist/_chunks-cjs/printPackageTree.cjs +42 -0
- package/dist/_chunks-cjs/printPackageTree.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveBuildContext.cjs +176 -0
- package/dist/_chunks-cjs/resolveBuildContext.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveBuildTasks.cjs +111 -0
- package/dist/_chunks-cjs/resolveBuildTasks.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveConfigProperty.cjs +6 -0
- package/dist/_chunks-cjs/resolveConfigProperty.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveWatchTasks.cjs +79 -0
- package/dist/_chunks-cjs/resolveWatchTasks.cjs.map +1 -0
- package/dist/_chunks-cjs/template.cjs +446 -0
- package/dist/_chunks-cjs/template.cjs.map +1 -0
- package/dist/_chunks-cjs/validatePkg.cjs +71 -0
- package/dist/_chunks-cjs/validatePkg.cjs.map +1 -0
- package/dist/_chunks-cjs/watchConfigFiles.cjs +59 -0
- package/dist/_chunks-cjs/watchConfigFiles.cjs.map +1 -0
- package/dist/_chunks-es/build.js +7 -107
- package/dist/_chunks-es/build.js.map +1 -1
- package/dist/_chunks-es/check.js +23 -44
- package/dist/_chunks-es/check.js.map +1 -1
- package/dist/_chunks-es/fileExists.js +12 -0
- package/dist/_chunks-es/fileExists.js.map +1 -0
- package/dist/_chunks-es/findConfigFile.js +23 -0
- package/dist/_chunks-es/findConfigFile.js.map +1 -0
- package/dist/_chunks-es/index.js +180 -754
- package/dist/_chunks-es/index.js.map +1 -1
- package/dist/_chunks-es/index2.js +769 -0
- package/dist/_chunks-es/index2.js.map +1 -0
- package/dist/_chunks-es/index3.js +51 -0
- package/dist/_chunks-es/index3.js.map +1 -0
- package/dist/_chunks-es/init.js +9 -493
- package/dist/_chunks-es/init.js.map +1 -1
- package/dist/_chunks-es/loadPkgWithReporting.js +100 -0
- package/dist/_chunks-es/loadPkgWithReporting.js.map +1 -0
- package/dist/_chunks-es/{isRecord.js → logger.js} +2 -15
- package/dist/_chunks-es/logger.js.map +1 -0
- package/dist/_chunks-es/printPackageTree.js +44 -0
- package/dist/_chunks-es/printPackageTree.js.map +1 -0
- package/dist/_chunks-es/resolveBuildContext.js +177 -0
- package/dist/_chunks-es/resolveBuildContext.js.map +1 -0
- package/dist/_chunks-es/resolveBuildTasks.js +109 -0
- package/dist/_chunks-es/resolveBuildTasks.js.map +1 -0
- package/dist/_chunks-es/resolveConfigProperty.js +7 -0
- package/dist/_chunks-es/resolveConfigProperty.js.map +1 -0
- package/dist/_chunks-es/resolveRolldownConfig.js +1 -1
- package/dist/_chunks-es/resolveWatchTasks.js +77 -0
- package/dist/_chunks-es/resolveWatchTasks.js.map +1 -0
- package/dist/_chunks-es/template.js +449 -0
- package/dist/_chunks-es/template.js.map +1 -0
- package/dist/_chunks-es/validatePkg.js +72 -0
- package/dist/_chunks-es/validatePkg.js.map +1 -0
- package/dist/_chunks-es/watch.js +9 -115
- package/dist/_chunks-es/watch.js.map +1 -1
- package/dist/_chunks-es/watchConfigFiles.js +45 -0
- package/dist/_chunks-es/watchConfigFiles.js.map +1 -0
- package/dist/cli.js +1 -1
- package/dist/index.js +4 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/_chunks-es/consoleSpy.js +0 -541
- package/dist/_chunks-es/consoleSpy.js.map +0 -1
- package/dist/_chunks-es/isRecord.js.map +0 -1
package/dist/_chunks-es/index.js
CHANGED
|
@@ -1,181 +1,182 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import
|
|
19
|
-
import
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
import
|
|
26
|
-
import
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
for (const msg of warnings) {
|
|
31
|
-
const sourceFilePath = msg.sourceFilePath && path.relative(cwd, msg.sourceFilePath);
|
|
32
|
-
msg.messageId !== "TS6307" && logger.log(
|
|
33
|
-
[
|
|
34
|
-
`${chalk.cyan(sourceFilePath || "?")}`,
|
|
35
|
-
`:${chalk.yellow(msg.sourceFileLine)}:${chalk.yellow(msg.sourceFileColumn)}`,
|
|
36
|
-
` - ${chalk.yellow("warning")} ${chalk.gray(msg.messageId)}
|
|
37
|
-
`,
|
|
38
|
-
msg.text,
|
|
39
|
-
`
|
|
40
|
-
`
|
|
41
|
-
].join("")
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
const errors = messages.filter((msg) => msg.logLevel === "error");
|
|
45
|
-
!warnings.length && errors.length && logger.log("");
|
|
46
|
-
for (const msg of errors) {
|
|
47
|
-
const sourceFilePath = msg.sourceFilePath && path.relative(cwd, msg.sourceFilePath);
|
|
48
|
-
logger.log(
|
|
49
|
-
[
|
|
50
|
-
`${chalk.cyan(sourceFilePath || "?")}`,
|
|
51
|
-
`:${chalk.yellow(msg.sourceFileLine)}:${chalk.yellow(msg.sourceFileColumn)}`,
|
|
52
|
-
` - ${chalk.red("error")} ${chalk.gray(msg.messageId)}
|
|
53
|
-
`,
|
|
54
|
-
msg.text,
|
|
55
|
-
`
|
|
56
|
-
`
|
|
57
|
-
].join("")
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
errors.length && process.exit(1);
|
|
1
|
+
import "./build.js";
|
|
2
|
+
import "./check.js";
|
|
3
|
+
import "./loadPkgWithReporting.js";
|
|
4
|
+
import config from "@sanity/browserslist-config";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
import "./init.js";
|
|
8
|
+
import "./logger.js";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { errorMap } from "zod-validation-error";
|
|
11
|
+
import "./watch.js";
|
|
12
|
+
function defineConfig(configOptions) {
|
|
13
|
+
return configOptions;
|
|
14
|
+
}
|
|
15
|
+
const DEFAULT_BROWSERSLIST_QUERY = config, fileEnding = /\.[mc]?js$/, dtsEnding = ".d.ts", defaultEnding = ".js", mjsEnding = ".mjs", cjsEnding = ".cjs", mtsEnding = ".d.mts", ctsEnding = ".d.cts";
|
|
16
|
+
function getTargetPaths(_type, expOrBundle) {
|
|
17
|
+
const type = _type === "module" ? "module" : "commonjs", set = /* @__PURE__ */ new Set();
|
|
18
|
+
return expOrBundle?.import && set.add(expOrBundle.import.replace(fileEnding, type === "module" ? dtsEnding : mtsEnding)), expOrBundle?.require && set.add(expOrBundle.require.replace(fileEnding, type === "commonjs" ? dtsEnding : ctsEnding)), isPkgExport$1(expOrBundle) && (expOrBundle.browser?.source || (expOrBundle.browser?.import && set.add(
|
|
19
|
+
expOrBundle.browser.import.replace(fileEnding, type === "module" ? dtsEnding : mtsEnding)
|
|
20
|
+
), expOrBundle.browser?.require && set.add(
|
|
21
|
+
expOrBundle.browser.require.replace(
|
|
22
|
+
fileEnding,
|
|
23
|
+
type === "commonjs" ? dtsEnding : ctsEnding
|
|
24
|
+
)
|
|
25
|
+
)), expOrBundle.node?.source || (expOrBundle.node?.import && set.add(
|
|
26
|
+
expOrBundle.node.import.replace(fileEnding, type === "module" ? dtsEnding : mtsEnding)
|
|
27
|
+
), expOrBundle.node?.require && set.add(
|
|
28
|
+
expOrBundle.node.require.replace(fileEnding, type === "commonjs" ? dtsEnding : ctsEnding)
|
|
29
|
+
))), Array.from(set);
|
|
61
30
|
}
|
|
62
|
-
function
|
|
63
|
-
|
|
64
|
-
if (diagnostic.file && diagnostic.start) {
|
|
65
|
-
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start), message = ts.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
66
|
-
`), file = path.relative(cwd, diagnostic.file.fileName), output = [
|
|
67
|
-
`${chalk.yellow(file)}:${chalk.blue(line + 1)}:${chalk.blue(character + 1)} - `,
|
|
68
|
-
`${chalk.gray(`TS${diagnostic.code}:`)} ${message}`
|
|
69
|
-
].join("");
|
|
70
|
-
diagnostic.category === ts.DiagnosticCategory.Error && logger.error(output), diagnostic.category === ts.DiagnosticCategory.Warning && logger.warn(output), diagnostic.category === ts.DiagnosticCategory.Message && logger.log(output), diagnostic.category === ts.DiagnosticCategory.Suggestion && logger.log(output);
|
|
71
|
-
} else
|
|
72
|
-
logger.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
73
|
-
`));
|
|
31
|
+
function isPkgExport$1(exp) {
|
|
32
|
+
return exp?.browser || exp?.node || exp?.default;
|
|
74
33
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
...tsconfig.options,
|
|
78
|
-
declaration: !0,
|
|
79
|
-
declarationDir: outDir,
|
|
80
|
-
emitDeclarationOnly: !0,
|
|
81
|
-
noEmit: !1,
|
|
82
|
-
noEmitOnError: strict ? !0 : tsconfig.options.noEmitOnError ?? !0,
|
|
83
|
-
outDir
|
|
84
|
-
}, program = ts.createProgram(tsconfig.fileNames, compilerOptions), emitResult = program.emit(), allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
85
|
-
for (const diagnostic of allDiagnostics)
|
|
86
|
-
printDiagnostic({ cwd, logger, diagnostic });
|
|
87
|
-
if (emitResult.emitSkipped && allDiagnostics.filter((diag) => diag.category === ts.DiagnosticCategory.Error).length)
|
|
88
|
-
throw new Error("failed to compile TypeScript definitions");
|
|
34
|
+
function isRecord(value) {
|
|
35
|
+
return !!value && !Array.isArray(value) && typeof value == "object";
|
|
89
36
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
37
|
+
const pkgExtMap = {
|
|
38
|
+
// pkg.type: "commonjs"
|
|
39
|
+
commonjs: {
|
|
40
|
+
commonjs: defaultEnding,
|
|
41
|
+
esm: mjsEnding
|
|
42
|
+
},
|
|
43
|
+
// pkg.type: "module"
|
|
44
|
+
module: {
|
|
45
|
+
commonjs: cjsEnding,
|
|
46
|
+
esm: defaultEnding
|
|
94
47
|
}
|
|
48
|
+
};
|
|
49
|
+
function validateExports(_exports, options) {
|
|
50
|
+
const { pkg } = options, type = pkg.type || "commonjs", ext = pkgExtMap[type], errors = [];
|
|
51
|
+
for (const exp of _exports)
|
|
52
|
+
exp.require && !exp.require.endsWith(ext.commonjs) && errors.push(
|
|
53
|
+
`package.json with \`type: "${type}"\` - \`exports["${exp._path}"].require\` must end with "${ext.commonjs}"`
|
|
54
|
+
), exp.import && !exp.import.endsWith(ext.esm) && errors.push(
|
|
55
|
+
`package.json with \`type: "${type}"\` - \`exports["${exp._path}"].import\` must end with "${ext.esm}"`
|
|
56
|
+
);
|
|
57
|
+
return errors;
|
|
95
58
|
}
|
|
96
|
-
function
|
|
97
|
-
const {
|
|
98
|
-
|
|
99
|
-
distPath,
|
|
100
|
-
exportPath,
|
|
101
|
-
filePath,
|
|
102
|
-
messages,
|
|
103
|
-
projectFolder,
|
|
104
|
-
mainEntryPointFilePath,
|
|
105
|
-
tsconfig,
|
|
106
|
-
tsconfigPath,
|
|
107
|
-
dtsRollupEnabled
|
|
108
|
-
} = options;
|
|
109
|
-
return {
|
|
110
|
-
apiReport: {
|
|
111
|
-
enabled: !1,
|
|
112
|
-
reportFileName: "<unscopedPackageName>.api.md"
|
|
113
|
-
},
|
|
114
|
-
bundledPackages,
|
|
115
|
-
// If `paths` are used for self-referencing imports (e.g. the module is named `sanity`, and the `sanity/structure` export is also importing from `sanity/router`),
|
|
116
|
-
compiler: tsconfig.options.paths ? {
|
|
117
|
-
overrideTsconfig: {
|
|
118
|
-
extends: tsconfigPath,
|
|
119
|
-
compilerOptions: {
|
|
120
|
-
// An empty object replaces whatever is in the original tsconfig file
|
|
121
|
-
paths: {}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
} : { tsconfigFilePath: tsconfigPath },
|
|
125
|
-
docModel: {
|
|
126
|
-
enabled: !1,
|
|
127
|
-
apiJsonFilePath: path.resolve(distPath, `${exportPath}.api.json`)
|
|
128
|
-
},
|
|
129
|
-
dtsRollup: {
|
|
130
|
-
enabled: dtsRollupEnabled,
|
|
131
|
-
untrimmedFilePath: path.resolve(distPath, filePath)
|
|
132
|
-
// betaTrimmedFilePath: path.resolve(distPath, filePath.replace('.d.ts', '-beta.d.ts')),
|
|
133
|
-
// publicTrimmedFilePath: path.resolve(distPath, filePath.replace('.d.ts', '-public.d.ts')),
|
|
134
|
-
},
|
|
135
|
-
tsdocMetadata: {
|
|
136
|
-
enabled: !1
|
|
137
|
-
},
|
|
138
|
-
messages,
|
|
139
|
-
mainEntryPointFilePath,
|
|
140
|
-
projectFolder
|
|
59
|
+
function parseExports(options) {
|
|
60
|
+
const { cwd, pkg, strict, strictOptions: strictOptions2, logger } = options, type = pkg.type || "commonjs", errors = [], report = (kind, message) => {
|
|
61
|
+
kind === "warn" ? logger.warn(message) : errors.push(message);
|
|
141
62
|
};
|
|
63
|
+
if (!pkg.main && strict && strictOptions2.alwaysPackageJsonMain !== "off" && report(strictOptions2.alwaysPackageJsonMain, "package.json: `main` must be declared"), !Array.isArray(pkg.files) && strict && strictOptions2.alwaysPackageJsonFiles !== "off" && report(
|
|
64
|
+
strictOptions2.alwaysPackageJsonFiles,
|
|
65
|
+
"package.json: `files` should be used over `.npmignore`"
|
|
66
|
+
), pkg.source) {
|
|
67
|
+
if (strict && pkg.exports?.["."] && typeof pkg.exports["."] == "object" && "source" in pkg.exports["."] && pkg.exports["."].source === pkg.source)
|
|
68
|
+
errors.push(
|
|
69
|
+
'package.json: the "source" property can be removed, as it is equal to exports["."].source.'
|
|
70
|
+
);
|
|
71
|
+
else if (!pkg.exports && pkg.main) {
|
|
72
|
+
const extMap = pkgExtMap[type], importExport = pkg.main.replace(fileEnding, extMap.esm), requireExport = pkg.main.replace(fileEnding, extMap.commonjs), defaultExport = pkg.main.replace(fileEnding, defaultEnding), maybeBrowserCondition = [];
|
|
73
|
+
if (pkg.browser) {
|
|
74
|
+
const browserConditions = [];
|
|
75
|
+
pkg.module && pkg.browser?.[pkg.module] ? browserConditions.push(
|
|
76
|
+
` "import": ${JSON.stringify(pkg.browser[pkg.module].replace(fileEnding, extMap.esm))}`
|
|
77
|
+
) : pkg.browser?.[pkg.main] && browserConditions.push(
|
|
78
|
+
` "import": ${JSON.stringify(pkg.browser?.[pkg.main].replace(fileEnding, extMap.esm))}`
|
|
79
|
+
), pkg.browser?.[pkg.main] && browserConditions.push(
|
|
80
|
+
` "require": ${JSON.stringify(pkg.browser[pkg.main].replace(fileEnding, extMap.commonjs))}`
|
|
81
|
+
), browserConditions.length && maybeBrowserCondition.push(
|
|
82
|
+
' "browser": {',
|
|
83
|
+
` "source": ${JSON.stringify(pkg.browser?.[pkg.source] || pkg.source)},`,
|
|
84
|
+
...browserConditions,
|
|
85
|
+
" }"
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
errors.push(
|
|
89
|
+
...[
|
|
90
|
+
"package.json: `exports` are missing, it should be:",
|
|
91
|
+
'"exports": {',
|
|
92
|
+
' ".": {',
|
|
93
|
+
` "source": ${JSON.stringify(pkg.source)},`,
|
|
94
|
+
// If browser conditions are detected then add them to the suggestion
|
|
95
|
+
...maybeBrowserCondition.length > 0 ? maybeBrowserCondition : [],
|
|
96
|
+
type === "commonjs" && ` "import": ${JSON.stringify(importExport)},`,
|
|
97
|
+
type === "module" && ` "require": ${JSON.stringify(requireExport)},`,
|
|
98
|
+
` "default": ${JSON.stringify(defaultExport)}`,
|
|
99
|
+
" },",
|
|
100
|
+
' "./package.json": "./package.json"',
|
|
101
|
+
"}"
|
|
102
|
+
].filter(Boolean)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (errors.length)
|
|
107
|
+
throw new Error(`
|
|
108
|
+
- ` + errors.join(`
|
|
109
|
+
- `));
|
|
110
|
+
if (!pkg.exports)
|
|
111
|
+
throw new Error(
|
|
112
|
+
`
|
|
113
|
+
- ` + [
|
|
114
|
+
"package.json: `exports` are missing, please set a minimal configuration, for example:",
|
|
115
|
+
'"exports": {',
|
|
116
|
+
' ".": {',
|
|
117
|
+
' "source": "./src/index.js",',
|
|
118
|
+
' "default": "./dist/index.js"',
|
|
119
|
+
" },",
|
|
120
|
+
' "./package.json": "./package.json"',
|
|
121
|
+
"}"
|
|
122
|
+
].join(`
|
|
123
|
+
- `)
|
|
124
|
+
);
|
|
125
|
+
const _exports = [];
|
|
126
|
+
strict && strictOptions2.noPackageJsonTypings !== "off" && "typings" in pkg && report(strictOptions2.noPackageJsonTypings, "package.json: `typings` should be `types`"), strict && strictOptions2.alwaysPackageJsonTypes !== "off" && !pkg.types && typeof pkg.exports?.["."] == "object" && "source" in pkg.exports["."] && pkg.exports["."].source?.endsWith(".ts") && report(
|
|
127
|
+
strictOptions2.alwaysPackageJsonTypes,
|
|
128
|
+
"package.json: `types` must be declared for the npm listing to show as a TypeScript module."
|
|
129
|
+
), strict && !pkg.exports["./package.json"] && errors.push('package.json: `exports["./package.json"] must be declared.');
|
|
130
|
+
for (const [exportPath, exportEntry] of Object.entries(pkg.exports))
|
|
131
|
+
if (exportPath.endsWith(".json") || typeof exportEntry == "string" && exportEntry.endsWith(".json"))
|
|
132
|
+
exportPath === "./package.json" && exportEntry !== "./package.json" && errors.push('package.json: `exports["./package.json"]` must be "./package.json".');
|
|
133
|
+
else if (exportPath.endsWith(".css"))
|
|
134
|
+
typeof exportEntry == "string" && !existsSync(resolve(cwd, exportEntry)) ? errors.push(
|
|
135
|
+
`package.json: \`exports[${JSON.stringify(exportPath)}]\`: file does not exist.`
|
|
136
|
+
) : typeof exportEntry != "string" && errors.push(
|
|
137
|
+
`package.json: \`exports[${JSON.stringify(exportPath)}]\`: export conditions not supported for CSS files.`
|
|
138
|
+
);
|
|
139
|
+
else if (!(isRecord(exportEntry) && "svelte" in exportEntry))
|
|
140
|
+
if (isPkgExport(exportEntry)) {
|
|
141
|
+
const exp = {
|
|
142
|
+
_exported: !0,
|
|
143
|
+
_path: exportPath,
|
|
144
|
+
...exportEntry
|
|
145
|
+
};
|
|
146
|
+
if (!exp.default) {
|
|
147
|
+
const fallback = type === "module" ? exp.import : exp.require;
|
|
148
|
+
fallback && (exp.default = fallback);
|
|
149
|
+
}
|
|
150
|
+
!exp.require && type === "commonjs" && exp.default && (exp.require = exp.default), !exp.import && type === "module" && exp.default && (exp.import = exp.default), exportPath === "." && (exportEntry.require && pkg.main && exportEntry.require !== pkg.main && errors.push(
|
|
151
|
+
'package.json: mismatch between "main" and "exports.require". These must be equal.'
|
|
152
|
+
), exportEntry.import && pkg.module && exportEntry.import !== pkg.module && errors.push(
|
|
153
|
+
'package.json: mismatch between "module" and "exports.import" These must be equal.'
|
|
154
|
+
)), _exports.push(exp);
|
|
155
|
+
} else isRecord(exportEntry) || errors.push("package.json: exports must be an object");
|
|
156
|
+
if (errors.push(...validateExports(_exports, { pkg })), errors.length)
|
|
157
|
+
throw new Error(`
|
|
158
|
+
- ` + errors.join(`
|
|
159
|
+
- `));
|
|
160
|
+
return _exports;
|
|
142
161
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const { customTags } = opts;
|
|
146
|
-
if (customTags.length === 0)
|
|
147
|
-
return;
|
|
148
|
-
const tsDocBaseBuf = await readFile(
|
|
149
|
-
require2.resolve("@microsoft/api-extractor/extends/tsdoc-base.json")
|
|
150
|
-
), tsDocBaseConfig = parse(tsDocBaseBuf.toString()), tagDefinitions = (tsDocBaseConfig.tagDefinitions || []).concat(
|
|
151
|
-
customTags.map((t) => ({
|
|
152
|
-
tagName: `@${t.name}`,
|
|
153
|
-
syntaxKind: t.syntaxKind,
|
|
154
|
-
allowMultiple: t.allowMultiple
|
|
155
|
-
}))
|
|
156
|
-
), supportForTags = { ...tsDocBaseConfig.supportForTags };
|
|
157
|
-
for (const customTag of customTags)
|
|
158
|
-
supportForTags[`@${customTag.name}`] = !0;
|
|
159
|
-
return TSDocConfigFile.loadFromObject({
|
|
160
|
-
...tsDocBaseConfig,
|
|
161
|
-
noStandardTags: !1,
|
|
162
|
-
tagDefinitions,
|
|
163
|
-
supportForTags
|
|
164
|
-
});
|
|
162
|
+
function isPkgExport(value) {
|
|
163
|
+
return isRecord(value) && "source" in value && typeof value.source == "string";
|
|
165
164
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
extractResult
|
|
169
|
-
}) {
|
|
170
|
-
const program = extractResult.compilerState.program, moduleBlocks = [], sourceFiles = [...program.getSourceFiles()].filter((sourceFile) => sourceFile.fileName.includes(tsOutDir));
|
|
171
|
-
for (const sourceFile of sourceFiles)
|
|
172
|
-
sourceFile.text.includes("declare module") && moduleBlocks.push(...extractModuleBlocks(sourceFile.text));
|
|
173
|
-
return moduleBlocks;
|
|
165
|
+
function defineTemplateOption(option) {
|
|
166
|
+
return option;
|
|
174
167
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
168
|
+
const toggle = z.union([z.literal("error"), z.literal("warn"), z.literal("off")]), strictOptions = z.object({
|
|
169
|
+
noPackageJsonTypings: toggle.default("error"),
|
|
170
|
+
noImplicitSideEffects: toggle.default("warn"),
|
|
171
|
+
noImplicitBrowsersList: toggle.default("warn"),
|
|
172
|
+
alwaysPackageJsonTypes: toggle.default("error"),
|
|
173
|
+
alwaysPackageJsonMain: toggle.default("error"),
|
|
174
|
+
alwaysPackageJsonFiles: toggle.default("error")
|
|
175
|
+
}).strict(), validationSchema = z.object({
|
|
176
|
+
strictOptions: strictOptions.default({})
|
|
177
|
+
});
|
|
178
|
+
function parseStrictOptions(input) {
|
|
179
|
+
return validationSchema.parse({ strictOptions: input }, { errorMap }).strictOptions;
|
|
179
180
|
}
|
|
180
181
|
const LOG_LEVELS = {
|
|
181
182
|
error: "error",
|
|
@@ -258,592 +259,17 @@ function getExtractMessagesConfig(options) {
|
|
|
258
259
|
}
|
|
259
260
|
};
|
|
260
261
|
}
|
|
261
|
-
async function extractTypes(options) {
|
|
262
|
-
const {
|
|
263
|
-
bundledPackages,
|
|
264
|
-
customTags,
|
|
265
|
-
distPath,
|
|
266
|
-
exportPath,
|
|
267
|
-
files,
|
|
268
|
-
filePaths,
|
|
269
|
-
projectPath,
|
|
270
|
-
rules,
|
|
271
|
-
sourceTypesPath,
|
|
272
|
-
tmpPath,
|
|
273
|
-
tsconfig,
|
|
274
|
-
tsconfigPath,
|
|
275
|
-
extractorDisabled
|
|
276
|
-
} = options, tsdocConfigFile = await createTSDocConfig({
|
|
277
|
-
customTags: customTags || []
|
|
278
|
-
}), filePath = filePaths[0].replace(/\.d\.[mc]ts$/, ".d.ts"), shouldCleanUpDts = !filePaths.includes(filePath), extractorConfig = ExtractorConfig.prepare({
|
|
279
|
-
configObject: createApiExtractorConfig({
|
|
280
|
-
bundledPackages,
|
|
281
|
-
distPath,
|
|
282
|
-
exportPath,
|
|
283
|
-
filePath,
|
|
284
|
-
messages: getExtractMessagesConfig({ rules, disabled: extractorDisabled }),
|
|
285
|
-
projectFolder: projectPath,
|
|
286
|
-
mainEntryPointFilePath: sourceTypesPath,
|
|
287
|
-
tsconfig,
|
|
288
|
-
tsconfigPath,
|
|
289
|
-
dtsRollupEnabled: !0
|
|
290
|
-
}),
|
|
291
|
-
configObjectFullPath: void 0,
|
|
292
|
-
tsdocConfigFile,
|
|
293
|
-
packageJsonFullPath: path.resolve(projectPath, "package.json")
|
|
294
|
-
}), messages = [], extractorResult = Extractor.invoke(extractorConfig, {
|
|
295
|
-
// Equivalent to the "--local" command-line parameter
|
|
296
|
-
localBuild: !0,
|
|
297
|
-
// Equivalent to the "--verbose" command-line parameter
|
|
298
|
-
showVerboseMessages: !0,
|
|
299
|
-
// handle messages
|
|
300
|
-
messageCallback(message) {
|
|
301
|
-
messages.push(message), message.handled = !0;
|
|
302
|
-
}
|
|
303
|
-
}), typesPath = path.resolve(distPath, filePath), typesBuf = await fs.readFile(typesPath), prettierConfig = await prettier.resolveConfig(typesPath);
|
|
304
|
-
await mkdirp(path.dirname(typesPath));
|
|
305
|
-
const moduleBlocks = await extractModuleBlocksFromTypes({
|
|
306
|
-
extractResult: extractorResult,
|
|
307
|
-
tsOutDir: tmpPath
|
|
308
|
-
}), code = [typesBuf.toString(), ...moduleBlocks].join(`
|
|
309
|
-
|
|
310
|
-
`), prettyCode = await prettier.format(code, {
|
|
311
|
-
...prettierConfig,
|
|
312
|
-
filepath: typesPath
|
|
313
|
-
});
|
|
314
|
-
for (const expFilePath of filePaths) {
|
|
315
|
-
const expTypesPath = path.resolve(distPath, expFilePath);
|
|
316
|
-
await fs.writeFile(expTypesPath, prettyCode), files.push({
|
|
317
|
-
type: "types",
|
|
318
|
-
path: expTypesPath
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
return shouldCleanUpDts && await fs.unlink(typesPath), { extractorResult, messages };
|
|
322
|
-
}
|
|
323
|
-
async function doExtract(ctx, task) {
|
|
324
|
-
const { config, cwd, files, logger, strict, ts: ts2, bundledPackages } = ctx;
|
|
325
|
-
if (!ts2.config || !ts2.configPath)
|
|
326
|
-
return { type: "dts", messages: [], results: [] };
|
|
327
|
-
const { outDir, rootDir = cwd } = ts2.config.options;
|
|
328
|
-
if (!outDir)
|
|
329
|
-
throw new Error("tsconfig.json is missing `compilerOptions.outDir`");
|
|
330
|
-
const tmpPath = path.resolve(outDir, "__tmp__");
|
|
331
|
-
await buildTypes({ cwd, logger, outDir: tmpPath, strict, tsconfig: ts2.config });
|
|
332
|
-
const messages = [], results = [];
|
|
333
|
-
for (const entry of task.entries) {
|
|
334
|
-
const exportPath = entry.exportPath === "." ? "./index" : entry.exportPath, sourceTypesPath = path.resolve(
|
|
335
|
-
tmpPath,
|
|
336
|
-
path.relative(rootDir, path.resolve(cwd, entry.sourcePath)).replace(/\.ts$/, ".d.ts")
|
|
337
|
-
), targetPaths = entry.targetPaths.map((targetPath) => path.resolve(cwd, targetPath)), filePaths = targetPaths.map((targetPath) => path.relative(outDir, targetPath)), result = await extractTypes({
|
|
338
|
-
bundledPackages: bundledPackages || [],
|
|
339
|
-
customTags: config?.extract?.customTags,
|
|
340
|
-
distPath: outDir,
|
|
341
|
-
exportPath,
|
|
342
|
-
files,
|
|
343
|
-
filePaths,
|
|
344
|
-
projectPath: cwd,
|
|
345
|
-
rules: config?.extract?.rules,
|
|
346
|
-
sourceTypesPath,
|
|
347
|
-
tsconfig: ts2.config,
|
|
348
|
-
tmpPath,
|
|
349
|
-
tsconfigPath: path.resolve(cwd, ts2.configPath || "tsconfig.json"),
|
|
350
|
-
extractorDisabled: config?.extract?.enabled === !1
|
|
351
|
-
});
|
|
352
|
-
messages.push(...result.messages);
|
|
353
|
-
const errors = result.messages.filter((msg) => msg.logLevel === "error");
|
|
354
|
-
if (errors.length > 0)
|
|
355
|
-
throw await rimraf$1(tmpPath), new DtsError(`encountered ${errors.length} errors when extracting types`, errors);
|
|
356
|
-
results.push({ sourcePath: path.resolve(cwd, entry.sourcePath), filePaths: targetPaths });
|
|
357
|
-
}
|
|
358
|
-
return await rimraf$1(tmpPath), { type: "dts", messages, results };
|
|
359
|
-
}
|
|
360
|
-
const dtsTask = {
|
|
361
|
-
name: (ctx, task) => [
|
|
362
|
-
`Build type definitions with ${chalk.bold(ctx.dts)}...`,
|
|
363
|
-
" entries:",
|
|
364
|
-
...task.entries.map((entry) => entry.targetPaths.map((targetPath) => [
|
|
365
|
-
` - ${chalk.cyan(entry.importId)}: `,
|
|
366
|
-
`${chalk.yellow(entry.sourcePath)} ${chalk.gray("\u2192")} ${chalk.yellow(targetPath)}`
|
|
367
|
-
].join("")).join(`
|
|
368
|
-
`))
|
|
369
|
-
].join(`
|
|
370
|
-
`),
|
|
371
|
-
exec: (ctx, task) => new Observable((observer) => {
|
|
372
|
-
doExtract(ctx, task).then((result) => {
|
|
373
|
-
observer.next(result), observer.complete();
|
|
374
|
-
}).catch((err) => {
|
|
375
|
-
observer.error(err);
|
|
376
|
-
});
|
|
377
|
-
}),
|
|
378
|
-
complete: (ctx, _task, result) => {
|
|
379
|
-
printExtractMessages(ctx, result.messages);
|
|
380
|
-
},
|
|
381
|
-
error: (ctx, _task, err) => {
|
|
382
|
-
const { logger } = ctx;
|
|
383
|
-
err instanceof DtsError ? printExtractMessages(ctx, err.messages) : err instanceof Error && logger.error(err);
|
|
384
|
-
}
|
|
385
|
-
}, dtsWatchTask = {
|
|
386
|
-
name: (_ctx, task) => [
|
|
387
|
-
"build type definitions",
|
|
388
|
-
...task.entries.map((entry) => entry.targetPaths.map((targetPath) => [
|
|
389
|
-
` - ${chalk.cyan(entry.importId)}: `,
|
|
390
|
-
`${chalk.yellow(entry.sourcePath)} ${chalk.gray("\u2192")} ${chalk.yellow(targetPath)}`
|
|
391
|
-
].join("")))
|
|
392
|
-
].join(`
|
|
393
|
-
`),
|
|
394
|
-
exec: (ctx, task) => {
|
|
395
|
-
const { config, cwd, files, logger, strict, ts: tsContext, bundledPackages } = ctx;
|
|
396
|
-
return new Observable((observer) => {
|
|
397
|
-
if (!tsContext.config || !tsContext.configPath) {
|
|
398
|
-
observer.next({ type: "dts", messages: [], results: [] }), observer.complete();
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
const { outDir, rootDir = cwd } = tsContext.config.options;
|
|
402
|
-
if (!outDir) {
|
|
403
|
-
observer.error(new Error("tsconfig.json is missing `compilerOptions.outDir`"));
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
const tmpPath = path.resolve(outDir, "__tmp__");
|
|
407
|
-
buildTypes({
|
|
408
|
-
cwd,
|
|
409
|
-
logger,
|
|
410
|
-
outDir: tmpPath,
|
|
411
|
-
tsconfig: tsContext.config,
|
|
412
|
-
strict
|
|
413
|
-
}).catch((err) => {
|
|
414
|
-
observer.error(err);
|
|
415
|
-
});
|
|
416
|
-
const host = ts.createWatchCompilerHost(
|
|
417
|
-
tsContext.configPath,
|
|
418
|
-
{
|
|
419
|
-
...tsContext.config.options,
|
|
420
|
-
declaration: !0,
|
|
421
|
-
declarationDir: tmpPath,
|
|
422
|
-
emitDeclarationOnly: !0,
|
|
423
|
-
noEmit: !1,
|
|
424
|
-
noEmitOnError: strict ? !0 : tsContext.config.options.noEmitOnError ?? !0,
|
|
425
|
-
outDir: tmpPath
|
|
426
|
-
},
|
|
427
|
-
ts.sys,
|
|
428
|
-
ts.createEmitAndSemanticDiagnosticsBuilderProgram,
|
|
429
|
-
(diagnostic) => {
|
|
430
|
-
logger.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
431
|
-
`));
|
|
432
|
-
},
|
|
433
|
-
(diagnostic) => {
|
|
434
|
-
logger.info(ts.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
435
|
-
`));
|
|
436
|
-
}
|
|
437
|
-
), origPostProgramCreate = host.afterProgramCreate;
|
|
438
|
-
host.afterProgramCreate = async (program) => {
|
|
439
|
-
origPostProgramCreate?.(program);
|
|
440
|
-
const messages = [], results = [];
|
|
441
|
-
for (const entry of task.entries) {
|
|
442
|
-
const exportPath = entry.exportPath === "." ? "./index" : entry.exportPath, sourceTypesPath = path.resolve(
|
|
443
|
-
tmpPath,
|
|
444
|
-
path.relative(rootDir, path.resolve(cwd, entry.sourcePath)).replace(/\.ts$/, ".d.ts")
|
|
445
|
-
), targetPaths = entry.targetPaths.map((targetPath) => path.resolve(cwd, targetPath)), filePaths = targetPaths.map((targetPath) => path.relative(outDir, targetPath));
|
|
446
|
-
try {
|
|
447
|
-
const result = await extractTypes({
|
|
448
|
-
bundledPackages: bundledPackages || [],
|
|
449
|
-
customTags: config?.extract?.customTags,
|
|
450
|
-
cwd,
|
|
451
|
-
distPath: outDir,
|
|
452
|
-
exportPath,
|
|
453
|
-
files,
|
|
454
|
-
filePaths,
|
|
455
|
-
projectPath: cwd,
|
|
456
|
-
rules: config?.extract?.rules,
|
|
457
|
-
sourceTypesPath,
|
|
458
|
-
tsconfig: tsContext.config,
|
|
459
|
-
tmpPath,
|
|
460
|
-
tsconfigPath: path.resolve(cwd, tsContext.configPath || "tsconfig.json"),
|
|
461
|
-
extractorDisabled: config?.extract?.enabled === !1
|
|
462
|
-
});
|
|
463
|
-
messages.push(...result.messages), results.push({ sourcePath: path.resolve(cwd, entry.sourcePath), filePaths: targetPaths });
|
|
464
|
-
} catch (err) {
|
|
465
|
-
if (err instanceof DtsError)
|
|
466
|
-
messages.push(...err.messages);
|
|
467
|
-
else {
|
|
468
|
-
observer.error(err);
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
observer.next({ type: "dts", messages, results });
|
|
474
|
-
};
|
|
475
|
-
const watchProgram = ts.createWatchProgram(host);
|
|
476
|
-
return () => {
|
|
477
|
-
watchProgram.close(), rimraf.sync(tmpPath);
|
|
478
|
-
};
|
|
479
|
-
});
|
|
480
|
-
},
|
|
481
|
-
complete: (ctx, task, result) => {
|
|
482
|
-
const { logger } = ctx;
|
|
483
|
-
printExtractMessages(ctx, result.messages), logger.success(
|
|
484
|
-
`build type definitions
|
|
485
|
-
${task.entries.map(
|
|
486
|
-
(entry) => ` - ${chalk.cyan(entry.importId)}: ${chalk.yellow(entry.sourcePath)} ${chalk.gray("\u2192")} ${chalk.yellow(entry.targetPaths.join(", "))}`
|
|
487
|
-
).join(`
|
|
488
|
-
`)}`
|
|
489
|
-
), logger.log("");
|
|
490
|
-
},
|
|
491
|
-
error: (ctx, _task, err) => {
|
|
492
|
-
const { logger } = ctx;
|
|
493
|
-
err instanceof DtsError ? printExtractMessages(ctx, err.messages) : err instanceof Error && logger.error(err);
|
|
494
|
-
}
|
|
495
|
-
}, rolldownDtsTask = {
|
|
496
|
-
name: (ctx, task) => {
|
|
497
|
-
const bundleEntries = task.entries.filter((e) => e.path.includes("__$$bundle_")), entries = task.entries.filter((e) => !e.path.includes("__$$bundle_")), targetLines = task.target.length ? [" target:", ...task.target.map((t) => ` - ${chalk.yellow(t)}`)] : [], bundlesLines = bundleEntries.length ? [
|
|
498
|
-
" bundles:",
|
|
499
|
-
...bundleEntries.map(
|
|
500
|
-
(e) => [
|
|
501
|
-
" - ",
|
|
502
|
-
`${chalk.yellow(e.source)} ${chalk.gray("\u2192")} ${chalk.yellow(replaceFileEnding(e.output))}`
|
|
503
|
-
].join("")
|
|
504
|
-
)
|
|
505
|
-
] : [], entriesLines = entries.length ? [
|
|
506
|
-
" entries:",
|
|
507
|
-
...entries.map(
|
|
508
|
-
(e) => [
|
|
509
|
-
" - ",
|
|
510
|
-
`${chalk.cyan(path.join(ctx.pkg.name, e.path))}: `,
|
|
511
|
-
`${chalk.yellow(e.source)} ${chalk.gray("\u2192")} ${chalk.yellow(replaceFileEnding(e.output))}`
|
|
512
|
-
].join("")
|
|
513
|
-
)
|
|
514
|
-
] : [];
|
|
515
|
-
return [
|
|
516
|
-
`Build type definitions with ${chalk.bold("rolldown")}...`,
|
|
517
|
-
` format: ${chalk.yellow(task.format)}`,
|
|
518
|
-
...targetLines,
|
|
519
|
-
...bundlesLines,
|
|
520
|
-
...entriesLines
|
|
521
|
-
].join(`
|
|
522
|
-
`);
|
|
523
|
-
},
|
|
524
|
-
exec: (ctx, task) => from(execPromise$1(ctx, task)),
|
|
525
|
-
complete: () => {
|
|
526
|
-
},
|
|
527
|
-
error: (_ctx, _task, err) => {
|
|
528
|
-
console.error(err);
|
|
529
|
-
}
|
|
530
|
-
};
|
|
531
|
-
async function execPromise$1(ctx, task) {
|
|
532
|
-
const [{ rolldown }, { resolveRolldownConfig }] = await Promise.all([
|
|
533
|
-
import("rolldown"),
|
|
534
|
-
import("./resolveRolldownConfig.js")
|
|
535
|
-
]), { distPath, files, logger } = ctx, outDir = path.relative(ctx.cwd, distPath), consoleSpy = createConsoleSpy({
|
|
536
|
-
onRestored: (messages) => {
|
|
537
|
-
for (const msg of messages) {
|
|
538
|
-
const text = String(msg.args[0]);
|
|
539
|
-
msg.code !== "CIRCULAR_DEPENDENCY" && (text.startsWith("Dynamic import can only") || text.startsWith("Sourcemap is likely to be incorrect") || (msg.type === "log" && logger.info(...msg.args), msg.type === "warn" && logger.warn(...msg.args), msg.type === "error" && logger.error(...msg.args)));
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
});
|
|
543
|
-
try {
|
|
544
|
-
const { inputOptions, outputOptions } = resolveRolldownConfig(ctx, task), bundle = await rolldown({
|
|
545
|
-
...inputOptions,
|
|
546
|
-
onwarn(warning) {
|
|
547
|
-
consoleSpy.messages.push({
|
|
548
|
-
type: "warn",
|
|
549
|
-
code: warning.code,
|
|
550
|
-
args: [warning.message]
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
}), { output } = await bundle.generate(outputOptions);
|
|
554
|
-
for (const chunkOrAsset of output)
|
|
555
|
-
chunkOrAsset.type === "asset" ? files.push({
|
|
556
|
-
type: "asset",
|
|
557
|
-
path: path.resolve(outDir, chunkOrAsset.fileName)
|
|
558
|
-
}) : files.push({
|
|
559
|
-
type: "chunk",
|
|
560
|
-
path: path.resolve(outDir, chunkOrAsset.fileName)
|
|
561
|
-
});
|
|
562
|
-
await bundle.write(outputOptions), await bundle.close(), consoleSpy.restore();
|
|
563
|
-
} catch (err) {
|
|
564
|
-
throw consoleSpy.restore(), err;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
function replaceFileEnding(path2) {
|
|
568
|
-
switch (!0) {
|
|
569
|
-
case path2.endsWith(".mjs"):
|
|
570
|
-
return path2.replace(/\.mjs$/, ".d.mts");
|
|
571
|
-
case path2.endsWith(".cjs"):
|
|
572
|
-
return path2.replace(/\.cjs$/, ".d.cts");
|
|
573
|
-
default:
|
|
574
|
-
return path2.replace(/\.js$/, ".d.ts");
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
function resolveRollupConfig(ctx, buildTask) {
|
|
578
|
-
const { format, runtime, target } = buildTask, { config, cwd, exports: _exports, external, distPath, logger, pkg, ts: ts2 } = ctx, outputExt = pkgExtMap[pkg.type || "commonjs"][format], minify = config?.minify ?? !1, outDir = path.relative(cwd, distPath), pathAliases = Object.fromEntries(
|
|
579
|
-
Object.entries(ts2.config?.options.paths || {}).map(([key, val]) => [key, path.resolve(cwd, ts2.config?.options.baseUrl || ".", val[0])])
|
|
580
|
-
), entries = buildTask.entries.map((entry) => ({
|
|
581
|
-
...entry,
|
|
582
|
-
name: path.relative(outDir, entry.output).replace(/\.[^/.]+$/, "")
|
|
583
|
-
}), {}), exportIds = _exports && Object.keys(_exports).map((exportPath) => path.join(pkg.name, exportPath)), sourcePaths = _exports && Object.values(_exports).map((e) => path.resolve(cwd, e.source)), replacements = Object.fromEntries(
|
|
584
|
-
Object.entries(config?.define || {}).map(([key, val]) => [key, JSON.stringify(val)])
|
|
585
|
-
), { optimizeLodash: enableOptimizeLodash = hasDependency(pkg, "lodash") } = config?.rollup || {}, defaultPlugins = [
|
|
586
|
-
replace({
|
|
587
|
-
preventAssignment: !0,
|
|
588
|
-
values: pkg.name === "@sanity/pkg-utils" ? { ...replacements } : {
|
|
589
|
-
"process.env.PKG_FILE_PATH": (arg) => {
|
|
590
|
-
const sourcePath = `./${path.relative(cwd, arg)}`, entry = entries.find((e) => e.source === sourcePath);
|
|
591
|
-
return entry ? JSON.stringify(
|
|
592
|
-
path.relative(cwd, path.resolve(outDir, entry.name + outputExt))
|
|
593
|
-
) : (console.error(`could not find source entry: ${sourcePath}`), "null");
|
|
594
|
-
},
|
|
595
|
-
"process.env.PKG_FORMAT": JSON.stringify(format),
|
|
596
|
-
"process.env.PKG_RUNTIME": JSON.stringify(runtime),
|
|
597
|
-
"process.env.PKG_VERSION": JSON.stringify(process.env.PKG_VERSION || pkg.version),
|
|
598
|
-
...replacements
|
|
599
|
-
}
|
|
600
|
-
}),
|
|
601
|
-
alias({
|
|
602
|
-
entries: { ...pathAliases }
|
|
603
|
-
}),
|
|
604
|
-
nodeResolve({
|
|
605
|
-
browser: runtime === "browser",
|
|
606
|
-
extensions: [".cjs", ".mjs", ".js", ".jsx", ".json", ".node"],
|
|
607
|
-
preferBuiltins: !0
|
|
608
|
-
}),
|
|
609
|
-
commonjs(),
|
|
610
|
-
json(),
|
|
611
|
-
(config?.babel?.reactCompiler || config?.babel?.styledComponents) && babel({
|
|
612
|
-
babelrc: !1,
|
|
613
|
-
presets: ["@babel/preset-typescript"],
|
|
614
|
-
babelHelpers: "bundled",
|
|
615
|
-
extensions: [".ts", ".tsx", ".js", ".jsx"],
|
|
616
|
-
plugins: [
|
|
617
|
-
// The styled-components plugin needs to run before the react-compiler plugin, in case the css prop is used
|
|
618
|
-
config?.babel?.styledComponents && [
|
|
619
|
-
"babel-plugin-styled-components",
|
|
620
|
-
{
|
|
621
|
-
// Unnecessary, as the way we use styled-components in Sanity is usually by wrapping `@sanity/ui` primitives, not declaring new ones like "const Button = styled.button``"
|
|
622
|
-
fileName: !1,
|
|
623
|
-
// Native template literals take less space than this transpilation
|
|
624
|
-
transpileTemplateLiterals: !1,
|
|
625
|
-
// Massively helps dead code elimination and tree-shaking
|
|
626
|
-
pure: !0,
|
|
627
|
-
// disabled, as pkg-utils tends to be used for npm publishing, while other tooling, like `sanity dev`, `next dev`, etc are used for testing
|
|
628
|
-
cssProp: !1,
|
|
629
|
-
...typeof config.babel.styledComponents == "object" ? config.babel.styledComponents : {}
|
|
630
|
-
}
|
|
631
|
-
],
|
|
632
|
-
config?.babel?.reactCompiler && [
|
|
633
|
-
"babel-plugin-react-compiler",
|
|
634
|
-
config?.reactCompilerOptions || {}
|
|
635
|
-
]
|
|
636
|
-
].filter(Boolean)
|
|
637
|
-
}),
|
|
638
|
-
esbuild({
|
|
639
|
-
jsx: config?.jsx ?? "automatic",
|
|
640
|
-
jsxFactory: config?.jsxFactory ?? "createElement",
|
|
641
|
-
jsxFragment: config?.jsxFragment ?? "Fragment",
|
|
642
|
-
jsxImportSource: config?.jsxImportSource ?? "react",
|
|
643
|
-
target,
|
|
644
|
-
tsconfig: ctx.ts.configPath || "tsconfig.json",
|
|
645
|
-
treeShaking: !0,
|
|
646
|
-
minifySyntax: config?.minify !== !1,
|
|
647
|
-
supported: {
|
|
648
|
-
"template-literal": !0
|
|
649
|
-
}
|
|
650
|
-
}),
|
|
651
|
-
Array.isArray(config?.babel?.plugins) && getBabelOutputPlugin({
|
|
652
|
-
babelrc: !1,
|
|
653
|
-
plugins: config.babel.plugins
|
|
654
|
-
}),
|
|
655
|
-
enableOptimizeLodash && optimizeLodashImports({
|
|
656
|
-
useLodashEs: format === "esm" && hasDependency(pkg, "lodash-es") ? !0 : void 0,
|
|
657
|
-
...typeof config?.rollup?.optimizeLodash == "boolean" ? {} : config?.rollup?.optimizeLodash
|
|
658
|
-
}),
|
|
659
|
-
minify && terser({
|
|
660
|
-
compress: { directives: !1, passes: 10 },
|
|
661
|
-
ecma: 2020,
|
|
662
|
-
output: {
|
|
663
|
-
comments: (_node, comment) => {
|
|
664
|
-
const text = comment.value;
|
|
665
|
-
return comment.type === "comment2" ? /@preserve|@license|@cc_on/i.test(text) : !1;
|
|
666
|
-
},
|
|
667
|
-
preserve_annotations: !0
|
|
668
|
-
}
|
|
669
|
-
})
|
|
670
|
-
].filter(Boolean), userPlugins = config?.rollup?.plugins, plugins = Array.isArray(userPlugins) ? defaultPlugins.concat(userPlugins) : resolveConfigProperty(config?.rollup?.plugins, defaultPlugins), hashChunkFileNames = config?.rollup?.hashChunkFileNames ?? !1, chunkFileNames = `${hashChunkFileNames ? "_chunks" : "_chunks-[format]"}/${hashChunkFileNames ? "[name]-[hash]" : "[name]"}${outputExt}`, entryFileNames = `[name]${outputExt}`;
|
|
671
|
-
return {
|
|
672
|
-
inputOptions: {
|
|
673
|
-
context: cwd,
|
|
674
|
-
external: (id, importer) => {
|
|
675
|
-
if (exportIds?.includes(id))
|
|
676
|
-
return !0;
|
|
677
|
-
if (importer && (id.startsWith(".") || id.startsWith("/"))) {
|
|
678
|
-
const idPath = path.resolve(path.dirname(importer), id);
|
|
679
|
-
if (sourcePaths?.includes(idPath))
|
|
680
|
-
return logger.warn(
|
|
681
|
-
`detected self-referencing import \u2013 treating as external: ${path.relative(
|
|
682
|
-
cwd,
|
|
683
|
-
idPath
|
|
684
|
-
)}`
|
|
685
|
-
), !0;
|
|
686
|
-
}
|
|
687
|
-
const idParts = id.split("/"), name = idParts[0].startsWith("@") ? `${idParts[0]}/${idParts[1]}` : idParts[0];
|
|
688
|
-
return !!(name && external.includes(name));
|
|
689
|
-
},
|
|
690
|
-
input: entries.reduce(
|
|
691
|
-
(acc, entry) => Object.assign(acc, { [entry.name]: entry.source }),
|
|
692
|
-
{}
|
|
693
|
-
),
|
|
694
|
-
watch: {
|
|
695
|
-
chokidar: {
|
|
696
|
-
usePolling: !0
|
|
697
|
-
}
|
|
698
|
-
},
|
|
699
|
-
plugins,
|
|
700
|
-
treeshake: {
|
|
701
|
-
preset: "recommended",
|
|
702
|
-
propertyReadSideEffects: !1,
|
|
703
|
-
moduleSideEffects: "no-external",
|
|
704
|
-
...config?.rollup?.treeshake
|
|
705
|
-
},
|
|
706
|
-
experimentalLogSideEffects: config?.rollup?.experimentalLogSideEffects
|
|
707
|
-
},
|
|
708
|
-
outputOptions: {
|
|
709
|
-
chunkFileNames,
|
|
710
|
-
compact: minify,
|
|
711
|
-
dir: outDir,
|
|
712
|
-
entryFileNames,
|
|
713
|
-
esModule: !0,
|
|
714
|
-
format,
|
|
715
|
-
interop: "compat",
|
|
716
|
-
sourcemap: config?.sourcemap ?? !0,
|
|
717
|
-
hoistTransitiveImports: !1,
|
|
718
|
-
minifyInternalExports: minify,
|
|
719
|
-
...config?.rollup?.output
|
|
720
|
-
}
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
function hasDependency(pkg, packageName) {
|
|
724
|
-
return pkg.dependencies ? packageName in pkg.dependencies : pkg.peerDependencies ? packageName in pkg.peerDependencies : !1;
|
|
725
|
-
}
|
|
726
|
-
const rollupTask = {
|
|
727
|
-
name: (ctx, task) => {
|
|
728
|
-
const bundleEntries = task.entries.filter((e) => e.path.includes("__$$bundle_")), entries = task.entries.filter((e) => !e.path.includes("__$$bundle_")), targetLines = task.target.length ? [" target:", ...task.target.map((t) => ` - ${chalk.yellow(t)}`)] : [], bundlesLines = bundleEntries.length ? [
|
|
729
|
-
" bundles:",
|
|
730
|
-
...bundleEntries.map(
|
|
731
|
-
(e) => [
|
|
732
|
-
" - ",
|
|
733
|
-
`${chalk.yellow(e.source)} ${chalk.gray("\u2192")} ${chalk.yellow(e.output)}`
|
|
734
|
-
].join("")
|
|
735
|
-
)
|
|
736
|
-
] : [], entriesLines = entries.length ? [
|
|
737
|
-
" entries:",
|
|
738
|
-
...entries.map(
|
|
739
|
-
(e) => [
|
|
740
|
-
" - ",
|
|
741
|
-
`${chalk.cyan(path.join(ctx.pkg.name, e.path))}: `,
|
|
742
|
-
`${chalk.yellow(e.source)} ${chalk.gray("\u2192")} ${chalk.yellow(e.output)}`
|
|
743
|
-
].join("")
|
|
744
|
-
)
|
|
745
|
-
] : [];
|
|
746
|
-
return [
|
|
747
|
-
"Build javascript files...",
|
|
748
|
-
` format: ${chalk.yellow(task.format)}`,
|
|
749
|
-
...targetLines,
|
|
750
|
-
...bundlesLines,
|
|
751
|
-
...entriesLines
|
|
752
|
-
].join(`
|
|
753
|
-
`);
|
|
754
|
-
},
|
|
755
|
-
exec: (ctx, task) => new Observable((observer) => {
|
|
756
|
-
execPromise(ctx, task).then((result) => {
|
|
757
|
-
observer.next(result), observer.complete();
|
|
758
|
-
}).catch((err) => observer.error(err));
|
|
759
|
-
}),
|
|
760
|
-
complete: () => {
|
|
761
|
-
},
|
|
762
|
-
error: (_ctx, _task, err) => {
|
|
763
|
-
console.error(err);
|
|
764
|
-
}
|
|
765
|
-
};
|
|
766
|
-
async function execPromise(ctx, task) {
|
|
767
|
-
const { distPath, files, logger } = ctx, outDir = path.relative(ctx.cwd, distPath), consoleSpy = createConsoleSpy({
|
|
768
|
-
onRestored: (messages) => {
|
|
769
|
-
for (const msg of messages) {
|
|
770
|
-
const text = String(msg.args[0]);
|
|
771
|
-
msg.code !== "CIRCULAR_DEPENDENCY" && (text.startsWith("Dynamic import can only") || text.startsWith("Sourcemap is likely to be incorrect") || (msg.type === "log" && logger.info(...msg.args), msg.type === "warn" && logger.warn(...msg.args), msg.type === "error" && logger.error(...msg.args)));
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
});
|
|
775
|
-
try {
|
|
776
|
-
const { inputOptions, outputOptions } = resolveRollupConfig(ctx, task), bundle = await rollup({
|
|
777
|
-
...inputOptions,
|
|
778
|
-
onwarn(warning) {
|
|
779
|
-
consoleSpy.messages.push({
|
|
780
|
-
type: "warn",
|
|
781
|
-
code: warning.code,
|
|
782
|
-
args: [warning.message]
|
|
783
|
-
});
|
|
784
|
-
}
|
|
785
|
-
}), { output } = await bundle.generate(outputOptions);
|
|
786
|
-
for (const chunkOrAsset of output)
|
|
787
|
-
chunkOrAsset.type === "asset" ? files.push({
|
|
788
|
-
type: "asset",
|
|
789
|
-
path: path.resolve(outDir, chunkOrAsset.fileName)
|
|
790
|
-
}) : files.push({
|
|
791
|
-
type: "chunk",
|
|
792
|
-
path: path.resolve(outDir, chunkOrAsset.fileName)
|
|
793
|
-
});
|
|
794
|
-
await bundle.write(outputOptions), await bundle.close(), consoleSpy.restore();
|
|
795
|
-
} catch (err) {
|
|
796
|
-
throw consoleSpy.restore(), err;
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
const rollupWatchTask = {
|
|
800
|
-
name: (ctx, task) => `build javascript files (target ${task.target.join(" + ")}, format ${task.format})
|
|
801
|
-
${task.entries.map((e) => `${chalk.blue(path.join(ctx.pkg.name, e.path))}: ${e.source} -> ${e.output}`).join(`
|
|
802
|
-
`)}`,
|
|
803
|
-
exec: (ctx, task) => {
|
|
804
|
-
const { inputOptions, outputOptions } = resolveRollupConfig(ctx, task);
|
|
805
|
-
return new Observable((observer) => {
|
|
806
|
-
const watchOptions = {
|
|
807
|
-
...inputOptions,
|
|
808
|
-
output: outputOptions
|
|
809
|
-
}, watcher = watch(watchOptions);
|
|
810
|
-
return watcher.on("event", (event) => {
|
|
811
|
-
observer.next(event);
|
|
812
|
-
}), () => {
|
|
813
|
-
watcher.close();
|
|
814
|
-
};
|
|
815
|
-
});
|
|
816
|
-
},
|
|
817
|
-
complete: (ctx, task, event) => {
|
|
818
|
-
const { logger } = ctx;
|
|
819
|
-
if (event.code === "BUNDLE_END") {
|
|
820
|
-
logger.success(
|
|
821
|
-
`build javascript files (target ${task.target.join(" + ")}, format ${task.format})
|
|
822
|
-
${task.entries.map((e) => `${chalk.blue(path.join(ctx.pkg.name, e.path))}: ${e.source} -> ${e.output}`).join(`
|
|
823
|
-
`)}`
|
|
824
|
-
), logger.log("");
|
|
825
|
-
return;
|
|
826
|
-
}
|
|
827
|
-
if (event.code !== "BUNDLE_START" && event.code !== "END" && event.code === "ERROR") {
|
|
828
|
-
logger.error(event.code, event);
|
|
829
|
-
return;
|
|
830
|
-
}
|
|
831
|
-
},
|
|
832
|
-
error: (ctx, _task, err) => {
|
|
833
|
-
const { logger } = ctx;
|
|
834
|
-
err instanceof Error && logger.log(err);
|
|
835
|
-
}
|
|
836
|
-
}, buildTaskHandlers = {
|
|
837
|
-
"build:dts": dtsTask,
|
|
838
|
-
"build:js": rollupTask,
|
|
839
|
-
"rolldown:dts": rolldownDtsTask
|
|
840
|
-
}, watchTaskHandlers = {
|
|
841
|
-
"watch:dts": dtsWatchTask,
|
|
842
|
-
"watch:js": rollupWatchTask
|
|
843
|
-
};
|
|
844
262
|
export {
|
|
845
|
-
|
|
263
|
+
DEFAULT_BROWSERSLIST_QUERY,
|
|
264
|
+
defineConfig,
|
|
265
|
+
defineTemplateOption,
|
|
846
266
|
getExtractMessagesConfig,
|
|
847
|
-
|
|
267
|
+
getTargetPaths,
|
|
268
|
+
isRecord,
|
|
269
|
+
parseExports,
|
|
270
|
+
parseStrictOptions,
|
|
271
|
+
pkgExtMap,
|
|
272
|
+
strictOptions,
|
|
273
|
+
toggle
|
|
848
274
|
};
|
|
849
275
|
//# sourceMappingURL=index.js.map
|