Comparing version 1.0.0 to 1.1.0
@@ -5,4 +5,9 @@ import * as console from './console.js'; | ||
import './tsconfig.js'; | ||
import { unlink as unlinkImports } from './imports.js'; | ||
import { unlink as unlinkSelfDep } from './self-dep.js'; | ||
import pkg from './package.js'; | ||
export default (res) => { | ||
setFolderDialect('src'); | ||
unlinkImports(pkg, 'src'); | ||
unlinkSelfDep(pkg, 'src'); | ||
fail('build failed'); | ||
@@ -9,0 +14,0 @@ console.error(res); |
import './tsconfig.js'; | ||
declare const _default: () => void; | ||
declare const _default: () => Promise<void>; | ||
export default _default; | ||
//# sourceMappingURL=build.d.ts.map |
@@ -9,6 +9,11 @@ import chalk from 'chalk'; | ||
import dialects from './dialects.js'; | ||
import { link as linkImports, save as saveImports, unlink as unlinkImports, } from './imports.js'; | ||
import pkg from './package.js'; | ||
import { link as linkSelfDep, unlink as unlinkSelfDep, } from './self-dep.js'; | ||
import './tsconfig.js'; | ||
import writePackage from './write-package.js'; | ||
export default () => { | ||
export default async () => { | ||
rimrafSync('.tshy-build-tmp'); | ||
linkSelfDep(pkg, 'src'); | ||
await linkImports(pkg, 'src'); | ||
if (dialects.includes('esm')) | ||
@@ -18,2 +23,4 @@ buildESM(); | ||
buildCommonJS(); | ||
await unlinkImports(pkg, 'src'); | ||
unlinkSelfDep(pkg, 'src'); | ||
console.debug(chalk.cyan.dim('moving to ./dist')); | ||
@@ -23,2 +30,16 @@ syncContentSync('.tshy-build-tmp', 'dist'); | ||
rimrafSync('.tshy-build-tmp'); | ||
linkSelfDep(pkg, 'dist'); | ||
if (pkg.imports) { | ||
console.debug('linking package imports', pkg.imports); | ||
if (dialects.includes('commonjs')) | ||
await linkImports(pkg, 'dist/commonjs', true); | ||
if (dialects.includes('esm')) | ||
await linkImports(pkg, 'dist/esm', true); | ||
if (saveImports('dist/.tshy-link-imports.mjs')) { | ||
pkg.scripts = pkg.scripts || {}; | ||
pkg.scripts.preinstall = | ||
'node -e "import(process.argv[1]).catch(()=>{})" ' + | ||
'dist/.tshy-link-imports.mjs'; | ||
} | ||
} | ||
console.debug(chalk.cyan.dim('chmod bins')); | ||
@@ -25,0 +46,0 @@ bins(); |
@@ -11,4 +11,4 @@ #!/usr/bin/env node | ||
console.debug(chalk.cyan.dim('exports'), exp); | ||
build(); | ||
await build(); | ||
console.log(chalk.bold.green('success!')); | ||
//# sourceMappingURL=index.js.map |
@@ -0,15 +1,21 @@ | ||
import chalk from 'chalk'; | ||
import { writeFileSync } from 'fs'; | ||
import { rimrafSync } from 'rimraf'; | ||
import * as console from './console.js'; | ||
import chalk from 'chalk'; | ||
const writeDialectPJ = (f, mode) => mode | ||
? writeFileSync(f, JSON.stringify({ | ||
import pkg from './package.js'; | ||
const writeDialectPJ = (d, mode) => { | ||
if (!mode) { | ||
return rimrafSync(`${d}/package.json`); | ||
} | ||
const v = { | ||
type: mode === 'commonjs' ? 'commonjs' : 'module', | ||
})) | ||
: rimrafSync(f); | ||
imports: pkg.imports, | ||
}; | ||
writeFileSync(`${d}/package.json`, JSON.stringify(v)); | ||
}; | ||
export default (where, mode) => { | ||
if (mode) | ||
console.debug(chalk.cyan.dim('set dialect'), { where, mode }); | ||
writeDialectPJ(`${where}/package.json`, mode); | ||
writeDialectPJ(where, mode); | ||
}; | ||
//# sourceMappingURL=set-folder-dialect.js.map |
@@ -34,2 +34,3 @@ export type TshyConfig = { | ||
tshy?: TshyConfig; | ||
imports?: Record<string, any>; | ||
[k: string]: any; | ||
@@ -36,0 +37,0 @@ }; |
{ | ||
"name": "tshy", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "TypeScript HYbridizer - Hybrid (CommonJS/ESM) TypeScript node package builder", | ||
@@ -16,2 +16,3 @@ "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)", | ||
"mkdirp": "^3.0.1", | ||
"resolve-import": "^1.4.1", | ||
"rimraf": "^5.0.1", | ||
@@ -24,3 +25,3 @@ "sync-content": "^1.0.2", | ||
"prepublishOnly": "git push origin --follow-tags", | ||
"prepare": "tsc -p tsconfig/esm.json && bash scripts/fixup.sh", | ||
"prepare": "tsc -p .tshy/esm.json && bash scripts/fixup.sh", | ||
"pretest": "npm run prepare", | ||
@@ -51,3 +52,3 @@ "presnap": "npm run prepare", | ||
"prettier": "^2.8.8", | ||
"tap": "^18.0.0-26", | ||
"tap": "^18.0.3", | ||
"typedoc": "^0.25.1" | ||
@@ -54,0 +55,0 @@ }, |
134
README.md
@@ -105,3 +105,3 @@ # tshy - TypeScript HYbridizer | ||
{ "browser": "./browser-thing.js" }, | ||
{ "require": [{ "types": "./using-require.d.ts" }, "./using-require.js"], | ||
{ "require": [{ "types": "./using-require.d.ts" }, "./using-require.js"]}, | ||
{ "types": "./blah.d.ts" }, | ||
@@ -218,13 +218,2 @@ "./etc.js" | ||
## Package `#imports` | ||
Using the `imports` field in `package.json` is not currently | ||
supported, because this looks at the nearest `package.json` to | ||
get local imports, and the package.json files placed in | ||
`dist/{commonjs,esm}` can't have local imports outside of their | ||
folders. | ||
There's a way it could theoretically be done, but it's a bit | ||
complicated. A future version may support this. | ||
## TSConfigs | ||
@@ -262,1 +251,122 @@ | ||
destroyed. | ||
## Package `#imports` | ||
If you use `"imports"` in your package.json, then tshy will set | ||
`scripts.preinstall` to set up some symbolic links to make it | ||
work. This just means you can't use `scripts.preinstall` for | ||
anything else if you use `"imports"`. | ||
<details> | ||
<summary>tl;dr explanation</summary> | ||
The `"imports"` field in package.json allows you to set local | ||
package imports, which have the same kind of conditional import | ||
logic as `"exports"`. This is especially useful when you have a | ||
vendored dependency with `require` and `import` variants, modules | ||
that have to be bundled in different ways for different | ||
environments, or different dependencies for different | ||
environments. | ||
These package imports are _always_ resolved against the nearest | ||
`package.json` file, and tshy uses generated package.json files | ||
to set the module dialect to `"type":"module"` in `dist/esm` and | ||
`"type":"commonjs"` in `dist/commonjs`, and it swaps the | ||
`src/package.json` file between this during the `tsc` builds. | ||
Furthermore, local package imports may not be relative files | ||
outside the package folder. They may only be local files within | ||
the local package, or dependencies resolved in `node_modules`. | ||
To support this, tshy copies the `imports` field from the | ||
project's package.json into these dialect-setting generated | ||
package.json files, and creates symlinks into the appropriate | ||
places so that they resolve to the same files on disk. | ||
Because symlinks may not be included in npm packages (and even if | ||
they are included, they won't be unpacked at install time), the | ||
symlinks it places in `./dist` wouldn't do much good. In order to | ||
work around _this_ restriction, tshy creates a node program at | ||
`dist/.tshy-link-imports.mjs`, which generates the symlinks at | ||
install time via the `preinstall` script. | ||
</details> | ||
## Local Package `exports` | ||
In order to facilitate local package exports, tshy will create a | ||
symlink to the current package temporarily in | ||
`./src/node_modules` and permanently in `./dist/node_modules`. | ||
If you rely on this feature, you may need to add a `paths` | ||
section to your `tsconfig.json` so that you don't get nagged | ||
constantly by your editor about missing type references. | ||
<details> | ||
<summary>tl;dr explanation</summary> | ||
Similar to local module imports, Node supports importing the | ||
`exports` of the current package as if it was a dependency of | ||
itself. The generated `package.json` files mess with this similar | ||
to `imports`, but it's much easier to work around. | ||
For example, if you had this in your package.json: | ||
```json | ||
{ | ||
"name": "@my/package", | ||
"exports": { | ||
"./foo": { | ||
"import": "./lib/foo.mjs", | ||
"require": "./lib/foo.cjs" | ||
} | ||
} | ||
} | ||
``` | ||
Then any module in the package could do | ||
`import('@my/package/foo')` or `require('@my/package/foo')` to | ||
pull in the appropriate file. | ||
In order to make this wort, tshy links the current project | ||
directory into `./src/node_modules/<pkgname>` during the builds, | ||
and removes the link afterwards, so that TypeScript knows what | ||
those things refer to. | ||
The link is also created in the `dist` folder, but it's only | ||
relevant if your tests load the code from `./dist` rather than | ||
from `./src`. In the install, there's no need to re-create this | ||
link, because the package will be in a `node_modules` folder | ||
already. | ||
If you use this feature, you can put something like this in your | ||
`tsconfig.json` file so that your editor knows what those things | ||
refer to: | ||
```json | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@my/package/foo": ["./src/foo.js"], | ||
"@my/package/bar": ["./src/bar.js"] | ||
} | ||
} | ||
} | ||
``` | ||
Note the `.js` extension, rather than `.ts`. Add this for each | ||
submodule path that you use in this way, or use a wildcard if you | ||
prefer, though this might result in failing to catch errors if | ||
you use a submodule identifier that isn't actually exported: | ||
```json | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@my/package/*": ["./src/*.js"] | ||
} | ||
} | ||
} | ||
``` | ||
</details> |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
96947
103
740
370
7
10
+ Addedresolve-import@^1.4.1
+ Addedresolve-import@1.4.6(transitive)
+ Addedwalk-up-path@3.0.1(transitive)