@knighted/duel 3.2.3 → 3.2.4

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
@@ -53,7 +53,7 @@ And then running it:
53
53
  npm run build
54
54
  ```
55
55
 
56
- If everything worked, you should have an ESM build inside of `dist` and a CJS build inside of `dist/cjs`. Now you can update your [`exports`](https://nodejs.org/api/packages.html#exports) to match the build output.
56
+ If everything worked, you should have an ESM build inside of `dist` and a CJS build inside of `dist/cjs`. You can manually update your [`exports`](https://nodejs.org/api/packages.html#exports) to match the build output, or run `duel --exports <mode>` to generate them automatically (see [docs/exports.md](docs/exports.md)).
57
57
 
58
58
  It should work similarly for a CJS-first project. Except, your package.json file would use `"type": "commonjs"` and the dual build directory is in `dist/esm`.
59
59
 
package/dist/cjs/duel.cjs CHANGED
@@ -27,7 +27,7 @@ const getSubpath = (mode, relFromRoot) => {
27
27
  }
28
28
  if (mode === 'dir') {
29
29
  const last = segments.at(-1);
30
- return last ? `./${last}` : null;
30
+ return last ? `./${last}/*` : null;
31
31
  }
32
32
  if (mode === 'wildcard') {
33
33
  const first = segments[0];
@@ -56,6 +56,17 @@ const generateExports = async (options) => {
56
56
  if (esmRootPosix.startsWith(`${cjsRootPosix}/`)) {
57
57
  cjsIgnore.push(`${esmRootPosix}/**`);
58
58
  }
59
+ const toWildcardValue = value => {
60
+ const dir = node_path_1.posix.dirname(value);
61
+ const file = node_path_1.posix.basename(value);
62
+ const dtsMatch = file.match(/(\.d\.(?:ts|mts|cts))$/i);
63
+ if (dtsMatch) {
64
+ const ext = dtsMatch[1];
65
+ return dir === '.' ? `./*${ext}` : `${dir}/*${ext}`;
66
+ }
67
+ const ext = node_path_1.posix.extname(file);
68
+ return dir === '.' ? `./*${ext}` : `${dir}/*${ext}`;
69
+ };
59
70
  const recordPath = (kind, filePath, root) => {
60
71
  const relPkg = toPosix((0, node_path_1.relative)(pkgDir, filePath));
61
72
  const relFromRoot = toPosix((0, node_path_1.relative)(root, filePath));
@@ -65,18 +76,19 @@ const generateExports = async (options) => {
65
76
  baseEntry[kind] = withDot;
66
77
  baseMap.set(baseKey, baseEntry);
67
78
  const subpath = getSubpath(mode, relFromRoot);
79
+ const useWildcard = subpath?.includes('*');
68
80
  if (kind === 'types') {
69
81
  const mappedSubpath = baseToSubpath.get(baseKey);
70
82
  if (mappedSubpath) {
71
83
  const subEntry = subpathMap.get(mappedSubpath) ?? {};
72
- subEntry.types = withDot;
84
+ subEntry.types = useWildcard ? toWildcardValue(withDot) : withDot;
73
85
  subpathMap.set(mappedSubpath, subEntry);
74
86
  }
75
87
  return;
76
88
  }
77
89
  if (subpath && subpath !== '.') {
78
90
  const subEntry = subpathMap.get(subpath) ?? {};
79
- subEntry[kind] = withDot;
91
+ subEntry[kind] = useWildcard ? toWildcardValue(withDot) : withDot;
80
92
  subpathMap.set(subpath, subEntry);
81
93
  baseToSubpath.set(baseKey, subpath);
82
94
  }
@@ -148,9 +160,10 @@ const generateExports = async (options) => {
148
160
  exportsMap[subpath] = out;
149
161
  }
150
162
  }
151
- if (!exportsMap['.'] && baseMap.size) {
152
- const [subpath, entry] = subpathMap.entries().next().value ?? [];
153
- if (entry) {
163
+ if (!exportsMap['.']) {
164
+ const firstNonWildcard = [...subpathMap.entries()].find(([key]) => !key.includes('*'));
165
+ if (firstNonWildcard) {
166
+ const [subpath, entry] = firstNonWildcard;
154
167
  const out = {};
155
168
  if (entry.types) {
156
169
  out.types = entry.types;
@@ -169,7 +182,9 @@ const generateExports = async (options) => {
169
182
  }
170
183
  if (Object.keys(out).length) {
171
184
  exportsMap['.'] = out;
172
- exportsMap[subpath] ??= out;
185
+ if (!exportsMap[subpath]) {
186
+ exportsMap[subpath] = out;
187
+ }
173
188
  }
174
189
  }
175
190
  }
package/dist/esm/duel.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { argv, platform } from 'node:process';
3
- import { join, dirname, resolve, relative, parse as parsePath } from 'node:path';
3
+ import { join, dirname, resolve, relative, parse as parsePath, posix } from 'node:path';
4
4
  import { spawn } from 'node:child_process';
5
5
  import { writeFile, rm, rename, mkdir, cp, access, readFile } from 'node:fs/promises';
6
6
  import { randomBytes } from 'node:crypto';
@@ -24,7 +24,7 @@ const getSubpath = (mode, relFromRoot) => {
24
24
  }
25
25
  if (mode === 'dir') {
26
26
  const last = segments.at(-1);
27
- return last ? `./${last}` : null;
27
+ return last ? `./${last}/*` : null;
28
28
  }
29
29
  if (mode === 'wildcard') {
30
30
  const first = segments[0];
@@ -53,6 +53,17 @@ const generateExports = async (options) => {
53
53
  if (esmRootPosix.startsWith(`${cjsRootPosix}/`)) {
54
54
  cjsIgnore.push(`${esmRootPosix}/**`);
55
55
  }
56
+ const toWildcardValue = value => {
57
+ const dir = posix.dirname(value);
58
+ const file = posix.basename(value);
59
+ const dtsMatch = file.match(/(\.d\.(?:ts|mts|cts))$/i);
60
+ if (dtsMatch) {
61
+ const ext = dtsMatch[1];
62
+ return dir === '.' ? `./*${ext}` : `${dir}/*${ext}`;
63
+ }
64
+ const ext = posix.extname(file);
65
+ return dir === '.' ? `./*${ext}` : `${dir}/*${ext}`;
66
+ };
56
67
  const recordPath = (kind, filePath, root) => {
57
68
  const relPkg = toPosix(relative(pkgDir, filePath));
58
69
  const relFromRoot = toPosix(relative(root, filePath));
@@ -62,18 +73,19 @@ const generateExports = async (options) => {
62
73
  baseEntry[kind] = withDot;
63
74
  baseMap.set(baseKey, baseEntry);
64
75
  const subpath = getSubpath(mode, relFromRoot);
76
+ const useWildcard = subpath?.includes('*');
65
77
  if (kind === 'types') {
66
78
  const mappedSubpath = baseToSubpath.get(baseKey);
67
79
  if (mappedSubpath) {
68
80
  const subEntry = subpathMap.get(mappedSubpath) ?? {};
69
- subEntry.types = withDot;
81
+ subEntry.types = useWildcard ? toWildcardValue(withDot) : withDot;
70
82
  subpathMap.set(mappedSubpath, subEntry);
71
83
  }
72
84
  return;
73
85
  }
74
86
  if (subpath && subpath !== '.') {
75
87
  const subEntry = subpathMap.get(subpath) ?? {};
76
- subEntry[kind] = withDot;
88
+ subEntry[kind] = useWildcard ? toWildcardValue(withDot) : withDot;
77
89
  subpathMap.set(subpath, subEntry);
78
90
  baseToSubpath.set(baseKey, subpath);
79
91
  }
@@ -145,9 +157,10 @@ const generateExports = async (options) => {
145
157
  exportsMap[subpath] = out;
146
158
  }
147
159
  }
148
- if (!exportsMap['.'] && baseMap.size) {
149
- const [subpath, entry] = subpathMap.entries().next().value ?? [];
150
- if (entry) {
160
+ if (!exportsMap['.']) {
161
+ const firstNonWildcard = [...subpathMap.entries()].find(([key]) => !key.includes('*'));
162
+ if (firstNonWildcard) {
163
+ const [subpath, entry] = firstNonWildcard;
151
164
  const out = {};
152
165
  if (entry.types) {
153
166
  out.types = entry.types;
@@ -166,7 +179,9 @@ const generateExports = async (options) => {
166
179
  }
167
180
  if (Object.keys(out).length) {
168
181
  exportsMap['.'] = out;
169
- exportsMap[subpath] ??= out;
182
+ if (!exportsMap[subpath]) {
183
+ exportsMap[subpath] = out;
184
+ }
170
185
  }
171
186
  }
172
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/duel",
3
- "version": "3.2.3",
3
+ "version": "3.2.4",
4
4
  "description": "TypeScript dual packages.",
5
5
  "type": "module",
6
6
  "main": "dist/esm/duel.js",