Skip to content

Conversation

alex-snezhko
Copy link
Contributor

@alex-snezhko alex-snezhko commented Jun 21, 2025

close #8368

Move the logic to avoid compilation if the script is not JS or TS before the file is actually attempted to be compiled.

I tested with a similar example to the reproduction link given in the issue:

App.vue:

<script lang="coffee">
export default
  data: ->
    count: 0

  methods:
    increment: ->
      @count += 1
</script>

<template>
  <button type="button" @click="increment">Count: {{ count }}</button>
</template>

vite.config.js:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import CoffeeScript from "coffeescript"
import * as sfcCompiler from "<locally built SFC compiler path>"

export default defineConfig({
  plugins: [
    vue({ compiler: sfcCompiler }),
    {
      name: 'coffee_compile',
      transform: (src, id) => {
        // compile coffee files to js
        if (/\.coffee$/.test(id)) {
          const { js, sourceMap } = CoffeeScript.compile(src, { sourceMap: true })
          return { code: js, map: sourceMap }
        }
      }
    }
  ],
})

Builds and functions as expected:
Screenshot From 2025-06-20 19-49-55

Summary by CodeRabbit

  • New Features
    • Preserve script blocks using unsupported languages (e.g., CoffeeScript) without transforming them.
  • Bug Fixes
    • Avoid unnecessary processing for non-JS/TS scripts to reduce incorrect transformations.
  • Refactor
    • Centralized language-detection via new utilities to streamline JS/TS checks and defer processing.
  • Tests
    • Added a test validating behavior with unsupported script language attributes.

Copy link

coderabbitai bot commented Jun 21, 2025

Walkthrough

Refactors script language detection to use new isJS/isTS utilities, defers creating ScriptCompileContext until JS/TS scripts are detected, adds a test ensuring <script lang="coffee"> is returned unchanged with no AST, and centralizes language checks in script context utilities.

Changes

Cohort / File(s) Change Summary
New test
packages/compiler-sfc/__tests__/compileScript.spec.ts
Added test verifying <script lang="coffee"> is preserved verbatim, lang retained, and no scriptAst produced.
Language utils
packages/compiler-sfc/src/script/utils.ts
Added isJS(...langs) and isTS(...langs) helpers to centralize JS/TS language detection.
Compile entry
packages/compiler-sfc/src/compileScript.ts
Use isJS/isTS to compute isJSOrTS; early-return original script when not JS/TS; defer creating ScriptCompileContext until JS/TS detected.
Script context
packages/compiler-sfc/src/script/context.ts
Replaced inline language checks with calls to isJS/isTS when initializing isJS/isTS flags.
Normal script processing
packages/compiler-sfc/src/script/normalScript.ts
Removed prior inline early-return for non-JS/TS scripts so processing path inside the try block executes for all script languages.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant compileScript
    participant utils as isJS/isTS
    participant Response

    Caller->>compileScript: compile SFC (has <script lang="coffee">)
    compileScript->>utils: isJS(lang), isTS(lang)
    utils-->>compileScript: false / false
    compileScript-->>Response: return original script block (lang="coffee", no scriptAst)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Preserve and do not parse/transform custom script languages (e.g., CoffeeScript) in SFCs (#8368)
Ensure compileScript returns the original script for unsupported languages, avoiding syntax errors (#8368)
Add/verify tests for correct handling of non-JS/TS script blocks (#8368)

Out-of-scope changes

Code Change Explanation
Removal of early-return for non-JS/TS in normalScript processing (packages/compiler-sfc/src/script/normalScript.ts) The linked issue expects unsupported languages to be returned early; allowing non-JS/TS scripts to proceed into the try processing path is not required by the issue and may change downstream behavior (not clearly tied to the stated objectives).

Possibly related PRs

Suggested reviewers

  • LittleSound
  • Doctor-wu

Poem

"I nibble code and hop with glee,
A coffee script stays safe with me.
No parse, no AST to make a fuss,
I keep your lang as written thus.
Hoppity-hop — preserved and free!"


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 99b49cf and 80c40b3.

📒 Files selected for processing (1)
  • packages/compiler-sfc/src/compileScript.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/compiler-sfc/src/compileScript.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

pkg-pr-new bot commented Jun 21, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13508

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13508

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13508

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13508

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13508

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13508

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13508

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13508

@vue/shared

npm i https://pkg.pr.new/@vue/shared@13508

vue

npm i https://pkg.pr.new/vue@13508

@vue/compat

npm i https://pkg.pr.new/@vue/compat@13508

commit: 80c40b3

Copy link

github-actions bot commented Jun 21, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.5 kB 34.7 kB
vue.global.prod.js 159 kB 58.6 kB 52.2 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.6 kB 21.3 kB 19.4 kB
createSSRApp 58.9 kB 23 kB 21 kB
defineCustomElement 59.6 kB 22.8 kB 20.8 kB
overall 68.7 kB 26.4 kB 24.1 kB

@edison1105 edison1105 added scope: sfc ready for review This PR requires more reviews ready to merge The PR is ready to be merged. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed ready for review This PR requires more reviews labels Jun 23, 2025
@vuejs vuejs deleted a comment from edison1105 Jul 8, 2025
Copy link

netlify bot commented Sep 2, 2025

Deploy Preview for vue-sfc-playground failed. Why did it fail? →

Name Link
🔨 Latest commit 99b49cf
🔍 Latest deploy log https://app.netlify.com/projects/vue-sfc-playground/deploys/68b6b6e28ce87c000847ccb4

@edison1105
Copy link
Member

/ecosystem-ci run

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: 1

🧹 Nitpick comments (1)
packages/compiler-sfc/src/compileScript.ts (1)

216-219: Early-return for non-JS/TS <script setup> is correct; consider edge case with both blocks non-JS/TS.

If both <script> and <script setup> exist with the same custom lang, we currently return only scriptSetup and effectively drop the normal script. Is that intended? If not, we may need to surface both blocks to downstream tooling or enforce a single-block constraint for non-JS/TS.

Would you like me to draft a test covering “both blocks with lang='coffee'” and clarify expected behavior?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c4392aa and 99b49cf.

📒 Files selected for processing (2)
  • packages/compiler-sfc/__tests__/compileScript.spec.ts (1 hunks)
  • packages/compiler-sfc/src/compileScript.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/compiler-sfc/tests/compileScript.spec.ts
🧰 Additional context used
🧬 Code graph analysis (1)
packages/compiler-sfc/src/compileScript.ts (3)
packages/compiler-sfc/src/script/utils.ts (2)
  • isJS (125-126)
  • isTS (127-128)
packages/compiler-sfc/src/script/context.ts (1)
  • ScriptCompileContext (14-151)
packages/compiler-sfc/src/script/normalScript.ts (1)
  • processNormalScript (10-53)
🪛 Biome (2.1.2)
packages/compiler-sfc/src/compileScript.ts

[error] 221-221: Shouldn't redeclare 'ctx'. Consider to delete it or rename it.

'ctx' is defined here:

(lint/suspicious/noRedeclare)

🪛 GitHub Check: test / lint-and-test-dts
packages/compiler-sfc/src/compileScript.ts

[failure] 221-221:
Cannot redeclare block-scoped variable 'ctx'.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: test / unit-test
  • GitHub Check: test / e2e-test
  • GitHub Check: test / unit-test-windows
🔇 Additional comments (4)
packages/compiler-sfc/src/compileScript.ts (4)

59-65: Good import additions for centralized lang detection.

Using isJS/isTS from script/utils.ts is the right direction to keep language checks consistent across the codebase.


182-184: LGTM: single boolean for JS/TS detection.

isJSOrTS correctly captures both script and scriptSetup lang variants.


199-204: Correct early-return for non-JS/TS normal <script>.

This avoids unnecessary parsing and lets downstream tools handle custom langs.


209-214: Duplicate lang-mismatch check; remove to avoid redundancy.

This block repeats the same error thrown at Lines 185–190 and is unnecessary.

Apply this diff to drop the duplicate:

-  if (script && scriptLang !== scriptSetupLang) {
-    throw new Error(
-      `[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
-        `language type.`,
-    )
-  }

Likely an incorrect or invalid review comment.

@vuejs vuejs deleted a comment from edison1105 Sep 2, 2025
@vue-bot
Copy link
Contributor

vue-bot commented Sep 2, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools success success
pinia success success
nuxt success success
router success success
vite-plugin-vue success success
radix-vue success success
vitepress success success
vuetify success success
vue-i18n success success
vant success failure
vueuse success success
primevue success success
test-utils success success
vue-macros failure failure
quasar success success
vue-simple-compiler success success

@edison1105 edison1105 merged commit 55922ff into vuejs:main Sep 2, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: sfc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using custom script languages (such as coffeescript) no longer works since vue 3.3 (works in 3.2)
3 participants