Skip to content

Commit

Permalink
define "source" export condition
Browse files Browse the repository at this point in the history
Thanks to @colinhacks for the idea.

Fix: #69
  • Loading branch information
isaacs committed Jun 7, 2024
1 parent d9cae01 commit a32205b
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 16 deletions.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,12 @@ appropriate build target locations, like:
"./foo": {
"import": {
"types": "./dist/esm/foo.d.ts",
"source": "./src/foo.ts",
"default": "./dist/esm/foo.js"
},
"require": {
"types": "./dist/commonjs/foo.d.ts",
"source": "./src/foo.ts",
"default": "./dist/commonjs/foo.js"
}
}
Expand Down Expand Up @@ -345,20 +347,24 @@ be:
".": {
"require": {
"types": "./dist/commonjs/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/commonjs/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/esm/index.js"
}
},
"./component/foo": {
"require": {
"types": "./dist/commonjs/component/foo.d.ts",
"source": "./src/component/foo.ts",
"default": "./dist/commonjs/component/foo.js"
},
"import": {
"types": "./dist/esm/component/foo.d.ts",
"source": "./src/component/foo.ts",
"default": "./dist/esm/component/foo.js"
}
},
Expand Down Expand Up @@ -404,6 +410,8 @@ your code. For this reason:
- Because it links files into place, a rebuild _is_ required when
a file is added or removed.
See also: "Loading from Source", below.
### Package `#imports`
You can use `"imports"` in your package.json, and it will be
Expand Down Expand Up @@ -597,10 +605,12 @@ will produce:
".": {
"require": {
"types": "./dist/commonjs/index.d.ts",
"source": "./src/index.js",
"default": "./dist/commonjs/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/esm/index.js"
}
}
Expand Down Expand Up @@ -705,22 +715,27 @@ Will result in:
".": {
"deno": {
"types": "./dist/deno/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/deno/index.js"
},
"browser": {
"types": "./dist/browser/index.d.ts",
"default": "./src/index.ts",
"default": "./dist/browser/index.js"
},
"webpack": {
"types": "./dist/webpack/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/webpack/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/commonjs/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/esm/index.js"
}
}
Expand All @@ -745,6 +760,44 @@ src/index-deno.mts # esm variant for deno
src/index-webpack.cts # cjs variant for webpack
```

If dialect overrides are used, then the `"source"` export
condition will refer to the original source for the override. For
example:

```json
{
"exports": {
".": {
"deno": {
"types": "./dist/deno/index.d.ts",
"source": "./src/index-deno.mts",
"default": "./dist/deno/index.js"
},
"browser": {
"types": "./dist/browser/index.d.ts",
"default": "./src/index-browser.mts",
"default": "./dist/browser/index.js"
},
"webpack": {
"types": "./dist/webpack/index.d.ts",
"source": "./src/index-webpack.cts",
"default": "./dist/webpack/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"source": "./src/index-cjs.cts",
"default": "./dist/commonjs/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/esm/index.js"
}
}
}
}
```

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`
Expand Down Expand Up @@ -801,6 +854,39 @@ provided for you.
Then the `tsconfig.json` file will be used as the default project
for code hints in VSCode, neovim, tests, etc.
### Loading from Source
If you are using tshy in a monorepo environment, you can
configure TypeScript (and other tools) to resolve to the _source_
files rather than built artifacts by telling them to use the
`"source"` export condition.
For example, in editors such as VS Code and neovim/CoC that use
the TypeScript language services, you can give them a `tsconfig`
that contains this:
```json
{
"compilerOptions": {
"customConditions": ["source"]
}
}
```
If you are loading your program with a custom Node.js importer
like [`tsx`](https://npm.im/tsx) that can load TypeScript
directly, you can specify it like this:
```bash
node --import=tsx --conditions=source ./script.ts
```
Other TypeScript-aware tools may have other mechanisms for
specifying export conditions. Refer to their documentation for
more information.
See also: "Live Dev", above.
### Custom `project`
Configure `tshy.project` if you want tshy to extend from a custom
Expand Down
27 changes: 23 additions & 4 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const getExports = (
const exp: ConditionalValueObject = (e[sub] = {})
if (impTarget) {
for (const d of esmDialects) {
const source = s && (polyfills.get(d)?.map.get(s) ?? s)

const target = getTargetForDialectCondition(
s,
d,
Expand All @@ -116,9 +118,13 @@ const getExports = (
)
if (target) {
exp[d] = liveDev
? target
? {
source,
default: target,
}
: {
types: target.replace(/\.js$/, '.d.ts'),
source,
default: target,
}
}
Expand All @@ -127,6 +133,7 @@ const getExports = (

if (reqTarget) {
for (const d of commonjsDialects) {
const source = s && (polyfills.get(d)?.map.get(s) ?? s)
const target = getTargetForDialectCondition(
s,
d,
Expand All @@ -136,9 +143,13 @@ const getExports = (
)
if (target) {
exp[d] = liveDev
? target
? {
source,
default: target,
}
: {
types: target.replace(/\.js$/, '.d.ts'),
source,
default: target,
}
}
Expand All @@ -147,17 +158,25 @@ const getExports = (
// put the default import/require after all the other special ones.
if (impTarget) {
exp.import = liveDev
? impTarget
? {
source: s,
default: impTarget,
}
: {
types: impTarget.replace(/\.(m?)js$/, '.d.$1ts'),
source: s,
default: impTarget,
}
}
if (reqTarget) {
exp.require = liveDev
? reqTarget
? {
source: s,
default: reqTarget,
}
: {
types: reqTarget.replace(/\.(c?)js$/, '.d.$1ts'),
source: s,
default: reqTarget,
}
}
Expand Down
Loading

0 comments on commit a32205b

Please sign in to comment.