@knighted/duel 1.0.0-rc.3 → 1.0.0-rc.5

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
@@ -11,12 +11,10 @@ Node.js tool for building a TypeScript dual package.
11
11
  * Bidirectional ESM ↔️ CJS dual builds inferred from the package.json `type`.
12
12
  * Correctly preserves module systems for `.mts` and `.cts` file extensions.
13
13
  * Use only one package.json and tsconfig.json.
14
- * Run builds in parallel.
15
14
 
16
15
  ## Requirements
17
16
 
18
17
  * Node >= 16.19.0.
19
- * A tsconfig.json with `outDir` defined.
20
18
 
21
19
  ## Example
22
20
 
@@ -72,7 +70,7 @@ Assuming an `outDir` of `dist`, running the above will create `dist/esm` and `di
72
70
 
73
71
  ### Parallel builds
74
72
 
75
- For a slightly faster build, you can run them in parallel. This is experimental, as your mileage may vary based on the size of your `node_modules` directory.
73
+ This is experimental, as your mileage may vary based on the size of your `node_modules` directory.
76
74
 
77
75
  ```json
78
76
  "scripts": {
@@ -80,7 +78,7 @@ For a slightly faster build, you can run them in parallel. This is experimental,
80
78
  }
81
79
  ```
82
80
 
83
- You _might_ save an extra few seconds from your builds, but not much more. This requires first copying your project to a parent directory of `--project` if it exists as a writable folder. Common [gitignored directories for Node.js projects](https://github.com/github/gitignore/blob/main/Node.gitignore) are not copied, with the exception of `node_modules`. See the [notes](#notes) as to why this can't be improved much further.
81
+ You _might_ reduce your build times, but only if your project has minimal dependencies. This requires first copying your project to a parent directory of `--project` if it exists as a writable folder. Common [gitignored directories for Node.js projects](https://github.com/github/gitignore/blob/main/Node.gitignore) are not copied, with the exception of `node_modules`. See the [notes](#notes) as to why this can't be improved much further. In most cases, you're better off with serial builds.
84
82
 
85
83
  ## Options
86
84
 
@@ -89,7 +87,7 @@ The available options are limited, because you should define most of them inside
89
87
  * `--project, -p` The path to the project's configuration file. Defaults to `tsconfig.json`.
90
88
  * `--pkg-dir, -k` The directory to start looking for a package.json file. Defaults to the cwd.
91
89
  * `--dirs, -d` Outputs both builds to directories inside of `outDir`. Defalts to `false`.
92
- * `--parallel, -l` Run the builds in parallel.
90
+ * `--parallel, -l` Run the builds in parallel. Defaults to `false`.
93
91
 
94
92
  You can run `duel --help` to get the same info. Below is the output of that:
95
93
 
@@ -118,5 +116,5 @@ These are definitely edge cases, and would only really come up if your project m
118
116
 
119
117
  ## Notes
120
118
 
121
- As far as I can tell, `duel` is one (if not the only) way to get a correct dual package build using only `tsc` while using only **one package.json** file and **one tsconfig.json** file, _and also_ preserving module system by file extension. The Microsoft backed TypeScript team [keep](https://github.com/microsoft/TypeScript/issues/54593) [talking](https://github.com/microsoft/TypeScript/pull/54546) about dual build support, but their philosophy is mainly one of self perseverance, rather than collaboration. For instance, they continue to refuse to rewrite specifiers. The downside of their decisions, and the fact that `npm` does not support using alternative names for the package.json file, is that `duel` must copy your project
119
+ As far as I can tell, `duel` is one (if not the only) way to get a correct dual package build using `tsc` while using only **one package.json** file and **one tsconfig.json** file, _and also preserving module system by file extension_. The Microsoft backed TypeScript team [keep](https://github.com/microsoft/TypeScript/issues/54593) [talking](https://github.com/microsoft/TypeScript/pull/54546) about dual build support, but their philosophy is mainly one of self-preservation, rather than collaboration. For instance, they continue to [refuse to rewrite specifiers](https://github.com/microsoft/TypeScript/issues/16577). The downside of their decisions, and the fact that `npm` does not support using alternative names for the package.json file, is that `duel` must copy your project
122
120
  directory before attempting to run the builds in parallel.
package/dist/duel.cjs CHANGED
@@ -49,6 +49,7 @@ const duel = async args => {
49
49
  } = ctx;
50
50
  const pkgDir = (0, _nodePath.dirname)(pkg.path);
51
51
  const outDir = tsconfig.compilerOptions?.outDir ?? 'dist';
52
+ const absoluteOutDir = (0, _nodePath.resolve)(projectDir, outDir);
52
53
  const originalType = pkg.packageJson.type ?? 'commonjs';
53
54
  const isCjsBuild = originalType !== 'commonjs';
54
55
  const targetExt = isCjsBuild ? '.cjs' : '.mjs';
@@ -111,6 +112,7 @@ const duel = async args => {
111
112
  return;
112
113
  }
113
114
  (0, _util.log)('Preparing parallel build...');
115
+ const prepStart = _nodePerf_hooks.performance.now();
114
116
  await (0, _promises.cp)(projectDir, paraTempDir, {
115
117
  recursive: true,
116
118
  /**
@@ -128,6 +130,7 @@ const duel = async args => {
128
130
  await (0, _promises.writeFile)((0, _nodePath.join)(paraTempDir, 'package.json'), JSON.stringify({
129
131
  type: isCjsBuild ? 'commonjs' : 'module'
130
132
  }));
133
+ (0, _util.log)(`Prepared in ${Math.round(_nodePerf_hooks.performance.now() - prepStart)}ms.`);
131
134
  (0, _util.log)('Starting parallel dual builds...');
132
135
  let success = false;
133
136
  const startTime = _nodePerf_hooks.performance.now();
@@ -140,14 +143,15 @@ const duel = async args => {
140
143
  (0, _util.logError)(message);
141
144
  }
142
145
  if (success) {
143
- const absoluteOutDir = (0, _nodePath.resolve)(projectDir, outDir);
144
146
  const absoluteDualOutDir = (0, _nodePath.join)(paraTempDir, dualOutDir);
145
147
  const filenames = await (0, _glob.glob)(`${absoluteDualOutDir}/**/*{.js,.d.ts}`, {
146
148
  ignore: 'node_modules/**'
147
149
  });
148
150
  await updateSpecifiersAndFileExtensions(filenames);
149
151
  // Copy over and cleanup
150
- await (0, _promises.rename)(absoluteDualOutDir, (0, _nodePath.join)(absoluteOutDir, isCjsBuild ? 'cjs' : 'esm'));
152
+ await (0, _promises.cp)(absoluteDualOutDir, (0, _nodePath.join)(absoluteOutDir, isCjsBuild ? 'cjs' : 'esm'), {
153
+ recursive: true
154
+ });
151
155
  await (0, _promises.rm)(paraTempDir, {
152
156
  force: true,
153
157
  recursive: true
@@ -169,7 +173,6 @@ const duel = async args => {
169
173
  if (success) {
170
174
  const dualConfigPath = (0, _nodePath.join)(projectDir, `tsconfig.${hex}.json`);
171
175
  const dualOutDir = isCjsBuild ? (0, _nodePath.join)(outDir, 'cjs') : (0, _nodePath.join)(outDir, 'esm');
172
- // Using structuredClone() would require node >= 17.0.0
173
176
  const tsconfigDual = getOverrideTsConfig(dualOutDir);
174
177
  const pkgRename = 'package.json.bak';
175
178
 
package/dist/duel.js CHANGED
@@ -43,6 +43,7 @@ const duel = async args => {
43
43
  } = ctx;
44
44
  const pkgDir = dirname(pkg.path);
45
45
  const outDir = tsconfig.compilerOptions?.outDir ?? 'dist';
46
+ const absoluteOutDir = resolve(projectDir, outDir);
46
47
  const originalType = pkg.packageJson.type ?? 'commonjs';
47
48
  const isCjsBuild = originalType !== 'commonjs';
48
49
  const targetExt = isCjsBuild ? '.cjs' : '.mjs';
@@ -105,6 +106,7 @@ const duel = async args => {
105
106
  return;
106
107
  }
107
108
  log('Preparing parallel build...');
109
+ const prepStart = performance.now();
108
110
  await cp(projectDir, paraTempDir, {
109
111
  recursive: true,
110
112
  /**
@@ -122,6 +124,7 @@ const duel = async args => {
122
124
  await writeFile(join(paraTempDir, 'package.json'), JSON.stringify({
123
125
  type: isCjsBuild ? 'commonjs' : 'module'
124
126
  }));
127
+ log(`Prepared in ${Math.round(performance.now() - prepStart)}ms.`);
125
128
  log('Starting parallel dual builds...');
126
129
  let success = false;
127
130
  const startTime = performance.now();
@@ -134,14 +137,15 @@ const duel = async args => {
134
137
  logError(message);
135
138
  }
136
139
  if (success) {
137
- const absoluteOutDir = resolve(projectDir, outDir);
138
140
  const absoluteDualOutDir = join(paraTempDir, dualOutDir);
139
141
  const filenames = await glob(`${absoluteDualOutDir}/**/*{.js,.d.ts}`, {
140
142
  ignore: 'node_modules/**'
141
143
  });
142
144
  await updateSpecifiersAndFileExtensions(filenames);
143
145
  // Copy over and cleanup
144
- await rename(absoluteDualOutDir, join(absoluteOutDir, isCjsBuild ? 'cjs' : 'esm'));
146
+ await cp(absoluteDualOutDir, join(absoluteOutDir, isCjsBuild ? 'cjs' : 'esm'), {
147
+ recursive: true
148
+ });
145
149
  await rm(paraTempDir, {
146
150
  force: true,
147
151
  recursive: true
@@ -163,7 +167,6 @@ const duel = async args => {
163
167
  if (success) {
164
168
  const dualConfigPath = join(projectDir, `tsconfig.${hex}.json`);
165
169
  const dualOutDir = isCjsBuild ? join(outDir, 'cjs') : join(outDir, 'esm');
166
- // Using structuredClone() would require node >= 17.0.0
167
170
  const tsconfigDual = getOverrideTsConfig(dualOutDir);
168
171
  const pkgRename = 'package.json.bak';
169
172
 
package/dist/init.cjs CHANGED
@@ -109,8 +109,7 @@ const init = async args => {
109
109
  return false;
110
110
  }
111
111
  if (!tsconfig.compilerOptions?.outDir) {
112
- (0, _util.logError)('You must define an `outDir` in your project config.');
113
- return false;
112
+ (0, _util.log)('No outDir defined in tsconfig.json. Build output will be in "dist".');
114
113
  }
115
114
  const projectDir = (0, _nodePath.dirname)(configPath);
116
115
  return {
package/dist/init.js CHANGED
@@ -103,8 +103,7 @@ const init = async args => {
103
103
  return false;
104
104
  }
105
105
  if (!tsconfig.compilerOptions?.outDir) {
106
- logError('You must define an `outDir` in your project config.');
107
- return false;
106
+ log('No outDir defined in tsconfig.json. Build output will be in "dist".');
108
107
  }
109
108
  const projectDir = dirname(configPath);
110
109
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/duel",
3
- "version": "1.0.0-rc.3",
3
+ "version": "1.0.0-rc.5",
4
4
  "description": "TypeScript dual packages.",
5
5
  "type": "module",
6
6
  "main": "dist",
@@ -28,8 +28,8 @@
28
28
  "tsc",
29
29
  "typescript",
30
30
  "dual package",
31
- "cjs",
32
- "mjs"
31
+ "cts",
32
+ "mts"
33
33
  ],
34
34
  "files": [
35
35
  "dist"
@@ -52,7 +52,7 @@
52
52
  "c8": "^8.0.1",
53
53
  "eslint": "^8.45.0",
54
54
  "eslint-plugin-n": "^16.0.1",
55
- "prettier": "^3.0.0",
55
+ "prettier": "^3.0.1",
56
56
  "typescript": "^5.2.0-dev.20230727"
57
57
  },
58
58
  "dependencies": {