@batijs/build 0.0.599 → 0.0.600

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
@@ -1,9 +1,10 @@
1
- import { VikeMeta } from '@batijs/core';
1
+ import { VikeMeta } from "@batijs/core";
2
2
 
3
+ //#region src/index.d.ts
3
4
  declare function walk(dir: string): AsyncGenerator<string>;
4
5
  declare function main(options: {
5
- source: string | string[];
6
- dist: string;
6
+ source: string | string[];
7
+ dist: string;
7
8
  }, meta: VikeMeta): Promise<void>;
8
-
9
- export { main as default, walk };
9
+ //#endregion
10
+ export { main as default, walk };
package/dist/index.js CHANGED
@@ -1,423 +1,314 @@
1
- // src/index.ts
2
- import { existsSync } from "fs";
3
- import { mkdir, opendir, rm, rmdir, writeFile } from "fs/promises";
4
- import path from "path";
5
-
6
- // src/operations/file.ts
7
- import { readFile } from "fs/promises";
8
- import { relative } from "path";
9
- import { transformAndFormat as transformAndFormat2 } from "@batijs/core";
10
-
11
- // src/operations/merge-dts.ts
12
- import { parseModule, transformAndFormat } from "@batijs/core";
13
- async function mergeDts({
14
- fileContent,
15
- previousContent,
16
- filepath,
17
- meta
18
- }) {
19
- const previousAst = parseModule(previousContent);
20
- const currentAst = parseModule(fileContent);
21
- for (const imp of previousAst.imports.$items) {
22
- currentAst.imports[imp.local] = imp;
23
- }
24
- const index = currentAst.$ast.body.findIndex(
25
- (node) => node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration"
26
- );
27
- for (const node of previousAst.$ast.body) {
28
- if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") {
29
- continue;
30
- }
31
- if (index === -1) {
32
- currentAst.$ast.body.push(node);
33
- } else {
34
- currentAst.$ast.body.splice(index, 0, node);
35
- }
36
- }
37
- const res = await transformAndFormat(currentAst.generate().code, meta, {
38
- filepath
39
- });
40
- return clearExports(res.code, meta);
1
+ import { existsSync } from "node:fs";
2
+ import { mkdir, opendir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
3
+ import path, { extname, parse, relative } from "node:path";
4
+ import { formatCode, parseModule, transformAndFormat } from "@batijs/core";
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);
41
17
  }
42
18
  function clearExports(code, meta) {
43
- if (code.trim() === "export {};") {
44
- return void 0;
45
- }
46
- if (meta.BATI.has("biome")) {
47
- const index = code.indexOf("\nexport {};");
48
- const foundImport = code.match(/^import .* from /gm);
49
- if (index !== -1 && foundImport) {
50
- return code.slice(0, index) + "\n// biome-ignore lint/complexity/noUselessEmptyExport: ensure that the file is considered as a module" + code.slice(index);
51
- }
52
- }
53
- return code;
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;
54
26
  }
