@content-collections/core 0.7.2 → 0.8.0
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/README.md +1 -1
- package/dist/index.d.ts +22 -2
- package/dist/index.js +130 -58
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ Transform your content into type-safe data collections. Eliminate the need for m
|
|
|
17
17
|
- **Simple to use**:
|
|
18
18
|
No need to manually fetch and parse your content anymore. Just import it and start using Content Collections. It provides a simple API, allowing you to concentrate on building your app.
|
|
19
19
|
|
|
20
|
-
- **
|
|
20
|
+
- **Transformation**:
|
|
21
21
|
Content Collections allows you to transform your content before it enters your app. You can use it to modify your content, join two collections or even fetch data from a server.
|
|
22
22
|
|
|
23
23
|
## Installation
|
package/dist/index.d.ts
CHANGED
|
@@ -3,17 +3,34 @@ export * from 'zod';
|
|
|
3
3
|
|
|
4
4
|
type CacheFn = <TInput, TOutput>(input: TInput, compute: (input: TInput) => Promise<TOutput> | TOutput) => Promise<TOutput>;
|
|
5
5
|
|
|
6
|
+
declare const importSymbol: unique symbol;
|
|
7
|
+
type Import<T> = {
|
|
8
|
+
[importSymbol]: true;
|
|
9
|
+
path: string;
|
|
10
|
+
name?: string;
|
|
11
|
+
};
|
|
12
|
+
type GetTypeOfImport<T> = T extends Import<infer U> ? U : never;
|
|
13
|
+
declare function createDefaultImport<T>(path: string): Import<T>;
|
|
14
|
+
declare function createNamedImport<T>(name: string, path: string): Import<T>;
|
|
15
|
+
|
|
6
16
|
type Parsers = typeof parsers;
|
|
7
17
|
type Parser = keyof typeof parsers;
|
|
8
18
|
declare function parseYaml(content: string): any;
|
|
9
19
|
declare function frontmatterParser(fileContent: string): {
|
|
10
20
|
content: string;
|
|
11
21
|
};
|
|
22
|
+
declare function frontmatterOnlyParser(fileContent: string): {
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
};
|
|
12
25
|
declare const parsers: {
|
|
13
26
|
readonly frontmatter: {
|
|
14
27
|
readonly hasContent: true;
|
|
15
28
|
readonly parse: typeof frontmatterParser;
|
|
16
29
|
};
|
|
30
|
+
readonly "frontmatter-only": {
|
|
31
|
+
readonly hasContent: false;
|
|
32
|
+
readonly parse: typeof frontmatterOnlyParser;
|
|
33
|
+
};
|
|
17
34
|
readonly json: {
|
|
18
35
|
readonly hasContent: false;
|
|
19
36
|
readonly parse: (text: string, reviver?: (this: any, key: string, value: any) => any) => any;
|
|
@@ -86,7 +103,10 @@ type InvalidReturnType<TMessage extends string, TObject> = {
|
|
|
86
103
|
[InvalidReturnTypeSymbol]: TMessage;
|
|
87
104
|
object: TObject;
|
|
88
105
|
};
|
|
89
|
-
|
|
106
|
+
type ResolveImports<TTransformResult> = TTransformResult extends Import<any> ? GetTypeOfImport<TTransformResult> : TTransformResult extends Array<infer U> ? Array<ResolveImports<U>> : TTransformResult extends (...args: any[]) => any ? TTransformResult : TTransformResult extends object ? {
|
|
107
|
+
[K in keyof TTransformResult]: ResolveImports<TTransformResult[K]>;
|
|
108
|
+
} : TTransformResult;
|
|
109
|
+
declare function defineCollection<TName extends string, TShape extends ZodRawShape, TParser extends Parser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends Serializable ? Collection<TName, TShape, TParser, TSchema, TTransformResult, ResolveImports<TDocument>> : InvalidReturnType<NotSerializableError, TDocument>>(collection: CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>): TResult;
|
|
90
110
|
type Cache = "memory" | "file" | "none";
|
|
91
111
|
type Configuration<TCollections extends Array<AnyCollection>> = {
|
|
92
112
|
collections: TCollections;
|
|
@@ -261,4 +281,4 @@ declare function createBuilder(configurationPath: string, options?: Options, emi
|
|
|
261
281
|
}>;
|
|
262
282
|
type Builder = Awaited<ReturnType<typeof createBuilder>>;
|
|
263
283
|
|
|
264
|
-
export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, ConfigurationReloadError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, type Watcher, createBuilder, defineCollection, defineConfig };
|
|
284
|
+
export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, ConfigurationReloadError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, type Watcher, createBuilder, createDefaultImport, createNamedImport, defineCollection, defineConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/builder.ts
|
|
2
|
-
import
|
|
2
|
+
import path9 from "node:path";
|
|
3
3
|
|
|
4
4
|
// src/cache.ts
|
|
5
5
|
import { createHash } from "node:crypto";
|
|
@@ -102,7 +102,7 @@ async function createCacheManager(baseDirectory, configChecksum) {
|
|
|
102
102
|
|
|
103
103
|
// src/collector.ts
|
|
104
104
|
import { readFile as readFile2 } from "fs/promises";
|
|
105
|
-
import
|
|
105
|
+
import path3 from "node:path";
|
|
106
106
|
import { glob } from "tinyglobby";
|
|
107
107
|
|
|
108
108
|
// src/parser.ts
|
|
@@ -111,8 +111,8 @@ import { parse, stringify } from "yaml";
|
|
|
111
111
|
function parseYaml(content) {
|
|
112
112
|
return parse(content.trim());
|
|
113
113
|
}
|
|
114
|
-
function
|
|
115
|
-
|
|
114
|
+
function frontmatter(fileContent) {
|
|
115
|
+
return matter(fileContent, {
|
|
116
116
|
engines: {
|
|
117
117
|
yaml: {
|
|
118
118
|
parse: parseYaml,
|
|
@@ -120,16 +120,27 @@ function frontmatterParser(fileContent) {
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
});
|
|
123
|
+
}
|
|
124
|
+
function frontmatterParser(fileContent) {
|
|
125
|
+
const { data, content } = frontmatter(fileContent);
|
|
123
126
|
return {
|
|
124
127
|
...data,
|
|
125
128
|
content: content.trim()
|
|
126
129
|
};
|
|
127
130
|
}
|
|
131
|
+
function frontmatterOnlyParser(fileContent) {
|
|
132
|
+
const { data } = frontmatter(fileContent);
|
|
133
|
+
return data;
|
|
134
|
+
}
|
|
128
135
|
var parsers = {
|
|
129
136
|
frontmatter: {
|
|
130
137
|
hasContent: true,
|
|
131
138
|
parse: frontmatterParser
|
|
132
139
|
},
|
|
140
|
+
["frontmatter-only"]: {
|
|
141
|
+
hasContent: false,
|
|
142
|
+
parse: frontmatterOnlyParser
|
|
143
|
+
},
|
|
133
144
|
json: {
|
|
134
145
|
hasContent: false,
|
|
135
146
|
parse: JSON.parse
|
|
@@ -143,6 +154,7 @@ var parsers = {
|
|
|
143
154
|
// src/utils.ts
|
|
144
155
|
import camelcase from "camelcase";
|
|
145
156
|
import pluralize from "pluralize";
|
|
157
|
+
import path2 from "node:path";
|
|
146
158
|
function generateTypeName(name) {
|
|
147
159
|
const singularName = pluralize.singular(name);
|
|
148
160
|
return camelcase(singularName, { pascalCase: true });
|
|
@@ -156,17 +168,23 @@ function orderByPath(a, b) {
|
|
|
156
168
|
function removeChildPaths(paths) {
|
|
157
169
|
return Array.from(
|
|
158
170
|
new Set(
|
|
159
|
-
paths.filter((
|
|
171
|
+
paths.filter((path10) => {
|
|
160
172
|
return !paths.some((otherPath) => {
|
|
161
|
-
if (
|
|
173
|
+
if (path10 === otherPath) {
|
|
162
174
|
return false;
|
|
163
175
|
}
|
|
164
|
-
return
|
|
176
|
+
return path10.startsWith(otherPath);
|
|
165
177
|
});
|
|
166
178
|
})
|
|
167
179
|
)
|
|
168
180
|
);
|
|
169
181
|
}
|
|
182
|
+
function posixToNativePath(pathName) {
|
|
183
|
+
if (path2.sep !== path2.posix.sep) {
|
|
184
|
+
return pathName.replaceAll(path2.posix.sep, path2.sep);
|
|
185
|
+
}
|
|
186
|
+
return pathName;
|
|
187
|
+
}
|
|
170
188
|
|
|
171
189
|
// src/collector.ts
|
|
172
190
|
var CollectError = class extends Error {
|
|
@@ -189,7 +207,7 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
189
207
|
}
|
|
190
208
|
}
|
|
191
209
|
async function collectFile(collection, filePath) {
|
|
192
|
-
const absolutePath =
|
|
210
|
+
const absolutePath = path3.join(
|
|
193
211
|
baseDirectory,
|
|
194
212
|
collection.directory,
|
|
195
213
|
filePath
|
|
@@ -206,7 +224,7 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
206
224
|
};
|
|
207
225
|
} catch (error) {
|
|
208
226
|
emitter.emit("collector:parse-error", {
|
|
209
|
-
filePath:
|
|
227
|
+
filePath: path3.join(collection.directory, filePath),
|
|
210
228
|
error: new CollectError("Parse", String(error))
|
|
211
229
|
});
|
|
212
230
|
return null;
|
|
@@ -223,7 +241,7 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
223
241
|
return void 0;
|
|
224
242
|
}
|
|
225
243
|
async function resolveCollection(collection) {
|
|
226
|
-
const collectionDirectory =
|
|
244
|
+
const collectionDirectory = path3.join(baseDirectory, collection.directory);
|
|
227
245
|
const include = Array.isArray(collection.include) ? collection.include : [collection.include];
|
|
228
246
|
const filePaths = await glob(include, {
|
|
229
247
|
cwd: collectionDirectory,
|
|
@@ -232,7 +250,7 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
232
250
|
ignore: createIgnorePattern(collection)
|
|
233
251
|
});
|
|
234
252
|
const promises = filePaths.map(
|
|
235
|
-
(filePath) => collectFile(collection, filePath)
|
|
253
|
+
(filePath) => collectFile(collection, posixToNativePath(filePath))
|
|
236
254
|
);
|
|
237
255
|
const files = await Promise.all(promises);
|
|
238
256
|
return {
|
|
@@ -253,23 +271,23 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
253
271
|
}
|
|
254
272
|
|
|
255
273
|
// src/synchronizer.ts
|
|
256
|
-
import
|
|
274
|
+
import path4 from "node:path";
|
|
257
275
|
import picomatch from "picomatch";
|
|
258
276
|
function createSynchronizer(readCollectionFile, collections, baseDirectory = ".") {
|
|
259
277
|
function findCollections(filePath) {
|
|
260
|
-
const resolvedFilePath =
|
|
278
|
+
const resolvedFilePath = path4.resolve(filePath);
|
|
261
279
|
return collections.filter((collection) => {
|
|
262
280
|
return resolvedFilePath.startsWith(
|
|
263
|
-
|
|
281
|
+
path4.resolve(baseDirectory, collection.directory)
|
|
264
282
|
);
|
|
265
283
|
});
|
|
266
284
|
}
|
|
267
285
|
function createRelativePath(collectionPath, filePath) {
|
|
268
|
-
const resolvedCollectionPath =
|
|
269
|
-
const resolvedFilePath =
|
|
286
|
+
const resolvedCollectionPath = path4.resolve(baseDirectory, collectionPath);
|
|
287
|
+
const resolvedFilePath = path4.resolve(filePath);
|
|
270
288
|
let relativePath = resolvedFilePath.slice(resolvedCollectionPath.length);
|
|
271
|
-
if (relativePath.startsWith(
|
|
272
|
-
relativePath = relativePath.slice(
|
|
289
|
+
if (relativePath.startsWith(path4.sep)) {
|
|
290
|
+
relativePath = relativePath.slice(path4.sep.length);
|
|
273
291
|
}
|
|
274
292
|
return relativePath;
|
|
275
293
|
}
|
|
@@ -342,6 +360,27 @@ import { z as z2 } from "zod";
|
|
|
342
360
|
// src/serializer.ts
|
|
343
361
|
import serializeJs from "serialize-javascript";
|
|
344
362
|
import z from "zod";
|
|
363
|
+
|
|
364
|
+
// src/import.ts
|
|
365
|
+
var importSymbol = Symbol("import");
|
|
366
|
+
function isImport(value) {
|
|
367
|
+
return value && value[importSymbol];
|
|
368
|
+
}
|
|
369
|
+
function createDefaultImport(path10) {
|
|
370
|
+
return {
|
|
371
|
+
[importSymbol]: true,
|
|
372
|
+
path: path10
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function createNamedImport(name, path10) {
|
|
376
|
+
return {
|
|
377
|
+
[importSymbol]: true,
|
|
378
|
+
path: path10,
|
|
379
|
+
name
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// src/serializer.ts
|
|
345
384
|
var literalSchema = z.union([
|
|
346
385
|
// json
|
|
347
386
|
z.string(),
|
|
@@ -360,13 +399,39 @@ var schema = z.lazy(
|
|
|
360
399
|
);
|
|
361
400
|
var extension = "js";
|
|
362
401
|
var serializableSchema = z.record(schema);
|
|
402
|
+
function createImport(imp, variableName) {
|
|
403
|
+
const variableDeclaration = imp.name ? `{ ${imp.name} as ${variableName} }` : variableName;
|
|
404
|
+
return `import ${variableDeclaration} from "${imp.path}";
|
|
405
|
+
`;
|
|
406
|
+
}
|
|
363
407
|
function serialize(value) {
|
|
364
|
-
|
|
408
|
+
let serializedValue = "";
|
|
409
|
+
let counter = 0;
|
|
410
|
+
function handleImports(item) {
|
|
411
|
+
if (item instanceof Object) {
|
|
412
|
+
Object.entries(item).forEach(([key, value2]) => {
|
|
413
|
+
if (isImport(value2)) {
|
|
414
|
+
counter++;
|
|
415
|
+
const variableName = `__v_${counter}`;
|
|
416
|
+
serializedValue += createImport(value2, variableName);
|
|
417
|
+
item[key] = variableName;
|
|
418
|
+
} else if (value2 instanceof Object) {
|
|
419
|
+
handleImports(value2);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
value.forEach(handleImports);
|
|
425
|
+
serializedValue += "\n";
|
|
426
|
+
const js = serializeJs(value, {
|
|
365
427
|
space: 2,
|
|
366
428
|
unsafe: true,
|
|
367
429
|
ignoreFunction: true
|
|
430
|
+
}).replace(/"__v_(\d+)"/g, (_, index) => {
|
|
431
|
+
return `__v_${index}`;
|
|
368
432
|
});
|
|
369
|
-
|
|
433
|
+
serializedValue += "export default " + js;
|
|
434
|
+
return serializedValue;
|
|
370
435
|
}
|
|
371
436
|
|
|
372
437
|
// src/transformer.ts
|
|
@@ -377,8 +442,8 @@ var TransformError = class extends Error {
|
|
|
377
442
|
this.type = type;
|
|
378
443
|
}
|
|
379
444
|
};
|
|
380
|
-
function createPath(
|
|
381
|
-
let p =
|
|
445
|
+
function createPath(path10, ext) {
|
|
446
|
+
let p = path10.slice(0, -ext.length);
|
|
382
447
|
if (p.endsWith("/index")) {
|
|
383
448
|
p = p.slice(0, -6);
|
|
384
449
|
}
|
|
@@ -396,7 +461,7 @@ function createTransformer(emitter, cacheManager) {
|
|
|
396
461
|
});
|
|
397
462
|
}
|
|
398
463
|
async function parseFile(collection, file) {
|
|
399
|
-
const { data, path:
|
|
464
|
+
const { data, path: path10 } = file;
|
|
400
465
|
const schema2 = createSchema(collection.parser, collection.schema);
|
|
401
466
|
let parsedData = await schema2.safeParseAsync(data);
|
|
402
467
|
if (!parsedData.success) {
|
|
@@ -407,7 +472,7 @@ function createTransformer(emitter, cacheManager) {
|
|
|
407
472
|
});
|
|
408
473
|
return null;
|
|
409
474
|
}
|
|
410
|
-
const ext = extname(
|
|
475
|
+
const ext = extname(path10);
|
|
411
476
|
let extension2 = ext;
|
|
412
477
|
if (extension2.startsWith(".")) {
|
|
413
478
|
extension2 = extension2.slice(1);
|
|
@@ -415,11 +480,11 @@ function createTransformer(emitter, cacheManager) {
|
|
|
415
480
|
const document = {
|
|
416
481
|
...parsedData.data,
|
|
417
482
|
_meta: {
|
|
418
|
-
filePath:
|
|
419
|
-
fileName: basename(
|
|
420
|
-
directory: dirname(
|
|
483
|
+
filePath: path10,
|
|
484
|
+
fileName: basename(path10),
|
|
485
|
+
directory: dirname(path10),
|
|
421
486
|
extension: extension2,
|
|
422
|
-
path: createPath(
|
|
487
|
+
path: createPath(path10, ext)
|
|
423
488
|
}
|
|
424
489
|
};
|
|
425
490
|
return {
|
|
@@ -525,14 +590,14 @@ function createTransformer(emitter, cacheManager) {
|
|
|
525
590
|
|
|
526
591
|
// src/writer.ts
|
|
527
592
|
import fs from "node:fs/promises";
|
|
528
|
-
import
|
|
593
|
+
import path5 from "node:path";
|
|
529
594
|
import pluralize2 from "pluralize";
|
|
530
595
|
function createArrayConstName(name) {
|
|
531
596
|
let suffix = name.charAt(0).toUpperCase() + name.slice(1);
|
|
532
597
|
return "all" + pluralize2(suffix);
|
|
533
598
|
}
|
|
534
599
|
async function createDataFile(directory, collection) {
|
|
535
|
-
const dataPath =
|
|
600
|
+
const dataPath = path5.join(
|
|
536
601
|
directory,
|
|
537
602
|
`${createArrayConstName(collection.name)}.${extension}`
|
|
538
603
|
);
|
|
@@ -559,11 +624,11 @@ async function createJavaScriptFile(directory, configuration) {
|
|
|
559
624
|
}
|
|
560
625
|
content += "\n";
|
|
561
626
|
content += "export { " + collections.join(", ") + " };\n";
|
|
562
|
-
await fs.writeFile(
|
|
627
|
+
await fs.writeFile(path5.join(directory, "index.js"), content, "utf-8");
|
|
563
628
|
}
|
|
564
629
|
function createImportPath(directory, target) {
|
|
565
|
-
let importPath =
|
|
566
|
-
...
|
|
630
|
+
let importPath = path5.posix.join(
|
|
631
|
+
...path5.relative(directory, target).split(path5.sep)
|
|
567
632
|
);
|
|
568
633
|
if (!importPath.startsWith(".")) {
|
|
569
634
|
importPath = "./" + importPath;
|
|
@@ -591,7 +656,7 @@ import { GetTypeByName } from "@content-collections/core";
|
|
|
591
656
|
}
|
|
592
657
|
content += "\n";
|
|
593
658
|
content += "export {};\n";
|
|
594
|
-
await fs.writeFile(
|
|
659
|
+
await fs.writeFile(path5.join(directory, "index.d.ts"), content, "utf-8");
|
|
595
660
|
}
|
|
596
661
|
async function createWriter(directory) {
|
|
597
662
|
await fs.mkdir(directory, { recursive: true });
|
|
@@ -674,7 +739,7 @@ async function build({
|
|
|
674
739
|
import { createHash as createHash2 } from "node:crypto";
|
|
675
740
|
import { existsSync as existsSync2 } from "node:fs";
|
|
676
741
|
import fs3 from "node:fs/promises";
|
|
677
|
-
import
|
|
742
|
+
import path7 from "node:path";
|
|
678
743
|
|
|
679
744
|
// ../../node_modules/.pnpm/bundle-require@5.0.0_esbuild@0.21.4/node_modules/bundle-require/dist/index.js
|
|
680
745
|
import {
|
|
@@ -683,7 +748,7 @@ import {
|
|
|
683
748
|
} from "esbuild";
|
|
684
749
|
|
|
685
750
|
// ../../node_modules/.pnpm/load-tsconfig@0.2.5/node_modules/load-tsconfig/dist/index.js
|
|
686
|
-
import
|
|
751
|
+
import path6 from "path";
|
|
687
752
|
import fs2 from "fs";
|
|
688
753
|
import { createRequire } from "module";
|
|
689
754
|
var singleComment = Symbol("singleComment");
|
|
@@ -780,10 +845,10 @@ function jsoncParse(data) {
|
|
|
780
845
|
}
|
|
781
846
|
}
|
|
782
847
|
var req = true ? createRequire(import.meta.url) : __require;
|
|
783
|
-
var findUp = (name, startDir, stopDir =
|
|
848
|
+
var findUp = (name, startDir, stopDir = path6.parse(startDir).root) => {
|
|
784
849
|
let dir = startDir;
|
|
785
850
|
while (dir !== stopDir) {
|
|
786
|
-
const file =
|
|
851
|
+
const file = path6.join(dir, name);
|
|
787
852
|
if (fs2.existsSync(file))
|
|
788
853
|
return file;
|
|
789
854
|
if (!file.endsWith(".json")) {
|
|
@@ -791,17 +856,17 @@ var findUp = (name, startDir, stopDir = path5.parse(startDir).root) => {
|
|
|
791
856
|
if (fs2.existsSync(fileWithExt))
|
|
792
857
|
return fileWithExt;
|
|
793
858
|
}
|
|
794
|
-
dir =
|
|
859
|
+
dir = path6.dirname(dir);
|
|
795
860
|
}
|
|
796
861
|
return null;
|
|
797
862
|
};
|
|
798
863
|
var resolveTsConfigFromFile = (cwd, filename) => {
|
|
799
|
-
if (
|
|
864
|
+
if (path6.isAbsolute(filename))
|
|
800
865
|
return fs2.existsSync(filename) ? filename : null;
|
|
801
866
|
return findUp(filename, cwd);
|
|
802
867
|
};
|
|
803
868
|
var resolveTsConfigFromExtends = (cwd, name) => {
|
|
804
|
-
if (
|
|
869
|
+
if (path6.isAbsolute(name))
|
|
805
870
|
return fs2.existsSync(name) ? name : null;
|
|
806
871
|
if (name.startsWith("."))
|
|
807
872
|
return findUp(name, cwd);
|
|
@@ -810,14 +875,14 @@ var resolveTsConfigFromExtends = (cwd, name) => {
|
|
|
810
875
|
};
|
|
811
876
|
var loadTsConfigInternal = (dir = process.cwd(), name = "tsconfig.json", isExtends = false) => {
|
|
812
877
|
var _a, _b;
|
|
813
|
-
dir =
|
|
878
|
+
dir = path6.resolve(dir);
|
|
814
879
|
const id = isExtends ? resolveTsConfigFromExtends(dir, name) : resolveTsConfigFromFile(dir, name);
|
|
815
880
|
if (!id)
|
|
816
881
|
return null;
|
|
817
882
|
const data = jsoncParse(fs2.readFileSync(id, "utf-8"));
|
|
818
|
-
const configDir =
|
|
883
|
+
const configDir = path6.dirname(id);
|
|
819
884
|
if ((_a = data.compilerOptions) == null ? void 0 : _a.baseUrl) {
|
|
820
|
-
data.compilerOptions.baseUrl =
|
|
885
|
+
data.compilerOptions.baseUrl = path6.join(
|
|
821
886
|
configDir,
|
|
822
887
|
data.compilerOptions.baseUrl
|
|
823
888
|
);
|
|
@@ -880,21 +945,26 @@ function tsconfigResolvePaths(configPath) {
|
|
|
880
945
|
}
|
|
881
946
|
return tsconfig?.data?.compilerOptions?.paths || {};
|
|
882
947
|
}
|
|
948
|
+
var NON_NODE_MODULE_RE = /^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/;
|
|
883
949
|
function createExternalsPlugin(configPath) {
|
|
884
950
|
const resolvedPaths = tsconfigResolvePaths(configPath);
|
|
885
951
|
const resolvePatterns = tsconfigPathsToRegExp(resolvedPaths);
|
|
886
952
|
return {
|
|
887
953
|
name: "external-packages",
|
|
888
954
|
setup: (build4) => {
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
if (match(path9, resolvePatterns)) {
|
|
955
|
+
build4.onResolve({ filter: /.*/ }, ({ path: path10, kind }) => {
|
|
956
|
+
if (match(path10, resolvePatterns)) {
|
|
892
957
|
if (kind === "dynamic-import") {
|
|
893
|
-
return { path:
|
|
958
|
+
return { path: path10, external: true };
|
|
894
959
|
}
|
|
895
960
|
return;
|
|
896
961
|
}
|
|
897
|
-
|
|
962
|
+
if (!NON_NODE_MODULE_RE.test(path10)) {
|
|
963
|
+
return {
|
|
964
|
+
path: path10,
|
|
965
|
+
external: true
|
|
966
|
+
};
|
|
967
|
+
}
|
|
898
968
|
});
|
|
899
969
|
}
|
|
900
970
|
};
|
|
@@ -938,7 +1008,7 @@ function resolveCacheDir(config, options) {
|
|
|
938
1008
|
if (options.cacheDir) {
|
|
939
1009
|
return options.cacheDir;
|
|
940
1010
|
}
|
|
941
|
-
return
|
|
1011
|
+
return path7.join(path7.dirname(config), ".content-collections", "cache");
|
|
942
1012
|
}
|
|
943
1013
|
function createConfigurationReader() {
|
|
944
1014
|
return async (configurationPath, options = {
|
|
@@ -952,17 +1022,17 @@ function createConfigurationReader() {
|
|
|
952
1022
|
}
|
|
953
1023
|
const cacheDir = resolveCacheDir(configurationPath, options);
|
|
954
1024
|
await fs3.mkdir(cacheDir, { recursive: true });
|
|
955
|
-
const outfile =
|
|
1025
|
+
const outfile = path7.join(cacheDir, options.configName);
|
|
956
1026
|
try {
|
|
957
1027
|
const configurationPaths = await compile(configurationPath, outfile);
|
|
958
|
-
const module = await import(`file://${
|
|
1028
|
+
const module = await import(`file://${path7.resolve(outfile)}?x=${Date.now()}`);
|
|
959
1029
|
const hash = createHash2("sha256");
|
|
960
1030
|
hash.update(await fs3.readFile(outfile, "utf-8"));
|
|
961
1031
|
const checksum = hash.digest("hex");
|
|
962
1032
|
return {
|
|
963
1033
|
...module.default,
|
|
964
1034
|
path: configurationPath,
|
|
965
|
-
inputPaths: configurationPaths.map((p) =>
|
|
1035
|
+
inputPaths: configurationPaths.map((p) => path7.resolve(p)),
|
|
966
1036
|
generateTypes: true,
|
|
967
1037
|
checksum
|
|
968
1038
|
};
|
|
@@ -1006,7 +1076,7 @@ function createEmitter() {
|
|
|
1006
1076
|
|
|
1007
1077
|
// src/watcher.ts
|
|
1008
1078
|
import * as watcher from "@parcel/watcher";
|
|
1009
|
-
import
|
|
1079
|
+
import path8, { dirname as dirname3, resolve } from "node:path";
|
|
1010
1080
|
async function createWatcher(emitter, baseDirectory, configuration, sync) {
|
|
1011
1081
|
const onChange = async (error, events) => {
|
|
1012
1082
|
if (error) {
|
|
@@ -1021,10 +1091,10 @@ async function createWatcher(emitter, baseDirectory, configuration, sync) {
|
|
|
1021
1091
|
}
|
|
1022
1092
|
};
|
|
1023
1093
|
const paths = removeChildPaths([
|
|
1024
|
-
...configuration.collections.map((collection) =>
|
|
1094
|
+
...configuration.collections.map((collection) => path8.join(baseDirectory, collection.directory)).map((p) => resolve(p)),
|
|
1025
1095
|
...configuration.inputPaths.map((p) => dirname3(p))
|
|
1026
1096
|
]);
|
|
1027
|
-
const subscriptions = (await Promise.all(paths.map((
|
|
1097
|
+
const subscriptions = (await Promise.all(paths.map((path10) => watcher.subscribe(path10, onChange)))).filter(isDefined);
|
|
1028
1098
|
emitter.emit("watcher:subscribed", {
|
|
1029
1099
|
paths
|
|
1030
1100
|
});
|
|
@@ -1049,7 +1119,7 @@ function resolveOutputDir(baseDirectory, options) {
|
|
|
1049
1119
|
if (options.outputDir) {
|
|
1050
1120
|
return options.outputDir;
|
|
1051
1121
|
}
|
|
1052
|
-
return
|
|
1122
|
+
return path9.join(baseDirectory, ".content-collections", "generated");
|
|
1053
1123
|
}
|
|
1054
1124
|
var ConfigurationReloadError = class extends Error {
|
|
1055
1125
|
constructor(message) {
|
|
@@ -1060,7 +1130,7 @@ async function createBuilder(configurationPath, options = {
|
|
|
1060
1130
|
configName: defaultConfigName
|
|
1061
1131
|
}, emitter = createEmitter()) {
|
|
1062
1132
|
const readConfiguration = createConfigurationReader();
|
|
1063
|
-
const baseDirectory =
|
|
1133
|
+
const baseDirectory = path9.dirname(configurationPath);
|
|
1064
1134
|
const outputDirectory = resolveOutputDir(baseDirectory, options);
|
|
1065
1135
|
emitter.emit("builder:created", {
|
|
1066
1136
|
createdAt: Date.now(),
|
|
@@ -1182,6 +1252,8 @@ export {
|
|
|
1182
1252
|
ConfigurationReloadError,
|
|
1183
1253
|
TransformError,
|
|
1184
1254
|
createBuilder,
|
|
1255
|
+
createDefaultImport,
|
|
1256
|
+
createNamedImport,
|
|
1185
1257
|
defineCollection,
|
|
1186
1258
|
defineConfig
|
|
1187
1259
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@content-collections/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -24,11 +24,11 @@
|
|
|
24
24
|
"@types/picomatch": "^3.0.1",
|
|
25
25
|
"@types/pluralize": "^0.0.33",
|
|
26
26
|
"@types/serialize-javascript": "^5.0.4",
|
|
27
|
-
"@vitest/coverage-v8": "^2.1.
|
|
27
|
+
"@vitest/coverage-v8": "^2.1.5",
|
|
28
28
|
"tsup": "^8.2.4",
|
|
29
|
-
"tsx": "^4.
|
|
29
|
+
"tsx": "^4.19.2",
|
|
30
30
|
"typescript": "^5.5.4",
|
|
31
|
-
"vitest": "^2.1.
|
|
31
|
+
"vitest": "^2.1.5"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@parcel/watcher": "^2.4.1",
|