initial commit

This commit is contained in:
2026-04-05 16:08:59 +03:00
commit 75ab9bec9f
1117 changed files with 789034 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
'use strict';
var fs = require('node:fs/promises');
var utils = require('./utils-CylcaoNQ.cjs');
require('magic-string');
require('mlly');
function transformDtsDefaultCJSExports(code, fileName, options = {}) {
return utils.internalFixDefaultCJSExports(code, {
fileName,
// we don't need the imports (used only for exporting types optimization)
imports: []
}, options);
}
async function fixDtsFileDefaultCJSExports(dtsPath, options = {}) {
const result = transformDtsDefaultCJSExports(
await fs.readFile(dtsPath, "utf-8"),
dtsPath,
options
);
if (result) {
await fs.writeFile(dtsPath, result, "utf8");
}
return !!result;
}
async function transformESMDtsToCJSDts(dtsPath, dtsDestPath, options = {}) {
if (dtsPath === dtsDestPath) {
throw new Error(`dtsPath and dtsDestPath should be different: ${dtsPath}`);
}
const code = await fs.readFile(dtsPath, "utf-8");
const result = transformDtsDefaultCJSExports(
code,
dtsPath,
options
) ?? code;
const { transformLocalImports = defaultLocalImportsTransformer } = options;
await fs.writeFile(
dtsDestPath,
transformLocalImports(result, dtsPath, dtsDestPath),
"utf8"
);
}
function defaultLocalImportsTransformer(code, dtsPath, dtsDestPath) {
const from = dtsPath.endsWith(".d.mts") ? /\s+(from\s+["'].\.?\/.*(\.mjs)["'];?)\s+/g : /\s+(from\s+["'].\.?\/.*(\.js)["'];?)\s+/g;
let matcher = from.exec(code);
if (!matcher) {
return code;
}
const extension = dtsDestPath.endsWith("d.ts") ? ".js" : ".cjs";
while (matcher) {
code = code.replaceAll(matcher[1], matcher[1].replace(matcher[2], extension));
matcher = from.exec(code);
}
return code;
}
exports.defaultLocalImportsTransformer = defaultLocalImportsTransformer;
exports.fixDtsFileDefaultCJSExports = fixDtsFileDefaultCJSExports;
exports.transformDtsDefaultCJSExports = transformDtsDefaultCJSExports;
exports.transformESMDtsToCJSDts = transformESMDtsToCJSDts;

View File

@@ -0,0 +1,51 @@
interface Options {
warn?: (message: string) => void;
}
/**
* Fix default exports.
*
* **WARNING**: this function doesn't handle local imports/exports transformations.
*
* @param code The code to transform.
* @param fileName The file name.
* @param options The options to be used.
* @return The transformed code or `undefined` if no transformation was needed.
*/
declare function transformDtsDefaultCJSExports(code: string, fileName: string, options?: Options): string | undefined;
/**
* Fix default exports in the file and writes the changes to the file when needed, otherwise the files remains untouched.
*
* @param dtsPath The path to the file to fix.
* @param options The path
*/
declare function fixDtsFileDefaultCJSExports(dtsPath: string, options?: Options): Promise<boolean>;
interface TransformOptions {
warn?: (message: string) => void;
transformLocalImports?: (code: string, dtsPath: string, dtsDestPath: string) => string;
}
/**
* Given an `ESM` dts file, transform it to a `d.ts` or `d.cts` file fixing CJS default exports changing the import/exports when needed.
*
* @param dtsPath The source `ESM` (`d.ts` or `.d.mts`) file path.
* @param dtsDestPath The destination `.d.ts` or `.d.cts` file path.
* @param options The options to use.
* @see {@link defaultLocalImportsTransformer}
*/
declare function transformESMDtsToCJSDts(dtsPath: string, dtsDestPath: string, options?: TransformOptions): Promise<void>;
/**
* Given an `ESM` dts file, transform it to a `d.ts` or `d.cts` file fixing CJS default exports.
*
* **NOTE**: local imports/exports will be replaced with the corresponding extension using source and destination files:
* - when `dtsPath` is a `.d.ts` and `dtsDestPath` is a `d.cts`: `import { foo } from './foo.js'` -> `import { foo } from './foo.cjs'`
* - when `dtsPath` is a `.d.mts` and `dtsDestPath` is a `d.ts`: `import { foo } from './foo.mjs'` -> `import { foo } from './foo.js'`
* - when `dtsPath` is a `.d.mts` and `dtsDestPath` is a `d.cts`: `import { foo } from './foo.mjs'` -> `import { foo } from './foo.cjs'`
*
* @param code The code to transform.
* @param dtsPath The source `ESM` (`d.ts` or `.d.mts`) file path.
* @param dtsDestPath The destination `.d.ts` or `.d.cts` file path.
* @return The transformed code.
*/
declare function defaultLocalImportsTransformer(code: string, dtsPath: string, dtsDestPath: string): string;
export { type Options, type TransformOptions, defaultLocalImportsTransformer, fixDtsFileDefaultCJSExports, transformDtsDefaultCJSExports, transformESMDtsToCJSDts };

View File

@@ -0,0 +1,51 @@
interface Options {
warn?: (message: string) => void;
}
/**
* Fix default exports.
*
* **WARNING**: this function doesn't handle local imports/exports transformations.
*
* @param code The code to transform.
* @param fileName The file name.
* @param options The options to be used.
* @return The transformed code or `undefined` if no transformation was needed.
*/
declare function transformDtsDefaultCJSExports(code: string, fileName: string, options?: Options): string | undefined;
/**
* Fix default exports in the file and writes the changes to the file when needed, otherwise the files remains untouched.
*
* @param dtsPath The path to the file to fix.
* @param options The path
*/
declare function fixDtsFileDefaultCJSExports(dtsPath: string, options?: Options): Promise<boolean>;
interface TransformOptions {
warn?: (message: string) => void;
transformLocalImports?: (code: string, dtsPath: string, dtsDestPath: string) => string;
}
/**
* Given an `ESM` dts file, transform it to a `d.ts` or `d.cts` file fixing CJS default exports changing the import/exports when needed.
*
* @param dtsPath The source `ESM` (`d.ts` or `.d.mts`) file path.
* @param dtsDestPath The destination `.d.ts` or `.d.cts` file path.
* @param options The options to use.
* @see {@link defaultLocalImportsTransformer}
*/
declare function transformESMDtsToCJSDts(dtsPath: string, dtsDestPath: string, options?: TransformOptions): Promise<void>;
/**
* Given an `ESM` dts file, transform it to a `d.ts` or `d.cts` file fixing CJS default exports.
*
* **NOTE**: local imports/exports will be replaced with the corresponding extension using source and destination files:
* - when `dtsPath` is a `.d.ts` and `dtsDestPath` is a `d.cts`: `import { foo } from './foo.js'` -> `import { foo } from './foo.cjs'`
* - when `dtsPath` is a `.d.mts` and `dtsDestPath` is a `d.ts`: `import { foo } from './foo.mjs'` -> `import { foo } from './foo.js'`
* - when `dtsPath` is a `.d.mts` and `dtsDestPath` is a `d.cts`: `import { foo } from './foo.mjs'` -> `import { foo } from './foo.cjs'`
*
* @param code The code to transform.
* @param dtsPath The source `ESM` (`d.ts` or `.d.mts`) file path.
* @param dtsDestPath The destination `.d.ts` or `.d.cts` file path.
* @return The transformed code.
*/
declare function defaultLocalImportsTransformer(code: string, dtsPath: string, dtsDestPath: string): string;
export { type Options, type TransformOptions, defaultLocalImportsTransformer, fixDtsFileDefaultCJSExports, transformDtsDefaultCJSExports, transformESMDtsToCJSDts };

View File

@@ -0,0 +1,51 @@
interface Options {
warn?: (message: string) => void;
}
/**
* Fix default exports.
*
* **WARNING**: this function doesn't handle local imports/exports transformations.
*
* @param code The code to transform.
* @param fileName The file name.
* @param options The options to be used.
* @return The transformed code or `undefined` if no transformation was needed.
*/
declare function transformDtsDefaultCJSExports(code: string, fileName: string, options?: Options): string | undefined;
/**
* Fix default exports in the file and writes the changes to the file when needed, otherwise the files remains untouched.
*
* @param dtsPath The path to the file to fix.
* @param options The path
*/
declare function fixDtsFileDefaultCJSExports(dtsPath: string, options?: Options): Promise<boolean>;
interface TransformOptions {
warn?: (message: string) => void;
transformLocalImports?: (code: string, dtsPath: string, dtsDestPath: string) => string;
}
/**
* Given an `ESM` dts file, transform it to a `d.ts` or `d.cts` file fixing CJS default exports changing the import/exports when needed.
*
* @param dtsPath The source `ESM` (`d.ts` or `.d.mts`) file path.
* @param dtsDestPath The destination `.d.ts` or `.d.cts` file path.
* @param options The options to use.
* @see {@link defaultLocalImportsTransformer}
*/
declare function transformESMDtsToCJSDts(dtsPath: string, dtsDestPath: string, options?: TransformOptions): Promise<void>;
/**
* Given an `ESM` dts file, transform it to a `d.ts` or `d.cts` file fixing CJS default exports.
*
* **NOTE**: local imports/exports will be replaced with the corresponding extension using source and destination files:
* - when `dtsPath` is a `.d.ts` and `dtsDestPath` is a `d.cts`: `import { foo } from './foo.js'` -> `import { foo } from './foo.cjs'`
* - when `dtsPath` is a `.d.mts` and `dtsDestPath` is a `d.ts`: `import { foo } from './foo.mjs'` -> `import { foo } from './foo.js'`
* - when `dtsPath` is a `.d.mts` and `dtsDestPath` is a `d.cts`: `import { foo } from './foo.mjs'` -> `import { foo } from './foo.cjs'`
*
* @param code The code to transform.
* @param dtsPath The source `ESM` (`d.ts` or `.d.mts`) file path.
* @param dtsDestPath The destination `.d.ts` or `.d.cts` file path.
* @return The transformed code.
*/
declare function defaultLocalImportsTransformer(code: string, dtsPath: string, dtsDestPath: string): string;
export { type Options, type TransformOptions, defaultLocalImportsTransformer, fixDtsFileDefaultCJSExports, transformDtsDefaultCJSExports, transformESMDtsToCJSDts };

View File

@@ -0,0 +1,55 @@
import fs from 'node:fs/promises';
import { i as internalFixDefaultCJSExports } from './utils-DwzdDEfz.js';
import 'magic-string';
import 'mlly';
function transformDtsDefaultCJSExports(code, fileName, options = {}) {
return internalFixDefaultCJSExports(code, {
fileName,
// we don't need the imports (used only for exporting types optimization)
imports: []
}, options);
}
async function fixDtsFileDefaultCJSExports(dtsPath, options = {}) {
const result = transformDtsDefaultCJSExports(
await fs.readFile(dtsPath, "utf-8"),
dtsPath,
options
);
if (result) {
await fs.writeFile(dtsPath, result, "utf8");
}
return !!result;
}
async function transformESMDtsToCJSDts(dtsPath, dtsDestPath, options = {}) {
if (dtsPath === dtsDestPath) {
throw new Error(`dtsPath and dtsDestPath should be different: ${dtsPath}`);
}
const code = await fs.readFile(dtsPath, "utf-8");
const result = transformDtsDefaultCJSExports(
code,
dtsPath,
options
) ?? code;
const { transformLocalImports = defaultLocalImportsTransformer } = options;
await fs.writeFile(
dtsDestPath,
transformLocalImports(result, dtsPath, dtsDestPath),
"utf8"
);
}
function defaultLocalImportsTransformer(code, dtsPath, dtsDestPath) {
const from = dtsPath.endsWith(".d.mts") ? /\s+(from\s+["'].\.?\/.*(\.mjs)["'];?)\s+/g : /\s+(from\s+["'].\.?\/.*(\.js)["'];?)\s+/g;
let matcher = from.exec(code);
if (!matcher) {
return code;
}
const extension = dtsDestPath.endsWith("d.ts") ? ".js" : ".cjs";
while (matcher) {
code = code.replaceAll(matcher[1], matcher[1].replace(matcher[2], extension));
matcher = from.exec(code);
}
return code;
}
export { defaultLocalImportsTransformer, fixDtsFileDefaultCJSExports, transformDtsDefaultCJSExports, transformESMDtsToCJSDts };

View File

@@ -0,0 +1,55 @@
import fs from 'node:fs/promises';
import { i as internalFixDefaultCJSExports } from './utils-DwzdDEfz.mjs';
import 'magic-string';
import 'mlly';
function transformDtsDefaultCJSExports(code, fileName, options = {}) {
return internalFixDefaultCJSExports(code, {
fileName,
// we don't need the imports (used only for exporting types optimization)
imports: []
}, options);
}
async function fixDtsFileDefaultCJSExports(dtsPath, options = {}) {
const result = transformDtsDefaultCJSExports(
await fs.readFile(dtsPath, "utf-8"),
dtsPath,
options
);
if (result) {
await fs.writeFile(dtsPath, result, "utf8");
}
return !!result;
}
async function transformESMDtsToCJSDts(dtsPath, dtsDestPath, options = {}) {
if (dtsPath === dtsDestPath) {
throw new Error(`dtsPath and dtsDestPath should be different: ${dtsPath}`);
}
const code = await fs.readFile(dtsPath, "utf-8");
const result = transformDtsDefaultCJSExports(
code,
dtsPath,
options
) ?? code;
const { transformLocalImports = defaultLocalImportsTransformer } = options;
await fs.writeFile(
dtsDestPath,
transformLocalImports(result, dtsPath, dtsDestPath),
"utf8"
);
}
function defaultLocalImportsTransformer(code, dtsPath, dtsDestPath) {
const from = dtsPath.endsWith(".d.mts") ? /\s+(from\s+["'].\.?\/.*(\.mjs)["'];?)\s+/g : /\s+(from\s+["'].\.?\/.*(\.js)["'];?)\s+/g;
let matcher = from.exec(code);
if (!matcher) {
return code;
}
const extension = dtsDestPath.endsWith("d.ts") ? ".js" : ".cjs";
while (matcher) {
code = code.replaceAll(matcher[1], matcher[1].replace(matcher[2], extension));
matcher = from.exec(code);
}
return code;
}
export { defaultLocalImportsTransformer, fixDtsFileDefaultCJSExports, transformDtsDefaultCJSExports, transformESMDtsToCJSDts };

View File

@@ -0,0 +1,25 @@
'use strict';
var utils = require('./utils-CylcaoNQ.cjs');
require('magic-string');
require('mlly');
function cjsExportsDtsMatcher(info) {
return info.type === "chunk" && info.exports?.length > 0 && info.exports.includes("default") && /\.d\.c?ts$/.test(info.fileName);
}
function defaultCjsExportsDtsMatcher(info) {
return cjsExportsDtsMatcher(info) && info.isEntry;
}
function FixDtsDefaultCjsExportsPlugin(options = {}) {
const { matcher = defaultCjsExportsDtsMatcher } = options;
return {
name: "fix-dts-default-cjs-exports-plugin",
renderChunk(code, info) {
return matcher(info) ? utils.internalFixDefaultCJSExports(code, info, options) : void 0;
}
};
}
exports.FixDtsDefaultCjsExportsPlugin = FixDtsDefaultCjsExportsPlugin;
exports.cjsExportsDtsMatcher = cjsExportsDtsMatcher;
exports.defaultCjsExportsDtsMatcher = defaultCjsExportsDtsMatcher;

View File

@@ -0,0 +1,11 @@
import { RenderedChunk, Plugin } from 'rollup';
interface PluginOptions {
warn?: (message: string) => void;
matcher?: (info: RenderedChunk) => boolean;
}
declare function cjsExportsDtsMatcher(info: RenderedChunk): boolean;
declare function defaultCjsExportsDtsMatcher(info: RenderedChunk): boolean;
declare function FixDtsDefaultCjsExportsPlugin(options?: PluginOptions): Plugin;
export { FixDtsDefaultCjsExportsPlugin, type PluginOptions, cjsExportsDtsMatcher, defaultCjsExportsDtsMatcher };

View File

@@ -0,0 +1,11 @@
import { RenderedChunk, Plugin } from 'rollup';
interface PluginOptions {
warn?: (message: string) => void;
matcher?: (info: RenderedChunk) => boolean;
}
declare function cjsExportsDtsMatcher(info: RenderedChunk): boolean;
declare function defaultCjsExportsDtsMatcher(info: RenderedChunk): boolean;
declare function FixDtsDefaultCjsExportsPlugin(options?: PluginOptions): Plugin;
export { FixDtsDefaultCjsExportsPlugin, type PluginOptions, cjsExportsDtsMatcher, defaultCjsExportsDtsMatcher };

View File

@@ -0,0 +1,11 @@
import { RenderedChunk, Plugin } from 'rollup';
interface PluginOptions {
warn?: (message: string) => void;
matcher?: (info: RenderedChunk) => boolean;
}
declare function cjsExportsDtsMatcher(info: RenderedChunk): boolean;
declare function defaultCjsExportsDtsMatcher(info: RenderedChunk): boolean;
declare function FixDtsDefaultCjsExportsPlugin(options?: PluginOptions): Plugin;
export { FixDtsDefaultCjsExportsPlugin, type PluginOptions, cjsExportsDtsMatcher, defaultCjsExportsDtsMatcher };

View File

@@ -0,0 +1,21 @@
import { i as internalFixDefaultCJSExports } from './utils-DwzdDEfz.js';
import 'magic-string';
import 'mlly';
function cjsExportsDtsMatcher(info) {
return info.type === "chunk" && info.exports?.length > 0 && info.exports.includes("default") && /\.d\.c?ts$/.test(info.fileName);
}
function defaultCjsExportsDtsMatcher(info) {
return cjsExportsDtsMatcher(info) && info.isEntry;
}
function FixDtsDefaultCjsExportsPlugin(options = {}) {
const { matcher = defaultCjsExportsDtsMatcher } = options;
return {
name: "fix-dts-default-cjs-exports-plugin",
renderChunk(code, info) {
return matcher(info) ? internalFixDefaultCJSExports(code, info, options) : void 0;
}
};
}
export { FixDtsDefaultCjsExportsPlugin, cjsExportsDtsMatcher, defaultCjsExportsDtsMatcher };

View File

@@ -0,0 +1,21 @@
import { i as internalFixDefaultCJSExports } from './utils-DwzdDEfz.mjs';
import 'magic-string';
import 'mlly';
function cjsExportsDtsMatcher(info) {
return info.type === "chunk" && info.exports?.length > 0 && info.exports.includes("default") && /\.d\.c?ts$/.test(info.fileName);
}
function defaultCjsExportsDtsMatcher(info) {
return cjsExportsDtsMatcher(info) && info.isEntry;
}
function FixDtsDefaultCjsExportsPlugin(options = {}) {
const { matcher = defaultCjsExportsDtsMatcher } = options;
return {
name: "fix-dts-default-cjs-exports-plugin",
renderChunk(code, info) {
return matcher(info) ? internalFixDefaultCJSExports(code, info, options) : void 0;
}
};
}
export { FixDtsDefaultCjsExportsPlugin, cjsExportsDtsMatcher, defaultCjsExportsDtsMatcher };

View File

@@ -0,0 +1,201 @@
'use strict';
var MagicString = require('magic-string');
var mlly = require('mlly');
function internalFixDefaultCJSExports(code, info, options) {
const parsedExports = extractExports(code, info, options);
if (!parsedExports) {
return;
}
if (parsedExports.defaultExport.specifier) {
const imports = [];
for (const imp of mlly.findStaticImports(code)) {
if (!imp.imports) {
continue;
}
imports.push(mlly.parseStaticImport(imp));
}
const specifier = parsedExports.defaultExport.specifier;
const defaultImport = imports.find((i) => i.specifier === specifier);
return parsedExports.defaultExport._type === "named" ? handleDefaultNamedCJSExport(
code,
info,
parsedExports,
imports,
options,
defaultImport
) : handleDefaultCJSExportAsDefault(
code,
parsedExports,
imports,
defaultImport
) || handleNoSpecifierDefaultCJSExport(code, info, parsedExports);
}
return handleNoSpecifierDefaultCJSExport(code, info, parsedExports);
}
function extractExports(code, info, options) {
const defaultExport = mlly.findExports(code).find(
(e) => e.names.includes("default")
);
if (!defaultExport) {
options.warn?.(
/* c8 ignore next */
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
const match = defaultExport.code.match(/export\s*\{([^}]*)\}/);
if (!match?.length) {
options.warn?.(
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
let defaultAlias;
const exportsEntries = [];
for (const exp of match[1].split(",").map((e) => e.trim())) {
if (exp === "default") {
defaultAlias = exp;
continue;
}
const m = exp.match(/\s*as\s+default\s*/);
if (m) {
defaultAlias = exp.replace(m[0], "");
} else {
exportsEntries.push(exp);
}
}
if (!defaultAlias) {
options.warn?.(
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
return {
defaultExport,
defaultAlias,
exports: exportsEntries
};
}
function handleDefaultCJSExportAsDefault(code, { defaultExport, exports }, imports, defaultImport) {
if (defaultImport) {
return exports.length === 0 ? code.replace(
defaultExport.code,
`export = ${defaultImport.defaultImport}`
) : code.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultImport.defaultImport};
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
);
} else {
const magicString = new MagicString(code);
const lastImportPosition = imports.length > 0 ? imports.at(-1)?.end || 0 : 0;
if (lastImportPosition > 0) {
magicString.appendRight(
lastImportPosition,
`
import _default from '${defaultExport.specifier}';
`
);
} else {
magicString.prepend(
`import _default from '${defaultExport.specifier}';
`
);
}
return exports.length > 0 ? magicString.replace(
defaultExport.code,
`// @ts-ignore
export = _default;
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
).toString() : magicString.replace(defaultExport.code, "export = _default").toString();
}
}
function handleDefaultNamedCJSExport(code, info, parsedExports, imports, options, defaultImport) {
const { defaultAlias, defaultExport, exports } = parsedExports;
if (defaultAlias === "default") {
if (defaultImport && !defaultImport.defaultImport) {
options.warn?.(
`Cannot parse default export name from ${defaultImport.specifier} import at ${info.fileName}!.`
);
return;
}
return handleDefaultCJSExportAsDefault(
code,
parsedExports,
imports,
defaultImport
);
}
if (defaultImport) {
const namedExports = defaultImport.namedImports;
if (namedExports?.[defaultAlias] === defaultAlias) {
return exports.length === 0 ? code.replace(defaultExport.code, `export = ${defaultAlias}`) : code.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultAlias};
export { ${exports.join(", ")} }`
);
} else {
options.warn?.(
`Cannot parse "${defaultAlias}" named export from ${defaultImport.specifier} import at ${info.fileName}!.`
);
return void 0;
}
}
const magicString = new MagicString(code);
const lastImportPosition = imports.length > 0 ? imports.at(-1)?.end || 0 : 0;
if (lastImportPosition > 0) {
magicString.appendRight(
lastImportPosition,
`
import { ${defaultAlias} } from '${defaultExport.specifier}';
`
);
} else {
magicString.prepend(
`import { ${defaultAlias} } from '${defaultExport.specifier}';
`
);
}
return exports.length > 0 ? magicString.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultAlias};
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
).toString() : magicString.replace(defaultExport.code, `export = ${defaultAlias}`).toString();
}
function handleNoSpecifierDefaultCJSExport(code, info, { defaultAlias, defaultExport, exports }) {
let exportStatement = exports.length > 0 ? void 0 : "";
if (exportStatement === void 0) {
let someExternalExport = false;
const typeExportRegexp = /\s*type\s+/;
const allRemainingExports = exports.map((exp) => {
if (someExternalExport) {
return [exp, ""];
}
if (!info.imports.includes(exp)) {
const m = exp.match(typeExportRegexp);
if (m) {
const name = exp.replace(m[0], "").trim();
if (!info.imports.includes(name)) {
return [exp, name];
}
}
}
someExternalExport = true;
return [exp, ""];
});
exportStatement = someExternalExport ? `;
export { ${allRemainingExports.map(([e, _]) => e).join(", ")} }` : `;
export type { ${allRemainingExports.map(([_, t]) => t).join(", ")} }`;
}
return code.replace(
defaultExport.code,
`${exportStatement.length > 0 ? "// @ts-ignore\n" : ""}export = ${defaultAlias}${exportStatement}`
);
}
exports.internalFixDefaultCJSExports = internalFixDefaultCJSExports;

View File

@@ -0,0 +1,199 @@
import MagicString from 'magic-string';
import { findStaticImports, parseStaticImport, findExports } from 'mlly';
function internalFixDefaultCJSExports(code, info, options) {
const parsedExports = extractExports(code, info, options);
if (!parsedExports) {
return;
}
if (parsedExports.defaultExport.specifier) {
const imports = [];
for (const imp of findStaticImports(code)) {
if (!imp.imports) {
continue;
}
imports.push(parseStaticImport(imp));
}
const specifier = parsedExports.defaultExport.specifier;
const defaultImport = imports.find((i) => i.specifier === specifier);
return parsedExports.defaultExport._type === "named" ? handleDefaultNamedCJSExport(
code,
info,
parsedExports,
imports,
options,
defaultImport
) : handleDefaultCJSExportAsDefault(
code,
parsedExports,
imports,
defaultImport
) || handleNoSpecifierDefaultCJSExport(code, info, parsedExports);
}
return handleNoSpecifierDefaultCJSExport(code, info, parsedExports);
}
function extractExports(code, info, options) {
const defaultExport = findExports(code).find(
(e) => e.names.includes("default")
);
if (!defaultExport) {
options.warn?.(
/* c8 ignore next */
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
const match = defaultExport.code.match(/export\s*\{([^}]*)\}/);
if (!match?.length) {
options.warn?.(
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
let defaultAlias;
const exportsEntries = [];
for (const exp of match[1].split(",").map((e) => e.trim())) {
if (exp === "default") {
defaultAlias = exp;
continue;
}
const m = exp.match(/\s*as\s+default\s*/);
if (m) {
defaultAlias = exp.replace(m[0], "");
} else {
exportsEntries.push(exp);
}
}
if (!defaultAlias) {
options.warn?.(
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
return {
defaultExport,
defaultAlias,
exports: exportsEntries
};
}
function handleDefaultCJSExportAsDefault(code, { defaultExport, exports }, imports, defaultImport) {
if (defaultImport) {
return exports.length === 0 ? code.replace(
defaultExport.code,
`export = ${defaultImport.defaultImport}`
) : code.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultImport.defaultImport};
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
);
} else {
const magicString = new MagicString(code);
const lastImportPosition = imports.length > 0 ? imports.at(-1)?.end || 0 : 0;
if (lastImportPosition > 0) {
magicString.appendRight(
lastImportPosition,
`
import _default from '${defaultExport.specifier}';
`
);
} else {
magicString.prepend(
`import _default from '${defaultExport.specifier}';
`
);
}
return exports.length > 0 ? magicString.replace(
defaultExport.code,
`// @ts-ignore
export = _default;
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
).toString() : magicString.replace(defaultExport.code, "export = _default").toString();
}
}
function handleDefaultNamedCJSExport(code, info, parsedExports, imports, options, defaultImport) {
const { defaultAlias, defaultExport, exports } = parsedExports;
if (defaultAlias === "default") {
if (defaultImport && !defaultImport.defaultImport) {
options.warn?.(
`Cannot parse default export name from ${defaultImport.specifier} import at ${info.fileName}!.`
);
return;
}
return handleDefaultCJSExportAsDefault(
code,
parsedExports,
imports,
defaultImport
);
}
if (defaultImport) {
const namedExports = defaultImport.namedImports;
if (namedExports?.[defaultAlias] === defaultAlias) {
return exports.length === 0 ? code.replace(defaultExport.code, `export = ${defaultAlias}`) : code.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultAlias};
export { ${exports.join(", ")} }`
);
} else {
options.warn?.(
`Cannot parse "${defaultAlias}" named export from ${defaultImport.specifier} import at ${info.fileName}!.`
);
return void 0;
}
}
const magicString = new MagicString(code);
const lastImportPosition = imports.length > 0 ? imports.at(-1)?.end || 0 : 0;
if (lastImportPosition > 0) {
magicString.appendRight(
lastImportPosition,
`
import { ${defaultAlias} } from '${defaultExport.specifier}';
`
);
} else {
magicString.prepend(
`import { ${defaultAlias} } from '${defaultExport.specifier}';
`
);
}
return exports.length > 0 ? magicString.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultAlias};
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
).toString() : magicString.replace(defaultExport.code, `export = ${defaultAlias}`).toString();
}
function handleNoSpecifierDefaultCJSExport(code, info, { defaultAlias, defaultExport, exports }) {
let exportStatement = exports.length > 0 ? void 0 : "";
if (exportStatement === void 0) {
let someExternalExport = false;
const typeExportRegexp = /\s*type\s+/;
const allRemainingExports = exports.map((exp) => {
if (someExternalExport) {
return [exp, ""];
}
if (!info.imports.includes(exp)) {
const m = exp.match(typeExportRegexp);
if (m) {
const name = exp.replace(m[0], "").trim();
if (!info.imports.includes(name)) {
return [exp, name];
}
}
}
someExternalExport = true;
return [exp, ""];
});
exportStatement = someExternalExport ? `;
export { ${allRemainingExports.map(([e, _]) => e).join(", ")} }` : `;
export type { ${allRemainingExports.map(([_, t]) => t).join(", ")} }`;
}
return code.replace(
defaultExport.code,
`${exportStatement.length > 0 ? "// @ts-ignore\n" : ""}export = ${defaultAlias}${exportStatement}`
);
}
export { internalFixDefaultCJSExports as i };

View File

@@ -0,0 +1,199 @@
import MagicString from 'magic-string';
import { findStaticImports, parseStaticImport, findExports } from 'mlly';
function internalFixDefaultCJSExports(code, info, options) {
const parsedExports = extractExports(code, info, options);
if (!parsedExports) {
return;
}
if (parsedExports.defaultExport.specifier) {
const imports = [];
for (const imp of findStaticImports(code)) {
if (!imp.imports) {
continue;
}
imports.push(parseStaticImport(imp));
}
const specifier = parsedExports.defaultExport.specifier;
const defaultImport = imports.find((i) => i.specifier === specifier);
return parsedExports.defaultExport._type === "named" ? handleDefaultNamedCJSExport(
code,
info,
parsedExports,
imports,
options,
defaultImport
) : handleDefaultCJSExportAsDefault(
code,
parsedExports,
imports,
defaultImport
) || handleNoSpecifierDefaultCJSExport(code, info, parsedExports);
}
return handleNoSpecifierDefaultCJSExport(code, info, parsedExports);
}
function extractExports(code, info, options) {
const defaultExport = findExports(code).find(
(e) => e.names.includes("default")
);
if (!defaultExport) {
options.warn?.(
/* c8 ignore next */
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
const match = defaultExport.code.match(/export\s*\{([^}]*)\}/);
if (!match?.length) {
options.warn?.(
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
let defaultAlias;
const exportsEntries = [];
for (const exp of match[1].split(",").map((e) => e.trim())) {
if (exp === "default") {
defaultAlias = exp;
continue;
}
const m = exp.match(/\s*as\s+default\s*/);
if (m) {
defaultAlias = exp.replace(m[0], "");
} else {
exportsEntries.push(exp);
}
}
if (!defaultAlias) {
options.warn?.(
`No default export found in ${info.fileName}, it contains default export but cannot be parsed.`
);
return;
}
return {
defaultExport,
defaultAlias,
exports: exportsEntries
};
}
function handleDefaultCJSExportAsDefault(code, { defaultExport, exports }, imports, defaultImport) {
if (defaultImport) {
return exports.length === 0 ? code.replace(
defaultExport.code,
`export = ${defaultImport.defaultImport}`
) : code.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultImport.defaultImport};
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
);
} else {
const magicString = new MagicString(code);
const lastImportPosition = imports.length > 0 ? imports.at(-1)?.end || 0 : 0;
if (lastImportPosition > 0) {
magicString.appendRight(
lastImportPosition,
`
import _default from '${defaultExport.specifier}';
`
);
} else {
magicString.prepend(
`import _default from '${defaultExport.specifier}';
`
);
}
return exports.length > 0 ? magicString.replace(
defaultExport.code,
`// @ts-ignore
export = _default;
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
).toString() : magicString.replace(defaultExport.code, "export = _default").toString();
}
}
function handleDefaultNamedCJSExport(code, info, parsedExports, imports, options, defaultImport) {
const { defaultAlias, defaultExport, exports } = parsedExports;
if (defaultAlias === "default") {
if (defaultImport && !defaultImport.defaultImport) {
options.warn?.(
`Cannot parse default export name from ${defaultImport.specifier} import at ${info.fileName}!.`
);
return;
}
return handleDefaultCJSExportAsDefault(
code,
parsedExports,
imports,
defaultImport
);
}
if (defaultImport) {
const namedExports = defaultImport.namedImports;
if (namedExports?.[defaultAlias] === defaultAlias) {
return exports.length === 0 ? code.replace(defaultExport.code, `export = ${defaultAlias}`) : code.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultAlias};
export { ${exports.join(", ")} }`
);
} else {
options.warn?.(
`Cannot parse "${defaultAlias}" named export from ${defaultImport.specifier} import at ${info.fileName}!.`
);
return void 0;
}
}
const magicString = new MagicString(code);
const lastImportPosition = imports.length > 0 ? imports.at(-1)?.end || 0 : 0;
if (lastImportPosition > 0) {
magicString.appendRight(
lastImportPosition,
`
import { ${defaultAlias} } from '${defaultExport.specifier}';
`
);
} else {
magicString.prepend(
`import { ${defaultAlias} } from '${defaultExport.specifier}';
`
);
}
return exports.length > 0 ? magicString.replace(
defaultExport.code,
`// @ts-ignore
export = ${defaultAlias};
export { ${exports.join(", ")} } from '${defaultExport.specifier}'`
).toString() : magicString.replace(defaultExport.code, `export = ${defaultAlias}`).toString();
}
function handleNoSpecifierDefaultCJSExport(code, info, { defaultAlias, defaultExport, exports }) {
let exportStatement = exports.length > 0 ? void 0 : "";
if (exportStatement === void 0) {
let someExternalExport = false;
const typeExportRegexp = /\s*type\s+/;
const allRemainingExports = exports.map((exp) => {
if (someExternalExport) {
return [exp, ""];
}
if (!info.imports.includes(exp)) {
const m = exp.match(typeExportRegexp);
if (m) {
const name = exp.replace(m[0], "").trim();
if (!info.imports.includes(name)) {
return [exp, name];
}
}
}
someExternalExport = true;
return [exp, ""];
});
exportStatement = someExternalExport ? `;
export { ${allRemainingExports.map(([e, _]) => e).join(", ")} }` : `;
export type { ${allRemainingExports.map(([_, t]) => t).join(", ")} }`;
}
return code.replace(
defaultExport.code,
`${exportStatement.length > 0 ? "// @ts-ignore\n" : ""}export = ${defaultAlias}${exportStatement}`
);
}
export { internalFixDefaultCJSExports as i };