@batijs/build 0.0.649 → 0.0.651

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/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ declare function main(options: {
6
6
  source: string | string[];
7
7
  dist: string;
8
8
  env?: EnvRegistry;
9
+ deploy?: string[];
9
10
  }, meta: VikeMeta): Promise<void>;
10
11
  //#endregion
11
12
  export { main as default, walk };
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
@@ -134,7 +131,7 @@ async function transformFileAfterExec(filepath, fileContent) {
134
131
  async function importTransformer(p) {
135
132
  return (await (isWin ? import(`file://${p}`) : import(p))).default;
136
133
  }
137
- async function executeOperationTransform(op, { meta, previousOperationSameDestination, packageJson, env }) {
134
+ async function executeOperationTransform(op, { meta, previousOperationSameDestination, packageJson, env, deploy }) {
138
135
  const transformer = await importTransformer(op.sourceAbsolute);
139
136
  const previousContent = previousOperationSameDestination?.content;
140
137
  const fileContent = await transformFileAfterExec(op.destination, await transformer({
@@ -143,7 +140,8 @@ async function executeOperationTransform(op, { meta, previousOperationSameDestin
143
140
  source: op.source,
144
141
  target: op.destination,
145
142
  packageJson,
146
- env
143
+ env,
144
+ deploy
147
145
  }));
148
146
  return { content: fileContent !== null ? fileContent : void 0 };
149
147
  }
@@ -151,14 +149,14 @@ async function executeOperationTransform(op, { meta, previousOperationSameDestin
151
149
  //#region src/relations.ts
152
150
  var RelationFile = class RelationFile {
153
151
  pathAbsolute;
154
- includeIfImported;
152
+ keepFileIfImported;
155
153
  static allPathAbsolute = /* @__PURE__ */ new Map();
156
- static allIncludeIfImported = [];
157
- constructor(pathAbsolute, includeIfImported) {
154
+ static allKeepFileIfImported = [];
155
+ constructor(pathAbsolute, keepFileIfImported) {
158
156
  this.pathAbsolute = pathAbsolute;
159
- this.includeIfImported = includeIfImported;
157
+ this.keepFileIfImported = keepFileIfImported;
160
158
  RelationFile.allPathAbsolute.set(pathAbsolute, this);
161
- if (includeIfImported) RelationFile.allIncludeIfImported.push(this);
159
+ if (keepFileIfImported) RelationFile.allKeepFileIfImported.push(this);
162
160
  }
163
161
  };
164
162
  var RelationImport = class RelationImport {
@@ -177,10 +175,10 @@ var RelationImport = class RelationImport {
177
175
  static computeUnimportedFiles() {
178
176
  const unimportedFiles = [];
179
177
  const importedByVolatileFile = [];
180
- for (const file of RelationFile.allIncludeIfImported) {
178
+ for (const file of RelationFile.allKeepFileIfImported) {
181
179
  const importedFile = RelationImport.allImports.find((ai) => ai.importTargetRelationFile === file);
182
180
  if (!importedFile) unimportedFiles.push(file);
183
- else if (importedFile.source.includeIfImported) importedByVolatileFile.push(importedFile);
181
+ else if (importedFile.source.keepFileIfImported) importedByVolatileFile.push(importedFile);
184
182
  }
185
183
  return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
186
184
  }
@@ -254,8 +252,9 @@ async function* walk(dir) {
254
252
  async function main(options, meta) {
255
253
  const sources = Array.isArray(options.source) ? options.source : [options.source];
256
254
  const env = options.env ?? [];
257
- function updateAllImports(target, imports, includeIfImported) {
258
- const rf = new RelationFile(target, includeIfImported);
255
+ const deploy = options.deploy ?? [];
256
+ function updateAllImports(target, imports, keepFileIfImported) {
257
+ const rf = new RelationFile(target, keepFileIfImported);
259
258
  if (!imports) return;
260
259
  for (const imp of imports.values()) new RelationImport(rf, path.resolve(path.dirname(target), imp));
261
260
  }
@@ -300,12 +299,13 @@ Please report this issue to https://github.com/vikejs/bati`);
300
299
  meta,
301
300
  previousOperationSameDestination: previousOp
302
301
  });
303
- 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")));
304
303
  } else if (op.kind === "transform") report = await executeOperationTransform(op, {
305
304
  meta,
306
305
  previousOperationSameDestination: previousOp,
307
306
  packageJson,
308
- env
307
+ env,
308
+ deploy
309
309
  });
310
310
  if (report.content) {
311
311
  await safeWriteFile(op.destination, report.content.trimStart());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@batijs/build",
3
- "version": "0.0.649",
3
+ "version": "0.0.651",
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.649",
17
- "@batijs/core": "0.0.649",
18
- "@batijs/features": "0.0.649",
16
+ "@batijs/compile": "0.0.651",
17
+ "@batijs/core": "0.0.651",
18
+ "@batijs/features": "0.0.651",
19
19
  "@types/node": "^20.19.37",
20
20
  "tsdown": "^0.22.2"
21
21
  },
22
22
  "peerDependencies": {
23
- "@batijs/core": "0.0.649"
23
+ "@batijs/core": "0.0.651"
24
24
  },
25
25
  "main": "./dist/index.js",
26
26
  "module": "./dist/index.js",