Skip to content
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

refactor(vite): drop externality and use vite internal config #30634

Merged
merged 6 commits into from
Jan 18, 2025

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Jan 16, 2025

🔗 Linked issue

📚 Description

this drops externality which should clean up the dependency tree a bit (graph).

this will need testing to ensure behaviour is still the same in dev - you can use the packages published in this comment to do so.

Copy link

stackblitz bot commented Jan 16, 2025

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

socket-security bot commented Jan 16, 2025

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher

🚮 Removed packages: npm/[email protected]

View full report↗︎

Copy link

pkg-pr-new bot commented Jan 16, 2025

Open in Stackblitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@30634

nuxt

npm i https://pkg.pr.new/nuxt@30634

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@30634

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@30634

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@30634

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@30634

commit: 83b0bd3

@danielroe danielroe marked this pull request as ready for review January 17, 2025 11:11
@danielroe danielroe requested a review from antfu January 17, 2025 11:11
Copy link

coderabbitai bot commented Jan 17, 2025

Walkthrough

The pull request introduces significant changes to module handling in the Vite and Nuxt integration, specifically focusing on external module resolution. The primary modifications involve removing the external.ts utility file, which contained the createIsExternal function, and updating the vite-node.ts configuration. The changes streamline the approach to managing module dependencies by simplifying the external module detection mechanism.

The implementation now uses a more direct method of handling module externalization, with updated import statements and modified dependency configuration. The createViteNodeApp function has been adjusted to remove the invalidates parameter, and a new external array has been introduced to manage shared modules. These changes indicate a fundamental restructuring of how external modules are identified and processed within the development environment, reflecting an evolution in the project's approach to module resolution and dependency management.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a448fd and 83b0bd3.

📒 Files selected for processing (1)
  • packages/vite/src/vite-node.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build
  • GitHub Check: code
🔇 Additional comments (2)
packages/vite/src/vite-node.ts (2)

9-11: Imports are correctly added

The imports for getQuery, withTrailingSlash, and escapeStringRegexp are appropriate and support the new configurations.


123-127: Reconsider the necessity of 'external' configurations

These external patterns may already be set in ssr.noExternal and respected by vite-node. Including them here might be redundant.

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • Generate unit testing code 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/vite/src/vite-node.ts (1)

119-126: Review the broad inclusion of modules in 'deps.inline'.

The regular expressions added to deps.inline may inline more modules than necessary:

  • /^virtual:/ matches all modules starting with virtual:.
  • /\.ts$/ matches all files ending with .ts.
  • /^#/ matches all modules starting with #.
  • /\?/ matches any module containing ?.

Inlining all TypeScript files and modules containing ? might impact performance. Consider refining these patterns to target only the required modules.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc669cb and 3a448fd.

⛔ Files ignored due to path filters (2)
  • packages/vite/package.json is excluded by !**/package.json
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !pnpm-lock.yaml
📒 Files selected for processing (2)
  • packages/vite/src/utils/external.ts (0 hunks)
  • packages/vite/src/vite-node.ts (2 hunks)
💤 Files with no reviewable changes (1)
  • packages/vite/src/utils/external.ts
🔇 Additional comments (3)
packages/vite/src/vite-node.ts (3)

9-9: Import of 'withTrailingSlash' is appropriate.

The addition of withTrailingSlash from the 'ufo' module appears correct, as it is utilised later in constructing paths with trailing slashes.


11-11: Usage of 'escapeStringRegexp' enhances security.

Employing escapeStringRegexp to escape paths when creating regular expressions helps prevent RegExp injection vulnerabilities.


128-129: Verify the constructed regular expression for external dependencies.

The regular expression created for deps.external aims to match the shared directory:

new RegExp('^' + escapeStringRegexp(withTrailingSlash(resolve(ctx.nuxt.options.rootDir, ctx.nuxt.options.dir.shared))))

Ensure that:

  • ctx.nuxt.options.dir.shared accurately reflects the shared directory path.
  • The resolved path correctly targets the intended modules without excluding necessary files or including unintended ones.

To confirm the regular expression matches the intended files, you can run the following script:

✅ Verification successful

The regular expression construction for external dependencies is correctly implemented

The implementation follows Nuxt's established patterns and safely handles path resolution by:

  • Using proper escaping via escape-string-regexp
  • Ensuring consistent path matching with withTrailingSlash
  • Correctly resolving paths relative to the root directory

