@foxford/tsup-config 1.0.0 → 1.0.1

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/index.cjs CHANGED
@@ -64,6 +64,7 @@ var __async = (__this, __arguments, generator) => {
64
64
  // src/index.js
65
65
  var src_exports = {};
66
66
  __export(src_exports, {
67
+ addExtensionsToRelativeImports: () => addExtensionsToRelativeImports,
67
68
  assertSourceExports: () => assertSourceExports,
68
69
  copyReadme: () => copyReadme,
69
70
  createTsupConfig: () => createTsupConfig,
@@ -141,8 +142,8 @@ function writeBuildPackageJson({ entries, extensions, main, module: moduleField,
141
142
  "files"
142
143
  ]);
143
144
  const packageJson = Object.fromEntries(Object.entries(source).filter(([field]) => !DEV_ONLY.has(field)));
144
- packageJson.main = main;
145
- packageJson.module = moduleField;
145
+ if (main !== void 0) packageJson.main = main;
146
+ if (moduleField !== void 0) packageJson.module = moduleField;
146
147
  packageJson.exports = toBuildExports(entries, { extensions });
147
148
  import_node_fs.default.writeFileSync(import_node_path.default.join(outDir, "package.json"), `${JSON.stringify(packageJson, null, 2)}
148
149
  `);
@@ -169,9 +170,35 @@ function updateBuildFiles(outDir = "build") {
169
170
  function copyReadme(outDir = "build") {
170
171
  import_node_fs.default.copyFileSync("README.mdx", import_node_path.default.join(outDir, "README.mdx"));
171
172
  }
173
+ function addExtensionsToRelativeImports(outDir = "build") {
174
+ const RELATIVE = /(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)(['"])(\.[^'"]*)\2/g;
175
+ let patched = 0;
176
+ const walk = (dir) => {
177
+ for (const entry of import_node_fs.default.readdirSync(dir, { withFileTypes: true })) {
178
+ const full = import_node_path.default.join(dir, entry.name);
179
+ if (entry.isDirectory()) {
180
+ walk(full);
181
+ continue;
182
+ }
183
+ if (!entry.name.endsWith(".js")) continue;
184
+ const source = import_node_fs.default.readFileSync(full, "utf-8");
185
+ const result = source.replace(RELATIVE, (match, prefix, quote, specifier) => {
186
+ if (/\.[cm]?jsx?$/.test(specifier)) return match;
187
+ const target = import_node_path.default.resolve(import_node_path.default.dirname(full), specifier);
188
+ const suffix = import_node_fs.default.existsSync(`${target}.js`) ? ".js" : import_node_fs.default.existsSync(import_node_path.default.join(target, "index.js")) ? "/index.js" : null;
189
+ if (suffix === null) return match;
190
+ patched += 1;
191
+ return `${prefix}${quote}${specifier}${suffix}${quote}`;
192
+ });
193
+ if (result !== source) import_node_fs.default.writeFileSync(full, result);
194
+ }
195
+ };
196
+ walk(outDir);
197
+ return patched;
198
+ }
172
199
  function createTsupConfig({ entries, overrides = {} }) {
173
200
  const extensions = (name) => ({
174
- import: `./${name}.mjs`,
201
+ import: `./${name}.js`,
175
202
  require: `./${name}.cjs`,
176
203
  types: `./${name}.d.ts`
177
204
  });
@@ -193,23 +220,20 @@ function createTsupConfig({ entries, overrides = {} }) {
193
220
  format: ["cjs", "esm"],
194
221
  onSuccess: () => __async(null, null, function* () {
195
222
  copyReadme();
196
- writeBuildPackageJson({
223
+ const root = entries["."];
224
+ writeBuildPackageJson(__spreadValues({
197
225
  entries,
198
- extensions,
199
- main: "./index.cjs",
200
- module: "./index.mjs"
201
- });
226
+ extensions
227
+ }, root === void 0 ? {} : { main: extensions(artifactName(".", root)).require, module: extensions(artifactName(".", root)).import }));
202
228
  }),
203
229
  outDir: "build",
204
- // Явные расширения: при "type": "module" tsup по умолчанию отдаёт ESM как .js,
205
- // и ссылки на ./*.mjs в build/package.json оказываются битыми
206
- outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".mjs" }),
207
230
  sourcemap: false,
208
231
  splitting: false
209
232
  }, overrides);
210
233
  }
211
234
  // Annotate the CommonJS export names for ESM import in node:
212
235
  0 && (module.exports = {
236
+ addExtensionsToRelativeImports,
213
237
  assertSourceExports,
214
238
  copyReadme,
215
239
  createTsupConfig,
package/index.d.ts CHANGED
@@ -36,5 +36,11 @@ export function verifyBuildExports(outDir?: string): void
36
36
  /** Копирует README.mdx рядом с артефактом. */
37
37
  export function copyReadme(outDir?: string): void
38
38
 
39
+ /**
40
+ * Дописывает расширения в относительных импортах собранного ESM (Node их требует,
41
+ * а tsc переносит спецификаторы из исходника как есть). Возвращает число правок.
42
+ */
43
+ export function addExtensionsToRelativeImports(outDir?: string): number
44
+
39
45
  /** Готовый tsup-конфиг публикуемого пакета монорепо. */
40
46
  export function createTsupConfig(options: { entries: Entries; overrides?: Options }): Options
package/index.mjs CHANGED
@@ -102,8 +102,8 @@ function writeBuildPackageJson({ entries, extensions, main, module: moduleField,
102
102
  "files"
103
103
  ]);
104
104
  const packageJson = Object.fromEntries(Object.entries(source).filter(([field]) => !DEV_ONLY.has(field)));
105
- packageJson.main = main;
106
- packageJson.module = moduleField;
105
+ if (main !== void 0) packageJson.main = main;
106
+ if (moduleField !== void 0) packageJson.module = moduleField;
107
107
  packageJson.exports = toBuildExports(entries, { extensions });
108
108
  fs.writeFileSync(path.join(outDir, "package.json"), `${JSON.stringify(packageJson, null, 2)}
109
109
  `);
@@ -130,9 +130,35 @@ function updateBuildFiles(outDir = "build") {
130
130
  function copyReadme(outDir = "build") {
131
131
  fs.copyFileSync("README.mdx", path.join(outDir, "README.mdx"));
132
132
  }
133
+ function addExtensionsToRelativeImports(outDir = "build") {
134
+ const RELATIVE = /(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)(['"])(\.[^'"]*)\2/g;
135
+ let patched = 0;
136
+ const walk = (dir) => {
137
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
138
+ const full = path.join(dir, entry.name);
139
+ if (entry.isDirectory()) {
140
+ walk(full);
141
+ continue;
142
+ }
143
+ if (!entry.name.endsWith(".js")) continue;
144
+ const source = fs.readFileSync(full, "utf-8");
145
+ const result = source.replace(RELATIVE, (match, prefix, quote, specifier) => {
146
+ if (/\.[cm]?jsx?$/.test(specifier)) return match;
147
+ const target = path.resolve(path.dirname(full), specifier);
148
+ const suffix = fs.existsSync(`${target}.js`) ? ".js" : fs.existsSync(path.join(target, "index.js")) ? "/index.js" : null;
149
+ if (suffix === null) return match;
150
+ patched += 1;
151
+ return `${prefix}${quote}${specifier}${suffix}${quote}`;
152
+ });
153
+ if (result !== source) fs.writeFileSync(full, result);
154
+ }
155
+ };
156
+ walk(outDir);
157
+ return patched;
158
+ }
133
159
  function createTsupConfig({ entries, overrides = {} }) {
134
160
  const extensions = (name) => ({
135
- import: `./${name}.mjs`,
161
+ import: `./${name}.js`,
136
162
  require: `./${name}.cjs`,
137
163
  types: `./${name}.d.ts`
138
164
  });
@@ -154,22 +180,19 @@ function createTsupConfig({ entries, overrides = {} }) {
154
180
  format: ["cjs", "esm"],
155
181
  onSuccess: () => __async(null, null, function* () {
156
182
  copyReadme();
157
- writeBuildPackageJson({
183
+ const root = entries["."];
184
+ writeBuildPackageJson(__spreadValues({
158
185
  entries,
159
- extensions,
160
- main: "./index.cjs",
161
- module: "./index.mjs"
162
- });
186
+ extensions
187
+ }, root === void 0 ? {} : { main: extensions(artifactName(".", root)).require, module: extensions(artifactName(".", root)).import }));
163
188
  }),
164
189
  outDir: "build",
165
- // Явные расширения: при "type": "module" tsup по умолчанию отдаёт ESM как .js,
166
- // и ссылки на ./*.mjs в build/package.json оказываются битыми
167
- outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".mjs" }),
168
190
  sourcemap: false,
169
191
  splitting: false
170
192
  }, overrides);
171
193
  }
172
194
  export {
195
+ addExtensionsToRelativeImports,
173
196
  assertSourceExports,
174
197
  copyReadme,
175
198
  createTsupConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foxford/tsup-config",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Общая сборка публикуемых пакетов: декларация точек входа, tsup-конфиг, генерация публикуемого package.json",
5
5
  "keywords": [
6
6
  "foxford",
@@ -37,6 +37,6 @@
37
37
  "index.mjs",
38
38
  "package.json"
39
39
  ],
40
- "sha": "fd3591a",
40
+ "sha": "d0aa793",
41
41
  "scripts": {}
42
42
  }