55
-
56
- // src/operations/file.ts
57
- async function executeOperationFile(op, {
58
- meta,
59
- previousOperationSameDestination
60
- }) {
61
- const code = await readFile(op.sourceAbsolute, { encoding: "utf-8" });
62
- const filepath = relative(op.source, op.sourceAbsolute);
63
- const result = await transformAndFormat2(code, meta, {
64
- filepath
65
- });
66
- let fileContent = result.code;
67
- if (op.sourceAbsolute.endsWith(".d.ts")) {
68
- if (previousOperationSameDestination?.content) {
69
- fileContent = await mergeDts({
70
- fileContent,
71
- previousContent: previousOperationSameDestination.content,
72
- meta,
73
- filepath
74
- });
75
- } else {
76
- fileContent = clearExports(fileContent, meta);
77
- }
78
- }
79
- return {
80
- context: result.context,
81
- content: fileContent ? fileContent.trimStart() : void 0
82
- };
27
+ //#endregion
28
+ //#region src/operations/file.ts
29
+ async function executeOperationFile(op, { meta, previousOperationSameDestination }) {
30
+ const code = await readFile(op.sourceAbsolute, { encoding: "utf-8" });
31
+ const filepath = relative(op.source, op.sourceAbsolute);
32
+ const result = await transformAndFormat(code, meta, { filepath });
33
+ let fileContent = result.code;
34
+ if (op.sourceAbsolute.endsWith(".d.ts")) if (previousOperationSameDestination?.content) fileContent = await mergeDts({
35
+ fileContent,
36
+ previousContent: previousOperationSameDestination.content,
37
+ meta,
38
+ filepath
39
+ });
40
+ else fileContent = clearExports(fileContent, meta);
41
+ return {
42
+ context: result.context,
43
+ content: fileContent ? fileContent.trimStart() : void 0
44
+ };
83
45
  }
84
-
85
- // src/utils.ts
46
+ //#endregion
47
+ //#region src/utils.ts
86
48
  function orderBy(array, getter) {
87
- return array.slice().sort((a, b) => {
88
- const valueA = getter(a);
89
- const valueB = getter(b);
90
- if (valueA < valueB) return -1;
91
- if (valueA > valueB) return 1;
92
- return 0;
93
- });
49
+ return array.slice().sort((a, b) => {
50
+ const valueA = getter(a);
51
+ const valueB = getter(b);
52
+ if (valueA < valueB) return -1;
53
+ if (valueA > valueB) return 1;
54
+ return 0;
55
+ });
94
56
  }
95
-
96
- // src/operations/rearranger.ts
57
+ //#endregion
58
+ //#region src/operations/rearranger.ts
97
59
  var OperationsRearranger = class {
98
- files;
99
- constructor() {
100
- this.files = /* @__PURE__ */ new Map();
101
- }
102
- addFile(file) {
103
- if (!this.files.has(file.destination)) {
104
- this.files.set(file.destination, []);
105
- }
106
- this.files.get(file.destination).push(file);
107
- }
108
- *compute() {
109
- for (const file of this.files.values()) {
110
- if (file.filter((op) => op.important).length >= 2) {
111
- throw new Error(`Error while trying to generate file: '${file[0].destination}'.
60
+ files;
61
+ constructor() {
62
+ this.files = /* @__PURE__ */ new Map();
63
+ }
64
+ addFile(file) {
65
+ if (!this.files.has(file.destination)) this.files.set(file.destination, []);
66
+ this.files.get(file.destination).push(file);
67
+ }
68
+ *compute() {
69
+ for (const file of this.files.values()) {
70
+ if (file.filter((op) => op.important).length >= 2) throw new Error(`Error while trying to generate file: '${file[0].destination}'.
112
71
  Multiple important file is not yet supported.
113
72
  Please report this issue to https://github.com/vikejs/bati`);
114
- }
115
- const newOrder = orderBy(file, (op) => {
116
- if (op.kind === "file" && !op.important) return 1;
117
- if (op.kind === "file" && op.important) return 2;
118
- if (op.kind === "transform" && !op.important) return 3;
119
- if (op.kind === "transform" && op.important) return 4;
120
- throw new Error("Unhandled OperationsRearranger.compute orderBy case");
121
- });
122
- if (file[0].sourceAbsolute.endsWith(".d.ts")) {
123
- yield* newOrder.filter((op) => op.kind === "file");
124
- } else {
125
- const input = newOrder.filter((op) => op.kind === "file").at(-1);
126
- if (input) {
127
- yield input;
128
- }
129
- }
130
- yield* newOrder.filter((op) => op.kind !== "file");
131
- }
132
- }
73
+ const newOrder = orderBy(file, (op) => {
74
+ if (op.kind === "file" && !op.important) return 1;
75
+ if (op.kind === "file" && op.important) return 2;
76
+ if (op.kind === "transform" && !op.important) return 3;
77
+ if (op.kind === "transform" && op.important) return 4;
78
+ throw new Error("Unhandled OperationsRearranger.compute orderBy case");
79
+ });
80
+ if (file[0].sourceAbsolute.endsWith(".d.ts")) yield* newOrder.filter((op) => op.kind === "file");
81
+ else {
82
+ const input = newOrder.filter((op) => op.kind === "file").at(-1);
83
+ if (input) yield input;
84
+ }
85
+ yield* newOrder.filter((op) => op.kind !== "file");
86
+ }
87
+ }
133
88
  };