This pattern is consistently used across the codebase, particularly in both vite-node.ts and server.ts, confirming its reliability for matching shared directory paths.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: List files matching the external dependencies regex.

regex="^$(echo "$(node -e "console.log(require('path').resolve('${ctx.nuxt.options.rootDir}', '${ctx.nuxt.options.dir.shared}'))")/")"
regex=$(echo "$regex" | sed 's/[\/&]/\\&/g')

echo "Constructed regex: $regex"
echo "Files matching the regex:"
find . -type f | grep -E "$regex"

Length of output: 412


Script:

#!/bin/bash
# Search for shared directory configuration and similar patterns
echo "=== Searching for shared directory configuration ==="
rg -l "dir.shared" -g "!*.{lock,md}"

echo -e "\n=== Looking for similar regex constructions ==="
rg "new RegExp.*escapeStringRegexp.*resolve" -g "!*.{lock,md}"

echo -e "\n=== Checking path resolution patterns ==="
ast-grep --pattern 'resolve($_, $dir.shared)'

Length of output: 993


Script:

#!/bin/bash
# Examine the shared directory configuration
echo "=== Checking shared directory schema ==="
rg -A 5 "shared.*:" packages/schema/src/config/common.ts

echo -e "\n=== Checking for imports of the regex construction ==="
rg "escapeStringRegexp.*from|withTrailingSlash.*from" packages/vite/src/vite-node.ts

Length of output: 963

/^#/,
...transpile({ isServer: true, isDev: ctx.nuxt.options.dev }),
Copy link
Member Author

@danielroe danielroe Jan 17, 2025

Choose a reason for hiding this comment

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

we can drop this as it's already set on ssr.noExternal here:

noExternal: [
...transpile({ isServer: true, isDev: ctx.nuxt.options.dev }),
'/__vue-jsx',
'#app',
/^nuxt(\/|$)/,
/(nuxt|nuxt3|nuxt-nightly)\/(dist|src|app)/,
],

and this seems to be respected by vite-node when creating the server:

https://github.com/vitest-dev/vitest/blob/bf7b36ac1054fd80a54d393448e8fc25eec153c1/packages/vite-node/src/server.ts#L76-L95

@danielroe danielroe merged commit 6bf0691 into main Jan 18, 2025
46 checks passed
@danielroe danielroe deleted the refactor/drop-externality branch January 18, 2025 10:18
@github-actions github-actions bot mentioned this pull request Jan 18, 2025
Comment on lines +123 to +125
external: [
'#shared',
new RegExp('^' + escapeStringRegexp(withTrailingSlash(resolve(ctx.nuxt.options.rootDir, ctx.nuxt.options.dir.shared)))),
Copy link
Member

@Mini-ghost Mini-ghost Jan 19, 2025

Choose a reason for hiding this comment

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

@danielroe

I added some files in the shared/utils/ directory to use them in app/, and it seems that this part of the code might be causing an error where .ts files cannot be recognized:

Unknown file extension ".ts" for /Users/.../anything.ts

Copy link
Member Author

Choose a reason for hiding this comment

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

could you replicate in the basic fixture and push a pr with a failing test? 🙏

Copy link
Member

@Mini-ghost Mini-ghost Jan 19, 2025

Choose a reason for hiding this comment

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

Ooops! I added a test in the basic fixture, and everything worked well—I couldn’t replicate the issue. However, I did encounter an error with parsing .ts files under the playground/ folder.

at __node_internal_captureLargerStackTrace (node:internal/errors:497:5)
at new NodeError (node:internal/errors:406:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
at defaultLoad (node:internal/modules/esm/load:120:20)
at ModuleLoader.load (node:internal/modules/esm/loader:396:13)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:278:56)
at new ModuleJob (node:internal/modules/esm/module_job:65:26)
at #createModuleJob (node:internal/modules/esm/loader:290:17)
at ModuleLoader.getJobFromResolveResult (node:internal/modules/esm/loader:248:34)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:229:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:315:23)

Since nuxt-nightly hasn’t been updated to this version yet, I’ll test it again once the update is available.

Copy link
Member

Choose a reason for hiding this comment

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

I tried reproducing this issue using StackBlitz with Nuxt version 4.0.0-28957560.b6143461, and the error can be reproduced. Would it be helpful to open an issue for easier tracking?

👉 https://stackblitz.com/edit/nuxt-starter-f1lj7peg?file=nuxt.config.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants