Description
Vue version
3.3.4
Link to minimal reproduction
https://github.com/Boux/vite-coffee-bug
Steps to reproduce
clone my example repo by running
git clone https://github.com/Boux/vite-coffee-bug.git
cd vite-coffee-bug
npm install
npm run build
This is a simple vue project that uses vite to compile everything, it uses 2 plugins in the vite.config.js
:
@vitejs/plugin-vue
- a custom plugin that compiles coffeescript in
.coffee
files and in<script lang="coffee">
in.vue
files
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import CoffeeScript from "coffeescript"
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
{
name: 'coffee_compile',
transform: function(src, id) {
// compile coffee files to js
if (/\.coffee$/.test(id)) {
var {js, sourceMap} = CoffeeScript.compile(src, { sourceMap: true })
return { code: js, map: sourceMap }
}
}
}
],
})
What is expected?
The sfc compiler called from the @vitejs/plugin-vue
plugin should output something like this:
import _sfc_main from "/Users/boux/repo/vite_coffee_bug/src/App.vue?vue&type=script&lang.coffee"
export * from "/Users/boux/repo/vite_coffee_bug/src/App.vue?vue&type=script&lang.coffee"
import { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
function _sfc_render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", null, [
_createElementVNode("button", {
type: "button",
onClick: _cache[0] || (_cache[0] = $event => (_ctx.count++))
}, "count is " + _toDisplayString(_ctx.count), 1)
]))
}
import _export_sfc from 'plugin-vue:export-helper'
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render]])
which I can then use in the coffee_compile
plugin to compile the contents of import _sfc_main from "/Users/boux/repo/vite_coffee_bug/src/App.vue?vue&type=script&lang.coffee"
from coffeescript to javascript
What is actually happening?
The src compiler crashes with syntax errors as if it's trying to parse the code as typescript
# npm run build
> vite-coffee-bug@0.0.0 build
> vite build
vite v4.3.8 building for production...
✓ 3 modules transformed.
✓ built in 79ms
[vite:vue] [vue/compiler-sfc] Missing semicolon. (5:6)
/Users/boux/repo/vite-coffee-bug/src/App.vue
3 |
4 | export default
5 | data: -> { count: ref(0) }
| ^
6 | </script>
7 |
file: /Users/boux/repo/vite-coffee-bug/src/App.vue:5:6
error during build:
SyntaxError: [vue/compiler-sfc] Missing semicolon. (5:6)
/Users/boux/repo/vite-coffee-bug/src/App.vue
3 |
4 | export default
5 | data: -> { count: ref(0) }
| ^
6 | </script>
7 |
at instantiate (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:653:32)
at constructor (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:946:12)
at Parser.raise (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:3270:19)
at Parser.semicolon (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:3637:10)
at Parser.parseExportDefaultExpression (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:13759:10)
at Parser.parseExport (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:13663:25)
at Parser.parseStatementContent (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:12661:27)
at Parser.parseStatementLike (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:12549:17)
at Parser.parseModuleItem (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:12526:17)
at Parser.parseBlockOrModuleBlockBody (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:13121:36)
at Parser.parseBlockBody (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:13114:10)
at Parser.parseProgram (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:12437:10)
at Parser.parseTopLevel (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:12427:25)
at Parser.parse (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:14245:10)
at Object.parse (/Users/boux/repo/vite-coffee-bug/node_modules/@babel/parser/lib/index.js:14286:38)
at parse (/Users/boux/repo/vite-coffee-bug/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:15812:25)
at new ScriptCompileContext (/Users/boux/repo/vite-coffee-bug/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:15828:43)
at Object.compileScript (/Users/boux/repo/vite-coffee-bug/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:19817:15)
at resolveScript (file:///Users/boux/repo/vite-coffee-bug/node_modules/@vitejs/plugin-vue/dist/index.mjs:283:31)
at genScriptCode (file:///Users/boux/repo/vite-coffee-bug/node_modules/@vitejs/plugin-vue/dist/index.mjs:2469:18)
at transformMain (file:///Users/boux/repo/vite-coffee-bug/node_modules/@vitejs/plugin-vue/dist/index.mjs:2282:54)
at Object.transform (file:///Users/boux/repo/vite-coffee-bug/node_modules/@vitejs/plugin-vue/dist/index.mjs:2794:16)
at file:///Users/boux/repo/vite-coffee-bug/node_modules/rollup/dist/es/shared/node-entry.js:24592:40
System Info
Tested on both `Mac OS 13.3.1` and up-to-date `Arch Linux`
Both on `node v16.16.0 (npm v8.11.0)` and `node v20.2.0 (npm v9.6.6)`
System:
OS: macOS 13.3.1
CPU: (8) arm64 Apple M1
Memory: 62.44 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v16.16.0/bin/yarn
npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
Browsers:
Brave Browser: 113.1.51.110
Chrome: 113.0.5672.92
Firefox: 112.0.2
Safari: 16.4
npmPackages:
vue: 3.3 => 3.3.4
Any additional comments?
I am not sure if something has to be changed in @vitejs/plugin-vue
when it calls the compiler, or if the compiler itself has a bug, or if this intended. It doesn't seem intended after reading the vue 3.3 blog post: https://blog.vuejs.org/posts/vue-3-3#script-setup-typescript-dx-improvements. They talk about fixes in their compiler for typescript when the code is in a <script setup>
, but I'm not using a <script setup>
and much less typescript. Nothing about restricting custom script languages