-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Class fields initialized in the wrong order when useDefineForClassFields
is set to true
#11722
Comments
It seems this is happening because Vite transpiles TS to JS (ESNext) first and then transpiles to lower target. There's a inconsistency in TypeScript with different target: microsoft/TypeScript#45995, microsoft/TypeScript#50971 class A {
public tag: string = "[" + this.name + "]";
constructor(public name: string) {
}
}
let a = new A("asd");
console.log(a.tag); does work for ES2022<. But doesn't work with ES2022+/ESNext. |
Hey @sapphi-red , thanks for reply. But I have target set to I tried setting |
I mean Vite first transpiles your TS code to JS with The only workaround I can think of is to move the assignment inside the constructor. export class A {
public tag: string;
constructor(public name: string) {
this.tag = "[" + this.name + "]";
}
}
let a = new A("asd");
console.log(a.tag); |
Ah, I see. Yes, moving it to a constructor is a workaround we're currently utilizing. The problem is that it's not being shown as an error by TS, so we have to make everyone aware of this and hope no one will do it. Would be nice to have eslint rule for this till it's fixed, but I haven't found any that can suit this case. |
This is a huge bug! It essentially eliminates the usage of MobX in any non-trivial codebase. In our case, we would have to change thousands of places in our codebase (despite being correct according to TypeScript syntax) and afterwards risk mistakes that are invisible until run-time. For what it's worth, Vite docs recognize the significance of https://vitejs.dev/guide/features.html#typescript-compiler-options --- EDIT ---Looks like putting the following in import { defineConfig } from 'vite';
export default defineConfig(
{
esbuild: {
target: "es2020"
},
// ...
}
) This is the same |
How do I fix this for scripts with |
Describe the bug
When
useDefineForClassFields
flag is set to true in thetsconfig.json
then the output produced by vite has class properties initialized in the wrong order.Sample
ts
code:The
tag
property relies on thename
property, which is guaranteed to be a string, but in the output that vite produces the tag property is initialized before the name is.Stackblitz link
TS compiles it in the right order Playground Link
However if I just compile the code with
esbuild
of the same version that is specified in the vite dependencies it produces the correct order:Stackblitz link
What can be the reason for this difference in the ourput?
tsconfig
:vite.config.ts
:esbuild output produced with this command
Reproduction
https://stackblitz.com/edit/vitejs-vite-eidnfv?file=dist/assets/main-57fe9a19.js
Steps to reproduce
npm run esbuild
dist
andes-dist
System Info
Used Package Manager
pnpm
Logs
❯ pnpm vite build --debug
vite:config bundled config file loaded in 322.33ms +0ms
vite:esbuild init tsconfck (root: C:/dev/_playground/esbuild_class_props_test) +0ms
vite:esbuild init tsconfck (root: C:/dev/_playground/esbuild_class_props_test) +2ms
vite:esbuild init tsconfck (root: C:/dev/_playground/esbuild_class_props_test) +1ms
vite:esbuild init tsconfck (root: C:/dev/_playground/esbuild_class_props_test) +1ms
vite:esbuild init tsconfck end +1ms
vite:esbuild init tsconfck end +1ms
vite:esbuild init tsconfck end +0ms
vite:esbuild init tsconfck end +0ms
vite:config using resolved config: {
vite:config build: {
vite:config target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
vite:config cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
vite:config outDir: 'dist',
vite:config assetsDir: 'assets',
vite:config assetsInlineLimit: 4096,
vite:config cssCodeSplit: true,
vite:config sourcemap: false,
vite:config rollupOptions: { input: [Object] },
vite:config minify: false,
vite:config terserOptions: {},
vite:config write: true,
vite:config emptyOutDir: null,
vite:config copyPublicDir: true,
vite:config manifest: false,
vite:config lib: false,
vite:config ssr: false,
vite:config ssrManifest: false,
vite:config reportCompressedSize: true,
vite:config chunkSizeWarningLimit: 500,
vite:config watch: null,
vite:config commonjsOptions: { include: [Array], extensions: [Array] },
vite:config dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
vite:config modulePreload: { polyfill: true }
vite:config },
vite:config optimizeDeps: {
vite:config disabled: 'build',
vite:config force: undefined,
vite:config esbuildOptions: { preserveSymlinks: false }
vite:config },
vite:config configFile: 'C:/dev/_playground/esbuild_class_props_test/vite.config.ts',
vite:config configFileDependencies: [ 'C:/dev/_playground/esbuild_class_props_test/vite.config.ts' ],
vite:config inlineConfig: {
vite:config root: undefined,
vite:config base: undefined,
vite:config mode: undefined,
vite:config configFile: undefined,
vite:config logLevel: undefined,
vite:config clearScreen: undefined,
vite:config optimizeDeps: { force: undefined },
vite:config build: {}
vite:config },
vite:config root: 'C:/dev/_playground/esbuild_class_props_test',
vite:config base: '/',
vite:config rawBase: '/',
vite:config resolve: {
vite:config mainFields: [ 'module', 'jsnext:main', 'jsnext' ],
vite:config browserField: true,
vite:config conditions: [],
vite:config extensions: [
vite:config '.mjs', '.js',
vite:config '.mts', '.ts',
vite:config '.jsx', '.tsx',
vite:config '.json'
vite:config ],
vite:config dedupe: [],
vite:config preserveSymlinks: false,
vite:config alias: [ [Object], [Object] ]
vite:config },
vite:config publicDir: 'C:\dev\_playground\esbuild_class_props_test\public',
vite:config cacheDir: 'C:/dev/_playground/esbuild_class_props_test/node_modules/.vite',
vite:config command: 'build',
vite:config mode: 'production',
vite:config ssr: {
vite:config format: 'esm',
vite:config target: 'node',
vite:config optimizeDeps: { disabled: true, esbuildOptions: [Object] }
vite:config },
vite:config isWorker: false,
vite:config mainConfig: null,
vite:config isProduction: true,
vite:config plugins: [
vite:config 'vite:build-metadata',
vite:config 'vite:pre-alias',
vite:config 'alias',
vite:config 'vite:modulepreload-polyfill',
vite:config 'vite:resolve',
vite:config 'vite:html-inline-proxy',
vite:config 'vite:css',
vite:config 'vite:esbuild',
vite:config 'vite:json',
vite:config 'vite:wasm-helper',
vite:config 'vite:worker',
vite:config 'vite:asset',
vite:config 'vite:wasm-fallback',
vite:config 'vite:define',
vite:config 'vite:css-post',
vite:config 'vite:build-html',
vite:config 'vite:worker-import-meta-url',
vite:config 'vite:asset-import-meta-url',
vite:config 'vite:force-systemjs-wrap-complete',
vite:config 'vite:watch-package-data',
vite:config 'commonjs',
vite:config 'vite:data-uri',
vite:config 'vite:dynamic-import-vars',
vite:config 'vite:import-glob',
vite:config 'vite:build-import-analysis',
vite:config 'vite:esbuild-transpile',
vite:config 'vite:reporter',
vite:config 'vite:load-fallback'
vite:config ],
vite:config server: {
vite:config preTransformRequests: true,
vite:config middlewareMode: false,
vite:config fs: { strict: true, allow: [Array], deny: [Array] }
vite:config },
vite:config preview: {
vite:config port: undefined,
vite:config strictPort: undefined,
vite:config host: undefined,
vite:config https: undefined,
vite:config open: undefined,
vite:config proxy: undefined,
vite:config cors: undefined,
vite:config headers: undefined
vite:config },
vite:config env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
vite:config assetsInclude: [Function: assetsInclude],
vite:config logger: {
vite:config hasWarned: false,
vite:config info: [Function: info],
vite:config warn: [Function: warn],
vite:config warnOnce: [Function: warnOnce],
vite:config error: [Function: error],
vite:config clearScreen: [Function: clearScreen],
vite:config hasErrorLogged: [Function: hasErrorLogged]
vite:config },
vite:config packageCache: Map(0) { set: [Function (anonymous)] },
vite:config createResolver: [Function: createResolver],
vite:config worker: {
vite:config format: 'iife',
vite:config plugins: [
vite:config 'vite:build-metadata',
vite:config 'vite:pre-alias',
vite:config 'alias',
vite:config 'vite:modulepreload-polyfill',
vite:config 'vite:resolve',
vite:config 'vite:html-inline-proxy',
vite:config 'vite:css',
vite:config 'vite:esbuild',
vite:config 'vite:json',
vite:config 'vite:wasm-helper',
vite:config 'vite:worker',
vite:config 'vite:asset',
vite:config 'vite:wasm-fallback',
vite:config 'vite:define',
vite:config 'vite:css-post',
vite:config 'vite:build-html',
vite:config 'vite:worker-import-meta-url',
vite:config 'vite:asset-import-meta-url',
vite:config 'vite:force-systemjs-wrap-complete',
vite:config 'vite:watch-package-data',
vite:config 'commonjs',
vite:config 'vite:data-uri',
vite:config 'vite:dynamic-import-vars',
vite:config 'vite:import-glob',
vite:config 'vite:build-import-analysis',
vite:config 'vite:esbuild-transpile',
vite:config 'vite:load-fallback'
vite:config ],
vite:config rollupOptions: {},
vite:config getSortedPlugins: [Function: getSortedPlugins],
vite:config getSortedPluginHooks: [Function: getSortedPluginHooks]
vite:config },
vite:config appType: 'spa',
vite:config experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
vite:config getSortedPlugins: [Function: getSortedPlugins],
vite:config getSortedPluginHooks: [Function: getSortedPluginHooks]
vite:config } +20ms
vite v4.0.4 building for production...
✓ 3 modules transformed.
dist/index.html 0.17 kB
dist/assets/main-57fe9a19.js 1.79 kB │ gzip: 0.74 kB
Validations
The text was updated successfully, but these errors were encountered: