Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: support define page #145

Closed
wants to merge 1 commit into from
Closed

Conversation

edwinhuish
Copy link
Contributor

@edwinhuish edwinhuish commented Mar 20, 2024

支持 definePage 宏 related #134

  • 支持对象、函数、异步函数。详见 playground 下的示例
  • 语法检查原生支持
  • 代码约束原生支持
  • 支持import

使用注意:

  • import 仅支持从 jsts 导入,不支持从 vue 文件里导入
  • import 导入暂不支持别名路径
  • definePage 和页面script不在同一个作用域,无法使用页面变量。

实现原理:

  • 使用 @vue/compiler-sfc@babel/types 获取宏代码节点

    const { macro } = findMacroWithImports(sfc.scriptSetup)

  • 使用 @babel/generator 还原为 JS code

    const code = generate(arg).code

  • 创建子进程,并调用 tsx 运行生成的 script,得到结果。

    let script = ''
    for (const imp of imports)
    script += `${generate(imp).code}\n`
    script += t.isFunctionExpression(arg) || t.isArrowFunctionExpression(arg)
    ? `Promise.resolve((${code})()).then(res=>console.log(JSON.stringify(res)))`
    : `console.log(JSON.stringify(${code}))`
    const result = await runProcess(tsx, ['-e', script], { cwd })
    return JSON.parse(result)

Summary by CodeRabbit

  • New Features

    • Added support for ESLint flat config to improve code quality.
    • Introduced a definePage function for more flexible page definitions.
    • New utility function for generating random text strings, enhancing dynamic content.
    • Enhanced page definition capabilities with support for async functions, nested functions, and object-based configurations.
    • Added functionality to import modules directly within Vue components for dynamic title texts.
  • Refactor

    • Refined page initialization and scanning logic for efficiency.
    • Updated methods to work with Page instances, improving the handling of page options.
    • Streamlined the process of parsing Vue single-file components (SFC) for better consistency.
  • Bug Fixes

    • Removed unnecessary code and imports to clean up the codebase.
  • Chores

    • Added a new child-process.ts file for running child processes, facilitating external command execution.
  • Documentation

    • Adjusted settings and configurations to prioritize ESLint, enhancing code documentation and maintainability.

Copy link

coderabbitai bot commented Mar 20, 2024

Walkthrough

The update entails enhancements in ESLint integration, Vue.js project page management, and development workflow optimizations. It introduces new features for defining and parsing pages, improves ESLint settings in VS Code, and streamlines interactions with Vue Single File Components (SFCs). Utility functions are introduced, and the playground showcases enhanced capabilities, emphasizing code quality and developer efficiency.

Changes

Files Change Summary
.vscode/settings.json Enhanced ESLint integration and disabled default formatter.
packages/core/client.d.ts, .../core/src/constant.ts, .../core/src/index.ts, .../core/src/types.ts Added definePage function, DEFINE_PAGE constant, and related types.
packages/core/src/context.ts Refactored page handling logic, updated import/export statements, and improved page initialization methods.
packages/core/src/page.ts Introduced Page class for managing options, parsing different file formats, and handling Vue SFC blocks.
packages/core/src/utils.ts Streamlined utility functions and imports, added async parsing function for SFCs.
packages/core/src/child-process.ts Added runProcess function for managing child processes with specified configurations.
packages/playground/src/pages/..., .../macros/utils.ts Expanded playground with new Vue.js examples showcasing macros, async functions, and utility functions.
packages/playground/src/uni-pages.d.ts Modified accessibility of navigation options declarations.

