Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsc-esm-fix

Package Overview
Dependencies
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsc-esm-fix - npm Package Compare versions

Comparing version 2.5.1 to 2.6.0

4

buildstamp.json
{
"git": {
"commitId": "9f8cc6e2cafe494ce7d4aa5ac33e04fc3180fb11",
"commitId": "b17160e74de4c99013d980eea39b927f411319ab",
"repoUrl": "https://github.com/antongolub/tsc-esm-fix",
"repoName": "antongolub/tsc-esm-fix"
},
"date": "2021-08-18T19:11:08.359Z"
"date": "2021-08-18T22:11:08.587Z"
}

@@ -0,1 +1,8 @@

# [2.6.0](https://github.com/antongolub/tsc-esm-fix/compare/v2.5.1...v2.6.0) (2021-08-18)
### Features
* provide ts-sources patching ([8bc4ab0](https://github.com/antongolub/tsc-esm-fix/commit/8bc4ab0d2ed1fee46669673eaba8e375a229a945))
## [2.5.1](https://github.com/antongolub/tsc-esm-fix/compare/v2.5.0...v2.5.1) (2021-08-18)

@@ -2,0 +9,0 @@

{
"name": "tsc-esm-fix",
"version": "2.5.1",
"version": "2.6.0",
"private": false,

@@ -5,0 +5,0 @@ "publishConfig": {

# tsc-esm-fix
Make tsc-compiled [`es2020/esnext`](https://www.typescriptlang.org/tsconfig/#module) bundles compatible with [esm/mjs requirements](https://nodejs.org/api/esm.html#esm_packages)
Make TS projects compatible with [esm/mjs requirements](https://nodejs.org/api/esm.html#esm_packages)

@@ -10,9 +10,13 @@ [![CI](https://github.com/antongolub/tsc-esm-fix/workflows/CI/badge.svg)](https://github.com/antongolub/tsc-esm-fix/actions)

### Motivation
### Problem
This workaround is aimed to bypass a pair of **tsc** and **ts-jest** issues _right here and right now_.
* [TS/13422](https://github.com/microsoft/TypeScript/issues/13422) / [TS/16577](https://github.com/microsoft/TypeScript/issues/16577): tsc should add `.js` extensions for relative module paths.
* [TS/13422](https://github.com/microsoft/TypeScript/issues/13422) / [TS/16577](https://github.com/microsoft/TypeScript/issues/16577): **tsc** should add `.js` extensions for relative module paths if compiled as [`es2020/esnext`](https://www.typescriptlang.org/tsconfig/#module).
* [ts-jest/1174](https://github.com/kulshekhar/ts-jest/issues/1174): `import.meta` is not allowed.
Hope one day this library will not be needed.
### Solutions
1. Post-process tsc-compiled outputs everytime after the build.
2. Patch project sources once as Sindre recommends in [ESM migration guide](https://github.com/sindresorhus/meta/discussions/15)
This lib may be applied in both cases.
### Features

@@ -40,2 +44,8 @@ * Injects extensions to imports/re-exports statements.

tsc-esm-fix [options]
# to post-process outputs everytime
tsc-esm-fix --target='target/es6'
# to patch ts sources once
tsc-esm-fix --target='src/main/ts' --ext='.js'
```

@@ -131,3 +141,4 @@

|`--tsconfig`| Path to project's ts-config(s) | `tsconfig.json`
|`--target` | Entry points where compiled files are placed for modification | If not specified inherited from tsconfig.json **compilerOptions.outDir**
|`--src` | Entry points where the ts-source files are placed. If defined `src` option suppresses `target` |
|`--target` | tsc-compiled output directory | If not specified inherited from tsconfig.json **compilerOptions.outDir**
|`--dirnameVar` | Replace `__dirname` usages with `import.meta` | true

@@ -157,4 +168,5 @@ |`--filenameVar` | Replace `__filename` var references `import.meta` | true

cwd: string
out?: string,
src?: string | string[]
target?: string | string[]
out?: string
tsconfig?: string | string[]

@@ -161,0 +173,0 @@ dirnameVar: boolean

@@ -5,2 +5,3 @@ export declare type IFixOptionsNormalized = {

out?: string;
src?: string | string[];
target?: string | string[];

@@ -7,0 +8,0 @@ tsconfig: string | string[];

@@ -1,2 +0,2 @@

!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("path"),require("fs"),require("fs-extra"),require("json5"),require("globby")):"function"==typeof define&&define.amd?define(["exports","path","fs","fs-extra","json5","globby"],n):n((e||self).tscEsmFix={},e.path,e.fs,e.fsExtra,e.json5,e.globby)}(this,function(e,n,r,t,o,u){function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=i(t),l=i(o);function s(){return(s=Object.assign||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}).apply(this,arguments)}var a=function(e){return r.readFileSync(e,{encoding:"utf8"})},f=function(e){return l.default.parse(a(e))},d=function(e){return Array.isArray(e)?e:[e]},p=r.unlinkSync,m=function(e){return e.replace(/\\/g,"/")},g=function e(r){var t=f(r);if(t.extends){var o=e(n.resolve(n.dirname(r),t.extends));return s({},o,{compilerOptions:s({},o.compilerOptions,t.compilerOptions)})}return t},b={cwd:process.cwd(),tsconfig:"./tsconfig.json",filenameVar:!0,dirnameVar:!0,ext:!0,unlink:!0,debug:!1},v=function(e,r){return d(e).reduce(function(e,t){var o,u,i,c=g(n.resolve(r,t)),l=null==c||null==(o=c.compilerOptions)?void 0:o.outDir,s=null==c||null==(u=c.compilerOptions)||null==(i=u.module)?void 0:i.toLowerCase();return!l||"es2020"!==s&&"esnext"!==s||e.push(l),e},[])},x=function(e,n){return e.map(function(e){return e.replace(/\.[^./\\]+$/,n)})},y=function(e,r,t,o){return e.replace(/(\sfrom |\simport[ (])(["'])([^"']+\/[^"']+)(["'])/g,function(e,u,i,c,l){return""+u+i+function(e,r,t,o){var u=n.dirname(e),i=n.resolve(o,"node_modules"),c=/^\..+\.[^./\\]+$/.test(r)?[r,r.replace(/\.[^./\\]+$/,"")]:[r];return[".js",".cjs",".mjs"].reduce(function(e,n){return c.forEach(function(r){return e.push(""+r+n,r+"/index"+n)}),e},[]).find(function(e){return t.includes(m(n.resolve(i,e)))||t.includes(m(n.resolve(u,e)))})||r}(r,c,t,o)+l})},h=function(e){return e.replace(/__dirname/g,"/file:\\/\\/(.+)\\/[^/]/.exec(import.meta.url)[1]")},j=function(e){return e.replace(/__filename/g,"/file:\\/\\/(.+)/.exec(import.meta.url)[1]")},_=function(e,n,r,t){var o=t.dirnameVar,u=t.filenameVar,i=e;return t.ext&&(i=y(i,n,r,t.cwd)),o&&(i=h(i)),u&&(i=j(i)),i};e.DEFAULT_FIX_OPTIONS=b,e.fix=function(e){try{var r=function(e){return s({},b,e)}(e),t=r.cwd,o=r.tsconfig,i=r.out,l=void 0===i?t:i,g=r.ext,y=r.unlink,h=r.debug?console.log:function(){},j=[].concat(d(r.target),v(o,t));h("debug:cwd",t),h("debug:targets",j);var w=j.map(function(e){return e+"/**/*.js"}),F=n.resolve(t,l);return h("debug:outdir",F),Promise.resolve(u.globby(w,{cwd:t,onlyFiles:!0,absolute:!0})).then(function(e){return Promise.resolve(function(e){try{return Promise.resolve(function(e){return u.globby(["node_modules/*/package.json"],{cwd:e,onlyFiles:!0,absolute:!0}).then(function(e){return e.filter(function(e){return f(e).exports}).map(function(e){return n.basename(n.dirname(e))})})}(e)).then(function(n){return u.globby(["node_modules/**/*.(m|c)?js","!node_modules/**/node_modules"].concat(n.map(function(e){return"!node_modules/"+e})),{cwd:e,onlyFiles:!0,absolute:!0})})}catch(e){return Promise.reject(e)}}(t)).then(function(n){h("debug:external-names",n);var o="string"==typeof g?x(e,g):e;h("debug:local-names",o);var u=[].concat(n,o);o.forEach(function(n,o){var i=n.replace(m(t),m(F)),l=a(e[o]);!function(e,n){c.default.outputFileSync(e,n,{encoding:"utf8"})}(i,_(l,n,u,r)),y&&t===F&&i!==e[o]&&p(e[o])})})})}catch(e){return Promise.reject(e)}},e.fixContents=_,e.fixDirnameVar=h,e.fixFilenameExtensions=x,e.fixFilenameVar=j,e.fixModuleReferences=y});
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("path"),require("fs"),require("fs-extra"),require("json5"),require("globby")):"function"==typeof define&&define.amd?define(["exports","path","fs","fs-extra","json5","globby"],n):n((e||self).tscEsmFix={},e.path,e.fs,e.fsExtra,e.json5,e.globby)}(this,function(e,n,r,t,o,u){function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=i(t),s=i(o);function l(){return(l=Object.assign||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}).apply(this,arguments)}var a=function(e){return r.readFileSync(e,{encoding:"utf8"})},f=function(e){return s.default.parse(a(e))},d=function(e){return e?Array.isArray(e)?e:[e]:[]},p=r.unlinkSync,m=function(e){return e.replace(/\\/g,"/")},g=function e(r){var t=f(r);if(t.extends){var o=e(n.resolve(n.dirname(r),t.extends));return l({},o,{compilerOptions:l({},o.compilerOptions,t.compilerOptions)})}return t},b={cwd:process.cwd(),tsconfig:"./tsconfig.json",filenameVar:!0,dirnameVar:!0,ext:!0,unlink:!0,debug:!1},v=function(e,r){return d(e).reduce(function(e,t){var o,u,i,c=g(n.resolve(r,t)),s=null==c||null==(o=c.compilerOptions)?void 0:o.outDir,l=null==c||null==(u=c.compilerOptions)||null==(i=u.module)?void 0:i.toLowerCase();return!s||"es2020"!==l&&"esnext"!==l||e.push(s),e},[])},x=function(e,n){return e.map(function(e){return e.replace(/\.[^./\\]+$/,n)})},y=function(e,r,t,o){return e.replace(/(\sfrom |\simport[ (])(["'])([^"']+\/[^"']+)(["'])/g,function(e,u,i,c,s){return""+u+i+function(e,r,t,o){var u=n.dirname(e),i=n.resolve(o,"node_modules"),c=/^\..+\.[^./\\]+$/.test(r)?[r,r.replace(/\.[^./\\]+$/,"")]:[r];return[".js",".cjs",".mjs"].reduce(function(e,n){return c.forEach(function(r){return e.push(""+r+n,r+"/index"+n)}),e},[]).find(function(e){return t.includes(m(n.resolve(i,e)))||t.includes(m(n.resolve(u,e)))})||r}(r,c,t,o)+s})},h=function(e){return e.replace(/__dirname/g,"/file:\\/\\/(.+)\\/[^/]/.exec(import.meta.url)[1]")},j=function(e){return e.replace(/__filename/g,"/file:\\/\\/(.+)/.exec(import.meta.url)[1]")},_=function(e,n,r,t){var o=t.dirnameVar,u=t.filenameVar,i=e;return t.ext&&(i=y(i,n,r,t.cwd)),o&&(i=h(i)),u&&(i=j(i)),i};e.DEFAULT_FIX_OPTIONS=b,e.fix=function(e){try{var r=function(e){return l({},b,e)}(e),t=r.cwd,o=r.target,i=r.src,s=r.tsconfig,g=r.out,y=r.ext,h=r.debug,j=r.unlink,w=n.resolve(t,void 0===g?t:g),F=h?console.log:function(){},O=d(i),P=[].concat(d(o),v(s,t));F("debug:cwd",t),F("debug:outdir",w),F("debug:sources",O),F("debug:targets",P);var E=O.length>0?O.map(function(e){return e+"/**/*.ts"}):P.map(function(e){return e+"/**/*.js"});return Promise.resolve(u.globby(E,{cwd:t,onlyFiles:!0,absolute:!0})).then(function(e){return Promise.resolve(function(e){try{return Promise.resolve(function(e){return u.globby(["node_modules/*/package.json"],{cwd:e,onlyFiles:!0,absolute:!0}).then(function(e){return e.filter(function(e){return f(e).exports}).map(function(e){return n.basename(n.dirname(e))})})}(e)).then(function(n){return u.globby(["node_modules/**/*.(m|c)?js","!node_modules/**/node_modules"].concat(n.map(function(e){return"!node_modules/"+e})),{cwd:e,onlyFiles:!0,absolute:!0})})}catch(e){return Promise.reject(e)}}(t)).then(function(n){F("debug:external-names",n);var o="string"==typeof y?x(e,y):e;F("debug:local-names",o);var u=[].concat(n,o);o.forEach(function(n,o){var s=(i?e[o]:n).replace(m(t),m(w)),l=a(e[o]);!function(e,n){c.default.outputFileSync(e,n,{encoding:"utf8"})}(s,_(l,n,u,r)),!i&&j&&t===w&&s!==e[o]&&p(e[o])})})})}catch(e){return Promise.reject(e)}},e.fixContents=_,e.fixDirnameVar=h,e.fixFilenameExtensions=x,e.fixFilenameVar=j,e.fixModuleReferences=y});
//# sourceMappingURL=tsc-esm-fix.umd.js.map

@@ -8,5 +8,5 @@ /// <reference types="node" />

export declare const readJson: <D = any>(file: string) => D;
export declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
export declare const asArray: <T>(value: T | T[] | undefined) => T[];
export declare const remove: typeof unlinkSync;
export declare const unixify: (path: string) => string;
export declare const resolveTsConfig: (file: string) => TSConfig;

@@ -5,2 +5,3 @@ export declare type IFixOptionsNormalized = {

out?: string;
src?: string | string[];
target?: string | string[];

@@ -7,0 +8,0 @@ tsconfig: string | string[];

@@ -8,5 +8,5 @@ /// <reference types="node" />

export declare const readJson: <D = any>(file: string) => D;
export declare const asArray: <T>(value: T) => T extends any[] ? T : T[];
export declare const asArray: <T>(value: T | T[] | undefined) => T[];
export declare const remove: typeof unlinkSync;
export declare const unixify: (path: string) => string;
export declare const resolveTsConfig: (file: string) => TSConfig;

@@ -151,11 +151,15 @@ import { basename, dirname, resolve } from 'path'

const _opts = normalizeOptions(opts)
const { cwd, target, tsconfig, out = cwd, ext, debug, unlink } = _opts
const { cwd, target, src, tsconfig, out = cwd, ext, debug, unlink } = _opts
const outDir = resolve(cwd, out)
const dbg = debug ? console.log : () => {} // eslint-disable-line
const targets = [...asArray(target), ...findTargets(tsconfig, cwd)]
const sources = asArray<string>(src)
const targets = [...asArray<string>(target), ...findTargets(tsconfig, cwd)]
dbg('debug:cwd', cwd)
dbg('debug:outdir', outDir)
dbg('debug:sources', sources)
dbg('debug:targets', targets)
const patterns = targets.map((target) => `${target}/**/*.js`)
const outDir = resolve(cwd, out)
dbg('debug:outdir', outDir)
const patterns = sources.length > 0
? sources.map((src) => `${src}/**/*.ts`)
: targets.map((target) => `${target}/**/*.js`)

@@ -176,3 +180,3 @@ const names = await globby(patterns, {

_names.forEach((name, i) => {
const nextName = name.replace(unixify(cwd), unixify(outDir))
const nextName = (!src ? name : names[i]).replace(unixify(cwd), unixify(outDir))
const contents = read(names[i])

@@ -183,3 +187,3 @@ const _contents = fixContents(contents, name, allNames, _opts)

if (unlink && cwd === outDir && nextName !== names[i]) {
if (!src && unlink && cwd === outDir && nextName !== names[i]) {
remove(names[i])

@@ -186,0 +190,0 @@ }

@@ -5,2 +5,3 @@ export type IFixOptionsNormalized = {

out?: string
src?: string | string[]
target?: string | string[]

@@ -7,0 +8,0 @@ tsconfig: string | string[]

@@ -18,4 +18,8 @@ import { readFileSync, unlinkSync } from 'fs'

export const asArray = <T>(value: T): T extends any[] ? T : T[] =>
(Array.isArray(value) ? value : [value]) as T extends any[] ? T : T[]
export const asArray = <T>(value: T[] | T | undefined): T[] =>
(value
? Array.isArray(value)
? value
: [value]
: [])

@@ -22,0 +26,0 @@ export const remove = unlinkSync

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc