fix(compiler-sfc): check lang before attempt to compile script
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:
Summary by CodeRabbit
- New Features
- Improved handling of script blocks with unrecognized language attributes, ensuring unsupported languages are preserved as-is and not processed.
- Bug Fixes
- Prevented unnecessary processing of non-JavaScript/TypeScript script blocks.
- Refactor
- Centralized language detection logic for scripts, streamlining checks for JavaScript and TypeScript.
- Tests
- Added a test to verify correct behavior with unsupported script languages.
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)
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
- vuejs/core#13194 โ Similar adjustments around early language checks and deferring
ScriptCompileContextcreation; strong code-level overlap.
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 99b49cf622fae7299fcaca4aa4bd37f16ed07bba and 80c40b38af9147b85c265390d2e06c289aec1120.
๐ 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.
๐ชง 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
@coderabbitaiin a new review comment at the desired location with your query. - PR comments: Tag
@coderabbitaiin 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 ignoreor@coderabbit ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile 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.
@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
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 |
Deploy Preview for vue-sfc-playground failed. Why did it fail? โ
| Name | Link |
|---|---|
| Latest commit | 99b49cf622fae7299fcaca4aa4bd37f16ed07bba |
| Latest deploy log | https://app.netlify.com/projects/vue-sfc-playground/deploys/68b6b6e28ce87c000847ccb4 |
/ecosystem-ci run
๐ Ran ecosystem CI: Open
| suite | result | latest scheduled |
|---|---|---|
| language-tools | :white_check_mark: success | :white_check_mark: success |
| pinia | :white_check_mark: success | :white_check_mark: success |
| nuxt | :white_check_mark: success | :white_check_mark: success |
| router | :white_check_mark: success | :white_check_mark: success |
| vite-plugin-vue | :white_check_mark: success | :white_check_mark: success |
| radix-vue | :white_check_mark: success | :white_check_mark: success |
| vitepress | :white_check_mark: success | :white_check_mark: success |
| vuetify | :white_check_mark: success | :white_check_mark: success |
| vue-i18n | :white_check_mark: success | :white_check_mark: success |
| vant | :white_check_mark: success | :x: failure |
| vueuse | :white_check_mark: success | :white_check_mark: success |
| primevue | :white_check_mark: success | :white_check_mark: success |
| test-utils | :white_check_mark: success | :white_check_mark: success |
| vue-macros | :x: failure | :x: failure |
| quasar | :white_check_mark: success | :white_check_mark: success |
| vue-simple-compiler | :white_check_mark: success | :white_check_mark: success |