@batijs/build 0.0.650 → 0.0.652

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.
Files changed (2) hide show
  1. package/dist/index.js +30 -33
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -1,28 +1,25 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { mkdir, opendir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
3
3
  import path, { extname, parse, relative } from "node:path";
4
- import { formatCode, parseModule, transformAndFormat } from "@batijs/core";
4
+ import { formatCode, markEmptyExport, mergeDts, transformAndFormat } from "@batijs/core";
5
5
  //#region src/operations/merge-dts.ts
6
- async function mergeDts({ fileContent, previousContent, filepath, meta }) {
7
- const previousAst = parseModule(previousContent);
8
- const currentAst = parseModule(fileContent);
9
- for (const imp of previousAst.imports.$items) currentAst.imports[imp.local] = imp;
10
- const index = currentAst.$ast.body.findIndex((node) => node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration");
11
- for (const node of previousAst.$ast.body) {
12
- if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") continue;
13
- if (index === -1) currentAst.$ast.body.push(node);
14
- else currentAst.$ast.body.splice(index, 0, node);
15
- }
16
- return clearExports((await transformAndFormat(currentAst.generate().code, meta, { filepath })).code, meta);
6
+ let merger;
7
+ let emptyExportMarker;
8
+ /**
9
+ * Merge two already-`$$`-transformed `.d.ts` files into one: concatenate them and run the `mergeDts`
10
+ * codemod, which hoists/dedupes imports and folds same-named `declare global` / `declare module` /
11
+ * `namespace` / `interface` declarations together. Then tidy whitespace and strip a now-empty
12
+ * `export {}`.
13
+ */
14
+ async function mergeDts$1({ fileContent, previousContent, filepath, meta }) {
15
+ merger ??= mergeDts.forTarget("tsx");
16
+ return clearExports(await formatCode((await merger).transform(`${previousContent}\n${fileContent}`, {}), { filepath }), meta);
17
17
  }
18
- function clearExports(code, meta) {
18
+ async function clearExports(code, meta) {
19
19
  if (code.trim() === "export {};") return;
20
- if (meta.BATI.has("biome")) {
21
- const index = code.indexOf("\nexport {};");
22
- const foundImport = code.match(/^import .* from /gm);
23
- if (index !== -1 && foundImport) return code.slice(0, index) + "\n// biome-ignore lint/complexity/noUselessEmptyExport: ensure that the file is considered as a module" + code.slice(index);
24
- }
25
- return code;
20
+ if (!meta.BATI.has("biome")) return code;
21
+ emptyExportMarker ??= markEmptyExport.forTarget("tsx");
22
+ return (await emptyExportMarker).transform(code, {});
26
23
  }
27
24
  //#endregion
28
25
  //#region src/operations/file.ts
@@ -31,13 +28,13 @@ async function executeOperationFile(op, { meta, previousOperationSameDestination
31
28
  const filepath = relative(op.source, op.sourceAbsolute);
32
29
  const result = await transformAndFormat(code, meta, { filepath });
33
30
  let fileContent = result.code;
34
- if (op.sourceAbsolute.endsWith(".d.ts")) if (previousOperationSameDestination?.content) fileContent = await mergeDts({
31
+ if (op.sourceAbsolute.endsWith(".d.ts")) if (previousOperationSameDestination?.content) fileContent = await mergeDts$1({
35
32
  fileContent,
36
33
  previousContent: previousOperationSameDestination.content,
37
- meta,
38
- filepath
34
+ filepath,
35
+ meta
39
36
  });
40
- else fileContent = clearExports(fileContent, meta);
37
+ else fileContent = await clearExports(fileContent, meta);
41
38
  return {
42
39
  context: result.context,
43
40
  content: fileContent ? fileContent.trimStart() : void 0
@@ -152,14 +149,14 @@ async function executeOperationTransform(op, { meta, previousOperationSameDestin
152
149
  //#region src/relations.ts
153
150
  var RelationFile = class RelationFile {
154
151
  pathAbsolute;
155
- includeIfImported;
152
+ keepFileIfImported;
156
153
  static allPathAbsolute = /* @__PURE__ */ new Map();
157
- static allIncludeIfImported = [];
158
- constructor(pathAbsolute, includeIfImported) {
154
+ static allKeepFileIfImported = [];
155
+ constructor(pathAbsolute, keepFileIfImported) {
159
156
  this.pathAbsolute = pathAbsolute;
160
- this.includeIfImported = includeIfImported;
157
+ this.keepFileIfImported = keepFileIfImported;
161
158
  RelationFile.allPathAbsolute.set(pathAbsolute, this);
162
- if (includeIfImported) RelationFile.allIncludeIfImported.push(this);
159
+ if (keepFileIfImported) RelationFile.allKeepFileIfImported.push(this);
163
160
  }
164
161
  };
165
162
  var RelationImport = class RelationImport {
@@ -178,10 +175,10 @@ var RelationImport = class RelationImport {
178
175
  static computeUnimportedFiles() {
179
176
  const unimportedFiles = [];
180
177
  const importedByVolatileFile = [];
181
- for (const file of RelationFile.allIncludeIfImported) {
178
+ for (const file of RelationFile.allKeepFileIfImported) {
182
179
  const importedFile = RelationImport.allImports.find((ai) => ai.importTargetRelationFile === file);
183
180
  if (!importedFile) unimportedFiles.push(file);
184
- else if (importedFile.source.includeIfImported) importedByVolatileFile.push(importedFile);
181
+ else if (importedFile.source.keepFileIfImported) importedByVolatileFile.push(importedFile);
185
182
  }
186
183
  return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
187
184
  }
@@ -256,8 +253,8 @@ async function main(options, meta) {
256
253
  const sources = Array.isArray(options.source) ? options.source : [options.source];
257
254
  const env = options.env ?? [];
258
255
  const deploy = options.deploy ?? [];
259
- function updateAllImports(target, imports, includeIfImported) {
260
- const rf = new RelationFile(target, includeIfImported);
256
+ function updateAllImports(target, imports, keepFileIfImported) {
257
+ const rf = new RelationFile(target, keepFileIfImported);
261
258
  if (!imports) return;
262
259
  for (const imp of imports.values()) new RelationImport(rf, path.resolve(path.dirname(target), imp));
263
260
  }
@@ -302,7 +299,7 @@ Please report this issue to https://github.com/vikejs/bati`);
302
299
  meta,
303
300
  previousOperationSameDestination: previousOp
304
301
  });
305
- updateAllImports(op.destinationAbsolute, report.context?.imports, Boolean(report.context?.flags.has("include-if-imported")));
302
+ updateAllImports(op.destinationAbsolute, report.context?.imports, Boolean(report.context?.flags.has("keep-file-if-imported")));
306
303
  } else if (op.kind === "transform") report = await executeOperationTransform(op, {
307
304
  meta,
308
305
  previousOperationSameDestination: previousOp,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@batijs/build",
3
- "version": "0.0.650",
3
+ "version": "0.0.652",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -13,14 +13,14 @@
13
13
  "repository": "https://github.com/vikejs/bati",
14
14
  "license": "MIT",
15
15
  "devDependencies": {
16
- "@batijs/compile": "0.0.650",
17
- "@batijs/core": "0.0.650",
18
- "@batijs/features": "0.0.650",
16
+ "@batijs/compile": "0.0.652",
17
+ "@batijs/core": "0.0.652",
18
+ "@batijs/features": "0.0.652",
19
19
  "@types/node": "^20.19.37",
20
20
  "tsdown": "^0.22.2"
21
21
  },
22
22
  "peerDependencies": {
23
- "@batijs/core": "0.0.650"
23
+ "@batijs/core": "0.0.652"
24
24
  },
25
25
  "main": "./dist/index.js",
26
26
  "module": "./dist/index.js",