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 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
- more than a year ago. However, some tools still rely on `main`
168
- and have not been updated to read the package entry points via
169
- `exports`.
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 either `commonjs` or `esm`. If `main` is set
173
- to `"commonjs"`, then the package will not have `"type":
174
- "module"` set.
180
+ by setting `main` to `true`.
175
181
 
176
- If the specified dialect is not built, or if a `"."` export is
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
- specified dialect, then the build will fail.
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": "commonjs"
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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A;;AAMD,wBAA0B"}
@@ -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 !== undefined) {
76
- if (!isDialect(c.main)) {
77
- fail(`config.main must be 'commonjs' or 'esm', got: ${c.main}`);
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 '${m}'`);
78
+ fail(`could not resolve exports['.'] for tshy.main 'require'`);
84
79
  return process.exit(1);
85
80
  }
86
- const types = resolveExport(pkg.exports['.'], [m, 'types']);
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;
@@ -1 +1 @@
1
- {"version":3,"file":"exports.js","sourceRoot":"","sources":["../../src/exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,QAAQ,MAAM,eAAe,CAAA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,CAAC,EAAE,OAAO,CAAC,CAAC,CAAA;QAC3D,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,OAAO,GAAG,CAAC,IAAI,CAAA;QACf,OAAO,GAAG,CAAC,KAAK,CAAA;KACjB;AACH,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'\nimport { isDialect } from './valid-dialects.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 !== undefined) {\n if (!isDialect(c.main)) {\n fail(`config.main must be 'commonjs' or 'esm', got: ${c.main}`)\n return process.exit(1)\n }\n const m = c.main === 'commonjs' ? 'require' : 'import'\n const mod = resolveExport(pkg.exports['.'], [m])\n if (!mod) {\n fail(`could not resolve exports['.'] for tshy.main '${m}'`)\n return process.exit(1)\n }\n const types = resolveExport(pkg.exports['.'], [m, 'types'])\n pkg.main = mod\n if (types && types !== mod) pkg.types = types\n else delete pkg.types\n } else {\n delete pkg.main\n delete pkg.types\n }\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"]}
1
+ {"version":3,"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,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"]}
@@ -2,7 +2,7 @@ export type TshyConfig = {
2
2
  exports?: Record<string, TshyExport>;
3
3
  dialects?: Dialect[];
4
4
  selfLink?: boolean;
5
- main?: Dialect;
5
+ main?: boolean;
6
6
  };
7
7
  export type Dialect = 'commonjs' | 'esm';
8
8
  export type ExportDetail = {
@@ -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?: Dialect\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"]}
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tshy",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "TypeScript HYbridizer - Hybrid (CommonJS/ESM) TypeScript node package builder",
5
5
  "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)",
6
6
  "license": "BlueOak-1.0.0",