gs-rollup 1.0.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/README.md +46 -0
- package/README.zh-CN.md +45 -0
- package/bin/gs-rollup +5 -0
- package/lib/core.cjs +42 -0
- package/lib/core.d.ts +11 -0
- package/lib/core.mjs +50 -0
- package/lib/dts.cjs +37 -0
- package/lib/dts.d.ts +6 -0
- package/lib/dts.mjs +41 -0
- package/lib/index.cjs +31 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.mjs +4 -0
- package/lib/plugins.cjs +108 -0
- package/lib/plugins.d.ts +27 -0
- package/lib/plugins.mjs +104 -0
- package/lib/tools.cjs +314 -0
- package/lib/tools.d.ts +34 -0
- package/lib/tools.mjs +332 -0
- package/lib/type.cjs +3 -0
- package/lib/type.d.ts +84 -0
- package/lib/type.mjs +5 -0
- package/package.json +74 -0
package/lib/tools.cjs
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var gsBase = require('gs-base'), node_path = require('node:path'), node_fs = require('node:fs'), plugins = require('./plugins'), fs = require('fs'), type = require('./type');
|
|
3
|
+
function detectEntry() {
|
|
4
|
+
try {
|
|
5
|
+
const pkg = JSON.parse(node_fs.readFileSync("package.json", "utf8"));
|
|
6
|
+
for (const k of ["exports", "module", "main"]) {
|
|
7
|
+
const v = pkg[k];
|
|
8
|
+
if (typeof v == "string" && node_fs.existsSync(v))
|
|
9
|
+
return v;
|
|
10
|
+
}
|
|
11
|
+
} catch {
|
|
12
|
+
}
|
|
13
|
+
const list = [
|
|
14
|
+
"src/index.ts",
|
|
15
|
+
"src/main.ts",
|
|
16
|
+
"src/index.js",
|
|
17
|
+
"index.ts"
|
|
18
|
+
];
|
|
19
|
+
for (const p of list)
|
|
20
|
+
if (node_fs.existsSync(p)) return p;
|
|
21
|
+
if (node_fs.existsSync("src")) {
|
|
22
|
+
const files = node_fs.readdirSync("src").filter((f) => /\.(ts|js)$/.test(f));
|
|
23
|
+
if (files.length === 1)
|
|
24
|
+
return node_path.join("src", files[0]);
|
|
25
|
+
}
|
|
26
|
+
return "src/index.ts";
|
|
27
|
+
}
|
|
28
|
+
class DefaultValues {
|
|
29
|
+
static #input;
|
|
30
|
+
static outputBase = "dist";
|
|
31
|
+
static outputCodeDir = "lib";
|
|
32
|
+
static external = [/^node:|dynamic|(?:^[^/.]{2}.*[^.]{4}|\.(vue|scss))$/];
|
|
33
|
+
static get input() {
|
|
34
|
+
return this.#input || (this.#input = detectEntry());
|
|
35
|
+
}
|
|
36
|
+
static set input(value) {
|
|
37
|
+
this.#input = value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function formatInput(arg) {
|
|
41
|
+
const { input = DefaultValues.input, includeInputDir, includeInputSrc } = arg || {};
|
|
42
|
+
if (gsBase.isString(input))
|
|
43
|
+
return {
|
|
44
|
+
[parseName(input, includeInputDir, includeInputSrc)]: input
|
|
45
|
+
};
|
|
46
|
+
if (Array.isArray(input)) {
|
|
47
|
+
let result = {};
|
|
48
|
+
for (const path of input) {
|
|
49
|
+
const name = parseName(path, includeInputDir, includeInputSrc);
|
|
50
|
+
let tmpName = name;
|
|
51
|
+
for (let i = 1; result[tmpName]; i++)
|
|
52
|
+
tmpName = `${name}${i}`;
|
|
53
|
+
result[tmpName] = path;
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
} else if (gsBase.isObject(input))
|
|
57
|
+
return { ...input };
|
|
58
|
+
}
|
|
59
|
+
function parseName(input, includeInputDir, includeInputSrc) {
|
|
60
|
+
const { name, dir } = node_path.parse(input);
|
|
61
|
+
let result;
|
|
62
|
+
switch (name) {
|
|
63
|
+
case "index":
|
|
64
|
+
result = dir === "src" ? name : dir.replace(/^.+[\/\\]/, "");
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
result = name;
|
|
68
|
+
}
|
|
69
|
+
return includeInputDir && (result = node_path.join(dir, result), result = result.replace(/[\\/]+/g, "/")), (!includeInputSrc || !includeInputDir) && (result = result.replace(/^src[\/\\]/, "")), result;
|
|
70
|
+
}
|
|
71
|
+
function isStringInput(input) {
|
|
72
|
+
return gsBase.isString(input) || Array.isArray(input);
|
|
73
|
+
}
|
|
74
|
+
function getExternalByInput(currInput, inputEntries, arg) {
|
|
75
|
+
const {
|
|
76
|
+
external = DefaultValues.external,
|
|
77
|
+
externalByInput,
|
|
78
|
+
addExternal
|
|
79
|
+
} = arg || {};
|
|
80
|
+
if (externalByInput) {
|
|
81
|
+
const ex = getByInput(currInput, externalByInput);
|
|
82
|
+
if (ex)
|
|
83
|
+
return ex;
|
|
84
|
+
}
|
|
85
|
+
if (gsBase.isFunction(external))
|
|
86
|
+
return external;
|
|
87
|
+
const result = Array.isArray(external) ? [...external] : [external];
|
|
88
|
+
if (inputEntries.length > 1) {
|
|
89
|
+
const inputs = inputEntries.map(([, i]) => i).filter((i) => i !== currInput).map((i) => i.replace(/^src[\\/]|(?:[\\/]?index)?\.[tj]s$/g, "")).filter(Boolean);
|
|
90
|
+
inputs.length && result.push(RegExp(`^[./].*(${inputs.join("|")})`));
|
|
91
|
+
}
|
|
92
|
+
return addExternal && (Array.isArray(addExternal) ? result.push(...addExternal) : result.push(addExternal)), result;
|
|
93
|
+
}
|
|
94
|
+
function getByInput(input, externalByInput) {
|
|
95
|
+
return gsBase.isFunction(externalByInput) ? externalByInput(input) : externalByInput[externalByInput];
|
|
96
|
+
}
|
|
97
|
+
function itemAfterAddPlugin(options, arg) {
|
|
98
|
+
const {
|
|
99
|
+
processImport,
|
|
100
|
+
addPlugins
|
|
101
|
+
} = arg || {};
|
|
102
|
+
if (processImport || options.length > 1) {
|
|
103
|
+
const plugin = plugins.importReplace(gsBase.isBoolean(processImport) ? void 0 : processImport);
|
|
104
|
+
for (const item of options)
|
|
105
|
+
item.plugins = [...item.plugins || [], plugin];
|
|
106
|
+
}
|
|
107
|
+
if (addPlugins?.length)
|
|
108
|
+
for (const item of options)
|
|
109
|
+
item.plugins = [...item.plugins || [], ...addPlugins];
|
|
110
|
+
return options;
|
|
111
|
+
}
|
|
112
|
+
function margeEsImport(code) {
|
|
113
|
+
if (!code.includes(`
|
|
114
|
+
`))
|
|
115
|
+
return processSingleLine(code);
|
|
116
|
+
const lines = code.split(`
|
|
117
|
+
`), resultLines = [], moduleImports = /* @__PURE__ */ new Map();
|
|
118
|
+
for (const line of lines) {
|
|
119
|
+
if (!line.trim().startsWith("import "))
|
|
120
|
+
continue;
|
|
121
|
+
const importInfo = parseImportStatement(line);
|
|
122
|
+
if (!importInfo)
|
|
123
|
+
continue;
|
|
124
|
+
const { modulePath, type: type2, name, namedImports } = importInfo;
|
|
125
|
+
moduleImports.has(modulePath) || moduleImports.set(modulePath, {
|
|
126
|
+
defaultImports: [],
|
|
127
|
+
namedImports: [],
|
|
128
|
+
namespaceImport: void 0,
|
|
129
|
+
isProcessed: !1
|
|
130
|
+
});
|
|
131
|
+
const moduleInfo = moduleImports.get(modulePath);
|
|
132
|
+
type2 === "default" ? moduleInfo.defaultImports.push(name) : type2 === "named" ? moduleInfo.namedImports.push(...namedImports) : type2 === "namespace" && (moduleInfo.namespaceImport = name);
|
|
133
|
+
}
|
|
134
|
+
const processedModules = /* @__PURE__ */ new Set();
|
|
135
|
+
for (const line of lines)
|
|
136
|
+
if (line.trim().startsWith("import ")) {
|
|
137
|
+
const importInfo = parseImportStatement(line);
|
|
138
|
+
if (!importInfo) {
|
|
139
|
+
resultLines.push(line);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const { modulePath } = importInfo, moduleInfo = moduleImports.get(modulePath);
|
|
143
|
+
if (!processedModules.has(modulePath)) {
|
|
144
|
+
const mergedImport = generateMergedImport(modulePath, moduleInfo);
|
|
145
|
+
mergedImport && resultLines.push(...mergedImport.split(`
|
|
146
|
+
`)), processedModules.add(modulePath);
|
|
147
|
+
}
|
|
148
|
+
} else
|
|
149
|
+
resultLines.push(line);
|
|
150
|
+
return resultLines.join(`
|
|
151
|
+
`);
|
|
152
|
+
}
|
|
153
|
+
function parseImportStatement(line) {
|
|
154
|
+
if (line.includes("`"))
|
|
155
|
+
return null;
|
|
156
|
+
const namespaceMatch = /^\s*import\s+\*\s+as\s+([\w$]+)\s+from\s+(['"])([^'"]+)\2\s*;?\s*$/.exec(line);
|
|
157
|
+
if (namespaceMatch)
|
|
158
|
+
return {
|
|
159
|
+
modulePath: namespaceMatch[3],
|
|
160
|
+
type: "namespace",
|
|
161
|
+
name: `* as ${namespaceMatch[1]}`
|
|
162
|
+
// 保留完整的命名空间导入语法
|
|
163
|
+
};
|
|
164
|
+
const defaultAndNamedMatch = /^\s*import\s+([\w$]+)\s*,\s*\{([^}]*)}\s+from\s+(['"])([^'"]+)\3\s*;?\s*$/.exec(line);
|
|
165
|
+
if (defaultAndNamedMatch) {
|
|
166
|
+
const namedImports = defaultAndNamedMatch[2].split(",").map((name) => name.trim()).filter(Boolean);
|
|
167
|
+
return {
|
|
168
|
+
modulePath: defaultAndNamedMatch[4],
|
|
169
|
+
type: "named",
|
|
170
|
+
name: defaultAndNamedMatch[1],
|
|
171
|
+
namedImports
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const namedOnlyMatch = /^\s*import\s*\{([^}]*)}\s+from\s+(['"])([^'"]+)\2\s*;?\s*$/.exec(line);
|
|
175
|
+
if (namedOnlyMatch) {
|
|
176
|
+
const namedImports = namedOnlyMatch[1].split(",").map((name) => name.trim()).filter(Boolean);
|
|
177
|
+
return {
|
|
178
|
+
modulePath: namedOnlyMatch[3],
|
|
179
|
+
type: "named",
|
|
180
|
+
name: "",
|
|
181
|
+
namedImports
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const defaultOnlyMatch = /^\s*import\s+([\w$]+)\s+from\s+(['"])([^'"]+)\2\s*;?\s*$/.exec(line);
|
|
185
|
+
return defaultOnlyMatch ? {
|
|
186
|
+
modulePath: defaultOnlyMatch[3],
|
|
187
|
+
type: "default",
|
|
188
|
+
name: defaultOnlyMatch[1]
|
|
189
|
+
} : null;
|
|
190
|
+
}
|
|
191
|
+
function generateMergedImport(modulePath, info) {
|
|
192
|
+
if (info.namespaceImport)
|
|
193
|
+
return `import ${info.namespaceImport} from '${modulePath}';`;
|
|
194
|
+
let result = "";
|
|
195
|
+
const importParts = [];
|
|
196
|
+
info.defaultImports.length > 0 && importParts.push(info.defaultImports[0]), info.namedImports.length > 0 && importParts.push(`{ ${info.namedImports.join(", ")} }`), importParts.length > 0 && (result = `import ${importParts.join(", ")} from '${modulePath}';`);
|
|
197
|
+
for (let i = 1; i < info.defaultImports.length; i++)
|
|
198
|
+
result += `
|
|
199
|
+
import ${info.defaultImports[i]} from '${modulePath}';`;
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
function processSingleLine(code) {
|
|
203
|
+
const importRegex = /import\s+(?:\*\s+as\s+[\w$]+|[\w$]+|\{[^}]*})\s+from\s+(['"])([^'"]+)\1\s*;?/g, importMatches = [];
|
|
204
|
+
let match;
|
|
205
|
+
for (; (match = importRegex.exec(code)) !== null; )
|
|
206
|
+
importMatches.push({
|
|
207
|
+
start: match.index,
|
|
208
|
+
end: match.index + match[0].length,
|
|
209
|
+
stmt: match[0]
|
|
210
|
+
});
|
|
211
|
+
if (importMatches.length === 0)
|
|
212
|
+
return code;
|
|
213
|
+
const moduleImports = /* @__PURE__ */ new Map();
|
|
214
|
+
for (const { stmt } of importMatches) {
|
|
215
|
+
const importInfo = parseImportStatement(stmt);
|
|
216
|
+
if (!importInfo)
|
|
217
|
+
continue;
|
|
218
|
+
const { modulePath, type: type2, name, namedImports } = importInfo;
|
|
219
|
+
moduleImports.has(modulePath) || moduleImports.set(modulePath, {
|
|
220
|
+
defaultImports: [],
|
|
221
|
+
namedImports: [],
|
|
222
|
+
namespaceImport: void 0
|
|
223
|
+
});
|
|
224
|
+
const moduleInfo = moduleImports.get(modulePath);
|
|
225
|
+
type2 === "default" ? moduleInfo.defaultImports.push(name) : type2 === "named" ? (name && moduleInfo.defaultImports.push(name), namedImports && moduleInfo.namedImports.push(...namedImports)) : type2 === "namespace" && (moduleInfo.namespaceImport = name);
|
|
226
|
+
}
|
|
227
|
+
let result = "";
|
|
228
|
+
const processedModules = /* @__PURE__ */ new Set();
|
|
229
|
+
importMatches.length > 0 && (result += code.slice(0, importMatches[0].start));
|
|
230
|
+
for (const { stmt } of importMatches) {
|
|
231
|
+
const importInfo = parseImportStatement(stmt);
|
|
232
|
+
if (!importInfo) {
|
|
233
|
+
result += stmt;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const { modulePath } = importInfo;
|
|
237
|
+
if (!processedModules.has(modulePath)) {
|
|
238
|
+
const moduleInfo = moduleImports.get(modulePath), mergedImport = generateMergedImport(modulePath, {
|
|
239
|
+
...moduleInfo
|
|
240
|
+
});
|
|
241
|
+
mergedImport && (result += mergedImport), processedModules.add(modulePath);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (importMatches.length > 0) {
|
|
245
|
+
const lastImport = importMatches[importMatches.length - 1];
|
|
246
|
+
result += code.slice(lastImport.end);
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
function isCjsFormat(name) {
|
|
251
|
+
return type.cjsFormats.includes(name);
|
|
252
|
+
}
|
|
253
|
+
function isEsFormat(name) {
|
|
254
|
+
return type.esFormats.includes(name);
|
|
255
|
+
}
|
|
256
|
+
function isEsOrCjsFormat(name) {
|
|
257
|
+
return type.esFormats.includes(name) || type.cjsFormats.includes(name);
|
|
258
|
+
}
|
|
259
|
+
function formatOutput(file, outputBase, outputCodeDir, format, extension) {
|
|
260
|
+
return extension === void 0 && (extension = getExt(format)), outputBase && (outputCodeDir = node_path.join(outputBase, outputCodeDir)), extension && !extension.startsWith(".") && (extension = `.${extension}`), outputCodeDir && (file = node_path.join(outputCodeDir, file)), `${file.replace(/\\/g, "/")}${extension}`;
|
|
261
|
+
}
|
|
262
|
+
function getExt(format) {
|
|
263
|
+
return format === ".d.ts" ? format : isCjsFormat(format) ? ".cjs" : isEsFormat(format) ? ".mjs" : `.${format}.js`;
|
|
264
|
+
}
|
|
265
|
+
const defaultPackageJsonFileName = "package.json";
|
|
266
|
+
function processPackageJson(arg) {
|
|
267
|
+
const {
|
|
268
|
+
minify,
|
|
269
|
+
input = defaultPackageJsonFileName,
|
|
270
|
+
overwriteProps,
|
|
271
|
+
deleteProps,
|
|
272
|
+
deleteChildProps,
|
|
273
|
+
before,
|
|
274
|
+
exports: exports$1 = DefaultValues.input,
|
|
275
|
+
after
|
|
276
|
+
} = arg || {};
|
|
277
|
+
let pkg = JSON.parse(fs.readFileSync(input, "utf8"));
|
|
278
|
+
const bv = before?.(pkg);
|
|
279
|
+
if (bv && (pkg = bv), deleteProps && exeDeleteProps(pkg, deleteProps), deleteChildProps)
|
|
280
|
+
for (const [prop, pattern] of Object.entries(deleteChildProps))
|
|
281
|
+
exeDeleteProps(pkg[prop], pattern);
|
|
282
|
+
exports$1 && (pkg = processExports(pkg, arg)), overwriteProps && (pkg = { ...pkg, ...overwriteProps });
|
|
283
|
+
const av = after?.(pkg);
|
|
284
|
+
return av && (pkg = av), JSON.stringify(pkg, null, minify ? void 0 : 2);
|
|
285
|
+
}
|
|
286
|
+
function exeDeleteProps(obj, pattern) {
|
|
287
|
+
for (const prop of Object.keys(obj))
|
|
288
|
+
pattern.test(prop) && delete obj[prop];
|
|
289
|
+
}
|
|
290
|
+
function processExports(pkg, arg) {
|
|
291
|
+
const { exports: exp = {} } = arg || {}, exOpn = Array.isArray(exp) || gsBase.isString(exp) ? { input: exp } : exp, { input = DefaultValues.input } = exOpn, inputRecord = formatInput({ ...exOpn, input }), exports$1 = pkg.exports = {}, {
|
|
292
|
+
formats = ["cjs", "es", ".d.ts"],
|
|
293
|
+
outputCodeDir = DefaultValues.outputCodeDir,
|
|
294
|
+
default: dft = "index"
|
|
295
|
+
} = exOpn;
|
|
296
|
+
for (const k of Object.keys(inputRecord)) {
|
|
297
|
+
const name = k === dft ? "." : `./${k}`;
|
|
298
|
+
exports$1[name] = formatExports(k, outputCodeDir, formats);
|
|
299
|
+
}
|
|
300
|
+
if (exports$1["."]) {
|
|
301
|
+
const def = exports$1["."];
|
|
302
|
+
def.types && (pkg.types = def.types), def.require && (pkg.main = def.require), def.import && (pkg.module = def.import);
|
|
303
|
+
}
|
|
304
|
+
return pkg;
|
|
305
|
+
}
|
|
306
|
+
function formatExports(file, outputCodeDir, formats) {
|
|
307
|
+
const result = {};
|
|
308
|
+
for (const fmt of formats) {
|
|
309
|
+
const output = `./${formatOutput(file, "", outputCodeDir, fmt)}`;
|
|
310
|
+
fmt === ".d.ts" ? result.types = output : isEsFormat(fmt) ? result.import = output : isCjsFormat(fmt) && (result.require = output);
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
exports.DefaultValues = DefaultValues, exports.defaultPackageJsonFileName = defaultPackageJsonFileName, exports.detectEntry = detectEntry, exports.formatInput = formatInput, exports.formatOutput = formatOutput, exports.getExternalByInput = getExternalByInput, exports.isCjsFormat = isCjsFormat, exports.isEsFormat = isEsFormat, exports.isEsOrCjsFormat = isEsOrCjsFormat, exports.isStringInput = isStringInput, exports.itemAfterAddPlugin = itemAfterAddPlugin, exports.margeEsImport = margeEsImport, exports.processPackageJson = processPackageJson;
|
package/lib/tools.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { IDefineArg, IDefineDtsArg, IPackageJsonArg } from './type';
|
|
2
|
+
import { InputOption, ExternalOption, RollupOptions, ModuleFormat } from 'rollup';
|
|
3
|
+
|
|
4
|
+
declare function formatInput(arg?: IDefineArg): Record<string, string>;
|
|
5
|
+
|
|
6
|
+
declare function isStringInput(input: InputOption): any;
|
|
7
|
+
|
|
8
|
+
declare function detectEntry(): string;
|
|
9
|
+
|
|
10
|
+
declare function getExternalByInput(currInput: string, inputEntries: [string, string][], arg?: IDefineDtsArg): ExternalOption | undefined;
|
|
11
|
+
|
|
12
|
+
declare function itemAfterAddPlugin(options: RollupOptions[], arg?: IDefineDtsArg): RollupOptions[];
|
|
13
|
+
|
|
14
|
+
declare function margeEsImport(code: string): string;
|
|
15
|
+
|
|
16
|
+
declare class DefaultValues {
|
|
17
|
+
#private;
|
|
18
|
+
static outputBase: string;
|
|
19
|
+
static outputCodeDir: string;
|
|
20
|
+
static external: ExternalOption;
|
|
21
|
+
static get input(): InputOption;
|
|
22
|
+
static set input(value: InputOption);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare const defaultPackageJsonFileName = "package.json";
|
|
26
|
+
declare function processPackageJson(arg?: IPackageJsonArg): string;
|
|
27
|
+
|
|
28
|
+
declare function formatOutput(file: string, outputBase: any, outputCodeDir: any, format: ModuleFormat | '.d.ts', extension?: string): string;
|
|
29
|
+
|
|
30
|
+
declare function isCjsFormat(name: ModuleFormat): boolean;
|
|
31
|
+
declare function isEsFormat(name: ModuleFormat): boolean;
|
|
32
|
+
declare function isEsOrCjsFormat(name: ModuleFormat): boolean;
|
|
33
|
+
|
|
34
|
+
export { DefaultValues, defaultPackageJsonFileName, detectEntry, formatInput, formatOutput, getExternalByInput, isCjsFormat, isEsFormat, isEsOrCjsFormat, isStringInput, itemAfterAddPlugin, margeEsImport, processPackageJson };
|
package/lib/tools.mjs
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { isString, isObject, isFunction, isBoolean } from 'gs-base';
|
|
2
|
+
import { join, parse } from 'node:path';
|
|
3
|
+
import { readFileSync, existsSync, readdirSync } from 'node:fs';
|
|
4
|
+
import { importReplace } from './plugins';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import { cjsFormats, esFormats } from './type';
|
|
7
|
+
function detectEntry() {
|
|
8
|
+
try {
|
|
9
|
+
const pkg = JSON.parse(readFileSync("package.json", "utf8"));
|
|
10
|
+
for (const k of ["exports", "module", "main"]) {
|
|
11
|
+
const v = pkg[k];
|
|
12
|
+
if (typeof v == "string" && existsSync(v))
|
|
13
|
+
return v;
|
|
14
|
+
}
|
|
15
|
+
} catch {
|
|
16
|
+
}
|
|
17
|
+
const list = [
|
|
18
|
+
"src/index.ts",
|
|
19
|
+
"src/main.ts",
|
|
20
|
+
"src/index.js",
|
|
21
|
+
"index.ts"
|
|
22
|
+
];
|
|
23
|
+
for (const p of list)
|
|
24
|
+
if (existsSync(p)) return p;
|
|
25
|
+
if (existsSync("src")) {
|
|
26
|
+
const files = readdirSync("src").filter((f) => /\.(ts|js)$/.test(f));
|
|
27
|
+
if (files.length === 1)
|
|
28
|
+
return join("src", files[0]);
|
|
29
|
+
}
|
|
30
|
+
return "src/index.ts";
|
|
31
|
+
}
|
|
32
|
+
class DefaultValues {
|
|
33
|
+
static #input;
|
|
34
|
+
static outputBase = "dist";
|
|
35
|
+
static outputCodeDir = "lib";
|
|
36
|
+
static external = [/^node:|dynamic|(?:^[^/.]{2}.*[^.]{4}|\.(vue|scss))$/];
|
|
37
|
+
static get input() {
|
|
38
|
+
return this.#input || (this.#input = detectEntry());
|
|
39
|
+
}
|
|
40
|
+
static set input(value) {
|
|
41
|
+
this.#input = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function formatInput(arg) {
|
|
45
|
+
const { input = DefaultValues.input, includeInputDir, includeInputSrc } = arg || {};
|
|
46
|
+
if (isString(input))
|
|
47
|
+
return {
|
|
48
|
+
[parseName(input, includeInputDir, includeInputSrc)]: input
|
|
49
|
+
};
|
|
50
|
+
if (Array.isArray(input)) {
|
|
51
|
+
let result = {};
|
|
52
|
+
for (const path of input) {
|
|
53
|
+
const name = parseName(path, includeInputDir, includeInputSrc);
|
|
54
|
+
let tmpName = name;
|
|
55
|
+
for (let i = 1; result[tmpName]; i++)
|
|
56
|
+
tmpName = `${name}${i}`;
|
|
57
|
+
result[tmpName] = path;
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
} else if (isObject(input))
|
|
61
|
+
return { ...input };
|
|
62
|
+
}
|
|
63
|
+
function parseName(input, includeInputDir, includeInputSrc) {
|
|
64
|
+
const { name, dir } = parse(input);
|
|
65
|
+
let result;
|
|
66
|
+
switch (name) {
|
|
67
|
+
case "index":
|
|
68
|
+
result = dir === "src" ? name : dir.replace(/^.+[\/\\]/, "");
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
result = name;
|
|
72
|
+
}
|
|
73
|
+
return includeInputDir && (result = join(dir, result), result = result.replace(/[\\/]+/g, "/")), (!includeInputSrc || !includeInputDir) && (result = result.replace(/^src[\/\\]/, "")), result;
|
|
74
|
+
}
|
|
75
|
+
function isStringInput(input) {
|
|
76
|
+
return isString(input) || Array.isArray(input);
|
|
77
|
+
}
|
|
78
|
+
function getExternalByInput(currInput, inputEntries, arg) {
|
|
79
|
+
const {
|
|
80
|
+
external = DefaultValues.external,
|
|
81
|
+
externalByInput,
|
|
82
|
+
addExternal
|
|
83
|
+
} = arg || {};
|
|
84
|
+
if (externalByInput) {
|
|
85
|
+
const ex = getByInput(currInput, externalByInput);
|
|
86
|
+
if (ex)
|
|
87
|
+
return ex;
|
|
88
|
+
}
|
|
89
|
+
if (isFunction(external))
|
|
90
|
+
return external;
|
|
91
|
+
const result = Array.isArray(external) ? [...external] : [external];
|
|
92
|
+
if (inputEntries.length > 1) {
|
|
93
|
+
const inputs = inputEntries.map(([, i]) => i).filter((i) => i !== currInput).map((i) => i.replace(/^src[\\/]|(?:[\\/]?index)?\.[tj]s$/g, "")).filter(Boolean);
|
|
94
|
+
inputs.length && result.push(RegExp(`^[./].*(${inputs.join("|")})`));
|
|
95
|
+
}
|
|
96
|
+
return addExternal && (Array.isArray(addExternal) ? result.push(...addExternal) : result.push(addExternal)), result;
|
|
97
|
+
}
|
|
98
|
+
function getByInput(input, externalByInput) {
|
|
99
|
+
return isFunction(externalByInput) ? externalByInput(input) : externalByInput[externalByInput];
|
|
100
|
+
}
|
|
101
|
+
function itemAfterAddPlugin(options, arg) {
|
|
102
|
+
const {
|
|
103
|
+
processImport,
|
|
104
|
+
addPlugins
|
|
105
|
+
} = arg || {};
|
|
106
|
+
if (processImport || options.length > 1) {
|
|
107
|
+
const plugin = importReplace(isBoolean(processImport) ? void 0 : processImport);
|
|
108
|
+
for (const item of options)
|
|
109
|
+
item.plugins = [...item.plugins || [], plugin];
|
|
110
|
+
}
|
|
111
|
+
if (addPlugins?.length)
|
|
112
|
+
for (const item of options)
|
|
113
|
+
item.plugins = [...item.plugins || [], ...addPlugins];
|
|
114
|
+
return options;
|
|
115
|
+
}
|
|
116
|
+
function margeEsImport(code) {
|
|
117
|
+
if (!code.includes(`
|
|
118
|
+
`))
|
|
119
|
+
return processSingleLine(code);
|
|
120
|
+
const lines = code.split(`
|
|
121
|
+
`), resultLines = [], moduleImports = /* @__PURE__ */ new Map();
|
|
122
|
+
for (const line of lines) {
|
|
123
|
+
if (!line.trim().startsWith("import "))
|
|
124
|
+
continue;
|
|
125
|
+
const importInfo = parseImportStatement(line);
|
|
126
|
+
if (!importInfo)
|
|
127
|
+
continue;
|
|
128
|
+
const { modulePath, type, name, namedImports } = importInfo;
|
|
129
|
+
moduleImports.has(modulePath) || moduleImports.set(modulePath, {
|
|
130
|
+
defaultImports: [],
|
|
131
|
+
namedImports: [],
|
|
132
|
+
namespaceImport: void 0,
|
|
133
|
+
isProcessed: !1
|
|
134
|
+
});
|
|
135
|
+
const moduleInfo = moduleImports.get(modulePath);
|
|
136
|
+
type === "default" ? moduleInfo.defaultImports.push(name) : type === "named" ? moduleInfo.namedImports.push(...namedImports) : type === "namespace" && (moduleInfo.namespaceImport = name);
|
|
137
|
+
}
|
|
138
|
+
const processedModules = /* @__PURE__ */ new Set();
|
|
139
|
+
for (const line of lines)
|
|
140
|
+
if (line.trim().startsWith("import ")) {
|
|
141
|
+
const importInfo = parseImportStatement(line);
|
|
142
|
+
if (!importInfo) {
|
|
143
|
+
resultLines.push(line);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const { modulePath } = importInfo, moduleInfo = moduleImports.get(modulePath);
|
|
147
|
+
if (!processedModules.has(modulePath)) {
|
|
148
|
+
const mergedImport = generateMergedImport(modulePath, moduleInfo);
|
|
149
|
+
mergedImport && resultLines.push(...mergedImport.split(`
|
|
150
|
+
`)), processedModules.add(modulePath);
|
|
151
|
+
}
|
|
152
|
+
} else
|
|
153
|
+
resultLines.push(line);
|
|
154
|
+
return resultLines.join(`
|
|
155
|
+
`);
|
|
156
|
+
}
|
|
157
|
+
function parseImportStatement(line) {
|
|
158
|
+
if (line.includes("`"))
|
|
159
|
+
return null;
|
|
160
|
+
const namespaceMatch = /^\s*import\s+\*\s+as\s+([\w$]+)\s+from\s+(['"])([^'"]+)\2\s*;?\s*$/.exec(line);
|
|
161
|
+
if (namespaceMatch)
|
|
162
|
+
return {
|
|
163
|
+
modulePath: namespaceMatch[3],
|
|
164
|
+
type: "namespace",
|
|
165
|
+
name: `* as ${namespaceMatch[1]}`
|
|
166
|
+
// 保留完整的命名空间导入语法
|
|
167
|
+
};
|
|
168
|
+
const defaultAndNamedMatch = /^\s*import\s+([\w$]+)\s*,\s*\{([^}]*)}\s+from\s+(['"])([^'"]+)\3\s*;?\s*$/.exec(line);
|
|
169
|
+
if (defaultAndNamedMatch) {
|
|
170
|
+
const namedImports = defaultAndNamedMatch[2].split(",").map((name) => name.trim()).filter(Boolean);
|
|
171
|
+
return {
|
|
172
|
+
modulePath: defaultAndNamedMatch[4],
|
|
173
|
+
type: "named",
|
|
174
|
+
name: defaultAndNamedMatch[1],
|
|
175
|
+
namedImports
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const namedOnlyMatch = /^\s*import\s*\{([^}]*)}\s+from\s+(['"])([^'"]+)\2\s*;?\s*$/.exec(line);
|
|
179
|
+
if (namedOnlyMatch) {
|
|
180
|
+
const namedImports = namedOnlyMatch[1].split(",").map((name) => name.trim()).filter(Boolean);
|
|
181
|
+
return {
|
|
182
|
+
modulePath: namedOnlyMatch[3],
|
|
183
|
+
type: "named",
|
|
184
|
+
name: "",
|
|
185
|
+
namedImports
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const defaultOnlyMatch = /^\s*import\s+([\w$]+)\s+from\s+(['"])([^'"]+)\2\s*;?\s*$/.exec(line);
|
|
189
|
+
return defaultOnlyMatch ? {
|
|
190
|
+
modulePath: defaultOnlyMatch[3],
|
|
191
|
+
type: "default",
|
|
192
|
+
name: defaultOnlyMatch[1]
|
|
193
|
+
} : null;
|
|
194
|
+
}
|
|
195
|
+
function generateMergedImport(modulePath, info) {
|
|
196
|
+
if (info.namespaceImport)
|
|
197
|
+
return `import ${info.namespaceImport} from '${modulePath}';`;
|
|
198
|
+
let result = "";
|
|
199
|
+
const importParts = [];
|
|
200
|
+
info.defaultImports.length > 0 && importParts.push(info.defaultImports[0]), info.namedImports.length > 0 && importParts.push(`{ ${info.namedImports.join(", ")} }`), importParts.length > 0 && (result = `import ${importParts.join(", ")} from '${modulePath}';`);
|
|
201
|
+
for (let i = 1; i < info.defaultImports.length; i++)
|
|
202
|
+
result += `
|
|
203
|
+
import ${info.defaultImports[i]} from '${modulePath}';`;
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
function processSingleLine(code) {
|
|
207
|
+
const importRegex = /import\s+(?:\*\s+as\s+[\w$]+|[\w$]+|\{[^}]*})\s+from\s+(['"])([^'"]+)\1\s*;?/g, importMatches = [];
|
|
208
|
+
let match;
|
|
209
|
+
for (; (match = importRegex.exec(code)) !== null; )
|
|
210
|
+
importMatches.push({
|
|
211
|
+
start: match.index,
|
|
212
|
+
end: match.index + match[0].length,
|
|
213
|
+
stmt: match[0]
|
|
214
|
+
});
|
|
215
|
+
if (importMatches.length === 0)
|
|
216
|
+
return code;
|
|
217
|
+
const moduleImports = /* @__PURE__ */ new Map();
|
|
218
|
+
for (const { stmt } of importMatches) {
|
|
219
|
+
const importInfo = parseImportStatement(stmt);
|
|
220
|
+
if (!importInfo)
|
|
221
|
+
continue;
|
|
222
|
+
const { modulePath, type, name, namedImports } = importInfo;
|
|
223
|
+
moduleImports.has(modulePath) || moduleImports.set(modulePath, {
|
|
224
|
+
defaultImports: [],
|
|
225
|
+
namedImports: [],
|
|
226
|
+
namespaceImport: void 0
|
|
227
|
+
});
|
|
228
|
+
const moduleInfo = moduleImports.get(modulePath);
|
|
229
|
+
type === "default" ? moduleInfo.defaultImports.push(name) : type === "named" ? (name && moduleInfo.defaultImports.push(name), namedImports && moduleInfo.namedImports.push(...namedImports)) : type === "namespace" && (moduleInfo.namespaceImport = name);
|
|
230
|
+
}
|
|
231
|
+
let result = "";
|
|
232
|
+
const processedModules = /* @__PURE__ */ new Set();
|
|
233
|
+
importMatches.length > 0 && (result += code.slice(0, importMatches[0].start));
|
|
234
|
+
for (const { stmt } of importMatches) {
|
|
235
|
+
const importInfo = parseImportStatement(stmt);
|
|
236
|
+
if (!importInfo) {
|
|
237
|
+
result += stmt;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const { modulePath } = importInfo;
|
|
241
|
+
if (!processedModules.has(modulePath)) {
|
|
242
|
+
const moduleInfo = moduleImports.get(modulePath), mergedImport = generateMergedImport(modulePath, {
|
|
243
|
+
...moduleInfo
|
|
244
|
+
});
|
|
245
|
+
mergedImport && (result += mergedImport), processedModules.add(modulePath);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (importMatches.length > 0) {
|
|
249
|
+
const lastImport = importMatches[importMatches.length - 1];
|
|
250
|
+
result += code.slice(lastImport.end);
|
|
251
|
+
}
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
function isCjsFormat(name) {
|
|
255
|
+
return cjsFormats.includes(name);
|
|
256
|
+
}
|
|
257
|
+
function isEsFormat(name) {
|
|
258
|
+
return esFormats.includes(name);
|
|
259
|
+
}
|
|
260
|
+
function isEsOrCjsFormat(name) {
|
|
261
|
+
return esFormats.includes(name) || cjsFormats.includes(name);
|
|
262
|
+
}
|
|
263
|
+
function formatOutput(file, outputBase, outputCodeDir, format, extension) {
|
|
264
|
+
return extension === void 0 && (extension = getExt(format)), outputBase && (outputCodeDir = join(outputBase, outputCodeDir)), extension && !extension.startsWith(".") && (extension = `.${extension}`), outputCodeDir && (file = join(outputCodeDir, file)), `${file.replace(/\\/g, "/")}${extension}`;
|
|
265
|
+
}
|
|
266
|
+
function getExt(format) {
|
|
267
|
+
return format === ".d.ts" ? format : isCjsFormat(format) ? ".cjs" : isEsFormat(format) ? ".mjs" : `.${format}.js`;
|
|
268
|
+
}
|
|
269
|
+
const defaultPackageJsonFileName = "package.json";
|
|
270
|
+
function processPackageJson(arg) {
|
|
271
|
+
const {
|
|
272
|
+
minify,
|
|
273
|
+
input = defaultPackageJsonFileName,
|
|
274
|
+
overwriteProps,
|
|
275
|
+
deleteProps,
|
|
276
|
+
deleteChildProps,
|
|
277
|
+
before,
|
|
278
|
+
exports: exports$1 = DefaultValues.input,
|
|
279
|
+
after
|
|
280
|
+
} = arg || {};
|
|
281
|
+
let pkg = JSON.parse(fs.readFileSync(input, "utf8"));
|
|
282
|
+
const bv = before?.(pkg);
|
|
283
|
+
if (bv && (pkg = bv), deleteProps && exeDeleteProps(pkg, deleteProps), deleteChildProps)
|
|
284
|
+
for (const [prop, pattern] of Object.entries(deleteChildProps))
|
|
285
|
+
exeDeleteProps(pkg[prop], pattern);
|
|
286
|
+
exports$1 && (pkg = processExports(pkg, arg)), overwriteProps && (pkg = { ...pkg, ...overwriteProps });
|
|
287
|
+
const av = after?.(pkg);
|
|
288
|
+
return av && (pkg = av), JSON.stringify(pkg, null, minify ? void 0 : 2);
|
|
289
|
+
}
|
|
290
|
+
function exeDeleteProps(obj, pattern) {
|
|
291
|
+
for (const prop of Object.keys(obj))
|
|
292
|
+
pattern.test(prop) && delete obj[prop];
|
|
293
|
+
}
|
|
294
|
+
function processExports(pkg, arg) {
|
|
295
|
+
const { exports: exp = {} } = arg || {}, exOpn = Array.isArray(exp) || isString(exp) ? { input: exp } : exp, { input = DefaultValues.input } = exOpn, inputRecord = formatInput({ ...exOpn, input }), exports$1 = pkg.exports = {}, {
|
|
296
|
+
formats = ["cjs", "es", ".d.ts"],
|
|
297
|
+
outputCodeDir = DefaultValues.outputCodeDir,
|
|
298
|
+
default: dft = "index"
|
|
299
|
+
} = exOpn;
|
|
300
|
+
for (const k of Object.keys(inputRecord)) {
|
|
301
|
+
const name = k === dft ? "." : `./${k}`;
|
|
302
|
+
exports$1[name] = formatExports(k, outputCodeDir, formats);
|
|
303
|
+
}
|
|
304
|
+
if (exports$1["."]) {
|
|
305
|
+
const def = exports$1["."];
|
|
306
|
+
def.types && (pkg.types = def.types), def.require && (pkg.main = def.require), def.import && (pkg.module = def.import);
|
|
307
|
+
}
|
|
308
|
+
return pkg;
|
|
309
|
+
}
|
|
310
|
+
function formatExports(file, outputCodeDir, formats) {
|
|
311
|
+
const result = {};
|
|
312
|
+
for (const fmt of formats) {
|
|
313
|
+
const output = `./${formatOutput(file, "", outputCodeDir, fmt)}`;
|
|
314
|
+
fmt === ".d.ts" ? result.types = output : isEsFormat(fmt) ? result.import = output : isCjsFormat(fmt) && (result.require = output);
|
|
315
|
+
}
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
export {
|
|
319
|
+
DefaultValues,
|
|
320
|
+
defaultPackageJsonFileName,
|
|
321
|
+
detectEntry,
|
|
322
|
+
formatInput,
|
|
323
|
+
formatOutput,
|
|
324
|
+
getExternalByInput,
|
|
325
|
+
isCjsFormat,
|
|
326
|
+
isEsFormat,
|
|
327
|
+
isEsOrCjsFormat,
|
|
328
|
+
isStringInput,
|
|
329
|
+
itemAfterAddPlugin,
|
|
330
|
+
margeEsImport,
|
|
331
|
+
processPackageJson
|
|
332
|
+
};
|
package/lib/type.cjs
ADDED