-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support format config #238
Conversation
Reviewer's Guide by SourceryThis pull request adds support for a new 'format' configuration option, allowing users to choose between 'esm' and 'cjs' output formats. It also preserves the Velite config file extension in the output Sequence diagram for the outputEntry function with format supportsequenceDiagram
participant BuildProcess
participant OutputEntry
participant Config
BuildProcess->>OutputEntry: Call outputEntry(dest, format, configPath, collections)
OutputEntry->>Config: Get configModPath
alt format is 'cjs'
OutputEntry->>OutputEntry: Use CommonJS export syntax
else format is 'esm'
OutputEntry->>OutputEntry: Use ES Module export syntax
end
OutputEntry->>BuildProcess: Return control
Class diagram for the updated Output interfaceclassDiagram
class Output {
boolean clean
String format
}
note for Output "Added 'format' attribute with options 'esm' or 'cjs'"
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
WalkthroughThe changes introduced in this pull request enhance the build and output processes by adding a new Changes
Assessment against linked issues
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @zce - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟡 Testing: 1 issue found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
equal(posts.length, 17991, 'posts output length should be 17991') | ||
|
||
const tags = await readFile('examples/nextjs/.velite/tags.json', 'utf8') | ||
equal(tags.length, 212, 'tags output length should be 212') | ||
|
||
await rm('examples/nextjs/.velite', { recursive: true, force: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add test cases for the new format config in NextJS setup
The test doesn't explicitly check for the new format config. Consider adding test cases to verify that both 'esm' and 'cjs' formats are correctly applied when specified in the config.
const config = await readFile('examples/nextjs/velite.config.ts', 'utf8')
t.true(config.includes("format: ['esm', 'cjs']"), 'Config includes both esm and cjs formats')
const esmEntry = await readFile('examples/nextjs/.velite/index.mjs', 'utf8')
t.true(esmEntry.length > 0, 'ESM entry file is generated')
const cjsEntry = await readFile('examples/nextjs/.velite/index.js', 'utf8')
t.true(cjsEntry.length > 0, 'CJS entry file is generated')
equal(entry.length, 288, 'entry output length should be 288')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (5)
test/basic.ts (2)
29-31
: Approve the repositioned tags.json test.The test for reading the
tags.json
file has been moved without changing its functionality or expected output. This change maintains the integrity of the test suite.Consider grouping related file tests together for better readability. For example, you could move this test next to other JSON file tests like
categories.json
orposts.json
.
Line range hint
1-34
: Consider adding tests for new features and resolved issues.While the current changes maintain the existing test coverage, they don't explicitly test the new format config support or address the JSON import issues mentioned in the PR objectives (specifically issue #221).
Consider adding the following test cases:
- A test that verifies the correct handling of the new
format
configuration option (both 'esm' and 'cjs').- A test that ensures JSON imports work correctly, especially in environments using ES modules.
Would you like assistance in drafting these additional test cases?
src/config.ts (2)
111-112
: LGTM! Consider minor improvement for consistency.The addition of the
format
property aligns well with the PR objectives. It allows users to specify the output format while defaulting to 'esm', which is a good choice for modern JavaScript projects.For consistency with the
clean
property, consider using a similar pattern for theformat
property:- format: loadedConfig.output?.format ?? 'esm' + format: options.format ?? loadedConfig.output?.format ?? 'esm'This change would allow the format to be overridden via the
options
parameter, similar to howclean
is handled. However, this would require updating theoptions
type in the function signature.
111-112
: Consider addressing JSON import issues (Issue #221).While the addition of the
format
property is a positive step towards improving format handling, it may not fully address the JSON import issue mentioned in #221. To comprehensively resolve this issue, consider the following suggestions:
- Implement specific handling for JSON imports in the build process.
- Add a configuration option for specifying import attributes for different file types.
- Enhance type inference for JSON imports in the generated
.d.ts
files.Would you like assistance in drafting a more detailed plan to address the JSON import issue?
src/types.ts (1)
144-148
: LGTM! Consider enhancing the JSDoc comment.The addition of the
format
property to theOutput
interface is well-implemented and aligns with the PR objectives. It provides the necessary flexibility for users to choose between ESM and CJS output formats, addressing the issues mentioned in #221.Consider enhancing the JSDoc comment to provide more context:
/** * Output entry file format + * This determines whether the output will be in ECMAScript Module (ESM) or CommonJS (CJS) format. * @default 'esm' */ format: 'esm' | 'cjs'
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (6)
- src/build.ts (1 hunks)
- src/config.ts (1 hunks)
- src/output.ts (3 hunks)
- src/types.ts (1 hunks)
- test/basic.ts (1 hunks)
- test/nextjs.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (11)
test/nextjs.ts (6)
20-20
: LGTM. Verify the content ofcategories.json
.The expected length for
categories.json
has decreased from 880 to 649 bytes. This reduction could be due to a more compact representation of categories or removal of unnecessary data.To ensure the change is correct and all necessary category information is preserved, please verify the content of the
categories.json
file:#!/bin/bash # Description: Verify the content of categories.json file # Expected: The file should contain all necessary category information in a possibly more compact format cat examples/nextjs/.velite/categories.json
23-23
: LGTM. Verify the content ofpages.json
.The expected length for
pages.json
has slightly decreased from 5003 to 4942 bytes. This small reduction could be due to minor optimizations in how page data is stored or represented.To ensure the change is correct and all necessary page information is preserved, please verify the content of the
pages.json
file:#!/bin/bash # Description: Verify the content of pages.json file # Expected: The file should contain all necessary page information, possibly in a slightly optimized format cat examples/nextjs/.velite/pages.json
14-29
: Summary of changes in test assertionsThe updates to the expected file lengths in this test file reflect the changes introduced by the new format configuration option. Here's a summary of the implications:
- Slight increases (e.g.,
index.d.ts
) suggest minor additions to type definitions or configurations.- Significant decreases (e.g.,
options.json
,posts.json
) indicate major optimizations in data representation.- The addition of the
tags.json
assertion improves test coverage.These changes align well with the PR objectives of supporting a new format configuration and improving the flexibility of the Velite framework. However, it's crucial to verify that no critical data has been lost due to these optimizations, especially for files with substantial size reductions.
To ensure the overall integrity of the build output, please run the following command and verify that all expected files are present with non-zero sizes:
#!/bin/bash # Description: Verify the presence and non-zero size of all expected output files # Expected: All listed files should be present with non-zero sizes ls -l examples/nextjs/.velite/This will help confirm that all necessary files are being generated correctly with the new format configuration.
28-29
: LGTM. New assertion fortags.json
added.A new assertion for
tags.json
has been added, expecting a length of 212 bytes. This addition aligns with the PR objectives of improving the flexibility and usability of the Velite framework.To ensure the new
tags.json
file contains the expected data, please verify its content:Please review the output to confirm that the tags data is correctly structured and contains all necessary information.
26-26
: LGTM, but careful verification needed. Significant reduction inposts.json
size.The expected length for
posts.json
has decreased substantially from 20171 to 17991 bytes. This significant reduction (2180 bytes) could be due to major optimizations in how post data is stored or represented, possibly related to the new format configuration.Given the substantial size reduction, it's crucial to verify that all necessary post information is preserved:
Please carefully review the output to ensure no critical post data has been lost due to this optimization.
17-17
: LGTM. Verify the content ofoptions.json
.The expected length for
options.json
has decreased significantly from 1121 to 775 bytes. This reduction could be due to a more efficient representation of the configuration, possibly related to the new format option.To ensure the change is correct and no important options have been removed, please verify the content of the
options.json
file:src/config.ts (1)
111-112
: Verify the usage of the newformat
property throughout the codebase.The addition of the
format
property to the configuration looks good. To ensure full compatibility and proper usage, it's important to verify that this new property is correctly utilized in other parts of the codebase where the configuration is consumed.Please run the following script to check for usage of the new
format
property:This script will help identify areas where the new
format
property is being used or where it might need to be implemented to fully leverage the new configuration option.src/types.ts (1)
Line range hint
1-148
: Overall, the changes in this file are well-implemented and aligned with the PR objectives.The addition of the
format
property to theOutput
interface enhances the configuration options for Velite, allowing users to specify the desired module format for the generated output. This change is backwards-compatible and addresses the issues mentioned in the PR objectives, particularly the JSON import problem in Node.js applications (#221).The rest of the file remains unchanged, maintaining the existing type definitions and structure.
src/build.ts (1)
276-276
: LGTM! Verify related changes across the codebase.The addition of the
output.format
parameter to theoutputEntry
function call aligns with the PR objectives and should provide more flexibility in output formats. This change appears to address issues like the JSON import problem mentioned in #221.To ensure consistency across the codebase, please run the following script to verify related changes:
✅ Verification successful
Verified! All related changes have been correctly implemented across the codebase.
The
outputEntry
function definition and its usages have been appropriately updated to include theformat
parameter. Additionally, the configuration files have been updated to support the newformat
property.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify changes related to the new 'format' parameter # Test 1: Check for updates to the outputEntry function definition echo "Checking outputEntry function definition:" ast-grep --lang typescript --pattern 'function outputEntry($_,$_,$_,$_) { $$$ }' # Test 2: Search for other occurrences of outputEntry to ensure they've been updated echo "Checking other outputEntry calls:" rg 'outputEntry\(' --type ts # Test 3: Verify the 'format' property is properly added to the configuration echo "Checking configuration updates:" rg 'format:' --type tsLength of output: 754
src/output.ts (2)
77-77
: Approved: Conditional JSON formatting based on environmentUsing
isProduction
to conditionally control JSON formatting is a good practice. It optimizes the output for production by reducing file size and maintains readability during development.
34-34
: Ensure all invocations ofoutputEntry
include the newformat
parameterThe function signature of
outputEntry
has been updated to include a newformat
parameter. Please verify that all calls tooutputEntry
across the codebase have been updated accordingly to prevent any runtime errors.You can use the following script to identify any outdated function calls:
@@ -12,22 +12,22 @@ test('standalone fixtures', async t => { | |||
equal(entry.length, 288, 'entry output length should be 288') | |||
|
|||
const dts = await readFile('examples/basic/.velite/index.d.ts', 'utf8') | |||
equal(dts.length, 628, 'dts output length should be 628') | |||
equal(dts.length, 636, 'dts output length should be 636') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Missing index.d.ts
File in .velite
Directory
The expected examples/basic/.velite/index.d.ts
file was not found in the repository. Please verify that the build process correctly generates this declaration file.
🔗 Analysis chain
Approve the updated dts output length.
The change in the expected length of the dts
output from 628 to 636 likely reflects updates in the generated TypeScript declaration file. This aligns with the PR objectives of supporting format config and addressing JSON import issues.
To ensure the accuracy of this change, please run the following script to verify the actual length of the generated .velite/index.d.ts
file:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the length of the generated .velite/index.d.ts file
# Test: Check the length of the .velite/index.d.ts file
dts_length=$(wc -c < examples/basic/.velite/index.d.ts)
echo "Actual dts length: $dts_length"
if [ "$dts_length" -eq 636 ]; then
echo "Length matches the expected value."
else
echo "Length does not match the expected value."
fi
Length of output: 375
Script:
#!/bin/bash
# Description: Locate all instances of index.d.ts in the repository
fd 'index\.d\.ts$'
Length of output: 20
Script:
#!/bin/bash
# Description: Search for all TypeScript declaration files in the repository
fd '\.d\.ts$' --exclude 'node_modules' --exclude 'dist' --exclude 'build'
Length of output: 69
@@ -11,22 +11,22 @@ test('integration with nextjs fixtures', async t => { | |||
equal(entry.length, 288, 'entry output length should be 288') | |||
|
|||
const dts = await readFile('examples/nextjs/.velite/index.d.ts', 'utf8') | |||
equal(dts.length, 628, 'dts output length should be 628') | |||
equal(dts.length, 636, 'dts output length should be 636') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Issue Detected: Missing index.d.ts
File
The test test/nextjs.ts
includes an assertion for index.d.ts
, but the file does not exist in the repository. This discrepancy may lead to failing tests or incomplete functionality.
- File Missing:
examples/nextjs/.velite/index.d.ts
does not exist.
🔗 Analysis chain
LGTM. Verify the content of index.d.ts
.
The expected length for index.d.ts
has been updated from 628 to 636 bytes. This small increase aligns with the PR's objective of supporting a new format configuration.
To ensure the change is correct, please verify the content of the index.d.ts
file:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the content of index.d.ts file
# Expected: The file should contain new type definitions or modifications related to the format configuration
cat examples/nextjs/.velite/index.d.ts
Length of output: 190
Script:
#!/bin/bash
# Description: Check if index.d.ts exists and display its contents if it does.
if [ -f examples/nextjs/.velite/index.d.ts ]; then
echo "File exists. Displaying contents:"
cat examples/nextjs/.velite/index.d.ts
else
echo "File examples/nextjs/.velite/index.d.ts does not exist."
fi
Length of output: 168
Script:
#!/bin/bash
# Description: Locate the index.d.ts file in the repository and display its contents if found.
index_file=$(fd index.d.ts .)
if [ -n "$index_file" ]; then
echo "Found index.d.ts at: $index_file"
echo "Displaying contents of index.d.ts:"
cat "$index_file"
else
echo "index.d.ts file does not exist in the repository."
fi
Length of output: 157
if (format === 'cjs') { | ||
entry.push(`exports.${name} = require('./${name}.json')`) | ||
} else { | ||
entry.push(`export { default as ${name} } from './${name}.json'`) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate the format
parameter to ensure robustness
Currently, the code conditionally handles 'cjs'
and defaults to ES module syntax for other values of format
. To prevent potential issues with unexpected format
values, consider validating the format
parameter and providing a default or throwing an error for unsupported formats.
You could implement a validation mechanism like this:
export const outputEntry = async (dest: string, format: Output['format'], configPath: string, collections: Collections): Promise<void> => {
+ if (!['cjs', 'esm'].includes(format)) {
+ throw new Error(`Unsupported format '${format}'. Expected 'cjs' or 'esm'.`)
+ }
const begin = performance.now()
This ensures that only supported formats are processed and helps catch configuration errors early.
Committable suggestion was skipped due to low confidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (6)
docs/guide/quick-start.md (4)
Line range hint
1-243
: Overall document structure and content look good, with some suggestions for improvement.The quick start guide is well-structured and comprehensive. However, consider the following suggestions to enhance consistency and clarity:
- Add a brief introduction at the beginning to set context for new users.
- Consider adding a table of contents for easier navigation.
- Ensure consistent capitalization in headings (e.g., "Define Collections" vs "Create Contents Files").
- Add a conclusion or "Next Steps" section at the end to guide users on what to do after completing the quick start.
Line range hint
76-95
: Enhance the "Create Contents Files" section with more context and explanation.While the example directory structure and content file are helpful, consider adding:
- A brief explanation of why users need to create these content files.
- More details about the structure of the markdown frontmatter and its relationship to the schema defined in the configuration.
- An example of the
other.yml
file to show the difference between markdown and YAML content files.These additions would provide users with a clearer understanding of how content files relate to the overall Velite workflow.
Line range hint
97-146
: Consider adding explanations for each generated file in the build output.The build output structure is clearly presented, but users might benefit from understanding the purpose of each generated file. Consider adding brief explanations, such as:
posts.json
andothers.json
: Contain the processed data for each collection.index.d.ts
: TypeScript declaration file for type checking and autocompletion.index.js
: The main entry point for importing the processed data in your project.- Files in
public/static
: Processed and optimized assets referenced in your content.This additional context would help users understand the role of each file in their project.
242-243
: Expand on the new output format configuration feature.The introduction of the ability to specify the output format is a significant new feature. However, the current description is quite brief. Consider expanding this section to include:
- An example of how to specify the output format in the configuration file.
- Explanation of when users might choose one format over the other (ESM vs CommonJS).
- Any implications or considerations when changing the output format.
For example:
export default defineConfig({ // ... other config options format: 'esm', // or 'cjs' for CommonJS // ... collections and other options })This additional information would help users understand and effectively use this new feature.
docs/reference/config.md (2)
130-135
: LGTM! The newoutput.format
property is well-documented.The addition of the
output.format
property is clearly explained and aligns with the PR objectives. The documentation provides the necessary information about the property's type, default value, and purpose.Some suggestions for minor improvements:
- Consider adding an example usage of this property in a configuration file.
- It might be helpful to briefly explain the difference between 'esm' and 'cjs' for users who are not familiar with these terms.
Would you like me to provide an example and a brief explanation of 'esm' and 'cjs' to enhance the documentation?
130-135
: Confirm alignment with PR objectivesThe addition of the
output.format
property aligns well with the PR objectives and the AI-generated summary. It addresses the need for specifying the output format as either 'esm' or 'cjs'.To further improve the documentation:
- Consider mentioning that this change helps resolve issues JSON import issue in a Node.js application #221, Support
moduleResolution: "NodeNext"
#231, and feat: addpreserveConfigExtension
option #232, as stated in the PR objectives.- It might be beneficial to add a note about how this change affects the Velite configuration file's extension preservation in the output directory, specifically in the
.velite/index.d.ts
file.Would you like me to draft additional content addressing these points?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- docs/guide/quick-start.md (1 hunks)
- docs/reference/config.md (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
docs/reference/config.md (1)
130-135
: Verify integration with existingoutput
propertiesThe new
output.format
property is well-integrated into the existingoutput
section of the configuration documentation. To ensure completeness:
- Confirm that this new property doesn't have any side effects on other
output
properties, especiallyoutput.data
andoutput.assets
.- Consider adding a note about how this property interacts with other configuration options, if applicable.
Could you please run the following script to check for any potential conflicts or interactions with other
output
properties?
What's Changed
format: 'esm' | 'cjs'
config.velite/index.d.ts
Related Issues
closes #221 #231 #232
Summary by CodeRabbit
New Features
format
parameter for output flexibility during the build process, supporting both ES module and CommonJS formats.Bug Fixes