@batijs/build 0.0.281 → 0.0.286

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 +85 -28
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -180,6 +180,80 @@ Please report this issue to https://github.com/vikejs/bati`);
180
180
  }
181
181
  };
182
182
 
183
+ // src/relations.ts
184
+ import { extname } from "node:path";
185
+ var RelationFile = class _RelationFile {
186
+ constructor(pathAbsolute, includeIfImported) {
187
+ this.pathAbsolute = pathAbsolute;
188
+ this.includeIfImported = includeIfImported;
189
+ _RelationFile.allPathAbsolute.set(pathAbsolute, this);
190
+ if (includeIfImported) {
191
+ _RelationFile.allIncludeIfImported.push(this);
192
+ }
193
+ }
194
+ static allPathAbsolute = /* @__PURE__ */ new Map();
195
+ static allIncludeIfImported = [];
196
+ };
197
+ var RelationImport = class _RelationImport {
198
+ constructor(source, importTarget) {
199
+ this.source = source;
200
+ this.importTarget = importTarget;
201
+ _RelationImport.allImports.push(this);
202
+ }
203
+ static allImports = [];
204
+ get importTargetRelationFile() {
205
+ const potentialTargets = importToPotentialTargets(this.importTarget);
206
+ for (const target of potentialTargets) {
207
+ if (RelationFile.allPathAbsolute.has(target)) {
208
+ return RelationFile.allPathAbsolute.get(target);
209
+ }
210
+ }
211
+ }
212
+ static computeUnimportedFiles() {
213
+ const unimportedFiles = [];
214
+ const importedByVolatileFile = [];
215
+ for (const file of RelationFile.allIncludeIfImported) {
216
+ const importedFile = _RelationImport.allImports.find((ai) => ai.importTargetRelationFile === file);
217
+ if (!importedFile) {
218
+ unimportedFiles.push(file);
219
+ } else if (importedFile.source.includeIfImported) {
220
+ importedByVolatileFile.push(importedFile);
221
+ }
222
+ }
223
+ return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
224
+ }
225
+ };
226
+ function computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles) {
227
+ const copyImportedByVolatileFile = Array.from(importedByVolatileFile);
228
+ let redo = false;
229
+ for (const relationImport of copyImportedByVolatileFile) {
230
+ const found = unimportedFiles.find((uf) => uf === relationImport.source);
231
+ if (found) {
232
+ redo = true;
233
+ unimportedFiles.push(relationImport.importTargetRelationFile);
234
+ importedByVolatileFile = importedByVolatileFile.filter((i) => i !== relationImport);
235
+ }
236
+ }
237
+ if (redo) {
238
+ computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
239
+ }
240
+ return unimportedFiles;
241
+ }
242
+ function importToPotentialTargets(imp) {
243
+ let subject = imp;
244
+ const ext = extname(imp);
245
+ const targets = [];
246
+ if (ext.match(/^\.[jt]sx?$/)) {
247
+ subject = subject.replace(/^\.[jt]sx?$/, "");
248
+ }
249
+ if (!ext || subject !== imp) {
250
+ targets.push(...[".js", ".jsx", ".ts", ".tsx", ".cjs", ".mjs"].map((e) => `${subject}${e}`));
251
+ } else {
252
+ targets.push(imp);
253
+ }
254
+ return targets;
255
+ }
256
+
183
257
  // src/index.ts
184
258
  var reIgnoreFile = /^(chunk-|asset-|#)/gi;
185
259
  function toDist(filepath, source, dist) {
@@ -224,32 +298,16 @@ async function* walk(dir) {
224
298
  } else if (d.isFile()) yield entry;
225
299
  }
226
300
  }
227
- function importToPotentialTargets(imp) {
228
- let subject = imp;
229
- const ext = path.extname(imp);
230
- const targets = [];
231
- if (ext.match(/^\.[jt]sx?$/)) {
232
- subject = subject.replace(/^\.[jt]sx?$/, "");
233
- }
234
- if (!ext || subject !== imp) {
235
- targets.push(...[".js", ".jsx", ".ts", ".tsx", ".cjs", ".mjs"].map((e) => `${subject}${e}`));
236
- } else {
237
- targets.push(imp);
238
- }
239
- return targets;
240
- }
241
301
  async function main(options, meta) {
242
302
  const sources = Array.isArray(options.source) ? options.source : [options.source];
243
- const allImports = /* @__PURE__ */ new Set();
303
+ const allImports = /* @__PURE__ */ new Map();
244
304
  const filesContainingIncludeIfImported = /* @__PURE__ */ new Set();
245
- function updateAllImports(target, imports) {
305
+ function updateAllImports(target, imports, includeIfImported) {
306
+ const rf = new RelationFile(target, includeIfImported);
246
307
  if (!imports) return;
247
308
  for (const imp of imports.values()) {
248
309
  const importTarget = path.resolve(path.dirname(target), imp);
249
- const importTargets = importToPotentialTargets(importTarget);
250
- for (const imp2 of importTargets) {
251
- allImports.add(imp2);
252
- }
310
+ new RelationImport(rf, importTarget);
253
311
  }
254
312
  }
255
313
  const rearranger = new OperationsRearranger();
@@ -299,7 +357,11 @@ Please report this issue to https://github.com/vikejs/bati`
299
357
  meta,
300
358
  previousOperationSameDestination: previousOp
301
359
  });
302
- updateAllImports(op.destinationAbsolute, report.context?.imports);
360
+ updateAllImports(
361
+ op.destinationAbsolute,
362
+ report.context?.imports,
363
+ Boolean(report.context?.flags.has("include-if-imported"))
364
+ );
303
365
  } else if (op.kind === "transform") {
304
366
  report = await executeOperationTransform(op, {
305
367
  meta,
@@ -309,18 +371,13 @@ Please report this issue to https://github.com/vikejs/bati`
309
371
  if (report.content) {
310
372
  await safeWriteFile(op.destination, report.content.trimStart());
311
373
  }
312
- if (report.context?.flags.has("include-if-imported")) {
313
- filesContainingIncludeIfImported.add(op.destinationAbsolute);
314
- }
315
374
  previousOp = {
316
375
  ...op,
317
376
  ...report
318
377
  };
319
378
  }
320
- for (const target of filesContainingIncludeIfImported) {
321
- if (!allImports.has(target)) {
322
- await safeRmFile(target, { removeEmptyDir: true });
323
- }
379
+ for (const target of RelationImport.computeUnimportedFiles()) {
380
+ await safeRmFile(target.pathAbsolute, { removeEmptyDir: true });
324
381
  }
325
382
  }
326
383
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@batijs/build",
3
- "version": "0.0.281",
3
+ "version": "0.0.286",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -10,10 +10,10 @@
10
10
  "devDependencies": {
11
11
  "@types/node": "^18.19.14",
12
12
  "tsup": "^8.3.0",
13
- "@batijs/compile": "0.0.281"
13
+ "@batijs/compile": "0.0.286"
14
14
  },
15
15
  "dependencies": {
16
- "@batijs/core": "0.0.281"
16
+ "@batijs/core": "0.0.286"
17
17
  },
18
18
  "main": "./dist/index.js",
19
19
  "module": "./dist/index.js",