134
-
135
- // src/operations/transform.ts
136
- import { parse } from "path";
137
- import { formatCode } from "@batijs/core";
138
- var isWin = process.platform === "win32";
89
+ //#endregion
90
+ //#region src/operations/transform.ts
91
+ const isWin = process.platform === "win32";
139
92
  async function transformFileAfterExec(filepath, fileContent) {
140
- if (fileContent === void 0 || fileContent === null) return null;
141
- if (typeof fileContent === "object" && typeof fileContent.finalize === "function") {
142
- fileContent = fileContent.finalize();
143
- if (typeof fileContent !== "string") {
144
- throw new Error("finalize() must return a string");
145
- }
146
- }
147
- const parsed = parse(filepath);
148
- const toTest = [parsed.base, parsed.ext, parsed.name].filter(Boolean);
149
- for (const ext of toTest) {
150
- switch (ext) {
151
- case ".ts":
152
- case ".js":
153
- case ".tsx":
154
- case ".jsx":
155
- return formatCode(fileContent, {
156
- filepath
157
- });
158
- case ".env":
159
- case ".env.local":
160
- case ".env.development":
161
- case ".env.development.local":
162
- case ".env.test":
163
- case ".env.test.local":
164
- case ".env.production":
165
- case ".env.production.local":
166
- case ".html":
167
- case ".md":
168
- case ".toml":
169
- return fileContent;
170
- case ".json":
171
- case ".jsonc":
172
- if (typeof fileContent === "string") return fileContent;
173
- return JSON.stringify(fileContent, null, 2);
174
- case ".yml":
175
- case ".yaml":
176
- if (typeof fileContent === "string") return fileContent;
177
- return fileContent.toString();
178
- }
179
- }
180
- throw new Error(`Unsupported file extension ${parsed.base} (${filepath})`);
93
+ if (fileContent === void 0 || fileContent === null) return null;
94
+ if (typeof fileContent === "object" && typeof fileContent.finalize === "function") {
95
+ fileContent = fileContent.finalize();
96
+ if (typeof fileContent !== "string") throw new Error("finalize() must return a string");
97
+ }
98
+ const parsed = parse(filepath);
99
+ const toTest = [
100
+ parsed.base,
101
+ parsed.ext,
102
+ parsed.name
103
+ ].filter(Boolean);
104
+ for (const ext of toTest) switch (ext) {
105
+ case ".ts":
106
+ case ".js":
107
+ case ".tsx":
108
+ case ".jsx": return formatCode(fileContent, { filepath });
109
+ case ".env":
110
+ case ".env.local":
111
+ case ".env.development":
112
+ case ".env.development.local":
113
+ case ".env.test":
114
+ case ".env.test.local":
115
+ case ".env.production":
116
+ case ".env.production.local":
117
+ case ".html":
118
+ case ".md":
119
+ case ".toml": return fileContent;
120
+ case ".json":
121
+ case ".jsonc":
122
+ if (typeof fileContent === "string") return fileContent;
123
+ return JSON.stringify(fileContent, null, 2);
124
+ case ".yml":
125
+ case ".yaml":
126
+ if (typeof fileContent === "string") return fileContent;
127
+ return fileContent.toString();
128
+ }
129
+ throw new Error(`Unsupported file extension ${parsed.base} (${filepath})`);
181
130
  }
182
131
  async function importTransformer(p) {
183
- const importFile = isWin ? `file://${p}` : p;
184
- const f = await import(importFile);
185
- return f.default;
132
+ return (await (isWin ? import(`file://${p}`) : import(p))).default;
186
133
  }
