tsc-esm-fix
Advanced tools
Comparing version 3.0.2 to 3.1.0
{ | ||
"date": "2024-08-05T13:10:44.808Z", | ||
"date": "2024-10-05T09:30:24.144Z", | ||
"git_commit_branch": "master", | ||
"git_commit_id": "ad1869cb28fa25483da05ff5e1ed08d598351ef3", | ||
"git_commit_id": "b0fd156a5037b283c547d70dc26d43a719eb77af", | ||
"git_repo_url": "https://github.com/antongolub/tsc-esm-fix", | ||
"git_repo_name": "antongolub/tsc-esm-fix", | ||
"ci_run_id": "10249474840", | ||
"ci_run_url": "https://github.com/antongolub/tsc-esm-fix/actions/runs/10249474840" | ||
"ci_run_id": "11192161430", | ||
"ci_run_url": "https://github.com/antongolub/tsc-esm-fix/actions/runs/11192161430" | ||
} |
@@ -0,1 +1,9 @@ | ||
## [3.1.0](https://github.com/antongolub/tsc-esm-fix/compare/v3.0.2...v3.1.0) (2024-10-05) | ||
### Fixes & improvements | ||
* docs: mention `jsExt` and `tsExt` options ([b0fd156](https://github.com/antongolub/tsc-esm-fix/commit/b0fd156a5037b283c547d70dc26d43a719eb77af)) | ||
### Features | ||
* feat: allow configure `js` and `ts` extensions lists ([938320f](https://github.com/antongolub/tsc-esm-fix/commit/938320f893806f9469940922acaeb0341fccb972)) | ||
## [3.0.2](https://github.com/antongolub/tsc-esm-fix/compare/v3.0.1...v3.0.2) (2024-08-05) | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "tsc-esm-fix", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"bin": { | ||
@@ -56,9 +56,9 @@ "tsc-esm-fix": "./target/esm/cli.mjs" | ||
"@types/fs-extra": "^11.0.4", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^22.1.0", | ||
"@types/jest": "^29.5.13", | ||
"@types/node": "^22.7.4", | ||
"@types/semver": "^7.5.8", | ||
"cpy-cli": "^5.0.0", | ||
"cross-env": "^7.0.3", | ||
"esbuild": "^0.23.0", | ||
"esbuild-node-externals": "^1.14.0", | ||
"esbuild": "^0.24.0", | ||
"esbuild-node-externals": "^1.15.0", | ||
"esbuild-plugin-entry-chunks": "^0.1.15", | ||
@@ -78,6 +78,6 @@ "esbuild-plugin-extract-helpers": "^0.0.6", | ||
"tempy": "^3.1.0", | ||
"ts-jest": "^29.2.4", | ||
"typedoc": "^0.26.5", | ||
"typescript": "^5.5.4", | ||
"zx": "^8.1.4" | ||
"ts-jest": "^29.2.5", | ||
"typedoc": "^0.26.8", | ||
"typescript": "^5.6.2", | ||
"zx": "^8.1.8" | ||
}, | ||
@@ -84,0 +84,0 @@ "repository": { |
@@ -182,2 +182,4 @@ # tsc-esm-fix | ||
| `--ext` | Append extension to relative imports/re-exports | `.js` | | ||
| `--ts-ext` | Known TS extensions | `.ts,.tsx,.mts,.mtsx,.cts,.ctsx` | | ||
| `--js-ext` | Known JS extensions | `.js,.jsx,.mjs,.mjsx,.cjs,.cjsx` | | ||
| `--unlink` | Remove original files if ext changes | true | | ||
@@ -231,2 +233,4 @@ | `--fillBlank` | Fill blank files with `export {}` | false | | ||
ext: boolean | string | ||
tsExt: string | string[] | ||
jsExt: string | string[] | ||
unlink?: boolean, | ||
@@ -233,0 +237,0 @@ debug?: boolean | IFunction |
export declare const getTsconfigTargets: (tsconfig: string | string[], cwd: string) => string[]; | ||
export declare const getLocalModules: (sources: string[], targets: string[], cwd: string) => Promise<string[]>; | ||
export declare const getExternalModules: (cwd: string) => Promise<{ | ||
export declare const getLocalModules: (sources: string[], targets: string[], cwd: string, tsExt?: string[]) => Promise<string[]>; | ||
export declare const getExternalModules: (cwd: string, jsExt?: string[]) => Promise<{ | ||
exportedModules: string[]; | ||
@@ -5,0 +5,0 @@ anyModules: string[]; |
import { TFixer } from '../interface'; | ||
export declare const fixModuleReferences: TFixer; | ||
export declare const resolveDependency: (parent: string, nested: string, files: string[], cwd: string) => string; | ||
export declare const resolveDependency: (parent: string, nested: string, files: string[], cwd: string, jsExt?: string[]) => string; |
@@ -6,4 +6,4 @@ type IFunction<A extends any[] = any[], R = any> = (...args: A) => R; | ||
out?: string; | ||
src?: string | string[]; | ||
target?: string | string[]; | ||
src: string[]; | ||
target: string[]; | ||
tsconfig: string | string[]; | ||
@@ -16,2 +16,4 @@ dirnameVar: boolean; | ||
ext: boolean | string; | ||
tsExt: string[]; | ||
jsExt: string[]; | ||
unlink?: boolean; | ||
@@ -25,2 +27,6 @@ }; | ||
debug?: boolean | IFunction; | ||
src?: string | string[]; | ||
target?: string | string[]; | ||
tsExt?: string | string[]; | ||
jsExt?: string | string[]; | ||
}; | ||
@@ -35,5 +41,5 @@ export type TFixContext = { | ||
localModules: string[]; | ||
options: IFixOptionsNormalized; | ||
}; | ||
export type TResourceContext = { | ||
options: IFixOptionsNormalized; | ||
contents: string; | ||
@@ -46,4 +52,5 @@ filename: string; | ||
ignore: string[]; | ||
options: IFixOptionsNormalized; | ||
}; | ||
export type TFixer = (ctx: TResourceContext) => TResourceContext; | ||
export {}; |
@@ -1,3 +0,5 @@ | ||
import { IFixOptions, IFixOptionsNormalized } from "./interface"; | ||
import { IFixOptions, IFixOptionsNormalized } from './interface'; | ||
export declare const JS_EXT: string[]; | ||
export declare const TS_EXT: string[]; | ||
export declare const DEFAULT_FIX_OPTIONS: IFixOptionsNormalized; | ||
export declare const normalizeOptions: (opts?: IFixOptions) => IFixOptionsNormalized; |
@@ -13,1 +13,2 @@ import fse from 'fs-extra'; | ||
export declare const omitUndefinedKeys: <T extends Record<string, any>>(obj: T) => T; | ||
export declare const extToGlob: (ext: string[]) => string; |
@@ -19,2 +19,4 @@ #!/usr/bin/env node | ||
--ext Append extension (like '.mjs') to relative imports/re-exports | ||
--ts-ext Specify TS extensions. Defaults to '.ts,.tsx,.mts,.mtsx,.cts,.ctsx' | ||
--js-ext Specify JS extensions. Defaults to '.js,.jsx,.mjs,.mjsx,.cjs,.cjsx' | ||
--unlink Remove original files if ext changes | ||
@@ -36,2 +38,8 @@ --dirnameVar Replace __dirname refs with import.meta | ||
}, | ||
tsExt: { | ||
type: String, | ||
}, | ||
jsExt: { | ||
type: String, | ||
}, | ||
unlink: { | ||
@@ -38,0 +46,0 @@ type: Boolean, |
import { dirname, join, resolve } from 'node:path' | ||
import {asArray, glob, readJson, resolveTsConfig} from './util' | ||
import {asArray, extToGlob, glob, readJson, resolveTsConfig} from './util' | ||
import {DEFAULT_FIX_OPTIONS} from "./options"; | ||
@@ -33,4 +34,4 @@ type TPackageExports = [string, string[]][] | ||
export const getLocalModules = (sources: string[], targets: string[], cwd: string) => glob( | ||
getPatterns(sources, targets), | ||
export const getLocalModules = (sources: string[], targets: string[], cwd: string, tsExt: string[] = DEFAULT_FIX_OPTIONS.tsExt) => glob( | ||
getPatterns(sources, targets, tsExt), | ||
{ | ||
@@ -42,7 +43,7 @@ cwd, | ||
export const getExternalModules = async (cwd: string): Promise<{exportedModules: string[], anyModules: string[], allPackageNames: string[] }> => { | ||
export const getExternalModules = async (cwd: string, jsExt: string[] = DEFAULT_FIX_OPTIONS.jsExt): Promise<{exportedModules: string[], anyModules: string[], allPackageNames: string[] }> => { | ||
const allPackages = await getExternalPackages(cwd) | ||
const allPackageNames = allPackages.map(p => p.name) | ||
const exportedModules = (await Promise.all(allPackages.map(getPackageEntryPoints))).flat() | ||
const anyModules = await getAllModules(cwd) | ||
const anyModules = await getAllModules(cwd, jsExt) | ||
@@ -56,3 +57,3 @@ return { | ||
const getAllModules = async (cwd: string): Promise<string[]> => glob( | ||
const getAllModules = async (cwd: string, jsExt: string[] = DEFAULT_FIX_OPTIONS.jsExt): Promise<string[]> => glob( | ||
[ | ||
@@ -62,3 +63,3 @@ '!node_modules/.cache', | ||
'!node_modules/**/node_modules', | ||
'node_modules/**/*.{js,mjs,cjs}', | ||
`node_modules/${extToGlob(jsExt)}`, | ||
], | ||
@@ -72,5 +73,5 @@ { | ||
const getPatterns = (sources: string[], targets: string[]): string[] => | ||
const getPatterns = (sources: string[], targets: string[], tsExt: string[]): string[] => | ||
sources.length > 0 | ||
? sources.map((src) => src.includes('*') ? src : `${src}/**/*.{ts,tsx}`) | ||
? sources.map((src) => src.includes('*') ? src : `${src}/${extToGlob(tsExt)}`) | ||
: targets.map((target) => target.includes('*') ? target : `${target}/**/*.{js,d.ts}`) | ||
@@ -77,0 +78,0 @@ |
@@ -1,12 +0,13 @@ | ||
import {dirname, resolve} from 'node:path' | ||
import {TFixer} from '../interface' | ||
import {unixify} from '../util' | ||
import { dirname, resolve } from 'node:path' | ||
import { patchRefs } from 'depseek' | ||
import { TFixer } from '../interface' | ||
import { unixify } from '../util' | ||
import { DEFAULT_FIX_OPTIONS } from '../options' | ||
export const fixModuleReferences: TFixer = (ctx) => { | ||
const { contents, filename, filenames, options: {cwd}, ignore } = ctx | ||
const { contents, filename, filenames, options: {cwd, jsExt}, ignore } = ctx | ||
const _contents = patchRefs(contents, (value) => { | ||
const v = value.endsWith('/') ? value.slice(0, -1) : value | ||
return (v.includes('/') || v === '.' || v === '..') && !ignore.includes(v) | ||
? resolveDependency(filename, v, filenames, cwd) | ||
? resolveDependency(filename, v, filenames, cwd, jsExt) | ||
: value | ||
@@ -23,2 +24,3 @@ }) | ||
cwd: string, | ||
jsExt: string[] = DEFAULT_FIX_OPTIONS.jsExt, | ||
): string => { | ||
@@ -30,3 +32,3 @@ const dir = dirname(parent) | ||
: [nested] | ||
const variants = ['.js', '.cjs', '.mjs'].reduce<string[]>((m, e) => { | ||
const variants = jsExt.reduce<string[]>((m, e) => { | ||
bases.forEach((v) => m.push(`${v}${e}`, `${v}/index${e}`)) | ||
@@ -33,0 +35,0 @@ return m |
@@ -11,3 +11,2 @@ import path from 'node:path' | ||
import { | ||
asArray, | ||
existsSync, | ||
@@ -44,7 +43,5 @@ read, | ||
const resolve = async (opts: IFixOptionsNormalized): Promise<TFixContext> => { | ||
const {cwd, target, src, tsconfig, out = cwd, ext, debug, unlink, sourceMap} = opts | ||
const resolve = async (options: IFixOptionsNormalized): Promise<TFixContext> => { | ||
const {cwd, target: _targets, src: sources, tsconfig, out = cwd, ext, debug, tsExt, jsExt} = options | ||
const outDir = path.resolve(cwd, out) | ||
const sources = asArray<string>(src) | ||
const _targets = asArray<string>(target) | ||
const targets = _targets.length > 0 ? _targets : getTsconfigTargets(tsconfig, cwd) | ||
@@ -57,3 +54,3 @@ debug('debug:cwd', cwd) | ||
const isSource = sources.length > 0 | ||
const localModules = await getLocalModules(sources, targets, cwd) | ||
const localModules = await getLocalModules(sources, targets, cwd, tsExt) | ||
const { | ||
@@ -63,3 +60,3 @@ exportedModules, | ||
allPackageNames, | ||
} = await getExternalModules(cwd) | ||
} = await getExternalModules(cwd, jsExt) | ||
@@ -83,3 +80,4 @@ debug('debug:external-package-names', allPackageNames) | ||
_localModules, | ||
localModules | ||
localModules, | ||
options | ||
} | ||
@@ -89,3 +87,3 @@ } | ||
const patch = async (ctx: TFixContext, options: IFixOptionsNormalized) => { | ||
const {cwd, unlink, sourceMap} = options | ||
const {cwd, unlink, sourceMap, tsExt, jsExt} = options | ||
const { | ||
@@ -92,0 +90,0 @@ outDir, |
import {TFixer} from '../interface' | ||
export {fixBlankFiles} from './fix-blank-files' | ||
export {fixFilenameVar, fixDirnameVar} from './fix-dirname-var' | ||
export {fixModuleReferences} from './fix-module-ref' | ||
export {fixSourceMapRef} from './fix-sourcemap-ref' | ||
export {fixDefaultExport} from './fix-default-export' | ||
import { fixBlankFiles } from './fix-blank-files' | ||
@@ -15,2 +9,8 @@ import { fixDefaultExport } from './fix-default-export' | ||
export { fixBlankFiles } from './fix-blank-files' | ||
export { fixFilenameVar, fixDirnameVar } from './fix-dirname-var' | ||
export { fixModuleReferences } from './fix-module-ref' | ||
export { fixSourceMapRef } from './fix-sourcemap-ref' | ||
export { fixDefaultExport } from './fix-default-export' | ||
export const fixContents: TFixer = ( | ||
@@ -17,0 +17,0 @@ ctx |
@@ -7,4 +7,4 @@ type IFunction<A extends any[] = any[], R = any> = (...args: A) => R | ||
out?: string | ||
src?: string | string[] | ||
target?: string | string[] | ||
src : string[] | ||
target: string[] | ||
tsconfig: string | string[] | ||
@@ -17,2 +17,4 @@ dirnameVar: boolean | ||
ext: boolean | string | ||
tsExt: string[] | ||
jsExt: string[] | ||
unlink?: boolean | ||
@@ -26,3 +28,9 @@ } | ||
export type IFixOptions = Partial<Omit<IFixOptionsNormalized, 'debug'>> & {debug?: boolean | IFunction} | ||
export type IFixOptions = Partial<Omit<IFixOptionsNormalized, 'debug'>>& { | ||
debug?: boolean | IFunction | ||
src?: string | string[] | ||
target?:string | string[] | ||
tsExt?: string | string[] | ||
jsExt?: string | string[] | ||
} | ||
@@ -37,6 +45,6 @@ export type TFixContext = { | ||
localModules: string[] | ||
options: IFixOptionsNormalized | ||
} | ||
export type TResourceContext = { | ||
options: IFixOptionsNormalized | ||
contents: string | ||
@@ -49,4 +57,5 @@ filename: string | ||
ignore: string[] | ||
options: IFixOptionsNormalized | ||
} | ||
export type TFixer = (ctx: TResourceContext) => TResourceContext |
@@ -11,2 +11,3 @@ import { IFixOptionsNormalized, TResourceContext } from './interface' | ||
} from './fixes' | ||
import {DEFAULT_FIX_OPTIONS} from './options' | ||
@@ -20,3 +21,13 @@ export const fixModuleReferences = ( | ||
): string => | ||
_fixModuleReferences({contents, filename, filenames, options: {cwd}, ignore} as TResourceContext).contents | ||
_fixModuleReferences({ | ||
contents, | ||
filename, | ||
filenames, | ||
options: { | ||
cwd, | ||
tsExt: DEFAULT_FIX_OPTIONS.tsExt, | ||
jsExt: DEFAULT_FIX_OPTIONS.jsExt, | ||
}, | ||
ignore | ||
} as TResourceContext).contents | ||
@@ -23,0 +34,0 @@ export const fixDirnameVar = (contents: string, isSource?: boolean): string => |
@@ -1,10 +0,17 @@ | ||
import {IFixOptions, IFixOptionsNormalized} from "./interface"; | ||
import {omitUndefinedKeys} from './util' | ||
import { IFixOptions, IFixOptionsNormalized } from './interface' | ||
import { asArray, omitUndefinedKeys } from './util' | ||
export const JS_EXT = ['.js', '.jsx', '.mjs', '.mjsx', '.cjs', '.cjsx'] | ||
export const TS_EXT = ['.ts', '.tsx', '.mts', '.mtsx', '.cts', '.ctsx'] | ||
export const DEFAULT_FIX_OPTIONS: IFixOptionsNormalized = { | ||
cwd: process.cwd(), | ||
tsconfig: './tsconfig.json', | ||
tsconfig: ['./tsconfig.json'], | ||
src: [], | ||
target: [], | ||
filenameVar: true, | ||
dirnameVar: true, | ||
ext: true, | ||
tsExt: TS_EXT, | ||
jsExt: JS_EXT, | ||
unlink: true, | ||
@@ -15,11 +22,16 @@ debug: () => {}, // eslint-disable-line | ||
export const normalizeOptions = ( | ||
opts?: IFixOptions, | ||
opts: IFixOptions = {}, | ||
): IFixOptionsNormalized => ({ | ||
...DEFAULT_FIX_OPTIONS, | ||
...omitUndefinedKeys(opts || {}), | ||
debug: typeof opts?.debug === 'function' | ||
...omitUndefinedKeys(opts), | ||
tsconfig: opts.tsconfig ? asArray(opts.tsconfig) : DEFAULT_FIX_OPTIONS.tsconfig, | ||
src: asArray(opts.src), | ||
target: asArray(opts.target), | ||
jsExt: typeof opts.jsExt === 'string' ? opts.jsExt.split(',') : opts.jsExt || DEFAULT_FIX_OPTIONS.jsExt, | ||
tsExt: typeof opts.tsExt === 'string' ? opts.tsExt.split(',') : opts.tsExt || DEFAULT_FIX_OPTIONS.tsExt, | ||
debug: typeof opts.debug === 'function' | ||
? opts.debug | ||
: opts?.debug === true | ||
: opts.debug === true | ||
? console.log | ||
: DEFAULT_FIX_OPTIONS.debug, | ||
}) |
@@ -45,1 +45,3 @@ import { dirname, resolve } from 'node:path' | ||
Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== undefined)) as T | ||
export const extToGlob = (ext: string[]): string => `**/*.{${ext.map(e => e.slice(1)).join(',')}}` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113783
1977
263