Skip to content

Commit 84bfd33

Browse files
authored
fix: use import attributes rather than json assertions
1 parent 9f5304d commit 84bfd33

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

packages/@contentlayer/core/src/generation/generate-dotpkg.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,16 @@ const writeFilesForCache = ({
192192
T.succeedWith(() => process.versions.node.split('.').map((_) => parseInt(_, 10)) as [number, number, number]),
193193
)
194194

195-
// NOTE Type assert statements for `.json` files are neccessary from Node v16.14 onwards
196-
const needsJsonAssertStatement = nodeVersionMajor > 16 || (nodeVersionMajor === 16 && nodeVersionMinor >= 14)
197-
const assertStatement = needsJsonAssertStatement ? ` assert { type: 'json' }` : ''
195+
// NOTE: An import attribute for `.json` files is neccessary from Node v16.14 onwards.
196+
const needsTypeJsonAttribute = nodeVersionMajor > 16 || (nodeVersionMajor === 16 && nodeVersionMinor >= 14)
197+
const importAttributeStatement = needsJsonimportAttributeStatement ? ` with { type: 'json' }` : ''
198198

199199
const typeNameField = generationOptions.options.fieldOptions.typeFieldName
200200
const dataBarrelFiles = documentDefs.map((docDef) => ({
201201
content: makeDataExportFile({
202202
docDef,
203203
documentIds: allDocuments.filter((_) => _[typeNameField] === docDef.name).map((_) => _._id),
204-
assertStatement,
204+
importAttributeStatement,
205205
}),
206206
filePath: withPrefix('generated', docDef.name, `_index.mjs`),
207207
}))
@@ -255,7 +255,7 @@ const writeFilesForCache = ({
255255
filePath: withPrefix('generated', 'index.mjs'),
256256
content: makeIndexMjs({
257257
schemaDef,
258-
assertStatement,
258+
importAttributeStatement,
259259
bundleFilePath: relativeBundleFilePath,
260260
isDev,
261261
options,
@@ -338,19 +338,19 @@ const writeFileWithWrittenFilesCache =
338338
const makeDataExportFile = ({
339339
docDef,
340340
documentIds,
341-
assertStatement,
341+
importAttributeStatement,
342342
}: {
343343
docDef: DocumentTypeDef
344344
documentIds: string[]
345-
assertStatement: string
345+
importAttributeStatement: string
346346
}): string => {
347347
const dataVariableName = getDataVariableName({ docDef })
348348

349349
if (docDef.isSingleton) {
350350
const documentId = documentIds[0]!
351351
return `\
352352
// ${autogeneratedNote}
353-
export { default as ${dataVariableName} } from './${idToFileName(documentId)}.json'${assertStatement}
353+
export { default as ${dataVariableName} } from './${idToFileName(documentId)}.json'${importAttributeStatement}
354354
`
355355
}
356356

@@ -376,7 +376,7 @@ export { default as ${dataVariableName} } from './${idToFileName(documentId)}.js
376376
)
377377

378378
const docImports = documentIds
379-
.map((_) => `import ${idToVariableNameMap.get(_)} from './${idToFileName(_)}.json'${assertStatement}`)
379+
.map((_) => `import ${idToVariableNameMap.get(_)} from './${idToFileName(_)}.json'${importAttributeStatement}`)
380380
.join('\n')
381381

382382
return `\
@@ -390,13 +390,13 @@ export const ${dataVariableName} = [${Array.from(idToVariableNameMap.values()).j
390390

391391
const makeIndexMjs = ({
392392
schemaDef,
393-
assertStatement,
393+
importAttributeStatement,
394394
bundleFilePath,
395395
options,
396396
isDev,
397397
}: {
398398
schemaDef: SchemaDef
399-
assertStatement: string
399+
importAttributeStatement: string
400400
bundleFilePath: RelativePosixFilePath
401401
options: PluginOptions
402402
isDev: boolean
@@ -413,7 +413,7 @@ const makeIndexMjs = ({
413413
.map(({ documentDefName, dataVariableName }) =>
414414
isDev
415415
? `import { ${dataVariableName} } from './${documentDefName}/_index.mjs'`
416-
: `import ${dataVariableName} from './${documentDefName}/_index.json'${assertStatement}`,
416+
: `import ${dataVariableName} from './${documentDefName}/_index.json'${importAttributeStatement}`,
417417
)
418418
.join('\n')
419419

@@ -434,7 +434,7 @@ export const fetchContent = async (sourceKey) => {
434434
const worker = new Worker(workerFilePath, { workerData: { sourceKey } })
435435
436436
return new Promise((resolve, reject) => {
437-
worker.on('message', (data) => {
437+
worker.on('message', (data) => {
438438
if (data.result) {
439439
resolve(data.result)
440440
} else if (data.fatalError) {
@@ -485,7 +485,7 @@ export const makeDataTypes = ({ schemaDef, options }: { schemaDef: SchemaDef; op
485485
if (options.experimental.enableDynamicBuild === false) return ''
486486

487487
return `\
488-
export type FetchContentResult =
488+
export type FetchContentResult =
489489
| { _tag: 'Error', error: SourceProvideSchemaErrorJSON | SourceFetchDataErrorJSON }
490490
| { _tag: 'Data', data: DataExports }
491491

0 commit comments

Comments
 (0)