187
- async function executeOperationTransform(op, {
188
- meta,
189
- previousOperationSameDestination,
190
- packageJson
191
- }) {
192
- const transformer = await importTransformer(op.sourceAbsolute);
193
- const previousContent = previousOperationSameDestination?.content;
194
- const fileContent = await transformFileAfterExec(
195
- op.destination,
196
- await transformer({
197
- readfile: previousContent ? () => previousContent : void 0,
198
- meta,
199
- source: op.source,
200
- target: op.destination,
201
- packageJson
202
- })
203
- );
204
- return {
205
- content: fileContent !== null ? fileContent : void 0
206
- };
134
+ async function executeOperationTransform(op, { meta, previousOperationSameDestination, packageJson }) {
135
+ const transformer = await importTransformer(op.sourceAbsolute);
136
+ const previousContent = previousOperationSameDestination?.content;
137
+ const fileContent = await transformFileAfterExec(op.destination, await transformer({
138
+ readfile: previousContent ? () => previousContent : void 0,
139
+ meta,
140
+ source: op.source,
141
+ target: op.destination,
142
+ packageJson
143
+ }));
144
+ return { content: fileContent !== null ? fileContent : void 0 };
207
145
  }
208
-
209
- // src/relations.ts
210
- import { extname } from "path";
211
- var RelationFile = class _RelationFile {
212
- constructor(pathAbsolute, includeIfImported) {
213
- this.pathAbsolute = pathAbsolute;
214
- this.includeIfImported = includeIfImported;
215
- _RelationFile.allPathAbsolute.set(pathAbsolute, this);
216
- if (includeIfImported) {
217
- _RelationFile.allIncludeIfImported.push(this);
218
- }
219
- }
220
- static allPathAbsolute = /* @__PURE__ */ new Map();
221
- static allIncludeIfImported = [];
146
+ //#endregion
147
+ //#region src/relations.ts
148
+ var RelationFile = class RelationFile {
149
+ static allPathAbsolute = /* @__PURE__ */ new Map();
150
+ static allIncludeIfImported = [];
151
+ constructor(pathAbsolute, includeIfImported) {
152
+ this.pathAbsolute = pathAbsolute;
153
+ this.includeIfImported = includeIfImported;
154
+ RelationFile.allPathAbsolute.set(pathAbsolute, this);
155
+ if (includeIfImported) RelationFile.allIncludeIfImported.push(this);
156
+ }
222
157
  };
223
- var RelationImport = class _RelationImport {
224
- constructor(source, importTarget) {
225
- this.source = source;
226
- this.importTarget = importTarget;
227
- _RelationImport.allImports.push(this);
228
- }
229
- static allImports = [];
230
- get importTargetRelationFile() {
231
- const potentialTargets = importToPotentialTargets(this.importTarget);
232
- for (const target of potentialTargets) {
233
- if (RelationFile.allPathAbsolute.has(target)) {
234
- return RelationFile.allPathAbsolute.get(target);
235
- }
236
- }
237
- return void 0;
238
- }
239
- static computeUnimportedFiles() {
240
- const unimportedFiles = [];
241
- const importedByVolatileFile = [];
242
- for (const file of RelationFile.allIncludeIfImported) {
243
- const importedFile = _RelationImport.allImports.find((ai) => ai.importTargetRelationFile === file);
244
- if (!importedFile) {
245
- unimportedFiles.push(file);
246
- } else if (importedFile.source.includeIfImported) {
247
- importedByVolatileFile.push(importedFile);
248
- }
249
- }
250
- return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
251
- }
158
+ var RelationImport = class RelationImport {
159
+ static allImports = [];
160
+ constructor(source, importTarget) {
161
+ this.source = source;
162
+ this.importTarget = importTarget;
163
+ RelationImport.allImports.push(this);
164
+ }
165
+ get importTargetRelationFile() {
166
+ const potentialTargets = importToPotentialTargets(this.importTarget);
167
+ for (const target of potentialTargets) if (RelationFile.allPathAbsolute.has(target)) return RelationFile.allPathAbsolute.get(target);
168
+ }
169
+ static computeUnimportedFiles() {
170
+ const unimportedFiles = [];
171
+ const importedByVolatileFile = [];
172
+ for (const file of RelationFile.allIncludeIfImported) {
173
+ const importedFile = RelationImport.allImports.find((ai) => ai.importTargetRelationFile === file);
174
+ if (!importedFile) unimportedFiles.push(file);
175
+ else if (importedFile.source.includeIfImported) importedByVolatileFile.push(importedFile);
176
+ }
177
+ return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
178
+ }
252
179
  };
253
180
  function computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles) {
254
- const copyImportedByVolatileFile = Array.from(importedByVolatileFile);
255
- let redo = false;
256
- for (const relationImport of copyImportedByVolatileFile) {
257
- const found = unimportedFiles.find((uf) => uf === relationImport.source);
258
- if (found) {
259
- redo = true;
260
- unimportedFiles.push(relationImport.importTargetRelationFile);
261
- importedByVolatileFile = importedByVolatileFile.filter((i) => i !== relationImport);
262
- }
263
- }
264
- if (redo) {
265
- computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
266
- }
267
- return unimportedFiles;
181
+ const copyImportedByVolatileFile = Array.from(importedByVolatileFile);
182
+ let redo = false;
183
+ for (const relationImport of copyImportedByVolatileFile) if (unimportedFiles.find((uf) => uf === relationImport.source)) {
184
+ redo = true;
185
+ unimportedFiles.push(relationImport.importTargetRelationFile);
186
+ importedByVolatileFile = importedByVolatileFile.filter((i) => i !== relationImport);
187
+ }
188
+ if (redo) computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
189
+ return unimportedFiles;
268
190
  }
269
191
  function importToPotentialTargets(imp) {
270
- let subject = imp;
271
- const ext = extname(imp);
272
- const targets = [];
273
- if (ext.match(/^\.[jt]sx?$/)) {
274
- subject = subject.replace(/^\.[jt]sx?$/, "");
275
- }
276
- if (!ext || subject !== imp) {
277
- targets.push(...[".js", ".jsx", ".ts", ".tsx", ".cjs", ".mjs"].map((e) => `${subject}${e}`));
278
- } else {
279
- targets.push(imp);
280
- }
281
- return targets;
192
+ let subject = imp;
193
+ const ext = extname(imp);
194
+ const targets = [];
195
+ if (ext.match(/^\.m?[jt]sx?$/)) subject = subject.replace(/^\.m?[jt]sx?$/, "");
196
+ if (!ext || subject !== imp) targets.push(...[
197
+ ".js",
198
+ ".jsx",
199
+ ".ts",
200
+ ".tsx",
201
+ ".cjs",
202
+ ".mjs"
203
+ ].map((e) => `${subject}${e}`));
204
+ else targets.push(imp);
205
+ return targets;
282
206
  }
283
-
284
- // src/index.ts
285
- var reIgnoreFile = /^(chunk-|asset-|#)/gi;
207
+ //#endregion
208
+ //#region src/index.ts
209
+ const reIgnoreFile = /^(chunk-|asset-|#)/gi;
286
210
  function toDist(filepath, source, dist) {
287
- const split = filepath.split(path.sep);
288
- split[split.length - 1] = split[split.length - 1].replace(/^\$\$?(.*)\.[tj]sx?$/, "$1").replace(/^!(.*)$/, "$1");
289
- return split.join(path.sep).replace(source, dist);
211
+ const split = filepath.split(path.sep);
212
+ split[split.length - 1] = split[split.length - 1].replace(/^\$\$?(.*)\.m?[tj]sx?$/, "$1").replace(/^!(.*)$/, "$1");
213
+ return split.join(path.sep).replace(source, dist);
290
214
  }
291
215
  async function safeWriteFile(destination, content) {
292
- const destinationDir = path.dirname(destination);
293
- await mkdir(destinationDir, {
294
- recursive: true
295
- });
296
- await writeFile(destination, content, { encoding: "utf-8" });
216
+ await mkdir(path.dirname(destination), { recursive: true });
217
+ await writeFile(destination, content, { encoding: "utf-8" });
297
218
  }
298
219
  async function safeRmFile(destination, options) {
299
- try {
300
- await rm(destination, {
301
- force: true,
302
- maxRetries: 3,
303
- recursive: false,
304
- retryDelay: 150
305
- });
306
- if (options?.removeEmptyDir) {
307
- try {
308
- await rmdir(path.dirname(destination), {
309
- maxRetries: 3,
310
- retryDelay: 150
311
- });
312
- } catch {
313
- }
314
- }
315
- } catch {
316
- console.warn(`Failed to remove unnecessary file: ${destination}`);
317
- }
220
+ try {
221
+ await rm(destination, {
222
+ force: true,
223
+ maxRetries: 3,
224
+ recursive: false,
225
+ retryDelay: 150
226
+ });
227
+ if (options?.removeEmptyDir) try {
228
+ await rmdir(path.dirname(destination), {
229
+ maxRetries: 3,
230
+ retryDelay: 150
231
+ });
232
+ } catch {}
233
+ } catch {
234
+ console.warn(`Failed to remove unnecessary file: ${destination}`);
235
+ }
318
236
  }
319
237
  async function* walk(dir) {
320
- if (!existsSync(dir)) return;
321
- for await (const d of await opendir(dir)) {
322
- const entry = path.join(dir, d.name);
323
- if (d.isDirectory()) {
324
- yield* walk(entry);
325
- } else if (d.isFile()) yield entry;
326
- }
238
+ if (!existsSync(dir)) return;
239
+ for await (const d of await opendir(dir)) {
240
+ if (d.name.match(reIgnoreFile)) continue;
241
+ const entry = path.join(dir, d.name);
242
+ if (d.isDirectory()) yield* walk(entry);
243
+ else if (d.isFile()) yield entry;
244
+ }
327
245
  }
328
246
  async function main(options, meta) {
329
- const sources = Array.isArray(options.source) ? options.source : [options.source];
330
- function updateAllImports(target, imports, includeIfImported) {
331
- const rf = new RelationFile(target, includeIfImported);
332
- if (!imports) return;
333
- for (const imp of imports.values()) {
334
- const importTarget = path.resolve(path.dirname(target), imp);
335
- new RelationImport(rf, importTarget);
336
- }
337
- }
338
- const rearranger = new OperationsRearranger();
339
- for (const source of sources) {
340
- for await (const p of walk(source)) {
341
- const target = toDist(p, source, options.dist);
342
- const targetAbsolute = path.isAbsolute(target) ? target : path.resolve(target);
343
- const parsed = path.parse(p);
344
- if (parsed.name.match(reIgnoreFile)) {
345
- } else if (parsed.name.startsWith("$") && parsed.ext.match(/\.tsx?$/)) {
346
- throw new Error(
347
- `Typescript file needs to be compiled before it can be executed: '${p}'.
348
- Please report this issue to https://github.com/vikejs/bati`
349
- );
350
- } else if ((parsed.name.startsWith("!$") || parsed.name.startsWith("$")) && parsed.ext.match(/\.jsx?$/)) {
351
- rearranger.addFile({
352
- source,
353
- sourceAbsolute: p,
354
- destination: target,
355
- destinationAbsolute: targetAbsolute,
356
- kind: "transform",
357
- parsed,
358
- important: parsed.name.startsWith("!")
359
- });
360
- } else {
361
- rearranger.addFile({
362
- source,
363
- sourceAbsolute: p,
364
- destination: target,
365
- destinationAbsolute: targetAbsolute,
366
- kind: "file",
367
- parsed,
368
- important: parsed.name.startsWith("!")
369
- });
370
- }
371
- }
372
- }
373
- let previousOp;
374
- let previousOpContent;
375
- let packageJson = {};
376
- const packageJsonDistAbsolute = path.join(
377
- path.isAbsolute(options.dist) ? options.dist : path.resolve(options.dist),
378
- "package.json"
379
- );
380
- for (const op of rearranger.compute()) {
381
- if (previousOp?.destination !== op.destination) {
382
- previousOp = void 0;
383
- previousOpContent = void 0;
384
- }
385
- let report = {};
386
- if (op.kind === "file") {
387
- report = await executeOperationFile(op, {
388
- meta,
389
- previousOperationSameDestination: previousOp
390
- });
391
- updateAllImports(
392
- op.destinationAbsolute,
393
- report.context?.imports,
394
- Boolean(report.context?.flags.has("include-if-imported"))
395
- );
396
- } else if (op.kind === "transform") {
397
- report = await executeOperationTransform(op, {
398
- meta,
399
- previousOperationSameDestination: previousOp,
400
- packageJson
401
- });
402
- }
403
- if (report.content) {
404
- await safeWriteFile(op.destination, report.content.trimStart());
405
- if (op.destinationAbsolute === packageJsonDistAbsolute) {
406
- packageJson = JSON.parse(report.content);
407
- }
408
- }
409
- previousOpContent = report.content ?? previousOpContent;
410
- previousOp = {
411
- ...op,
412
- ...report,
413
- content: previousOpContent
414
- };
415
- }
416
- for (const target of RelationImport.computeUnimportedFiles()) {
417
- await safeRmFile(target.pathAbsolute, { removeEmptyDir: true });
418
- }
247
+ const sources = Array.isArray(options.source) ? options.source : [options.source];
248
+ function updateAllImports(target, imports, includeIfImported) {
249
+ const rf = new RelationFile(target, includeIfImported);
250
+ if (!imports) return;
251
+ for (const imp of imports.values()) new RelationImport(rf, path.resolve(path.dirname(target), imp));
252
+ }
253
+ const rearranger = new OperationsRearranger();
254
+ for (const source of sources) for await (const p of walk(source)) {
255
+ const target = toDist(p, source, options.dist);
256
+ const targetAbsolute = path.isAbsolute(target) ? target : path.resolve(target);
257
+ const parsed = path.parse(p);
258
+ if (parsed.name.match(reIgnoreFile)) {} else if (parsed.name.startsWith("$") && parsed.ext.match(/\.m?tsx?$/)) throw new Error(`Typescript file needs to be compiled before it can be executed: '${p}'.
259
+ Please report this issue to https://github.com/vikejs/bati`);
260
+ else if ((parsed.name.startsWith("!$") || parsed.name.startsWith("$")) && parsed.ext.match(/\.m?jsx?$/)) rearranger.addFile({
261
+ source,
262
+ sourceAbsolute: p,
263
+ destination: target,
264
+ destinationAbsolute: targetAbsolute,
265
+ kind: "transform",
266
+ parsed,
267
+ important: parsed.name.startsWith("!")
268
+ });
269
+ else rearranger.addFile({
270
+ source,
271
+ sourceAbsolute: p,
272
+ destination: target,
273
+ destinationAbsolute: targetAbsolute,
274
+ kind: "file",
275
+ parsed,
276
+ important: parsed.name.startsWith("!")
277
+ });
278
+ }
279
+ let previousOp;
280
+ let previousOpContent;
281
+ let packageJson = {};
282
+ const packageJsonDistAbsolute = path.join(path.isAbsolute(options.dist) ? options.dist : path.resolve(options.dist), "package.json");
283
+ for (const op of rearranger.compute()) {
284
+ if (previousOp?.destination !== op.destination) {
285
+ previousOp = void 0;
286
+ previousOpContent = void 0;
287
+ }
288
+ let report = {};
289
+ if (op.kind === "file") {
290
+ report = await executeOperationFile(op, {
291
+ meta,
292
+ previousOperationSameDestination: previousOp
293
+ });
294
+ updateAllImports(op.destinationAbsolute, report.context?.imports, Boolean(report.context?.flags.has("include-if-imported")));
295
+ } else if (op.kind === "transform") report = await executeOperationTransform(op, {
296
+ meta,
297
+ previousOperationSameDestination: previousOp,
298
+ packageJson
299
+ });
300
+ if (report.content) {
301
+ await safeWriteFile(op.destination, report.content.trimStart());
302
+ if (op.destinationAbsolute === packageJsonDistAbsolute) packageJson = JSON.parse(report.content);
303
+ }
304
+ previousOpContent = report.content ?? previousOpContent;
305
+ previousOp = {
306
+ ...op,
307
+ ...report,
308
+ content: previousOpContent
309
+ };
310
+ }
311
+ for (const target of RelationImport.computeUnimportedFiles()) await safeRmFile(target.pathAbsolute, { removeEmptyDir: true });
419
312
  }
420
- export {
421
- main as default,
422
- walk
423
- };
313
+ //#endregion
314
+ export { main as default, walk };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@batijs/build",
3
- "version": "0.0.599",
3
+ "version": "0.0.600",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -9,12 +9,13 @@
9
9
  "license": "MIT",
10
10
  "devDependencies": {
11
11
  "@types/node": "^20.19.37",
12
- "tsup": "^8.5.1",
13
- "@batijs/features": "0.0.599",
14
- "@batijs/compile": "0.0.599"
12
+ "tsdown": "^0.21.7",
13
+ "@batijs/core": "0.0.600",
14
+ "@batijs/features": "0.0.600",
15
+ "@batijs/compile": "0.0.600"
15
16
  },
16
- "dependencies": {
17
- "@batijs/core": "0.0.599"
17
+ "peerDependencies": {
18
+ "@batijs/core": "0.0.600"
18
19
  },
19
20
  "main": "./dist/index.js",
20
21
  "module": "./dist/index.js",
@@ -33,6 +34,6 @@
33
34
  ],
34
35
  "scripts": {
35
36
  "check-types": "tsc --noEmit",
36
- "build": "tsup"
37
+ "build": "tsdown"
37
38
  }
38
39
  }