tsc-esm-fix
Advanced tools
Comparing version 2.20.18 to 2.20.19
{ | ||
"date": "2023-12-02T08:39:57.914Z", | ||
"date": "2023-12-30T06:59:51.692Z", | ||
"git_commit_branch": "master", | ||
"git_commit_id": "43b77f3caaeab922a7cca7764484c446f51dae95", | ||
"git_commit_id": "b7d1265b1621bfdc272217ca492217b06e8906f0", | ||
"git_repo_url": "https://github.com/antongolub/tsc-esm-fix", | ||
"git_repo_name": "antongolub/tsc-esm-fix", | ||
"ci_run_id": "7069163947", | ||
"ci_run_url": "https://github.com/antongolub/tsc-esm-fix/actions/runs/7069163947" | ||
"ci_run_id": "7362675203", | ||
"ci_run_url": "https://github.com/antongolub/tsc-esm-fix/actions/runs/7362675203" | ||
} |
@@ -0,1 +1,7 @@ | ||
## [2.20.19](https://github.com/antongolub/tsc-esm-fix/compare/v2.20.18...v2.20.19) (2023-12-30) | ||
### Fixes & improvements | ||
* refactor: separate `fix` into `resolve` and `patch` stages ([925346a](https://github.com/antongolub/tsc-esm-fix/commit/925346ac13231ee4486fa28f09ac5c58c8dec391)) | ||
* refactor: decompose fix.ts ([9bebde8](https://github.com/antongolub/tsc-esm-fix/commit/9bebde8e165ccfc93cbea0d694bc1d93071bcdc4)) | ||
## [2.20.18](https://github.com/antongolub/tsc-esm-fix/compare/v2.20.17...v2.20.18) (2023-12-02) | ||
@@ -2,0 +8,0 @@ |
{ | ||
"name": "tsc-esm-fix", | ||
"version": "2.20.18", | ||
"version": "2.20.19", | ||
"private": false, | ||
@@ -49,3 +49,3 @@ "publishConfig": { | ||
"dependencies": { | ||
"fs-extra": "^11.1.1", | ||
"fs-extra": "^11.2.0", | ||
"globby": "^13.2.2", | ||
@@ -57,11 +57,11 @@ "json5": "^2.2.3", | ||
"devDependencies": { | ||
"@types/fs-extra": "^11.0.2", | ||
"@qiwi/npm-run-all": "^4.1.7", | ||
"@types/jest": "^29.5.5", | ||
"@types/node": "^20.6.2", | ||
"@types/semver": "^7.5.2", | ||
"@types/fs-extra": "^11.0.4", | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^20.10.6", | ||
"@types/semver": "^7.5.6", | ||
"cpy-cli": "^5.0.0", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.49.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint": "^8.56.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-config-qiwi": "^2.1.3", | ||
@@ -72,10 +72,10 @@ "glob-runner": "^2.1.1", | ||
"mkdirp": "^3.0.1", | ||
"prettier": "^3.0.3", | ||
"prettier": "^3.1.1", | ||
"prettier-config-qiwi": "^2.1.2", | ||
"rimraf": "^5.0.1", | ||
"rimraf": "^5.0.5", | ||
"semver": "^7.5.4", | ||
"tempy": "^3.1.0", | ||
"ts-jest": "^29.1.1", | ||
"typedoc": "^0.25.1", | ||
"typescript": "^5.2.2" | ||
"typedoc": "^0.25.4", | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -82,0 +82,0 @@ "repository": { |
@@ -1,15 +0,3 @@ | ||
import { IFixOptions, IFixOptionsNormalized } from './interface'; | ||
export declare const DEFAULT_FIX_OPTIONS: IFixOptionsNormalized; | ||
export declare const normalizeOptions: (opts?: IFixOptions) => IFixOptionsNormalized; | ||
export declare const findTargets: (tsconfig: string | string[], cwd: string) => string[]; | ||
export declare const resolveDependency: (parent: string, nested: string, files: string[], cwd: string) => string; | ||
import { IFixOptions } from './interface'; | ||
export declare const fixFilenameExtensions: (names: string[], ext: string) => string[]; | ||
export declare const fixModuleReferences: (contents: string, filename: string, filenames: string[], cwd: string, ignore: string[]) => string; | ||
export declare const fixDirnameVar: (contents: string, isSource?: boolean) => string; | ||
export declare const fixFilenameVar: (contents: string, isSource?: boolean) => string; | ||
export declare const fixDefaultExport: (contents: string) => string; | ||
export declare const fixBlankFiles: (contents: string) => string; | ||
export declare const fixSourceMapRef: (contents: string, originName: string, filename: string) => string; | ||
export declare const fixContents: (contents: string, filename: string, filenames: string[], { cwd, ext, dirnameVar, filenameVar, fillBlank, forceDefaultExport, sourceMap }: IFixOptionsNormalized, originName?: string, isSource?: boolean, ignore?: string[]) => string; | ||
export declare const getPatterns: (sources: string[], targets: string[]) => string[]; | ||
export declare const fix: (opts?: IFixOptions) => Promise<void>; |
@@ -1,2 +0,4 @@ | ||
export { fix, fixBlankFiles, fixContents, fixFilenameVar, fixDirnameVar, fixModuleReferences, fixFilenameExtensions, fixDefaultExport, fixSourceMapRef, DEFAULT_FIX_OPTIONS, } from './fix'; | ||
export { fix, fixFilenameExtensions, } from './fix'; | ||
export { DEFAULT_FIX_OPTIONS, } from './options'; | ||
export { fixBlankFiles, fixContents, fixFilenameVar, fixDirnameVar, fixModuleReferences, fixSourceMapRef, fixDefaultExport, } from './legacy'; | ||
export * from './interface'; |
@@ -12,4 +12,4 @@ type IFunction<A extends any[] = any[], R = any> = (...args: A) => R; | ||
fillBlank?: boolean; | ||
forceDefaultExport?: boolean; | ||
sourceMap?: boolean; | ||
forceDefaultExport?: boolean; | ||
ext: boolean | string; | ||
@@ -25,2 +25,22 @@ unlink?: boolean; | ||
}; | ||
export type TFixContext = { | ||
outDir: string; | ||
isSource: boolean; | ||
ignore: string[]; | ||
allJsModules: string[]; | ||
allModules: string[]; | ||
_localModules: string[]; | ||
localModules: string[]; | ||
}; | ||
export type TResourceContext = { | ||
options: IFixOptionsNormalized; | ||
contents: string; | ||
filename: string; | ||
filenames: string[]; | ||
originName: string; | ||
nextName: string; | ||
isSource: boolean; | ||
ignore: string[]; | ||
}; | ||
export type TFixer = (ctx: TResourceContext) => TResourceContext; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("node:path"),require("fs-extra"),require("json5"),require("globby"),require("node:fs")):"function"==typeof define&&define.amd?define(["exports","node:path","fs-extra","json5","globby","node:fs"],n):n((e||self).tscEsmFix={},e.node_path,e.fsExtra,e.json5,e.globby,e.node_fs)}(this,function(e,n,r,t,o,u){function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=/*#__PURE__*/i(r),c=/*#__PURE__*/i(t);function l(){return l=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e},l.apply(this,arguments)}var a=function(e){return s.default.readFileSync(e,{encoding:"utf8"})},f=function(e,n){return s.default.outputFileSync(e,n,{encoding:"utf8"})},d=function(e){return c.default.parse(a(e))},m=function(e){return e?Array.isArray(e)?e:[e]:[]},p=s.default.unlinkSync,g=function(e){return e.replace(/\\/g,"/")},v=function e(r){var t=d(r);if(t.extends){var o=e(n.resolve(n.dirname(r),t.extends));return l({},o,t,{compilerOptions:l({},o.compilerOptions,t.compilerOptions)})}return t},b={cwd:process.cwd(),tsconfig:"./tsconfig.json",filenameVar:!0,dirnameVar:!0,ext:!0,unlink:!0,debug:function(){}},h=function(e,r){return m(e).reduce(function(e,t){var o,u,i=v(n.resolve(r,t)),s=null==i||null==(o=i.compilerOptions)?void 0:o.outDir,c=null==i||null==(u=i.compilerOptions)||null==u.module.toLowerCase?void 0:u.module.toLowerCase();return s&&c.startsWith("es")?e.push(s):console.warn("tsconfig should declare `outDir` and `module` type es6 or above"),e},[])},x=function(e,n){return e.map(function(e){return e.endsWith(".d.ts")?e:e.replace(/\.[^./\\]+$/,n)})},y=function(e,r,t,o,u){return e.replace(/((?:\s|^)import\s+|\s+from\s+|\W(?:import|require)\s*\()(["'])([^"']+\/[^"']+|\.{1,2})\/?(["'])/g,function(e,i,s,c,l){return""+i+s+(u.includes(c)?c:function(e,r,t,o){var u=n.dirname(e),i=n.resolve(o,"node_modules"),s=/^\..+\.[^./\\]+$/.test(r)?[r,r.replace(/\.[^./\\]+$/,"")]:[r],c=[".js",".cjs",".mjs"].reduce(function(e,n){return s.forEach(function(r){return e.push(""+r+n,r+"/index"+n)}),e},[]);return c.find(function(e){return t.includes(g(n.resolve(u,e)))})||c.find(function(e){return t.includes(g(n.resolve(i,e)))})||r}(r,c,t,o))+l})},j=function(e,n){return e.replace(/__dirname/g,"`${process.platform === 'win32' ? '' : '/'}${/file:\\/{2,3}(.+)\\/[^/]/.exec(import.meta.url)"+(n?"!":"")+"[1]}`")},P=function(e,n){return e.replace(/__filename/g,"`${process.platform === 'win32' ? '' : '/'}${/file:\\/{2,3}(.+)/.exec(import.meta.url)"+(n?"!":"")+"[1]}`")},O=function(e){return e.includes("export default")?e:e+"\nexport default undefined\n"},_=function(e){return 0===e.trim().length?"\nexport {}\nexport default undefined\n":e},w=function(e,r,t){return r===t?e:e.replace("//# sourceMappingURL="+n.basename(r)+".map","//# sourceMappingURL="+n.basename(t)+".map")},F=function(e,n,r,t,o,u,i){var s=t.dirnameVar,c=t.filenameVar,l=t.fillBlank,a=t.forceDefaultExport,f=t.sourceMap;void 0===o&&(o=n),void 0===u&&(u=!1),void 0===i&&(i=[]);var d=e;return t.ext&&(d=y(d,n,r,t.cwd,i)),s&&(d=j(d,u)),c&&(d=P(d,u)),l&&(d=_(d)),a&&(d=O(d)),f&&(d=w(d,o,n)),d},k=function(e,r){if(!r)return e;var t=r;return t.includes("*")&&(t=t.slice(0,t.indexOf("*"))).includes("/")&&(t=t.slice(0,t.lastIndexOf("/"))),n.join(e,t)};e.DEFAULT_FIX_OPTIONS=b,e.fix=function(e){try{var r=function(e){return l({},b,e,{debug:"function"==typeof(null==e?void 0:e.debug)?e.debug:!0===(null==e?void 0:e.debug)?console.log:b.debug})}(e),t=r.cwd,i=r.target,s=r.src,c=r.tsconfig,v=r.out,y=r.ext,j=r.debug,P=r.unlink,O=r.sourceMap,_=n.resolve(t,void 0===v?t:v),w=m(s),M=[].concat(m(i),h(c,t));j("debug:cwd",t),j("debug:outdir",_),j("debug:sources",w),j("debug:targets",M);var E=w.length>0,S=function(e,n){return e.length>0?e.map(function(e){return e.includes("*")?e:e+"/**/*.{ts,tsx}"}):n.map(function(e){return e.includes("*")?e:e+"/**/*.{js,d.ts}"})}(w,M);return Promise.resolve(o.globby(S,{cwd:t,onlyFiles:!0,absolute:!0})).then(function(e){return Promise.resolve(function(e){try{return Promise.resolve(function(e){return o.globby(["node_modules/*/package.json","node_modules/@*/*/package.json"],{cwd:e,onlyFiles:!0,absolute:!0}).then(function(e){try{return Promise.resolve(Promise.all(e.map(function(e){try{return Promise.resolve(d(e)).then(function(r){var t=r.name,u=r.exports;if(!u)return{name:t};var i=n.dirname(e),s=function(e){var n=Object.entries(e),r=function e(n){return"string"==typeof n?[n]:Object.values(n).map(e).flat(2)};return"string"!=typeof e&&Object.keys(e).some(function(e){return e.startsWith(".")})?n.map(function(e){return[e[0],r(e[1])]}):[[".",r(e)]]}(u);return Promise.resolve(Promise.all(s.map(function(e){var r=e[0];return Promise.all(e[1].map(function(e){try{return Promise.resolve(o.globby(e,{cwd:i,onlyFiles:!0,absolute:!1})).then(function(o){return o.map(function(o){return n.join(o).replace(k(".",e),k(t,r))})})}catch(e){return Promise.reject(e)}}))}))).then(function(e){return{name:t,files:e.flat(2)}})})}catch(e){return Promise.reject(e)}}))).then(function(e){return e.reduce(function(e,n){var r,t=n.name,o=n.files;return t&&e.names.push(t),o&&(r=e.files).push.apply(r,o),e},{names:[],files:[]})})}catch(e){return Promise.reject(e)}})}(e)).then(function(n){var r=n.names,t=n.files;return Promise.resolve(o.globby(["!node_modules/.cache","!node_modules/.bin","!node_modules/**/node_modules"].concat(r.map(function(e){return"!node_modules/"+e}),["node_modules/**/*.(m|c)?js"]),{cwd:e,onlyFiles:!0,absolute:!0})).then(function(e){return{cjsModules:e,esmModules:t,allPackages:r}})})}catch(e){return Promise.reject(e)}}(t)).then(function(o){var i=o.cjsModules,s=o.esmModules,c=o.allPackages;j("debug:external-cjs-modules",i),j("debug:external-esm-modules",s);var l=[].concat(s,c),m="string"==typeof y?x(e,y):e,v=[].concat(i,m),b=[].concat(i,x(e,".js"));j("debug:local-modules",m),m.forEach(function(o,i){var s=o.endsWith(".d.ts")?b:v,c=e[i],m=(0===w.length?o:c).replace(g(t),g(_)),h=a(c),x=F(h,o,s,r,c,E,l);f(m,x),0===w.length&&P&&t===_&&m!==c&&p(c),O&&function(e,r,t){if(void 0===t&&(t=!1),e!==r){var o=e+".map";if(u.existsSync(o)){var i=r+".map",s=d(o);s.file=n.basename(r),f(i,JSON.stringify(s)),t&&p(o)}}}(c,m,P&&t===_)})})})}catch(e){return Promise.reject(e)}},e.fixBlankFiles=_,e.fixContents=F,e.fixDefaultExport=O,e.fixDirnameVar=j,e.fixFilenameExtensions=x,e.fixFilenameVar=P,e.fixModuleReferences=y,e.fixSourceMapRef=w}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("node:path"),require("fs-extra"),require("json5"),require("globby"),require("node:fs")):"function"==typeof define&&define.amd?define(["exports","node:path","fs-extra","json5","globby","node:fs"],n):n((e||self).tscEsmFix={},e.path,e.fsExtra,e.json5,e.globby,e.node_fs)}(this,function(e,n,t,r,o,u){function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=/*#__PURE__*/i(n),c=/*#__PURE__*/i(t),l=/*#__PURE__*/i(r);function a(){return a=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},a.apply(this,arguments)}var f=function(e){var n=e.contents;return a({},e,{contents:0===n.trim().length?"\nexport {}\nexport default undefined\n":n})},d=function(e){var n=e.contents;return a({},e,{contents:n.includes("export default")?n:n+"\nexport default undefined\n"})},m=function(e){return a({},e,{contents:e.contents.replace(/__dirname/g,"`${process.platform === 'win32' ? '' : '/'}${/file:\\/{2,3}(.+)\\/[^/]/.exec(import.meta.url)"+(e.isSource?"!":"")+"[1]}`")})},p=function(e){return a({},e,{contents:e.contents.replace(/__filename/g,"`${process.platform === 'win32' ? '' : '/'}${/file:\\/{2,3}(.+)/.exec(import.meta.url)"+(e.isSource?"!":"")+"[1]}`")})},g=function(e){return c.default.readFileSync(e,{encoding:"utf8"})},v=function(e,n){return c.default.outputFileSync(e,n,{encoding:"utf8"})},b=function(e){return l.default.parse(g(e))},h=function(e){return e?Array.isArray(e)?e:[e]:[]},y=c.default.unlinkSync,x=function(e){return e.replace(/\\/g,"/")},j=function e(t){var r=b(t);if(r.extends){var o=e(n.resolve(n.dirname(t),r.extends));return a({},o,r,{compilerOptions:a({},o.compilerOptions,r.compilerOptions)})}return r},P=function(e){var n=e.filename,t=e.filenames,r=e.options.cwd,o=e.ignore;return a({},e,{contents:e.contents.replace(/((?:\s|^)import\s+|\s+from\s+|\W(?:import|require)\s*\()(["'])([^"']+\/[^"']+|\.{1,2})\/?(["'])/g,function(e,u,i,s,c){return""+u+i+(o.includes(s)?s:M(n,s,t,r))+c})})},M=function(e,t,r,o){var u=n.dirname(e),i=n.resolve(o,"node_modules"),s=/^\..+\.[^./\\]+$/.test(t)?[t,t.replace(/\.[^./\\]+$/,"")]:[t],c=[".js",".cjs",".mjs"].reduce(function(e,n){return s.forEach(function(t){return e.push(""+t+n,t+"/index"+n)}),e},[]);return c.find(function(e){return r.includes(x(n.resolve(u,e)))})||c.find(function(e){return r.includes(x(n.resolve(i,e)))})||t},_=function(e){var t=e.contents,r=e.originName,o=e.filename;return a({},e,{contents:r===o?t:t.replace("//# sourceMappingURL="+n.basename(r)+".map","//# sourceMappingURL="+n.basename(o)+".map")})},w=function(e){var n=e.options,t=e;return n.ext&&(t=P(t)),n.dirnameVar&&(t=m(t)),n.filenameVar&&(t=p(t)),n.fillBlank&&(t=f(t)),n.forceDefaultExport&&(t=d(t)),n.sourceMap&&(t=_(t)),t},O=function(e,t){return h(e).reduce(function(e,r){var o,u,i=j(n.resolve(t,r)),s=null==i||null==(o=i.compilerOptions)?void 0:o.outDir,c=null==i||null==(u=i.compilerOptions)||null==u.module.toLowerCase?void 0:u.module.toLowerCase();return s&&c.startsWith("es")?e.push(s):console.warn("tsconfig should declare `outDir` and `module` type es6 or above"),e},[])},S=function(e,t){if(!t)return e;var r=t;return r.includes("*")&&(r=r.slice(0,r.indexOf("*"))).includes("/")&&(r=r.slice(0,r.lastIndexOf("/"))),n.join(e,r)},F={cwd:process.cwd(),tsconfig:"./tsconfig.json",filenameVar:!0,dirnameVar:!0,ext:!0,unlink:!0,debug:function(){}},k=function(e,n){return e.map(function(e){return e.endsWith(".d.ts")?e:e.replace(/\.[^./\\]+$/,n)})};e.DEFAULT_FIX_OPTIONS=F,e.fix=function(e){try{var t=function(e){return a({},F,e,{debug:"function"==typeof(null==e?void 0:e.debug)?e.debug:!0===(null==e?void 0:e.debug)?console.log:F.debug})}(e);return Promise.resolve(function(e){try{var t=e.cwd,r=e.target,u=e.src,i=e.tsconfig,c=e.out,l=e.ext,a=e.debug,f=s.default.resolve(t,void 0===c?t:c),d=h(u),m=[].concat(h(r),O(i,t));a("debug:cwd",t),a("debug:outdir",f),a("debug:sources",d),a("debug:targets",m);var p=d.length>0;return Promise.resolve(function(e,n,t){return o.globby(function(e,n){return e.length>0?e.map(function(e){return e.includes("*")?e:e+"/**/*.{ts,tsx}"}):n.map(function(e){return e.includes("*")?e:e+"/**/*.{js,d.ts}"})}(e,n),{cwd:t,onlyFiles:!0,absolute:!0})}(d,m,t)).then(function(e){return Promise.resolve(function(e){try{return Promise.resolve(function(e){return o.globby(["node_modules/*/package.json","node_modules/@*/*/package.json"],{cwd:e,onlyFiles:!0,absolute:!0}).then(function(e){try{return Promise.resolve(Promise.all(e.map(function(e){try{return Promise.resolve(b(e)).then(function(t){var r=t.name,u=t.exports;if(!u)return{name:r};var i=n.dirname(e),s=function(e){var n=Object.entries(e),t=function e(n){return"string"==typeof n?[n]:Object.values(n).map(e).flat(2)};return"string"!=typeof e&&Object.keys(e).some(function(e){return e.startsWith(".")})?n.map(function(e){return[e[0],t(e[1])]}):[[".",t(e)]]}(u);return Promise.resolve(Promise.all(s.map(function(e){var t=e[0];return Promise.all(e[1].map(function(e){try{return Promise.resolve(o.globby(e,{cwd:i,onlyFiles:!0,absolute:!1})).then(function(o){return o.map(function(o){return n.join(o).replace(S(".",e),S(r,t))})})}catch(e){return Promise.reject(e)}}))}))).then(function(e){return{name:r,files:e.flat(2)}})})}catch(e){return Promise.reject(e)}}))).then(function(e){return e.reduce(function(e,n){var t,r=n.name,o=n.files;return r&&e.names.push(r),o&&(t=e.files).push.apply(t,o),e},{names:[],files:[]})})}catch(e){return Promise.reject(e)}})}(e)).then(function(n){var t=n.names,r=n.files;return Promise.resolve(o.globby(["!node_modules/.cache","!node_modules/.bin","!node_modules/**/node_modules"].concat(t.map(function(e){return"!node_modules/"+e}),["node_modules/**/*.(m|c)?js"]),{cwd:e,onlyFiles:!0,absolute:!0})).then(function(e){return{cjsModules:e,esmModules:r,allPackages:t}})})}catch(e){return Promise.reject(e)}}(t)).then(function(n){var t=n.cjsModules,r=n.esmModules,o=n.allPackages;a("debug:external-cjs-modules",t),a("debug:external-esm-modules",r);var u=[].concat(r,o),i="string"==typeof l?k(e,l):e,s=[].concat(t,i),c=[].concat(t,k(e,".js"));return a("debug:local-modules",i),{outDir:f,isSource:p,ignore:u,allJsModules:c,allModules:s,_localModules:i,localModules:e}})})}catch(e){return Promise.reject(e)}}(t)).then(function(e){return Promise.resolve(function(e,n){try{var t=n.cwd,r=n.unlink,o=n.sourceMap,i=e.outDir,c=e.isSource,l=e.ignore,a=e.allJsModules,f=e.allModules,d=e.localModules;return Promise.resolve(Promise.all(e._localModules.map(function(e,m){try{var p=e.endsWith(".d.ts")?a:f,h=d[m],j=(c?h:e).replace(x(t),x(i)),P=g(h),M=w({options:n,contents:P,isSource:c,ignore:l,filename:e,filenames:p,originName:h,nextName:j});return v(j,M.contents),!c&&r&&t===i&&j!==h&&y(h),o&&function(e,n,t){if(void 0===t&&(t=!1),e!==n){var r=e+".map";if(u.existsSync(r)){var o=n+".map",i=b(r);i.file=s.default.basename(n),v(o,JSON.stringify(i)),t&&y(r)}}}(h,j,r&&t===i),Promise.resolve()}catch(e){return Promise.reject(e)}}))).then(function(){})}catch(e){return Promise.reject(e)}}(e,t)).then(function(){})})}catch(e){return Promise.reject(e)}},e.fixBlankFiles=function(e){return f({contents:e}).contents},e.fixContents=function(e,n,t,r,o,u,i){return void 0===o&&(o=n),void 0===u&&(u=!1),void 0===i&&(i=[]),w({contents:e,filename:n,filenames:t,options:r,originName:o,isSource:u,ignore:i}).contents},e.fixDefaultExport=function(e){return d({contents:e}).contents},e.fixDirnameVar=function(e,n){return m({contents:e,isSource:n}).contents},e.fixFilenameExtensions=k,e.fixFilenameVar=function(e,n){return p({contents:e,isSource:n}).contents},e.fixModuleReferences=function(e,n,t,r,o){return P({contents:e,filename:n,filenames:t,options:{cwd:r},ignore:o}).contents},e.fixSourceMapRef=function(e,n,t){return _({contents:e,originName:n,filename:t}).contents}}); | ||
//# sourceMappingURL=tsc-esm-fix.umd.js.map |
@@ -1,15 +0,3 @@ | ||
import { IFixOptions, IFixOptionsNormalized } from './interface.js'; | ||
export declare const DEFAULT_FIX_OPTIONS: IFixOptionsNormalized; | ||
export declare const normalizeOptions: (opts?: IFixOptions) => IFixOptionsNormalized; | ||
export declare const findTargets: (tsconfig: string | string[], cwd: string) => string[]; | ||
export declare const resolveDependency: (parent: string, nested: string, files: string[], cwd: string) => string; | ||
import { IFixOptions } from './interface.js'; | ||
export declare const fixFilenameExtensions: (names: string[], ext: string) => string[]; | ||
export declare const fixModuleReferences: (contents: string, filename: string, filenames: string[], cwd: string, ignore: string[]) => string; | ||
export declare const fixDirnameVar: (contents: string, isSource?: boolean) => string; | ||
export declare const fixFilenameVar: (contents: string, isSource?: boolean) => string; | ||
export declare const fixDefaultExport: (contents: string) => string; | ||
export declare const fixBlankFiles: (contents: string) => string; | ||
export declare const fixSourceMapRef: (contents: string, originName: string, filename: string) => string; | ||
export declare const fixContents: (contents: string, filename: string, filenames: string[], { cwd, ext, dirnameVar, filenameVar, fillBlank, forceDefaultExport, sourceMap }: IFixOptionsNormalized, originName?: string, isSource?: boolean, ignore?: string[]) => string; | ||
export declare const getPatterns: (sources: string[], targets: string[]) => string[]; | ||
export declare const fix: (opts?: IFixOptions) => Promise<void>; |
@@ -1,2 +0,4 @@ | ||
export { fix, fixBlankFiles, fixContents, fixFilenameVar, fixDirnameVar, fixModuleReferences, fixFilenameExtensions, fixDefaultExport, fixSourceMapRef, DEFAULT_FIX_OPTIONS, } from './fix.js'; | ||
export { fix, fixFilenameExtensions, } from './fix.js'; | ||
export { DEFAULT_FIX_OPTIONS, } from './options.js'; | ||
export { fixBlankFiles, fixContents, fixFilenameVar, fixDirnameVar, fixModuleReferences, fixSourceMapRef, fixDefaultExport, } from './legacy.js'; | ||
export * from './interface.js'; |
@@ -12,4 +12,4 @@ type IFunction<A extends any[] = any[], R = any> = (...args: A) => R; | ||
fillBlank?: boolean; | ||
forceDefaultExport?: boolean; | ||
sourceMap?: boolean; | ||
forceDefaultExport?: boolean; | ||
ext: boolean | string; | ||
@@ -25,2 +25,22 @@ unlink?: boolean; | ||
}; | ||
export type TFixContext = { | ||
outDir: string; | ||
isSource: boolean; | ||
ignore: string[]; | ||
allJsModules: string[]; | ||
allModules: string[]; | ||
_localModules: string[]; | ||
localModules: string[]; | ||
}; | ||
export type TResourceContext = { | ||
options: IFixOptionsNormalized; | ||
contents: string; | ||
filename: string; | ||
filenames: string[]; | ||
originName: string; | ||
nextName: string; | ||
isSource: boolean; | ||
ignore: string[]; | ||
}; | ||
export type TFixer = (ctx: TResourceContext) => TResourceContext; | ||
export {}; |
@@ -1,14 +0,11 @@ | ||
import {basename, dirname, join, resolve} from 'node:path' | ||
import path from 'node:path' | ||
import {Options as GlobbyOptions} from 'globby' | ||
import {IFixOptions, IFixOptionsNormalized} from './interface' | ||
import { fixContents } from './fixes' | ||
import {IFixOptions, IFixOptionsNormalized, TFixContext, TResourceContext} from './interface' | ||
import { | ||
asArray, | ||
existsSync, | ||
globby, | ||
read, | ||
readJson, | ||
remove, | ||
resolveTsConfig, | ||
unixify, | ||
@@ -18,65 +15,13 @@ write, | ||
export const DEFAULT_FIX_OPTIONS: IFixOptionsNormalized = { | ||
cwd: process.cwd(), | ||
tsconfig: './tsconfig.json', | ||
filenameVar: true, | ||
dirnameVar: true, | ||
ext: true, | ||
unlink: true, | ||
debug: () => {}, // eslint-disable-line | ||
} | ||
import { | ||
getLocalModules, | ||
getExternalModules, | ||
getTsconfigTargets, | ||
} from './finder' | ||
export const normalizeOptions = ( | ||
opts?: IFixOptions, | ||
): IFixOptionsNormalized => ({ | ||
...DEFAULT_FIX_OPTIONS, | ||
...opts, | ||
debug: typeof opts?.debug === 'function' | ||
? opts.debug | ||
: opts?.debug === true | ||
? console.log | ||
: DEFAULT_FIX_OPTIONS.debug, | ||
}) | ||
import { | ||
normalizeOptions | ||
} from './options' | ||
export const findTargets = ( | ||
tsconfig: string | string[], | ||
cwd: string, | ||
): string[] => | ||
asArray(tsconfig).reduce<string[]>((targets, file) => { | ||
const tsconfigJson = resolveTsConfig(resolve(cwd, file)) | ||
const outDir = tsconfigJson?.compilerOptions?.outDir | ||
const module = tsconfigJson?.compilerOptions?.module.toLowerCase?.() | ||
if (outDir && module.startsWith('es')) { | ||
targets.push(outDir) | ||
} else { | ||
console.warn('tsconfig should declare `outDir` and `module` type es6 or above') | ||
} | ||
return targets | ||
}, []) | ||
export const resolveDependency = ( | ||
parent: string, | ||
nested: string, | ||
files: string[], | ||
cwd: string, | ||
): string => { | ||
const dir = dirname(parent) | ||
const nmdir = resolve(cwd, 'node_modules') | ||
const bases = /^\..+\.[^./\\]+$/.test(nested) | ||
? [nested, nested.replace(/\.[^./\\]+$/, '')] | ||
: [nested] | ||
const variants = ['.js', '.cjs', '.mjs'].reduce<string[]>((m, e) => { | ||
bases.forEach((v) => m.push(`${v}${e}`, `${v}/index${e}`)) | ||
return m | ||
}, []) | ||
return ( | ||
variants.find((f) => files.includes(unixify(resolve(dir, f)))) || | ||
variants.find((f) => files.includes(unixify(resolve(nmdir, f)))) || | ||
nested | ||
) | ||
} | ||
export const fixFilenameExtensions = (names: string[], ext: string): string[] => | ||
@@ -88,201 +33,14 @@ names.map((name) => | ||
export const fixModuleReferences = ( | ||
contents: string, | ||
filename: string, | ||
filenames: string[], | ||
cwd: string, | ||
ignore: string[], | ||
): string => | ||
contents.replace( | ||
/((?:\s|^)import\s+|\s+from\s+|\W(?:import|require)\s*\()(["'])([^"']+\/[^"']+|\.{1,2})\/?(["'])/g, | ||
(_matched, control, q1, from, q2) => | ||
`${control}${q1}${ignore.includes(from) ? from : resolveDependency( | ||
filename, | ||
from, | ||
filenames, | ||
cwd, | ||
)}${q2}`, | ||
) | ||
export const fix = async (opts?: IFixOptions): Promise<void> => { | ||
const options = normalizeOptions(opts) | ||
const ctx = await resolve(options) | ||
export const fixDirnameVar = (contents: string, isSource?: boolean): string => | ||
contents.replace( | ||
/__dirname/g, | ||
`\`\${process.platform === 'win32' ? '' : '/'}\${/file:\\/{2,3}(.+)\\/[^/]/.exec(import.meta.url)${isSource ? '!' : ''}[1]}\``, | ||
) // eslint-disable-line | ||
export const fixFilenameVar = (contents: string, isSource?: boolean): string => | ||
contents.replace(/__filename/g, `\`\${process.platform === 'win32' ? '' : '/'}\${/file:\\/{2,3}(.+)/.exec(import.meta.url)${isSource ? '!' : ''}[1]}\``) // eslint-disable-line | ||
export const fixDefaultExport = (contents: string): string => contents.includes('export default') | ||
? contents | ||
: `${contents} | ||
export default undefined | ||
` | ||
export const fixBlankFiles = (contents: string): string => contents.trim().length === 0 | ||
? ` | ||
export {} | ||
export default undefined | ||
` : contents | ||
export const fixSourceMapRef = (contents: string, originName: string, filename: string): string => | ||
originName === filename | ||
? contents | ||
: contents.replace( | ||
`//# sourceMappingURL=${basename(originName)}.map`, | ||
`//# sourceMappingURL=${basename(filename)}.map` | ||
) | ||
export const fixContents = ( | ||
contents: string, | ||
filename: string, | ||
filenames: string[], | ||
{cwd, ext, dirnameVar, filenameVar, fillBlank, forceDefaultExport, sourceMap}: IFixOptionsNormalized, | ||
originName = filename, // NOTE Weird contract to avoid breaking change | ||
isSource = false, | ||
ignore: string[] = [], | ||
): string => { | ||
let _contents = contents | ||
if (ext) { | ||
_contents = fixModuleReferences(_contents, filename, filenames, cwd, ignore) | ||
} | ||
if (dirnameVar) { | ||
_contents = fixDirnameVar(_contents, isSource) | ||
} | ||
if (filenameVar) { | ||
_contents = fixFilenameVar(_contents, isSource) | ||
} | ||
if (fillBlank) { | ||
_contents = fixBlankFiles(_contents) | ||
} | ||
if (forceDefaultExport) { | ||
_contents = fixDefaultExport(_contents) | ||
} | ||
if (sourceMap) { | ||
_contents = fixSourceMapRef(_contents, originName, filename) | ||
} | ||
return _contents | ||
await patch(ctx, options) | ||
} | ||
const resolvePrefix = (prefix: string, pattern?: string): string => { | ||
if (!pattern) { | ||
return prefix | ||
} | ||
let _pattern = pattern | ||
if (_pattern.includes('*')) { | ||
_pattern = _pattern.slice(0, _pattern.indexOf('*')) | ||
if (_pattern.includes('/')) { | ||
_pattern = _pattern.slice(0, _pattern.lastIndexOf('/')) | ||
} | ||
} | ||
return join(prefix, _pattern) | ||
} | ||
// https://nodejs.org/api/packages.html | ||
// https://webpack.js.org/guides/package-exports/ | ||
type Entry = string | string[] | Record<string, string | string[] | Record<string, string | string[]>> | ||
const getExportsEntries = (exports: string | Entry): [string, string[]][] => { | ||
const entries: [string, Entry][] = Object.entries(exports) | ||
const parseConditional = (e: Entry): string[] => typeof e === 'string' ? [e] : Object.values(e).map(parseConditional).flat(2) | ||
// has subpaths | ||
if (typeof exports !== 'string' && Object.keys(exports).some((k) => k.startsWith('.'))) { | ||
return entries.map(([k, v]) => [k, parseConditional(v)]) | ||
} | ||
return [['.', parseConditional(exports)]] | ||
} | ||
const getExternalEsmModules = (cwd: string): Promise<{ names: string[], files: string[] }> => | ||
globby(['node_modules/*/package.json', 'node_modules/@*/*/package.json'], { | ||
cwd, | ||
onlyFiles: true, | ||
absolute: true, | ||
} as GlobbyOptions).then(async (files: string[]) => | ||
(await Promise.all(files | ||
.map(async (f: string): Promise<{ name?: string, files?: string[] }> => { | ||
const {name, exports} = await readJson(f) | ||
if (!exports) { | ||
return {name} | ||
} | ||
const _dir = dirname(f) | ||
const exportsEntries = getExportsEntries(exports) | ||
return { | ||
name, | ||
files: (await Promise.all(exportsEntries.map(([key, values]) => | ||
Promise.all(values.map(async(value) => | ||
(await globby(value, {cwd: _dir, onlyFiles: true, absolute: false})) | ||
.map(file => join(file) | ||
.replace( | ||
resolvePrefix('.', value), | ||
resolvePrefix(name, key))) | ||
) | ||
)))).flat(2) | ||
} | ||
}))).reduce<{ names: string[], files: string[] }>((m, {name, files: _files}) => { | ||
if (name) { | ||
m.names.push(name) | ||
} | ||
if (_files) { | ||
m.files.push(..._files) | ||
} | ||
return m | ||
}, {names: [], files: []}), | ||
) | ||
const getExternalModules = async (cwd: string): Promise<{cjsModules: string[], esmModules: string[], allPackages: string[] }> => { | ||
const {names, files: esmModules} = await getExternalEsmModules(cwd) | ||
const cjsModules = await globby( | ||
[ | ||
'!node_modules/.cache', | ||
'!node_modules/.bin', | ||
'!node_modules/**/node_modules', | ||
...names.map(m => `!node_modules/${m}`), | ||
'node_modules/**/*.(m|c)?js', | ||
], | ||
{ | ||
cwd, | ||
onlyFiles: true, | ||
absolute: true, | ||
} as GlobbyOptions, | ||
) | ||
return { | ||
cjsModules, | ||
esmModules, | ||
allPackages: names, | ||
} | ||
} | ||
export const getPatterns = (sources: string[], targets: string[]): string[] => | ||
sources.length > 0 | ||
? sources.map((src) => src.includes('*') ? src : `${src}/**/*.{ts,tsx}`) | ||
: targets.map((target) => target.includes('*') ? target : `${target}/**/*.{js,d.ts}`) | ||
export const fix = async (opts?: IFixOptions): Promise<void> => { | ||
const _opts = normalizeOptions(opts) | ||
const {cwd, target, src, tsconfig, out = cwd, ext, debug, unlink, sourceMap} = _opts | ||
const outDir = resolve(cwd, out) | ||
const resolve = async (opts: IFixOptionsNormalized): Promise<TFixContext> => { | ||
const {cwd, target, src, tsconfig, out = cwd, ext, debug, unlink, sourceMap} = opts | ||
const outDir = path.resolve(cwd, out) | ||
const sources = asArray<string>(src) | ||
const targets = [...asArray<string>(target), ...findTargets(tsconfig, cwd)] | ||
const targets = [...asArray<string>(target), ...getTsconfigTargets(tsconfig, cwd)] | ||
debug('debug:cwd', cwd) | ||
@@ -294,8 +52,3 @@ debug('debug:outdir', outDir) | ||
const isSource = sources.length > 0 | ||
const patterns = getPatterns(sources, targets) | ||
const localModules = await globby(patterns, { | ||
cwd, | ||
onlyFiles: true, | ||
absolute: true, | ||
} as GlobbyOptions) | ||
const localModules = await getLocalModules(sources, targets, cwd) | ||
const { | ||
@@ -315,16 +68,51 @@ cjsModules, | ||
_localModules.forEach((name, i) => { | ||
return { | ||
outDir, | ||
isSource, | ||
ignore, | ||
allJsModules, | ||
allModules, | ||
_localModules, | ||
localModules | ||
} | ||
} | ||
const patch = async (ctx: TFixContext, options: IFixOptionsNormalized) => { | ||
const {cwd, unlink, sourceMap} = options | ||
const { | ||
outDir, | ||
isSource, | ||
ignore, | ||
allJsModules, | ||
allModules, | ||
_localModules, | ||
localModules | ||
} = ctx | ||
await Promise.all(_localModules.map(async (name, i) => { | ||
// NOTE d.ts may refer to .js ext only | ||
const all = name.endsWith('.d.ts') ? allJsModules : allModules | ||
const originName = localModules[i] | ||
const nextName = (sources.length === 0 ? name : originName).replace( | ||
unixify(cwd), | ||
unixify(outDir), | ||
) | ||
const nextName = (isSource ? originName : name) | ||
.replace( | ||
unixify(cwd), | ||
unixify(outDir), | ||
) | ||
const contents = read(originName) | ||
const _contents = fixContents(contents, name, all, _opts, originName, isSource, ignore) | ||
const ctx: TResourceContext = { | ||
options, | ||
contents, | ||
isSource, | ||
ignore, | ||
filename: name, | ||
filenames: all, | ||
originName, | ||
nextName, | ||
} | ||
const {contents: _contents} = fixContents(ctx) | ||
write(nextName, _contents) | ||
if (sources.length === 0 && unlink && cwd === outDir && nextName !== originName) { | ||
if (!isSource && unlink && cwd === outDir && nextName !== originName) { | ||
remove(originName) | ||
@@ -336,3 +124,3 @@ } | ||
} | ||
}) | ||
})) | ||
} | ||
@@ -353,3 +141,3 @@ | ||
contents.file = basename(nextName) | ||
contents.file = path.basename(nextName) | ||
write(nextMapfile, JSON.stringify(contents)) | ||
@@ -356,0 +144,0 @@ |
@@ -1,13 +0,6 @@ | ||
export { | ||
fix, | ||
fixBlankFiles, | ||
fixContents, | ||
fixFilenameVar, | ||
fixDirnameVar, | ||
fixModuleReferences, | ||
fixFilenameExtensions, | ||
fixDefaultExport, | ||
fixSourceMapRef, | ||
DEFAULT_FIX_OPTIONS, | ||
} from './fix' | ||
export * from './interface' | ||
export {fixBlankFiles} from './fix-blank-files' | ||
export {fixContents} from './fix-contents' | ||
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' |
type IFunction<A extends any[] = any[], R = any> = (...args: A) => R | ||
export type IFixOptionsNormalized = { | ||
cwd: string | ||
debug: IFunction | ||
out?: string | ||
src?: string | string[] | ||
target?: string | string[] | ||
tsconfig: string | string[] | ||
dirnameVar: boolean | ||
filenameVar: boolean | ||
fillBlank?: boolean | ||
sourceMap?: boolean | ||
forceDefaultExport?: boolean | ||
ext: boolean | string | ||
unlink?: boolean | ||
cwd: string | ||
debug: IFunction | ||
out?: string | ||
src?: string | string[] | ||
target?: string | string[] | ||
tsconfig: string | string[] | ||
dirnameVar: boolean | ||
filenameVar: boolean | ||
fillBlank?: boolean | ||
forceDefaultExport?: boolean | ||
sourceMap?: boolean | ||
ext: boolean | string | ||
unlink?: boolean | ||
} | ||
@@ -25,1 +25,24 @@ | ||
export type IFixOptions = Partial<Omit<IFixOptionsNormalized, 'debug'>> & {debug?: boolean | IFunction} | ||
export type TFixContext = { | ||
outDir: string | ||
isSource: boolean | ||
ignore: string[] | ||
allJsModules: string[] | ||
allModules: string[] | ||
_localModules: string[] | ||
localModules: string[] | ||
} | ||
export type TResourceContext = { | ||
options: IFixOptionsNormalized | ||
contents: string | ||
filename: string | ||
filenames: string[] | ||
originName: string | ||
nextName: string | ||
isSource: boolean | ||
ignore: string[] | ||
} | ||
export type TFixer = (ctx: TResourceContext) => TResourceContext |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
182624
70
1254
1
Updatedfs-extra@^11.2.0