Open octet-stream opened 9 months ago
I found a workaround with vite-plugin-typescript-transform
This is how to use it:
In vite.config.ts
add this plugin to plugins list (can be placed anywhere) and set enforce
option to pre
, then override jsx
option to preserve, so that it will be transformed later
import ts from "typescript"
import {defineConfig, type UserConfig} from "vite"
import {qwikVite} from "@builder.io/qwik/optimizer"
import {qwikCity} from "@builder.io/qwik-city/vite"
import tsconfigPaths from "vite-tsconfig-paths"
import {vitePluginTypescriptTransform} from "vite-plugin-typescript-transform"
export default defineConfig(() => ({
plugins: [
vitePluginTypescriptTransform({
enforce: "pre",
filter: {
files: {
include: /.tsx?$/,
}
},
tsconfig: {
override: {
jsx: ts.JsxEmit.Preserve
}
}
}),
qwikCity({trailingSlash: false}),
qwikVite(),
tsconfigPaths()
]
}))
This is not the solution and I believe this have to be supported out of the box, because decorators is common pattern.
Also, as far as I can tell, the problem occurs because rollup
can't parse modules with decorators, so this is a compilation problem. And as I said before - it happens only in dev mode and I have no idea why.
In production, the optimizer will transform the code, and it looks like it supports decorators. In dev mode, it's either not involved or only slightly involved, and rollup will have to handle the decorators.
So the solution would be either to run the optimizer in dev mode, which might be hard especially with hot reload (no idea), or to add decorator support to rollup somehow.
Since not everyone is using decorators, I think this should be a user side configuration, except if it can be supported with minimal overhead, in which case a PR is welcome.
@octet-stream
import { defineConfig, UserConfig } from 'vite'
import { qwikVite } from '@builder.io/qwik/optimizer'
import { qwikCity } from '@builder.io/qwik-city/vite'
import tsconfigPaths from 'vite-tsconfig-paths'
import { babel } from '@rollup/plugin-babel'
export default defineConfig((): UserConfig => ({
plugins: [
qwikCity({ trailingSlash: false }),
qwikVite(),
tsconfigPaths(),
babel({
babelHelpers: 'bundled',
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx'],
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }]
],
}),
],
server: {
headers: {
'Cache-Control': 'public, max-age=0'
}
},
preview: {
headers: {
'Cache-Control': 'public, max-age=600'
}
}
}))
it works fine if utilizing babel
.
@wmertens what do i do for qwik ?
I saw qwik directly run the Vite in the Dev,
offer a plugin? or run by qwik, such as qwik dev
?
@JerryWu1234 I don't understand what you mean.
In any case, you can change the config to only run babel during dev, and maybe only on the files with legacy decorators.
You could also use a codemod to remove the legacy decorators.
Ok I figured it out, so the optimizer does allow decorator syntax but it doesn't transpile it, not even in prod mode. During dev, vite does not add the vite:esbuild plugin, which does transpile it, so that's when it errors out.
I'm working on the optimizer so I'll see if I can just enable decorator syntax transpiling.
Ok I figured it out, so the optimizer does allow decorator syntax but it doesn't transpile it, not even in prod mode. During dev, vite does not add the vite:esbuild plugin, which does transpile it, so that's when it errors out.
I'm working on the optimizer so I'll see if I can just enable decorator syntax transpiling.
I'm not good at Rust, meanwhile I also have checked the result of the optimizer; which didn't any transplie surely, so I'll close this PR,however, please let me know if there is anything else I can do I'll pick other issue up,
Ok I know where to do it but I don't know how to get swc to do it. I asked in their discord but no response so far.
You mean how to get it to transpile decorators?
This works for legacy decorators and emits metadata:
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
}
}
}
Here's an example:
I meant using the rust API which is what Qwik is using... I'm getting there
Everything is in place to enable this now, except figuring out how to do the transform with SWC.
Everything is in place to enable this now, except figuring out how to do the transform with SWC.
@wmertens I saw you already knew how to enable it so that DId you submit a PR?
Which component is affected?
Qwik Rollup / Vite plugin
Describe the bug
I've been playing around with Qwik and Qwik City building simple demos. Pretty great framework so far, by the way! But when I decided to try it with Mikro ORM I stumbled upon following issue: Qwik breaks with TypeScript legacy decorators and Mikro ORM relies heavily on decorators for database schema definition, so I need those to work properly to be able to use Mikro ORM with Qwik app. The issue appears only in in dev mode.
Reproduction
https://github.com/octet-stream/qwik-legacy-decorators-issue
Steps to reproduce
pnpm i
pnpm start
and you'll see following errorThis error does not appear in preview mode. Run
pnpm preview
to verify it. Vite will open your browser and you'll see a todo list app. You will be able to add and remove todos.System Info
Additional Information
I saw similar issue, but the error is still there and happens only in dev mode, so I believe there's a way to make it work for dev mode as well without bringing some additional plugins. Also, from what I can tell reading the comments the solution with babel plugin doesn't work.