Socket
Socket
Sign inDemoInstall

tshy

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tshy - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

dist/esm/valid-extra-dialects.d.ts

42

dist/esm/build-commonjs.js
import chalk from 'chalk';
import { spawnSync } from 'node:child_process';
import { renameSync } from 'node:fs';
import { renameSync, unlinkSync } from 'node:fs';
import { relative, resolve } from 'node:path/posix';
import buildFail from './build-fail.js';
import config from './config.js';
import * as console from './console.js';

@@ -10,22 +11,29 @@ import polyfills from './polyfills.js';

import './tsconfig.js';
const { commonjsDialects = [] } = config;
export const buildCommonJS = () => {
console.debug(chalk.cyan.dim('building commonjs'));
setFolderDialect('src', 'commonjs');
const res = spawnSync('tsc -p .tshy/commonjs.json', {
shell: true,
stdio: 'inherit',
});
for (const d of ['commonjs', ...commonjsDialects]) {
const pf = polyfills.get(d === 'commonjs' ? 'cjs' : d);
console.debug(chalk.cyan.dim('building ' + d));
const res = spawnSync(`tsc -p .tshy/${d}.json`, {
shell: true,
stdio: 'inherit',
});
if (res.status || res.signal) {
setFolderDialect('src');
return buildFail(res);
}
setFolderDialect('.tshy-build-tmp/' + d, 'commonjs');
for (const [override, orig] of pf?.map.entries() ?? []) {
const stemFrom = resolve(`.tshy-build-tmp/${d}`, relative(resolve('src'), resolve(override))).replace(/\.cts$/, '');
const stemTo = resolve(`.tshy-build-tmp/${d}`, relative(resolve('src'), resolve(orig))).replace(/\.tsx?$/, '');
unlinkSync(`${stemTo}.js.map`);
unlinkSync(`${stemTo}.d.ts.map`);
renameSync(`${stemFrom}.cjs`, `${stemTo}.js`);
renameSync(`${stemFrom}.d.cts`, `${stemTo}.d.ts`);
}
console.error(chalk.cyan.bold('built commonjs'));
}
setFolderDialect('src');
if (res.status || res.signal)
return buildFail(res);
setFolderDialect('.tshy-build-tmp/commonjs', 'commonjs');
console.error(chalk.cyan.bold('built commonjs'));
// apply polyfills
for (const [f, t] of polyfills.entries()) {
const stemFrom = resolve('.tshy-build-tmp/commonjs', relative(resolve('src'), resolve(f))).replace(/\.cts$/, '');
const stemTo = resolve('.tshy-build-tmp/commonjs', relative(resolve('src'), resolve(t))).replace(/\.tsx?$/, '');
renameSync(`${stemFrom}.cjs`, `${stemTo}.js`);
renameSync(`${stemFrom}.d.cts`, `${stemTo}.d.ts`);
}
};
//# sourceMappingURL=build-commonjs.js.map
import chalk from 'chalk';
import { spawnSync } from 'node:child_process';
import { renameSync, unlinkSync } from 'node:fs';
import { relative, resolve } from 'node:path';
import buildFail from './build-fail.js';
import config from './config.js';
import * as console from './console.js';
import polyfills from './polyfills.js';
import setFolderDialect from './set-folder-dialect.js';
import './tsconfig.js';
const { esmDialects = [] } = config;
export const buildESM = () => {
console.debug(chalk.cyan.dim('building esm'));
setFolderDialect('src', 'esm');
const res = spawnSync('tsc -p .tshy/esm.json', {
shell: true,
stdio: 'inherit',
});
for (const d of ['esm', ...esmDialects]) {
const pf = polyfills.get(d);
console.debug(chalk.cyan.dim('building ' + d));
const res = spawnSync(`tsc -p .tshy/${d}.json`, {
shell: true,
stdio: 'inherit',
});
if (res.status || res.signal) {
setFolderDialect('src');
return buildFail(res);
}
setFolderDialect('.tshy-build-tmp/' + d, 'esm');
for (const [override, orig] of pf?.map.entries() ?? []) {
const stemFrom = resolve(`.tshy-build-tmp/${d}`, relative(resolve('src'), resolve(override))).replace(/\.mts$/, '');
const stemTo = resolve(`.tshy-build-tmp/${d}`, relative(resolve('src'), resolve(orig))).replace(/\.tsx?$/, '');
unlinkSync(`${stemTo}.js.map`);
unlinkSync(`${stemTo}.d.ts.map`);
renameSync(`${stemFrom}.mjs`, `${stemTo}.js`);
renameSync(`${stemFrom}.d.mts`, `${stemTo}.d.ts`);
}
console.error(chalk.cyan.bold('built ' + d));
}
setFolderDialect('src');
if (res.status || res.signal)
return buildFail(res);
setFolderDialect('.tshy-build-tmp/esm', 'esm');
console.error(chalk.cyan.bold('built esm'));
};
//# sourceMappingURL=build-esm.js.map

@@ -7,2 +7,3 @@ // get the config and package and stuff

import validExports from './valid-exports.js';
import validExtraDialects from './valid-extra-dialects.js';
const validBoolean = (e, name) => {

@@ -17,4 +18,5 @@ const v = e[name];

typeof e === 'object' &&
(e.exports === undefined || validExports(e['exports'])) &&
(e.dialects === undefined || validDialects(e['dialects'])) &&
(e.exports === undefined || validExports(e.exports)) &&
(e.dialects === undefined || validDialects(e.dialects)) &&
validExtraDialects(e) &&
validBoolean(e, 'selfLink') &&

@@ -21,0 +23,0 @@ validBoolean(e, 'main');

/// <reference types="node" resolution-mode="require"/>
import { Export, Package, TshyConfig, TshyExport } from './types.js';
export declare const getImpTarget: (s: string | TshyExport | undefined | null) => string | undefined | null;
export declare const getReqTarget: (s: string | TshyExport | undefined | null, polyfills: Map<string, string>) => string | null | undefined;
export declare const getExports: (c: TshyConfig, polyfills: Map<string, string>) => Record<string, Export>;
import { ExportsSubpaths } from 'resolve-import';
import type { PolyfillSet } from './polyfills.js';
import { Package, TshyConfig, TshyExport } from './types.js';
export declare const getImpTarget: (s: string | TshyExport | undefined | null, polyfills?: Map<string, PolyfillSet>) => string | null | undefined;
export declare const getReqTarget: (s: string | TshyExport | undefined | null, polyfills?: Map<string, PolyfillSet>) => string | null | undefined;
export declare const setMain: (c: TshyConfig | undefined, pkg: Package & {
exports: Record<string, Export>;
exports: ExportsSubpaths;
}) => undefined;
declare const _default: Record<string, Export>;
declare const _default: ExportsSubpaths;
export default _default;
//# sourceMappingURL=exports.d.ts.map

@@ -8,29 +8,23 @@ import { relative, resolve } from 'node:path/posix';

import { resolveExport } from './resolve-export.js';
export const getImpTarget = (s) => {
const { esmDialects = [], commonjsDialects = [] } = config;
const getTargetForDialectCondition = (s, dialect, condition, type, polyfills = new Map()) => {
if (s === undefined)
return undefined;
if (typeof s === 'string') {
const imp = s.endsWith('.cts') ? undefined : s;
return !imp || !imp.startsWith('./src/')
? imp
: dialects.includes('esm')
? `./dist/esm/${relative(resolve('./src'), resolve(imp)).replace(/\.(m?)tsx?$/, '.$1js')}`
// the excluded filename pattern
const xts = type === 'commonjs' ? '.mts' : '.cts';
if (s.endsWith(xts))
return undefined;
const pf = dialect === 'commonjs' ? 'cjs' : dialect;
return !s || !s.startsWith('./src/')
? s
: dialects.includes(type)
? `./dist/${dialect}/${relative(resolve('./src'), resolve(polyfills.get(pf)?.map.get(s) ?? s)).replace(/\.([mc]?)tsx?$/, '.$1js')}`
: undefined;
}
return resolveExport(s, ['import']);
return resolveExport(s, [condition]);
};
export const getReqTarget = (s, polyfills) => {
if (s === undefined)
return undefined;
if (typeof s === 'string') {
const req = s.endsWith('.mts') ? undefined : s;
return !req || !req.startsWith('./src/')
? req
: dialects.includes('commonjs')
? `./dist/commonjs/${relative(resolve('./src'), resolve(polyfills.get(req) || req)).replace(/\.(c?)tsx?$/, '.$1js')}`
: undefined;
}
return getReqTarget(resolveExport(s, ['require']), polyfills);
};
export const getExports = (c, polyfills) => {
export const getImpTarget = (s, polyfills = new Map()) => getTargetForDialectCondition(s, 'esm', 'import', 'esm', polyfills);
export const getReqTarget = (s, polyfills = new Map()) => getTargetForDialectCondition(s, 'commonjs', 'require', 'commonjs', polyfills);
const getExports = (c) => {
// by this time it always exports, will get the default if missing

@@ -46,3 +40,4 @@ /* c8 ignore start */

// external export, not built by us
if (typeof s !== 'string' || !s.startsWith('./src/')) {
if (s !== null &&
(typeof s !== 'string' || !s.startsWith('./src/'))) {
// already been validated, just accept as-is

@@ -52,3 +47,3 @@ e[sub] = s;

}
const impTarget = getImpTarget(s);
const impTarget = getImpTarget(s, polyfills);
const reqTarget = getReqTarget(s, polyfills);

@@ -62,2 +57,25 @@ // should be impossible

if (impTarget) {
for (const d of esmDialects) {
const target = getTargetForDialectCondition(s, d, d, 'esm', polyfills);
if (target) {
exp[d] = {
types: target.replace(/\.js$/, '.d.ts'),
default: target,
};
}
}
}
if (reqTarget) {
for (const d of commonjsDialects) {
const target = getTargetForDialectCondition(s, d, d, 'commonjs', polyfills);
if (target) {
exp[d] = {
types: target.replace(/\.js$/, '.d.ts'),
default: target,
};
}
}
}
// put the default import/require after all the other special ones.
if (impTarget) {
exp.import = {

@@ -105,5 +123,5 @@ types: impTarget.replace(/\.(m?)js$/, '.d.$1ts'),

delete pkg.module;
pkg.exports = getExports(config, polyfills);
pkg.exports = getExports(config);
setMain(config, pkg);
export default pkg.exports;
//# sourceMappingURL=exports.js.map

@@ -1,3 +0,10 @@

declare const _default: Map<string, string>;
export default _default;
export declare class PolyfillSet {
type: 'esm' | 'commonjs';
name: string;
map: Map<string, string>;
constructor(type: 'esm' | 'commonjs', name: string);
addFile(f: string, sources: Set<string>): void;
}
declare const polyfills: Map<string, PolyfillSet>;
export default polyfills;
//# sourceMappingURL=polyfills.d.ts.map
// the modules like -cjs.cts that override a module at .ts
import chalk from 'chalk';
import config from './config.js';
import * as console from './console.js';
import sources from './sources.js';
import * as console from './console.js';
const target = (f, sources) => {
const ts = f.replace(/\-cjs\.cts$/, '.ts');
const tsx = f.replace(/\-cjs\.cts$/, '.tsx');
return sources.has(ts) ? ts : sources.has(tsx) ? tsx : undefined;
};
const getPolyfills = (sources) => new Map([...sources]
.filter(f => f.endsWith('-cjs.cts'))
.map(f => [f, target(f, sources)])
.filter(([_, target]) => !!target));
const polyfills = getPolyfills(sources);
const { esmDialects = [], commonjsDialects = [] } = config;
export class PolyfillSet {
type;
name;
map = new Map();
constructor(type, name) {
this.type = type;
this.name = name;
}
addFile(f, sources) {
const dotts = this.type === 'commonjs' ? 'cts' : 'mts';
const ending = `-${this.name}.${dotts}`;
if (!f.endsWith(ending))
return;
const ts = f.substring(0, f.length - ending.length) + '.ts';
const tsx = ts + 'x';
if (sources.has(ts))
this.map.set(f, ts);
else if (sources.has(tsx))
this.map.set(f, tsx);
}
[Symbol.for('nodejs.util.inspect.custom')]() {
return [this.name, this.map];
}
}
const polyfills = new Map([
['cjs', new PolyfillSet('commonjs', 'cjs')],
]);
for (const d of commonjsDialects)
polyfills.set(d, new PolyfillSet('commonjs', d));
for (const d of esmDialects)
polyfills.set(d, new PolyfillSet('esm', d));
for (const f of sources) {
for (const pf of polyfills.values()) {
pf.addFile(f, sources);
}
}
// delete any polyfill types that have no entries
for (const [name, pf] of polyfills.entries()) {
if (pf.map.size === 0)
polyfills.delete(name);
}
if (polyfills.size) {
console.debug(chalk.cyan.dim('polyfills detected'), polyfills);
}
export default getPolyfills(sources);
export default polyfills;
//# sourceMappingURL=polyfills.js.map

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

export declare const resolveExport: (exp: any, conditions: ('import' | 'require' | 'types')[]) => string | undefined | null;
export declare const resolveExport: (exp: any, conditions: string[]) => string | undefined | null;
//# sourceMappingURL=resolve-export.d.ts.map
import chalk from 'chalk';
import { existsSync, writeFileSync } from 'fs';
import { mkdirpSync } from 'mkdirp';
import { join } from 'node:path/posix';
import { existsSync, readdirSync, unlinkSync, writeFileSync, } from 'node:fs';
import { resolve } from 'node:path';
import * as console from './console.js';
// the commonjs build needs to exclude anything that will be polyfilled
import polyfills from './polyfills.js';
import config from './config.js';
const { dialects = ['esm', 'commonjs'], esmDialects = [], commonjsDialects = [], } = config;
const recommended = {

@@ -34,16 +38,38 @@ compilerOptions: {

};
const commonjs = {
extends: './build.json',
include: ['../src/**/*.ts', '../src/**/*.cts', '../src/**/*.tsx'],
exclude: ['../src/**/*.mts', ...polyfills.values()].map(f => `.${f}`),
compilerOptions: {
outDir: '../.tshy-build-tmp/commonjs',
},
const commonjs = (dialect) => {
const exclude = ['../src/**/*.mts'];
for (const [d, pf] of polyfills) {
if (d === dialect)
continue;
for (const f of pf.map.keys()) {
exclude.push(`../${join(f)}`);
}
}
return {
extends: './build.json',
include: ['../src/**/*.ts', '../src/**/*.cts', '../src/**/*.tsx'],
exclude,
compilerOptions: {
outDir: '../.tshy-build-tmp/' +
(dialect === 'cjs' ? 'commonjs' : dialect),
},
};
};
const esm = {
extends: './build.json',
include: ['../src/**/*.ts', '../src/**/*.mts', '../src/**/*.tsx'],
compilerOptions: {
outDir: '../.tshy-build-tmp/esm',
},
const esm = (dialect) => {
const exclude = [];
for (const [d, pf] of polyfills) {
if (d === dialect)
continue;
for (const f of pf.map.keys()) {
exclude.push(`../${f}`);
}
}
return {
extends: './build.json',
include: ['../src/**/*.ts', '../src/**/*.mts', '../src/**/*.tsx'],
exclude,
compilerOptions: {
outDir: '../.tshy-build-tmp/' + dialect,
},
};
};

@@ -57,5 +83,18 @@ mkdirpSync('.tshy');

}
for (const f of readdirSync('.tshy')) {
unlinkSync(resolve('.tshy', f));
}
writeConfig('build', build);
writeConfig('commonjs', commonjs);
writeConfig('esm', esm);
if (dialects.includes('commonjs')) {
writeConfig('commonjs', commonjs('cjs'));
for (const d of commonjsDialects) {
writeConfig(d, commonjs(d));
}
}
if (dialects.includes('esm')) {
writeConfig('esm', esm('esm'));
for (const d of esmDialects) {
writeConfig(d, esm(d));
}
}
//# sourceMappingURL=tsconfig.js.map

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

import type { ConditionalValue, ExportsSubpaths } from 'resolve-import';
export type TshyConfig = {

@@ -6,2 +7,4 @@ exports?: Record<string, TshyExport>;

main?: boolean;
commonjsDialects?: string[];
esmDialects?: string[];
};

@@ -13,19 +16,3 @@ export type Dialect = 'commonjs' | 'esm';

};
export type TshyExport = string | ({
types?: string;
import?: string;
require?: string;
} & ({
import: string;
} | {
require: string;
})) | ({
types?: string;
import?: ExportDetail;
require?: ExportDetail;
} & ({
import: ExportDetail;
} | {
require: ExportDetail;
}));
export type TshyExport = ConditionalValue;
export type Package = {

@@ -38,3 +25,3 @@ name: string;

bin?: string | Record<string, string>;
exports?: Record<string, Export>;
exports?: ExportsSubpaths;
tshy?: TshyConfig;

@@ -44,17 +31,2 @@ imports?: Record<string, any>;

};
export type Export = string | {
import?: Export;
require?: Export;
types?: Export;
default?: Export;
} | {
import?: string | {
types: string;
default: string;
};
require?: string | {
types: string;
default: string;
};
};
//# sourceMappingURL=types.d.ts.map

@@ -1,3 +0,3 @@

declare const _default: (e: any) => e is Record<string, import("./types.js").TshyExport>;
declare const _default: (e: any) => e is Record<string, import("resolve-import").ConditionalValue>;
export default _default;
//# sourceMappingURL=valid-exports.d.ts.map

@@ -18,4 +18,4 @@ import addDot from './add-dot.js';

if (typeof exp !== 'object') {
fail(`tshy.exports ${sub} value must be valid package.json exports value, ` +
`got: ${JSON.stringify(exp)}`);
fail(`tshy.exports ${sub} value must be valid package.json exports ` +
`value, got: ${JSON.stringify(exp)}`);
return process.exit(1);

@@ -22,0 +22,0 @@ }

@@ -1,4 +0,4 @@

import { Export } from './types.js';
declare const _default: (exp: any) => exp is Export;
import type { ConditionalValueObject } from 'resolve-import';
declare const _default: (exp: any) => exp is ConditionalValueObject;
export default _default;
//# sourceMappingURL=valid-external-export.d.ts.map
{
"name": "tshy",
"version": "1.3.0",
"version": "1.4.0",
"description": "TypeScript HYbridizer - Hybrid (CommonJS/ESM) TypeScript node package builder",

@@ -16,3 +16,3 @@ "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)",

"mkdirp": "^3.0.1",
"resolve-import": "^1.4.1",
"resolve-import": "^1.4.4",
"rimraf": "^5.0.1",

@@ -35,3 +35,4 @@ "sync-content": "^1.0.2",

"tap": {
"coverage-map": "map.js"
"coverage-map": "map.js",
"typecheck": true
},

@@ -53,3 +54,3 @@ "engines": {

"prettier": "^2.8.8",
"tap": "^18.0.3",
"tap": "^18.5.0",
"typedoc": "^0.25.1"

@@ -56,0 +57,0 @@ },

@@ -270,2 +270,80 @@ # tshy - TypeScript HYbridizer

## Other Targets: `browser`, `deno`, etc.
If you have any other dialects that you'd like to support, you
can list them as either `commonjsDialects` or `esmDialects`,
depending on whether you want them to be built as CommonJS or
ESM.
Note that each added dialect you create will result in another
build in the `./dist` folder, so you may wish to use sparingly if
shipping a large project.
For example:
```json
{
"tshy": {
"exports": {
".": "./src/index.ts"
},
"esmDialects": ["deno", "browser"],
"commonjsDialects": ["webpack"]
}
}
```
Will result in:
```json
{
"exports": {
".": {
"deno": {
"types": "./dist/deno/index.d.ts",
"default": "./dist/deno/index.js"
},
"browser": {
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"webpack": {
"types": "./dist/webpack/index.d.ts",
"default": "./dist/webpack/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
}
}
}
}
```
In each of these, you can use the same kind of dialect override
that works for CommonJS polyfills described above. For
`commonjsDialects` types, create a file named
`<filename>-<dialect>.cts`, and for `esmDialects` types, create a
file named `<filename>-<dialect>.mts`.
For example, to provide deno, browser, and webpack overrides in
the setup above, the following files would be relevant:
```
src/index.ts # normal esm/cjs version
src/index-cjs.cts # cjs variant for default commonjs
src/index-browser.mts # esm variant for the browser
src/index-deno.mts # esm variant for deno
src/index-webpack.cts # cjs variant for webpack
```
Note that the `commonjs` override uses the abbreviated `cjs`
name (historical reasons, it was originally the only override
supported), and that the file extension must be `cts` or `mts`
depending on the dialect type that it is.
## Atomic Builds

@@ -272,0 +350,0 @@

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

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