Comparing version 1.13.1 to 1.14.0
import chalk from 'chalk'; | ||
import { spawnSync } from 'node:child_process'; | ||
import { renameSync, unlinkSync } from 'node:fs'; | ||
import { existsSync, renameSync, unlinkSync } from 'node:fs'; | ||
import { relative, resolve } from 'node:path'; | ||
@@ -12,2 +12,4 @@ import buildFail from './build-fail.js'; | ||
import tsc from './which-tsc.js'; | ||
const unlinkIfExist = (f) => existsSync(f) && unlinkSync(f); | ||
const renameIfExist = (f, to) => existsSync(f) && renameSync(f, to); | ||
const node = process.execPath; | ||
@@ -31,6 +33,6 @@ const { esmDialects = [] } = config; | ||
const stemTo = resolve(`.tshy-build/${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`); | ||
unlinkIfExist(`${stemTo}.js.map`); | ||
unlinkIfExist(`${stemTo}.d.ts.map`); | ||
renameIfExist(`${stemFrom}.mjs`, `${stemTo}.js`); | ||
renameIfExist(`${stemFrom}.d.mts`, `${stemTo}.d.ts`); | ||
} | ||
@@ -37,0 +39,0 @@ console.error(chalk.cyan.bold('built ' + d)); |
// get the config and package and stuff | ||
import chalk from 'chalk'; | ||
import { Minimatch } from 'minimatch'; | ||
import * as console from './console.js'; | ||
@@ -20,5 +21,9 @@ import fail from './fail.js'; | ||
}; | ||
const isStringArray = (e) => !!e && Array.isArray(e) && !e.some(e => typeof e !== 'string'); | ||
const validConfig = (e) => !!e && | ||
typeof e === 'object' && | ||
(e.exports === undefined || validExports(e.exports)) && | ||
(e.exports === undefined || | ||
typeof e.exports === 'string' || | ||
isStringArray(e.exports) || | ||
validExports(e.exports)) && | ||
(e.dialects === undefined || validDialects(e.dialects)) && | ||
@@ -30,5 +35,42 @@ (e.project === undefined || validProject(e.project)) && | ||
validBoolean(e, 'main'); | ||
const match = (e, pattern) => pattern.some(m => m.match(e)); | ||
const parsePattern = (p) => Array.isArray(p) | ||
? p.map(p => new Minimatch(p.replace(/^\.\//, ''))) | ||
: parsePattern([p]); | ||
const getConfig = (pkg, sources) => { | ||
const tshy = validConfig(pkg.tshy) ? pkg.tshy : {}; | ||
const ti = tshy; | ||
const tshy = validConfig(pkg.tshy) | ||
? pkg.tshy | ||
: {}; | ||
const exportsRaw = tshy.exports; | ||
if (typeof exportsRaw === 'string' || Array.isArray(exportsRaw)) { | ||
// Strip off the `./src` prefix and the extension | ||
// exports: "src/**/*.ts" => exports: {"./foo": "./src/foo.ts"} | ||
const exp = {}; | ||
const pattern = exportsRaw; | ||
const m = parsePattern(pattern); | ||
for (const e of sources) { | ||
if (!match(e.replace(/^\.\//, ''), m)) | ||
continue; | ||
// index is main, anything else is a subpath | ||
const sp = /^\.\/src\/index.([mc]?[jt]s|[jt]sx)$/.test(e) | ||
? '.' | ||
: './' + | ||
e | ||
.replace(/^\.\/src\//, '') | ||
.replace(/\.([mc]?[tj]s|[jt]sx)$/, ''); | ||
exp[sp] = `./${e}`; | ||
} | ||
/* c8 ignore start - should be impossible */ | ||
if (!validExports(exp)) { | ||
console.error('invalid exports pattern, using default exports'); | ||
delete tshy.exports; | ||
} | ||
else { | ||
/* c8 ignore stop */ | ||
exp['./package.json'] = './package.json'; | ||
tshy.exports = exp; | ||
} | ||
} | ||
const config = tshy; | ||
const ti = config; | ||
if (ti.imports) { | ||
@@ -44,5 +86,5 @@ console.debug(chalk.cyan.dim('imports') + | ||
validImports(pkg); | ||
pkg.tshy = tshy; | ||
pkg.tshy = config; | ||
if (tshy.exports) | ||
return tshy; | ||
return config; | ||
const e = { | ||
@@ -57,4 +99,4 @@ './package.json': './package.json', | ||
} | ||
tshy.exports = e; | ||
return tshy; | ||
config.exports = e; | ||
return config; | ||
}; | ||
@@ -61,0 +103,0 @@ const config = getConfig(pkg, sources); |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { ExportsSubpaths } from 'resolve-import'; | ||
@@ -3,0 +2,0 @@ import type { PolyfillSet } from './polyfills.js'; |
import type { ConditionalValue, ExportsSubpaths, Imports } from 'resolve-import'; | ||
export type TshyConfig = { | ||
exports?: Record<string, TshyExport>; | ||
export type TshyConfigMaybeGlobExports = { | ||
exports?: string | string[] | Record<string, TshyExport>; | ||
dialects?: Dialect[]; | ||
@@ -12,2 +12,5 @@ selfLink?: boolean; | ||
}; | ||
export type TshyConfig = TshyConfigMaybeGlobExports & { | ||
exports?: Record<string, TshyExport>; | ||
}; | ||
export type Dialect = 'commonjs' | 'esm'; | ||
@@ -27,3 +30,3 @@ export type ExportDetail = { | ||
exports?: ExportsSubpaths; | ||
tshy?: TshyConfig; | ||
tshy?: TshyConfigMaybeGlobExports; | ||
imports?: Imports; | ||
@@ -30,0 +33,0 @@ [k: string]: any; |
@@ -1,3 +0,3 @@ | ||
declare const _default: (e: any) => e is Record<string, import("resolve-import").ConditionalValue>; | ||
declare const _default: (e: any) => e is (string | string[] | Record<string, import("resolve-import").ConditionalValue>) & Record<string, import("resolve-import").ConditionalValue>; | ||
export default _default; | ||
//# sourceMappingURL=valid-exports.d.ts.map |
{ | ||
"name": "tshy", | ||
"version": "1.13.1", | ||
"version": "1.14.0", | ||
"description": "TypeScript HYbridizer - Hybrid (CommonJS/ESM) TypeScript node package builder", | ||
@@ -14,10 +14,11 @@ "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)", | ||
"chalk": "^5.3.0", | ||
"chokidar": "^3.5.3", | ||
"chokidar": "^3.6.0", | ||
"foreground-child": "^3.1.1", | ||
"minimatch": "^9.0.4", | ||
"mkdirp": "^3.0.1", | ||
"polite-json": "^4.0.1", | ||
"resolve-import": "^1.4.4", | ||
"resolve-import": "^1.4.5", | ||
"rimraf": "^5.0.1", | ||
"sync-content": "^1.0.2", | ||
"typescript": "5.2 || 5.3", | ||
"typescript": "^5.4.5", | ||
"walk-up-path": "^3.0.1" | ||
@@ -52,6 +53,6 @@ }, | ||
"devDependencies": { | ||
"@types/node": "^20.8.6", | ||
"@types/node": "^20.12.7", | ||
"prettier": "^2.8.8", | ||
"tap": "^18.5.2", | ||
"typedoc": "^0.25.7" | ||
"tap": "^18.7.2", | ||
"typedoc": "^0.25.13" | ||
}, | ||
@@ -58,0 +59,0 @@ "prettier": { |
@@ -7,3 +7,3 @@ # tshy - TypeScript HYbridizer | ||
This tool manages the `exports` in your package.json file, and | ||
builds your TypeScript program using `tsc` 5.2, emitting both ESM | ||
builds your TypeScript program using `tsc` 5.2+, emitting both ESM | ||
and CommonJS variants, [providing the full strength of | ||
@@ -318,2 +318,72 @@ TypeScript’s checking for both output | ||
### Glob Exports | ||
You can also specify one or more [glob](http://npm.im/glob) | ||
patterns to define your exported modules. This is handy if you | ||
want to export several things as subpath exports to avoid "bucket | ||
modules". | ||
Anything named `src/index.*` that is matched in this way will be | ||
used as the main `"."` export. Anything else will have the | ||
`./src` stripped from the front, and the file extension removed | ||
from the end. `./package.json` will always be exported, and any | ||
pattern matches outside of the `./src` folder will be ignored. | ||
```json | ||
{ | ||
"tshy": { | ||
"exports": "./src/**" | ||
} | ||
} | ||
``` | ||
If you use this config, and you have files at `./src/index.ts` | ||
and `./src/component/foo.tsx`, then the resulting exports will | ||
be: | ||
```json | ||
{ | ||
"exports": { | ||
".": { | ||
"require": { | ||
"types": "./dist/commonjs/index.d.ts", | ||
"default": "./dist/commonjs/index.js" | ||
}, | ||
"import": { | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
} | ||
}, | ||
"./component/foo": { | ||
"require": { | ||
"types": "./dist/commonjs/component/foo.d.ts", | ||
"default": "./dist/commonjs/component/foo.js" | ||
}, | ||
"import": { | ||
"types": "./dist/esm/component/foo.d.ts", | ||
"default": "./dist/esm/component/foo.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
} | ||
} | ||
``` | ||
You may also specify an array of glob exports, like so: | ||
```json | ||
{ | ||
"tshy": { | ||
"exports": [ | ||
"./src/*.ts", | ||
"./src/utils/*.ts" | ||
] | ||
} | ||
} | ||
``` | ||
This would export a file at `./src/foo.ts` as `./foo`, and a file | ||
at `./src/utils/bar.ts` as `./utils/bar`, but would ignore a file | ||
at `./internal/private.ts`. | ||
### Package `#imports` | ||
@@ -320,0 +390,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
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
187634
1440
906
11
+ Addedminimatch@^9.0.4
+ Addedtypescript@5.6.3(transitive)
- Removedtypescript@5.3.3(transitive)
Updatedchokidar@^3.6.0
Updatedresolve-import@^1.4.5
Updatedtypescript@^5.4.5