"In the realm of code, where logic does play,
A rabbit hopped in, with changes to say.
🐾 With a leap and a bound, ESLint's embraced,
Pages and playgrounds, so swiftly retraced.
🌟 Amidst Vue and scripts, it danced with delight,
Crafting a world, where code shines so bright."
🐇💻

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 00ba3cc and f5370ec.
Files ignored due to path filters (3)
  • package.json is excluded by: !**/*.json
  • packages/core/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (10)
  • .vscode/settings.json (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (8 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/i18n.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review due to trivial changes (1)
  • packages/playground/src/uni-pages.d.ts
Additional comments: 18
packages/playground/src/pages/i18n.vue (1)
  • 5-11: The usage of definePage within the <script setup> block correctly implements the new feature. This change aligns with the PR's objective to support the definePage macro, allowing for a more structured and declarative way to define page options. Ensure that the %app.name% placeholder is properly resolved in the context of your application.
packages/core/src/constant.ts (1)
  • 8-8: The addition of the DEFINE_PAGE constant is correct and aligns with the PR's objective to support the definePage macro. This constant will likely be used throughout the framework to reference the macro by name, ensuring consistency.
packages/core/client.d.ts (1)
  • 9-11: The declaration of the definePage function within the globalThis module is correctly implemented, allowing it to be recognized globally. This aligns with the PR's objective to introduce the definePage macro. Ensure that the DefinePageOptions type accurately reflects the options that can be passed to definePage.
.vscode/settings.json (1)
  • 2-42: The ESLint configuration adjustments, including enabling flat config support, disabling the default formatter in favor of ESLint, configuring editor settings for ESLint auto-fix, customizing ESLint rules severity, and enabling ESLint validation for various languages, are correctly implemented. These changes enhance the development experience by ensuring code quality and consistency. Ensure that these settings align with your project's coding standards and ESLint rules.
packages/core/src/utils.ts (2)
  • 2-2: Changing the import statement for ModuleNode and ViteDevServer from 'vite' to use type syntax is a good practice, as it clarifies that these imports are only used for type information and not included in the JavaScript output.
  • 54-56: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [36-60]

The removal of unused functions (getRouteSfcBlock and useCachedPages) and their internal logic is a positive change, as it helps to keep the codebase clean and maintainable. Ensure that these functions are indeed not used anywhere in the project to avoid breaking changes.

packages/core/src/types.ts (2)
  • 15-16: The addition of the RouteBlockLang type with specific string literals is a good practice, as it restricts the values to a predefined set, enhancing type safety and readability.
  • 146-152: The introduction of the DefinePageOptions interface with a deprecated path property is correctly implemented. However, ensure that the deprecation of the path property is clearly communicated to developers, possibly through documentation, to avoid confusion.
packages/core/src/index.ts (1)
  • 83-115: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-149]

The modifications in index.ts, including importing necessary functions from @vue/compiler-sfc, adding new constants, and adjusting the transform function to handle parsing of Vue single-file components (SFC), finding macros, and route blocks, are correctly implemented. These changes align with the PR's objective to support the definePage macro and enhance code transformation and parsing capabilities. Ensure that the transform function's logic correctly handles all edge cases and that the error messages are clear and informative for developers.

packages/core/src/page.ts (2)
  • 16-60: The introduction of the Page class, which centralizes the reading and parsing of page options from various file formats, is correctly implemented. This class enhances the maintainability and modularity of the codebase by encapsulating page-related logic. Ensure that the error handling in the readOptions method is robust and provides clear error messages to developers.
  • 63-149: The functions readPageOptionsFromFile, readPageOptionsFromMacro, and readPageOptionsFromBlock are well-implemented, providing a structured approach to parsing page options based on the file content and the specified route block language. Ensure that the parsing logic correctly handles all supported formats and that the error messages are informative.
packages/core/src/context.ts (7)
  • 22-22: The import of the Page class aligns with the PR's objective to support the definePage macro by introducing a centralized way to manage page metadata and options. This change is crucial for the refactoring efforts to work with Page instances.
  • 44-45: The introduction of pages and subPages as class properties is a significant change. It shifts the data structure from using paths to using Page instances, which likely enhances the flexibility and capabilities of page management within the framework. However, ensure that all references to these properties throughout the codebase have been updated accordingly.
Verification successful

The examination of the context around subPagesPath in declaration.ts indicates that it is used to generate paths for subpages, derived from ctx.subPageMetaData. This usage does not directly reference the new subPages property but seems to align with the intent of managing page paths within the framework. There's no evidence from the provided context that references to the old structure were not updated correctly. Therefore, based on the information available, it appears that the concern raised in the original review comment has been addressed as far as can be determined.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all references to `pages` and `subPages` are correctly updated.
rg --type ts "pagesPath|subPagesPath"

Length of output: 301



Script:

#!/bin/bash
# Examine the context around the usage of `subPagesPath` in declaration.ts to understand its relation to the new `subPages` property.
rg --type ts -C 5 "subPagesPath" packages/core/src/declaration.ts

Length of output: 714

* 83-83: The refactoring of the `scanPages` method to work with `Page` instances instead of paths is a key part of integrating the `definePage` macro. This change should streamline the page initialization process. However, it's important to ensure that the `Page` class's constructor and methods are designed to handle all necessary page-related functionalities efficiently.
Verification successful

The Page class's constructor and methods, including getOptions, hasChanged, and readOptions, are designed to efficiently and comprehensively handle page-related functionalities. The constructor initializes the context and file path, while the methods manage the reading, caching, and change detection of page options. This setup should support the integration of the definePage macro effectively by streamlining the page initialization process.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the efficiency and completeness of the Page class's constructor and methods.
ast-grep --lang typescript --pattern $'class Page {
  $$$
  constructor($_, $_) {
    $$$
  }
  $$$
}'

Length of output: 2632

* 87-97: The `scanSubPages` method has been updated to also work with `Page` instances, similar to the `scanPages` method. This consistency in handling both pages and sub-pages using the same data structure is beneficial. Ensure that the logic for handling sub-pages is thoroughly tested, especially in scenarios involving nested sub-pages or complex directory structures. * 181-182: The `parsePages` method's update to work with `Page` instances and potentially custom page metadata is a critical part of the new page handling logic. It's important to ensure that this method accurately merges generated and custom page metadata, especially when overrides are provided. Consider adding tests to verify the correctness of the merged metadata under various scenarios. * 250-262: The `getPageByPath` method has been updated to retrieve `Page` instances by their absolute path. This method is crucial for efficiently finding pages, especially during the update process. Ensure that this method is optimized for performance, considering the potential size of the `pages` and `subPages` collections. * 266-267: The logic within `updatePagesJSON` to skip updates if a page has not changed is a good optimization. However, ensure that the `hasChanged` method of the `Page` class is robust and accurately detects changes to avoid unnecessary updates or missing necessary ones.
Verification successful

The hasChanged method in the Page class is designed to be robust, leveraging the readOptions method to accurately detect changes by comparing the serialized current options with a previously stored version. This approach, combined with exception handling in the readOptions method, ensures a reliable mechanism for detecting changes in page options. Therefore, the optimization to skip updates if a page has not changed, as implemented, appears to be well-founded.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the robustness of the Page class's hasChanged method.
ast-grep --lang typescript --pattern $'class Page {
  $$$
  hasChanged() {
    $$$
  }
  $$$
}'

Length of output: 2625

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f5370ec and 896f5f4.
Files selected for processing (1)
  • packages/core/client.d.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/client.d.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 896f5f4 and e1658cf.
Files selected for processing (2)
  • README.md (1 hunks)
  • packages/core/src/page.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/page.ts
Additional comments: 2
README.md (2)
  • 57-73: The documentation for the definePage macro is clear and provides useful examples. However, it might be beneficial for readers if a brief explanation of the benefits or specific use cases of using definePage over traditional methods is included.
  • 75-75: The documentation for using SFC custom blocks for route data is comprehensive and informative. It might be helpful to include a note about any potential limitations or considerations when opting for this method to define routing data.

@edwinhuish
Copy link
Contributor Author

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e1658cf and d9b8fd2.
Files selected for processing (1)
  • packages/core/src/page.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/page.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d9b8fd2 and 89b2f17.
Files selected for processing (1)
  • packages/core/src/page.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/page.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 89b2f17 and e9dc596.
Files selected for processing (7)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (4)
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/playground/src/uni-pages.d.ts
Additional comments: 5
packages/core/src/context.ts (5)
  • 21-21: The addition of the Page import aligns with the PR's objectives and supports the new page management approach.
  • 43-45: The introduction of pages and subPages as class properties is a significant and positive change, facilitating the new approach to page management using the Page class.
  • 79-82: Refactoring the page initialization and scanning logic to work with Page instances instead of PagePath is a significant improvement, enhancing the module's functionality and maintainability.
  • 180-180: The method parsePages has been updated to work with Page instances, which is a crucial change for integrating the definePage macro. This update enhances the flexibility and accuracy of page parsing.
  • 249-261: The method getPageByPath has been updated to retrieve pages by path using Page instances. This change improves the accuracy and efficiency of page retrieval.

Comment on lines 1 to 14
<script lang="ts" setup>
definePage({
style:{
navigationBarTitleText: 'hello world'
},
middlewares: [
'auth'
]
})
</script>

<template>
<div>test</div>
</template>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of the definePage macro in a Vue component using an object configuration looks good. Ensure that there is sufficient documentation and tests covering the definePage macro to facilitate its adoption and ensure its reliability.

Would you like assistance in creating documentation or tests for the definePage macro?

Comment on lines 1 to 21
<script lang="ts" setup>
definePage(()=>{

const hello = 'hello'

const world = 'world'

return {
style:{
navigationBarTitleText: [hello, world].join(' ')
},
middlewares: [
'auth'
]
}
})
</script>

<template>
<div>test</div>
</template>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of the definePage macro in a Vue component using a function configuration looks good. Ensure that there is sufficient documentation and tests covering the definePage macro to facilitate its adoption and ensure its reliability.

Would you like assistance in creating documentation or tests for the definePage macro?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e9dc596 and 1bf0933.
Files selected for processing (1)
  • packages/core/src/page.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/page.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1bf0933 and 0aaac11.
Files selected for processing (4)
  • README.md (1 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (3)
  • README.md
  • packages/core/src/page.ts
  • packages/playground/src/uni-pages.d.ts

Comment on lines 2 to 17
definePage(async ()=>{

const randomTxt = await new Promise(resolve=> {
const txt = Math.random().toString(36).slice(-8)
resolve(txt)
});

return {
style:{
navigationBarTitleText: randomTxt
},
middlewares: [
'auth'
]
}
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of definePage with an async function is a powerful feature, allowing for dynamic page configuration. However, consider the following points for improvement:

  1. Error Handling: The async function lacks error handling. Wrapping the promise in a try-catch block would ensure that any errors during the promise resolution are caught and handled appropriately.
  2. Code Clarity: While the current implementation is concise, adding a brief comment explaining the purpose of generating a random string for navigationBarTitleText could improve code readability and maintainability.
  3. Middleware Documentation: If middlewares like 'auth' have specific requirements or behaviors, documenting them near their usage can help future developers understand their purpose and how to use them effectively.
definePage(async ()=>{
+  // Generate a random string to dynamically set the navigation bar title
+  try {
    const randomTxt = await new Promise(resolve=> {
      const txt = Math.random().toString(36).slice(-8)
      resolve(txt)
    });
+  } catch (error) {
+    console.error("Failed to generate random text for navigation bar title", error);
+    // Handle error appropriately
+  }
  return {
    style:{
      navigationBarTitleText: randomTxt
    },
+    // 'auth' middleware ensures that the user is authenticated before accessing this page
    middlewares: [
      'auth'
    ]
  }
})

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
definePage(async ()=>{
const randomTxt = await new Promise(resolve=> {
const txt = Math.random().toString(36).slice(-8)
resolve(txt)
});
return {
style:{
navigationBarTitleText: randomTxt
},
middlewares: [
'auth'
]
}
})
definePage(async ()=>{
// Generate a random string to dynamically set the navigation bar title
try {
const randomTxt = await new Promise(resolve=> {
const txt = Math.random().toString(36).slice(-8)
resolve(txt)
});
} catch (error) {
console.error("Failed to generate random text for navigation bar title", error);
// Handle error appropriately
}
return {
style:{
navigationBarTitleText: randomTxt
},
// 'auth' middleware ensures that the user is authenticated before accessing this page
middlewares: [
'auth'
]
}
})

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch from 0aaac11 to 0d0b0b1 Compare March 21, 2024 12:02
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fa093d0 and 0d0b0b1.
Files ignored due to path filters (3)
  • package.json is excluded by: !**/*.json
  • packages/core/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (13)
  • README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/i18n.vue (1 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (11)
  • README.md
  • packages/core/client.d.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/playground/src/pages/i18n.vue
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/uni-pages.d.ts
Additional comments: 5
packages/core/src/utils.ts (1)
  • 2-2: The change to use type imports for ModuleNode and ViteDevServer is a good practice in TypeScript, as it clarifies that these imports are only used for type information. This can help with build optimization and clarity in the codebase.
packages/core/src/context.ts (4)
  • 21-21: The changes in import statements, including the removal of old page management imports and the addition of the Page import, reflect a significant shift towards a more object-oriented approach in managing pages. This aligns well with the introduction of the definePage macro and should enhance modularity and maintainability.
  • 43-44: The modifications to the PageContext class, including the use of Page instances for managing pages and subpages, represent a significant improvement in the architecture. This approach allows for better encapsulation of page-related logic and data, enhancing the flexibility and maintainability of the codebase.
  • 180-180: The updates to methods for parsing and managing pages, including changes to work with Page instances, significantly improve the page management workflow. These changes enhance the clarity and efficiency of page parsing and retrieval, aligning well with the new object-oriented approach.
  • 82-82: The refactoring of page initialization and scanning logic to accommodate the new Page class represents a significant improvement in the codebase. This change simplifies the page management process, making the code cleaner, more maintainable, and easier to extend.

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch from 0d0b0b1 to 2ec3799 Compare March 21, 2024 12:26
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fa093d0 and 2ec3799.
Files ignored due to path filters (2)
  • packages/core/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (12)
  • README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (11)
  • README.md
  • packages/core/client.d.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/core/src/utils.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/uni-pages.d.ts
Additional comments: 7
packages/core/src/context.ts (7)
  • 21-21: The import of the Page class is correctly added to support the new definePage macro feature. Ensure that the Page class is implemented as expected and integrates well with the existing codebase.
  • 43-44: The declarations of pages and subPages as class properties are appropriate for managing instances of Page. This change aligns with the objective of refactoring page initialization and scanning logic to work with Page instances.
  • 82-82: The refactoring of the scanPages method to work with Page instances instead of PagePath is a significant improvement. It simplifies the handling of pages by directly associating them with their respective Page objects. However, ensure that the getPagePaths function is correctly updated to support this change.
  • 86-96: The scanSubPages method has been correctly refactored to work with Page instances, similar to the scanPages method. This consistency in handling both pages and subpages as Page instances is beneficial for maintainability and clarity.
  • 180-180: The parsePages method's refactoring to work with Page instances and its integration with type enhancements (e.g., PageMetaDatum) is a positive change. It improves the clarity and type safety of the page parsing logic. Ensure that the Page class's getOptions method is implemented correctly and returns the expected data structure.
  • 249-261: The getPageByPath method has been updated to retrieve pages by their absolute path, considering both main pages and subpages. This method is crucial for the dynamic update of pages JSON. Ensure that the Page class's file property correctly stores the absolutePath and is accessible as shown.
  • 265-266: The logic within updatePagesJSON for checking if a page has changed before proceeding with the update is a good performance optimization. However, ensure that the Page class's hasChanged method is implemented correctly and efficiently determines if a page's content has changed.

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch from 2ec3799 to 9acad28 Compare March 21, 2024 12:39
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fa093d0 and 9acad28.
Files ignored due to path filters (2)
  • packages/core/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (12)
  • README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (11)
  • README.md
  • packages/core/client.d.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/core/src/utils.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/uni-pages.d.ts
Additional comments: 7
packages/core/src/context.ts (7)
  • 21-21: The import of the Page class is correctly added to support the new definePage functionality. Ensure that the Page class is implemented as expected and integrates well with the existing codebase.
  • 43-44: The introduction of pages and subPages as class properties to store instances of Page is a significant change. This approach aligns with the objective of supporting the definePage macro by enabling more structured management of page definitions.
  • 82-82: Refactoring the scanPages method to work with Page instances instead of PagePath is a crucial change. It's important to ensure that the Page constructor is called with the correct arguments and that the Page instances are correctly managed within the PageContext.
  • 96-96: Similarly, the scanSubPages method has been refactored to create Page instances for subpages. This consistency in handling pages and subpages as Page instances is beneficial for maintainability and future enhancements.
  • 180-180: The parsePages method has been updated to work with Page instances. It's crucial to verify that page.getOptions() correctly retrieves the options for each page and that the method correctly handles the merging of page metadata.
  • 249-261: The getPageByPath method has been updated to retrieve Page instances by their absolute path. This change is essential for maintaining the integrity of page management. Ensure that the comparison of absolutePath is accurate and that the method correctly handles both main pages and subpages.
  • 265-266: The updatePagesJSON method now includes logic to check if a page has changed before proceeding with the update. This optimization can help reduce unnecessary updates. However, it's important to ensure that the hasChanged method of the Page class accurately detects changes.

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch 2 times, most recently from 218af7d to 0d7e869 Compare March 21, 2024 17:54
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fa093d0 and 0d7e869.
Files ignored due to path filters (3)
  • packages/core/package.json is excluded by: !**/*.json
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (18)
  • README.md (1 hunks)
  • packages/core/README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/child-process.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/import-module.vue (1 hunks)
  • packages/playground/src/pages/macros/import.vue (1 hunks)
  • packages/playground/src/pages/macros/nested-function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/pages/macros/utils.ts (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (11)
  • README.md
  • packages/core/client.d.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/core/src/utils.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/uni-pages.d.ts
Additional comments: 5
packages/playground/src/pages/macros/import-module.vue (1)
  • 1-15: The usage of definePage and the import of version from vite are correctly implemented.
packages/playground/src/pages/macros/nested-function.vue (1)
  • 1-24: The usage of definePage with a nested function getTitle is correctly implemented and serves as a good example of incorporating more complex logic within definePage.
packages/core/README.md (2)
  • 47-69: The documentation for the definePage macro provides clear and accurate examples of how to define page options using both object and function arguments.
  • 44-74: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [71-100]

The documentation for adding route meta directly to routes using a custom <route> block in SFCs is clear and informative, covering the supported parsers and how to specify a parser.

packages/core/src/context.ts (1)
  • 76-99: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [21-261]

The refactoring in context.ts introduces a more object-oriented approach to handling pages and sub-pages, improving maintainability and modularity. The changes appear to be correctly implemented.

Please verify the performance impact of these changes, especially in projects with a large number of pages.

Comment on lines +1 to +18
import type { SpawnOptionsWithoutStdio } from 'node:child_process'
import { spawn } from 'node:child_process'
import process from 'node:process'

export function runProcess(command: string, args: string[] = [], options?: SpawnOptionsWithoutStdio) {
return new Promise<string>((resolve, reject) => {
const child = spawn(command, args, {
env: {
...process.env,
},
...options,
})
const output = [] as string[]
child.stdout.on('data', chunk => output.push(chunk))
child.on('close', () => resolve(output.join('').trim()))
child.on('error', error => reject(error))
})
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runProcess function correctly spawns a child process and handles success cases. However, it currently does not handle stderr output from the child process, which could lead to missed error messages. Consider adding a listener for stderr similar to stdout.

+    child.stderr.on('data', chunk => output.push(chunk))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import type { SpawnOptionsWithoutStdio } from 'node:child_process'
import { spawn } from 'node:child_process'
import process from 'node:process'
export function runProcess(command: string, args: string[] = [], options?: SpawnOptionsWithoutStdio) {
return new Promise<string>((resolve, reject) => {
const child = spawn(command, args, {
env: {
...process.env,
},
...options,
})
const output = [] as string[]
child.stdout.on('data', chunk => output.push(chunk))
child.on('close', () => resolve(output.join('').trim()))
child.on('error', error => reject(error))
})
}
import type { SpawnOptionsWithoutStdio } from 'node:child_process'
import { spawn } from 'node:child_process'
import process from 'node:process'
export function runProcess(command: string, args: string[] = [], options?: SpawnOptionsWithoutStdio) {
return new Promise<string>((resolve, reject) => {
const child = spawn(command, args, {
env: {
...process.env,
},
...options,
})
const output = [] as string[]
child.stdout.on('data', chunk => output.push(chunk))
child.stderr.on('data', chunk => output.push(chunk))
child.on('close', () => resolve(output.join('').trim()))
child.on('error', error => reject(error))
})
}

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0d7e869 and 2633d21.
Files selected for processing (1)
  • packages/core/client.d.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/client.d.ts

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch 2 times, most recently from f540677 to 109d430 Compare March 22, 2024 03:52
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4f41408 and f540677.
Files ignored due to path filters (3)
  • packages/core/package.json is excluded by: !**/*.json
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (17)
  • README.md (1 hunks)
  • packages/core/README.md (1 hunks)
  • packages/core/src/child-process.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/import-module.vue (1 hunks)
  • packages/playground/src/pages/macros/import.vue (1 hunks)
  • packages/playground/src/pages/macros/nested-function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/pages/macros/utils.ts (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (15)
  • README.md
  • packages/core/README.md
  • packages/core/src/child-process.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/import-module.vue
  • packages/playground/src/pages/macros/import.vue
  • packages/playground/src/pages/macros/nested-function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/pages/macros/utils.ts
  • packages/playground/src/uni-pages.d.ts
Additional comments: 6
packages/core/src/utils.ts (2)
  • 2-2: The import statement for ModuleNode and ViteDevServer has been updated to use type syntax, which is a good practice for TypeScript to ensure these imports are used for type information only and not included in the JavaScript output.
  • 60-75: The parseSFC function is a new addition that utilizes @vue/compiler-sfc for parsing Vue Single File Components (SFCs). It's important to handle errors gracefully, and the catch block does this by throwing a new error with a helpful message. However, consider adding more specific error handling or logging for different types of errors that could occur during parsing.

Consider enhancing error handling in the parseSFC function to provide more specific feedback for different error scenarios, which could aid in debugging.

packages/core/src/context.ts (4)
  • 21-21: The import of the Page class is a key addition, indicating a shift towards using this class for page management. This aligns with the PR's objective of enhancing page definition capabilities.
  • 43-45: The introduction of pages and subPages as class properties to store instances of the Page class is a significant change. It's crucial to ensure that all methods interacting with these properties are updated accordingly.
  • 82-82: The scanPages method has been refactored to create Page instances for each path. This is a crucial part of integrating the Page class into the system. Ensure that the Page constructor and its methods are thoroughly tested to handle various page definitions correctly.
  • 180-180: The parsePages method has been updated to work with Page instances. This method is central to processing page definitions and generating metadata. It's important to verify that the getOptions method of the Page class correctly handles different page configurations.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4f41408 and 109d430.
Files ignored due to path filters (3)
  • packages/core/package.json is excluded by: !**/*.json
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (17)
  • README.md (1 hunks)
  • packages/core/README.md (1 hunks)
  • packages/core/src/child-process.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (4 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/import-module.vue (1 hunks)
  • packages/playground/src/pages/macros/import.vue (1 hunks)
  • packages/playground/src/pages/macros/nested-function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/pages/macros/utils.ts (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (16)
  • README.md
  • packages/core/README.md
  • packages/core/src/child-process.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/core/src/utils.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/import-module.vue
  • packages/playground/src/pages/macros/import.vue
  • packages/playground/src/pages/macros/nested-function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/pages/macros/utils.ts
  • packages/playground/src/uni-pages.d.ts
Additional comments: 7
packages/core/src/context.ts (7)
  • 21-21: The import of the Page class is correctly added to support the new functionality introduced by the definePage macro. This aligns with the PR objectives.
  • 43-44: The declarations of pages and subPages as class properties are correctly refactored to use instances of the Page class instead of PagePath. This change is necessary for the enhanced page handling logic.
  • 82-82: The refactoring of the scanPages method to initialize Page instances for each path is correctly implemented. This change is crucial for the new page handling logic that leverages the Page class.
  • 92-92: Similarly, the scanSubPages method is correctly refactored to create Page instances for subpages. This ensures consistency in handling both main pages and subpages.
  • 180-180: The parsePages method's refactoring to work with Page instances instead of PagePath is correctly done. This method now correctly awaits the resolution of page.getOptions() for each page, which is essential for asynchronous page option retrieval.
  • 249-261: The getPageByPath method is updated to retrieve Page instances by their absolute path. This update is necessary for the new page handling logic and correctly implemented.
  • 265-266: The updatePagesJSON method's logic to check if a page has changed before updating the pages JSON is correctly implemented. This optimization prevents unnecessary updates, aligning with best practices for performance.

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch 3 times, most recently from 22995a9 to e532f7f Compare March 22, 2024 04:04
@edwinhuish edwinhuish force-pushed the feat/support-definePage branch from e532f7f to ee67edd Compare March 22, 2024 09:16
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 71f60a2 and ee67edd.
Files ignored due to path filters (3)
  • packages/core/package.json is excluded by: !**/*.json
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (19)
  • README.md (1 hunks)
  • packages/core/README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/child-process.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (2 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/import-module.vue (1 hunks)
  • packages/playground/src/pages/macros/import.vue (1 hunks)
  • packages/playground/src/pages/macros/nested-function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/pages/macros/remove-console.vue (1 hunks)
  • packages/playground/src/pages/macros/utils.ts (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (17)
  • README.md
  • packages/core/README.md
  • packages/core/client.d.ts
  • packages/core/src/child-process.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/core/src/utils.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/import-module.vue
  • packages/playground/src/pages/macros/import.vue
  • packages/playground/src/pages/macros/nested-function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/pages/macros/utils.ts
  • packages/playground/src/uni-pages.d.ts
Additional comments: 9
packages/playground/src/pages/macros/remove-console.vue (2)
  • 3-3: The comment indicates that the console log statement should be removed. If this is for demonstration purposes related to the definePage macro's ability to handle or remove console logs, please ensure this is clearly documented to avoid confusion.
  • 12-14: The template block is straightforward and serves its purpose as a placeholder for page content.
packages/core/src/context.ts (7)
  • 21-21: The addition of the Page import aligns with the PR's objectives to enhance the development experience by using Page instances for better handling of page functionalities.
  • 43-44: The declaration of pages and subPages as arrays of Page instances supports the new approach of handling pages as objects, facilitating more sophisticated operations on pages.
  • 79-82: The refactoring of the scanPages method to directly create Page instances simplifies the process and aligns with the object-oriented approach of handling pages.
  • 86-96: The refactoring of the scanSubPages method to work with Page instances instead of paths simplifies the handling of sub-pages and enhances the object-oriented design.
  • 180-180: The update to the parsePages method to work with Page instances instead of paths is a necessary change that improves the flexibility and robustness of page handling.
  • 220-220: The mergePageMetaData method's update to work with Page instances supports the new approach and enhances the process of merging page metadata.
  • 249-249: The getPageByPath method's update to retrieve Page instances by path aligns with the new approach of handling pages as objects, improving the overall design.

@edwinhuish edwinhuish force-pushed the feat/support-definePage branch from ee67edd to 54d9c8b Compare March 22, 2024 10:25
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 71f60a2 and 54d9c8b.
Files ignored due to path filters (3)
  • packages/core/package.json is excluded by: !**/*.json
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (19)
  • README.md (1 hunks)
  • packages/core/README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/child-process.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (7 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (3 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/import-module.vue (1 hunks)
  • packages/playground/src/pages/macros/import.vue (1 hunks)
  • packages/playground/src/pages/macros/nested-function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/pages/macros/remove-console.vue (1 hunks)
  • packages/playground/src/pages/macros/utils.ts (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (17)
  • README.md
  • packages/core/README.md
  • packages/core/client.d.ts
  • packages/core/src/child-process.ts
  • packages/core/src/constant.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/import-module.vue
  • packages/playground/src/pages/macros/import.vue
  • packages/playground/src/pages/macros/nested-function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/pages/macros/remove-console.vue
  • packages/playground/src/pages/macros/utils.ts
  • packages/playground/src/uni-pages.d.ts
Additional comments: 7
packages/core/src/utils.ts (2)
  • 2-2: The change to use type imports for ModuleNode and ViteDevServer from 'vite' is a good practice for TypeScript, as it ensures these imports are used only for type checking and do not contribute to the runtime bundle size.
  • 61-76: The addition of the parseSFC function is a significant enhancement. It encapsulates the parsing logic for Single File Components (SFCs) using @vue/compiler-sfc, with error handling that throws a more informative error if the parsing fails. This function improves maintainability by centralizing SFC parsing logic and error handling. However, consider adding more specific error handling or logging to provide clearer insights into parsing failures.

Consider enhancing error handling within the parseSFC function to log specific parsing errors or to categorize errors based on their nature. This could help developers diagnose issues more effectively.

packages/core/src/context.ts (5)
  • 21-21: The import of the Page class aligns with the PR's objective to introduce a more structured approach to page management. This change suggests a shift towards object-oriented management of pages, which can enhance code readability and maintainability.
  • 43-45: The introduction of pages and subPages as class properties, with their types being arrays of Page instances or records of Page arrays, respectively, is a significant improvement. It indicates a move towards a more structured and scalable way of handling pages and subpages within the project. This change should facilitate easier manipulation and querying of page data throughout the application.
  • 82-82: The refactoring to initialize Page instances directly within the scanPages method simplifies the process of page scanning and instantiation. This approach encapsulates the creation and initialization of pages within a single, cohesive operation, improving code clarity and maintainability.
  • 180-181: The parsePages method's asynchronous nature and its use of Promise.all to concurrently process page options is an efficient way to handle potentially time-consuming operations. This method demonstrates good use of asynchronous programming patterns to improve performance.
  • 249-261: The getPageByPath method provides a clear and concise way to retrieve a page by its absolute path, searching through both main pages and subpages. This utility method enhances code reusability and simplifies the logic for finding pages, contributing to better code organization.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 54d9c8b and cb7ec63.
Files ignored due to path filters (2)
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (2)
  • packages/playground/src/pages/macros/yaml.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/playground/src/uni-pages.d.ts
Additional comments: 3
packages/playground/src/pages/macros/yaml.vue (3)
  • 5-11: The usage of definePage here demonstrates its capability to support functions, aligning with the PR objectives. Ensure that definePage operates in a separate scope from the page script as intended.
  • 3-3: The import of yamlParser is correctly implemented. Given the PR's enhancements on import support, ensure that this import does not introduce any unintended side effects or scope issues within the definePage macro.
  • 18-20: The template is simple and effective for demonstrating the component's functionality in the playground. Good to go!

@uni-helper uni-helper deleted a comment from coderabbitai bot Mar 22, 2024
@uni-helper uni-helper deleted a comment from coderabbitai bot Mar 22, 2024
@uni-helper uni-helper deleted a comment from coderabbitai bot Mar 22, 2024
@edwinhuish edwinhuish force-pushed the feat/support-definePage branch from cb7ec63 to db9b6b9 Compare March 22, 2024 16:30
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 71f60a2 and db9b6b9.
Files ignored due to path filters (3)
  • packages/core/package.json is excluded by: !**/*.json
  • packages/playground/package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
Files selected for processing (20)
  • README.md (1 hunks)
  • packages/core/README.md (1 hunks)
  • packages/core/client.d.ts (1 hunks)
  • packages/core/src/child-process.ts (1 hunks)
  • packages/core/src/constant.ts (1 hunks)
  • packages/core/src/context.ts (9 hunks)
  • packages/core/src/index.ts (3 hunks)
  • packages/core/src/page.ts (1 hunks)
  • packages/core/src/types.ts (3 hunks)
  • packages/core/src/utils.ts (3 hunks)
  • packages/playground/src/pages/macros/async-function.vue (1 hunks)
  • packages/playground/src/pages/macros/function.vue (1 hunks)
  • packages/playground/src/pages/macros/import-module.vue (1 hunks)
  • packages/playground/src/pages/macros/import.vue (1 hunks)
  • packages/playground/src/pages/macros/nested-function.vue (1 hunks)
  • packages/playground/src/pages/macros/object.vue (1 hunks)
  • packages/playground/src/pages/macros/remove-console.vue (1 hunks)
  • packages/playground/src/pages/macros/utils.ts (1 hunks)
  • packages/playground/src/pages/macros/yaml.vue (1 hunks)
  • packages/playground/src/uni-pages.d.ts (2 hunks)
Files skipped from review as they are similar to previous changes (19)
  • README.md
  • packages/core/README.md
  • packages/core/client.d.ts
  • packages/core/src/child-process.ts
  • packages/core/src/constant.ts
  • packages/core/src/context.ts
  • packages/core/src/index.ts
  • packages/core/src/page.ts
  • packages/core/src/types.ts
  • packages/playground/src/pages/macros/async-function.vue
  • packages/playground/src/pages/macros/function.vue
  • packages/playground/src/pages/macros/import-module.vue
  • packages/playground/src/pages/macros/import.vue
  • packages/playground/src/pages/macros/nested-function.vue
  • packages/playground/src/pages/macros/object.vue
  • packages/playground/src/pages/macros/remove-console.vue
  • packages/playground/src/pages/macros/utils.ts
  • packages/playground/src/pages/macros/yaml.vue
  • packages/playground/src/uni-pages.d.ts
Additional comments: 1
packages/core/src/utils.ts (1)
  • 2-2: The change to use type imports for ModuleNode and ViteDevServer from 'vite' is a good practice for TypeScript, as it clarifies that these imports are only used for type information and can be stripped away during compilation.

Comment on lines +61 to +76
export async function parseSFC(code: string, options?: SFCParseOptions): Promise<SFCDescriptor> {
try {
return (
VueParser(code, {
pad: 'space',
...options,
}).descriptor
// for @vue/compiler-sfc ^2.7
|| (VueParser as any)({
source: code,
...options,
})
)
}

async function hasChanged(filePath: string, routeBlock?: SFCBlock) {
if (!routeBlock)
routeBlock = await getRouteSfcBlock(normalizePath(filePath))

return !pages.has(filePath) || JSON.stringify(parseData(routeBlock)) !== pages.get(filePath)
}

return {
setCache,
hasChanged,
catch (error) {
throw new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required. \nOriginal error: \n${error}`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parseSFC function is a valuable addition for handling SFC parsing with proper error handling. However, when re-throwing the error, consider preserving the original error stack for easier debugging.

- throw new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required. \nOriginal error: \n${error}`)
+ const newError = new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required. \nOriginal error: \n${error}`);
+ newError.stack = error.stack;
+ throw newError;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export async function parseSFC(code: string, options?: SFCParseOptions): Promise<SFCDescriptor> {
try {
return (
VueParser(code, {
pad: 'space',
...options,
}).descriptor
// for @vue/compiler-sfc ^2.7
|| (VueParser as any)({
source: code,
...options,
})
)
}
async function hasChanged(filePath: string, routeBlock?: SFCBlock) {
if (!routeBlock)
routeBlock = await getRouteSfcBlock(normalizePath(filePath))
return !pages.has(filePath) || JSON.stringify(parseData(routeBlock)) !== pages.get(filePath)
}
return {
setCache,
hasChanged,
catch (error) {
throw new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required. \nOriginal error: \n${error}`)
export async function parseSFC(code: string, options?: SFCParseOptions): Promise<SFCDescriptor> {
try {
return (
VueParser(code, {
pad: 'space',
...options,
}).descriptor
// for @vue/compiler-sfc ^2.7
|| (VueParser as any)({
source: code,
...options,
})
)
}
catch (error) {
const newError = new Error(`[vite-plugin-uni-pages] Vue3's "@vue/compiler-sfc" is required. \nOriginal error: \n${error}`);
newError.stack = error.stack;
throw newError;

@Ares-Chang
Copy link
Member

Sorry,最近大家可能都比较忙,可能没有时间看,如果非必要请不要 @,感谢。

如果比较着急,可以看自己发个包用一下。

我看你的文件改动比较大,如果不冲突最好把重构和新功能分开 pr。

@edwinhuish
Copy link
Contributor Author

edwinhuish commented Mar 24, 2024

如果比较着急,可以看自己发个包用一下。

我最近没有用uniapp的需求。如果有其他人需要这个功能再考虑另发包。

我看你的文件改动比较大,如果不冲突最好把重构和新功能分开 pr。

改动的细分很难界定,曾经试过在其他库提几个文件的修改,因为代码样式、实现方式等,来来回回改近十个commit,拖了十多天才通过第一个PR,后续PR都一直等第一个PR合并才能继续。。。

另,正如我之前所说,我写这个PR,仅仅是为爱发电,如果你觉得改动太多,不好梳理,或者你们按照需求去拆分commit?对于commit的owner是否我,都没问题。

此 PR 暂关闭,分支在此仓库下,你们自行决定怎么处理吧?

@edwinhuish edwinhuish closed this Mar 24, 2024
@CrazyZhang3
Copy link
Contributor

个人感觉definePage比其他几种方式更优雅 希望早日能实现合并

@edwinhuish
Copy link
Contributor Author

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants