tshy 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/dist/esm/exports.d.ts.map +1 -1
- package/dist/esm/exports.js +11 -10
- package/dist/esm/exports.js.map +1 -1
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -164,18 +164,24 @@ package entry points.
|
|
|
164
164
|
|
|
165
165
|
By default, tshy deletes the `main` field, rather than maintain
|
|
166
166
|
this affordance for versions of node that met their end of life
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
167
|
+
long ago. However, some tools still rely on `main` and have not
|
|
168
|
+
been updated to read the package entry points via `exports`.
|
|
169
|
+
|
|
170
|
+
**Warning: this will likely cause incorrect types to be loaded in
|
|
171
|
+
some scenarios.**
|
|
172
|
+
|
|
173
|
+
Use with extreme caution. It's almost always better to _not_
|
|
174
|
+
define top-level `main` and `types` fields if you are shipping a
|
|
175
|
+
hybrid module. Users will need to update their `module` and
|
|
176
|
+
`moduleResolution` tsconfigs appropriately. **That is a good
|
|
177
|
+
thing, and will save them future headaches.**
|
|
170
178
|
|
|
171
179
|
You can tell tshy to export a top-level `main` and `types` field
|
|
172
|
-
by setting `main` to
|
|
173
|
-
to `"commonjs"`, then the package will not have `"type":
|
|
174
|
-
"module"` set.
|
|
180
|
+
by setting `main` to `true`.
|
|
175
181
|
|
|
176
|
-
If the
|
|
182
|
+
If the `commonjs` dialect is not built, or if a `"."` export is
|
|
177
183
|
not created, or if the `"."` export does not support the
|
|
178
|
-
|
|
184
|
+
`commonjs` dialect, then the build will fail.
|
|
179
185
|
|
|
180
186
|
For example, this config:
|
|
181
187
|
|
|
@@ -185,7 +191,7 @@ For example, this config:
|
|
|
185
191
|
"exports": {
|
|
186
192
|
".": "./src/index.ts"
|
|
187
193
|
},
|
|
188
|
-
"main":
|
|
194
|
+
"main": true
|
|
189
195
|
}
|
|
190
196
|
}
|
|
191
197
|
```
|
|
@@ -196,6 +202,7 @@ will produce:
|
|
|
196
202
|
{
|
|
197
203
|
"main": "./dist/commonjs/index.js",
|
|
198
204
|
"types": "./dist/commonjs/index.d.ts",
|
|
205
|
+
"type": "module",
|
|
199
206
|
"exports": {
|
|
200
207
|
".": {
|
|
201
208
|
"require": {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exports.d.ts","sourceRoot":"","sources":["../../src/exports.ts"],"names":[],"mappings":";AAOA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"exports.d.ts","sourceRoot":"","sources":["../../src/exports.ts"],"names":[],"mappings":";AAOA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEpE,eAAO,MAAM,YAAY,MACpB,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,KACxC,MAAM,GAAG,SAAS,GAAG,IAcvB,CAAA;AAED,eAAO,MAAM,YAAY,MACpB,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,aAC9B,IAAI,MAAM,EAAE,MAAM,CAAC,KAC7B,MAAM,GAAG,IAAI,GAAG,SAclB,CAAA;AAED,eAAO,MAAM,UAAU,MAClB,UAAU,aACF,IAAI,MAAM,EAAE,MAAM,CAAC,KAC7B,OAAO,MAAM,EAAE,MAAM,CAwCvB,CAAA;AAED,eAAO,MAAM,OAAO,MACf,UAAU,GAAG,SAAS,OACpB,OAAO,GAAG;IAAE,SAAS,OAAO,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,cAsBnD,CAAA;;AAMD,wBAA0B"}
|
package/dist/esm/exports.js
CHANGED
|
@@ -5,7 +5,6 @@ import fail from './fail.js';
|
|
|
5
5
|
import pkg from './package.js';
|
|
6
6
|
import polyfills from './polyfills.js';
|
|
7
7
|
import { resolveExport } from './resolve-export.js';
|
|
8
|
-
import { isDialect } from './valid-dialects.js';
|
|
9
8
|
export const getImpTarget = (s) => {
|
|
10
9
|
if (s === undefined)
|
|
11
10
|
return undefined;
|
|
@@ -72,18 +71,17 @@ export const getExports = (c, polyfills) => {
|
|
|
72
71
|
return e;
|
|
73
72
|
};
|
|
74
73
|
export const setMain = (c, pkg) => {
|
|
75
|
-
if (c?.main
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return process.exit(1);
|
|
79
|
-
}
|
|
80
|
-
const m = c.main === 'commonjs' ? 'require' : 'import';
|
|
81
|
-
const mod = resolveExport(pkg.exports['.'], [m]);
|
|
74
|
+
if (!!c?.main) {
|
|
75
|
+
c.main = true;
|
|
76
|
+
const mod = resolveExport(pkg.exports['.'], ['require']);
|
|
82
77
|
if (!mod) {
|
|
83
|
-
fail(`could not resolve exports['.'] for tshy.main '
|
|
78
|
+
fail(`could not resolve exports['.'] for tshy.main 'require'`);
|
|
84
79
|
return process.exit(1);
|
|
85
80
|
}
|
|
86
|
-
const types = resolveExport(pkg.exports['.'], [
|
|
81
|
+
const types = resolveExport(pkg.exports['.'], [
|
|
82
|
+
'require',
|
|
83
|
+
'types',
|
|
84
|
+
]);
|
|
87
85
|
pkg.main = mod;
|
|
88
86
|
if (types && types !== mod)
|
|
89
87
|
pkg.types = types;
|
|
@@ -91,9 +89,12 @@ export const setMain = (c, pkg) => {
|
|
|
91
89
|
delete pkg.types;
|
|
92
90
|
}
|
|
93
91
|
else {
|
|
92
|
+
if (c)
|
|
93
|
+
delete c.main;
|
|
94
94
|
delete pkg.main;
|
|
95
95
|
delete pkg.types;
|
|
96
96
|
}
|
|
97
|
+
pkg.type = 'module';
|
|
97
98
|
};
|
|
98
99
|
// These are all defined by exports, so it's just confusing otherwise
|
|
99
100
|
delete pkg.module;
|
package/dist/esm/exports.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exports.js","sourceRoot":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AAC,CAAC,CAAA;QACxD,IAAI,CAAC,GAAG,EAAE;YACR,IAAI,CAAC,wDAAwD,CAAC,CAAA;YAC9D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;SACvB;QACD,MAAM,KAAK,GAAG,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YAC5C,SAAS;YACT,OAAO;SACR,CAAC,CAAA;QACF,GAAG,CAAC,IAAI,GAAG,GAAG,CAAA;QACd,IAAI,KAAK,IAAI,KAAK,KAAK,GAAG;YAAE,GAAG,CAAC,KAAK,GAAG,KAAK,CAAA;;YACxC,OAAO,GAAG,CAAC,KAAK,CAAA;KACtB;SAAM;QACL,IAAI,CAAC;YAAE,OAAO,CAAC,CAAC,IAAI,CAAA;QACpB,OAAO,GAAG,CAAC,IAAI,CAAA;QACf,OAAO,GAAG,CAAC,KAAK,CAAA;KACjB;IACD,GAAG,CAAC,IAAI,GAAG,QAAQ,CAAA;AACrB,CAAC,CAAA;AAED,qEAAqE;AACrE,OAAO,GAAG,CAAC,MAAM,CAAA;AACjB,GAAG,CAAC,OAAO,GAAG,UAAU,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAC3C,OAAO,CAAC,MAAM,EAAE,GAAoD,CAAC,CAAA;AACrE,eAAe,GAAG,CAAC,OAAO,CAAA","sourcesContent":["import { relative, resolve } from 'node:path/posix'\nimport config from './config.js'\nimport dialects from './dialects.js'\nimport fail from './fail.js'\nimport pkg from './package.js'\nimport polyfills from './polyfills.js'\nimport { resolveExport } from './resolve-export.js'\nimport { Export, Package, TshyConfig, TshyExport } from './types.js'\n\nexport const getImpTarget = (\n s: string | TshyExport | undefined | null\n): string | undefined | null => {\n if (s === undefined) return undefined\n if (typeof s === 'string') {\n const imp = s.endsWith('.cts') ? undefined : s\n return !imp || !imp.startsWith('./src/')\n ? imp\n : dialects.includes('esm')\n ? `./dist/esm/${relative(\n resolve('./src'),\n resolve(imp)\n ).replace(/\\.(m?)tsx?$/, '.$1js')}`\n : undefined\n }\n return resolveExport(s, ['import'])\n}\n\nexport const getReqTarget = (\n s: string | TshyExport | undefined | null,\n polyfills: Map<string, string>\n): string | null | undefined => {\n if (s === undefined) return undefined\n if (typeof s === 'string') {\n const req = s.endsWith('.mts') ? undefined : s\n return !req || !req.startsWith('./src/')\n ? req\n : dialects.includes('commonjs')\n ? `./dist/commonjs/${relative(\n resolve('./src'),\n resolve(polyfills.get(req) || req)\n ).replace(/\\.(c?)tsx?$/, '.$1js')}`\n : undefined\n }\n return getReqTarget(resolveExport(s, ['require']), polyfills)\n}\n\nexport const getExports = (\n c: TshyConfig,\n polyfills: Map<string, string>\n): Record<string, Export> => {\n // by this time it always exports, will get the default if missing\n /* c8 ignore start */\n if (!c.exports) {\n fail('no exports on tshy config (is there code in ./src?)')\n return process.exit(1)\n }\n /* c8 ignore stop */\n const e: Record<string, Export> = {}\n for (const [sub, s] of Object.entries(c.exports)) {\n // external export, not built by us\n if (typeof s !== 'string' || !s.startsWith('./src/')) {\n // already been validated, just accept as-is\n e[sub] = s as Export\n continue\n }\n\n const impTarget = getImpTarget(s)\n const reqTarget = getReqTarget(s, polyfills)\n\n // should be impossible\n /* c8 ignore start */\n if (!impTarget && !reqTarget) continue\n /* c8 ignore stop */\n\n const exp: Export = (e[sub] = {})\n if (impTarget) {\n exp.import = {\n types: impTarget.replace(/\\.(m?)js$/, '.d.$1ts'),\n default: impTarget,\n }\n }\n if (reqTarget) {\n exp.require = {\n types: reqTarget.replace(/\\.(c?)js$/, '.d.$1ts'),\n default: reqTarget,\n }\n }\n }\n return e\n}\n\nexport const setMain = (\n c: TshyConfig | undefined,\n pkg: Package & { exports: Record<string, Export> }\n) => {\n if (!!c?.main) {\n c.main = true\n const mod = resolveExport(pkg.exports['.'], ['require'])\n if (!mod) {\n fail(`could not resolve exports['.'] for tshy.main 'require'`)\n return process.exit(1)\n }\n const types = resolveExport(pkg.exports['.'], [\n 'require',\n 'types',\n ])\n pkg.main = mod\n if (types && types !== mod) pkg.types = types\n else delete pkg.types\n } else {\n if (c) delete c.main\n delete pkg.main\n delete pkg.types\n }\n pkg.type = 'module'\n}\n\n// These are all defined by exports, so it's just confusing otherwise\ndelete pkg.module\npkg.exports = getExports(config, polyfills)\nsetMain(config, pkg as Package & { exports: Record<string, Export> })\nexport default pkg.exports\n"]}
|
package/dist/esm/types.d.ts
CHANGED
package/dist/esm/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type TshyConfig = {\n exports?: Record<string, TshyExport>\n dialects?: Dialect[]\n selfLink?: boolean\n main?:
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type TshyConfig = {\n exports?: Record<string, TshyExport>\n dialects?: Dialect[]\n selfLink?: boolean\n main?: boolean\n}\n\nexport type Dialect = 'commonjs' | 'esm'\n\nexport type ExportDetail = {\n default: string\n [k: string]: string\n}\n\nexport type TshyExport =\n | string\n | ({ types?: string; import?: string; require?: string } & (\n | { import: string }\n | { require: string }\n ))\n | ({\n types?: string\n import?: ExportDetail\n require?: ExportDetail\n } & ({ import: ExportDetail } | { require: ExportDetail }))\n\nexport type Package = {\n name: string\n version: string\n main?: string\n types?: string\n type?: 'module'\n bin?: string | Record<string, string>\n exports?: Record<string, Export>\n tshy?: TshyConfig\n imports?: Record<string, any>\n [k: string]: any\n}\n\n// VERY limited subset of the datatypes \"exports\" can be\n// but we're only writing our flavor, so it's fine.\nexport type Export =\n | string\n | {\n import?: Export\n require?: Export\n types?: Export\n default?: Export\n }\n | {\n import?:\n | string\n | {\n types: string\n default: string\n }\n require?:\n | string\n | {\n types: string\n default: string\n }\n }\n"]}
|
package/package.json
CHANGED