Skip to content
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 property with auto-accessors not compiles #18610

Closed
7 tasks done
dardino opened this issue Nov 7, 2024 · 2 comments
Closed
7 tasks done

Class property with auto-accessors not compiles #18610

dardino opened this issue Nov 7, 2024 · 2 comments
Labels
has workaround p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@dardino
Copy link

dardino commented Nov 7, 2024

Describe the bug

Hi, I have found this issue #11891 but my problem still remaining:

try to write something like this

export class Model {
  accessor counter: number = 0;
}

and run vite build, you obtain this result:

vite v5.4.10 building for production...
✓ 7 modules transformed.
x Build failed in 1.15s
error during build:
src/model.ts (2:11): Unexpected token `counter`. Expected * for generator, private key, identifier or async (Note that you need plugins to import files that are not JavaScript)
file: /home/projects/accessor-class-field-not-supported-by-vite/src/model.ts:2:11

1: export class Model {
2:   accessor counter: number = 0;
              ^
3: }

    at Module.getRollupError (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/parseAst.js:558:41)
    at ParseError.initialise (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:13306:41)
    at convertNode (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:15017:10)
    at convertProgram (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:14260:12)
    at Module.setSource (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:16003:24)
    at async ModuleLoader.addModuleSource (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:19881:13)
 ELIFECYCLE  Command failed with exit code 1.

Stackblitz demo: https://stackblitz.com/edit/accessor-class-field-not-supported-by-vite?file=src%2Fmodel.ts

EsBuild playground: https://esbuild.github.io/try/#dAAwLjI0LjAAewogIGxvYWRlcjogJ3RzJywKICB0YXJnZXQ6ICJFUzIwMjQiLAp9AGZ1bmN0aW9uIGRlY28oKSB7fQoKY2xhc3MgUHJvdmEgewogIGFjY2Vzc29yIGN1c3RvbTogc3RyaW5nID0gIiI7Cn0

Reproduction

https://stackblitz.com/edit/accessor-class-field-not-supported-by-vite?file=src%2Fmodel.ts

Steps to reproduce

open https://stackblitz.com/edit/accessor-class-field-not-supported-by-vite?file=src%2Fmodel.ts and look the error in the Terminal after the build

System Info

System:
    OS: Linux 5.15 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 8.62 GB / 15.49 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 22.1.0 - ~/.nvm/versions/node/v22.1.0/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v22.1.0/bin/npm
    pnpm: 9.12.3 - ~/.local/share/pnpm/pnpm
  Browsers:
    Chrome: 129.0.6668.89
  npmPackages:
    vite: ^5.4.10 => 5.4.10

Used Package Manager

pnpm

Logs

~/projects/accessor-class-field-not-supported-by-vite
❯ vite build --debug
  vite:config bundled config file loaded in 2265.07ms +0ms
{
  __APP_ENV__: undefined,
  __APP_VERSION__: '"0.0.0"',
  __APP_NAME__: '"vite-typescript-starter"'
}
  vite:config using resolved config: {
  vite:config   define: {
  vite:config     __APP_ENV__: undefined,
  vite:config     __APP_VERSION__: '"0.0.0"',
  vite:config     __APP_NAME__: '"vite-typescript-starter"'
  vite:config   },
  vite:config   build: {
  vite:config     target: 'ES2020',
  vite:config     cssTarget: 'ES2020',
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  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     ssrEmitAssets: 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     cssMinify: true
  vite:config   },
  vite:config   configFile: '/home/projects/accessor-class-field-not-supported-by-vite/vite.config.ts',
  vite:config   configFileDependencies: [
  vite:config     '/home/projects/accessor-class-field-not-supported-by-vite/package.json',
  vite:config     '/home/projects/accessor-class-field-not-supported-by-vite/vite.config.ts'
  vite:config   ],
  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     build: {}
  vite:config   },
  vite:config   root: '/home/projects/accessor-class-field-not-supported-by-vite',
  vite:config   base: '/',
  vite:config   decodedBase: '/',
  vite:config   rawBase: '/',
  vite:config   resolve: {
  vite:config     mainFields: [ 'browser', 'module', 'jsnext:main', 'jsnext' ],
  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: '/home/projects/accessor-class-field-not-supported-by-vite/public',
  vite:config   cacheDir: '/home/projects/accessor-class-field-not-supported-by-vite/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   ssr: {
  vite:config     target: 'node',
  vite:config     optimizeDeps: { noDiscovery: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   bundleChain: [],
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:watch-package-data',
  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     '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:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   css: { lightningcss: undefined },
  vite:config   esbuild: { jsxDev: false },
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     sourcemapIgnoreList: [Function: isInNodeModules$1],
  vite:config     middlewareMode: false,
  vite:config     fs: {
  vite:config       strict: true,
  vite:config       allow: [Array],
  vite:config       deny: [Array],
  vite:config       cachedChecks: undefined
  vite:config     }
  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   envDir: '/home/projects/accessor-class-field-not-supported-by-vite',
  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(1) {
  vite:config     'fnpd_/home/projects/accessor-class-field-not-supported-by-vite' => {
  vite:config       dir: '/home/projects/accessor-class-field-not-supported-by-vite',
  vite:config       data: [Object],
  vite:config       hasSideEffects: [Function: hasSideEffects],
  vite:config       webResolvedImports: {},
  vite:config       nodeResolvedImports: {},
  vite:config       setResolvedCache: [Function: setResolvedCache],
  vite:config       getResolvedCache: [Function: getResolvedCache]
  vite:config     },
  vite:config     set: [Function (anonymous)]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     holdUntilCrawlEnd: true,
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite:config   },
  vite:config   worker: { format: 'iife', plugins: '() => plugins', rollupOptions: {} },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config } +22ms
vite v5.4.10 building for production...
✓ 7 modules transformed.
x Build failed in 1.08s
error during build:
src/model.ts (2:11): Unexpected token `counter`. Expected * for generator, private key, identifier or async (Note that you need plugins to import files that are not JavaScript)
file: /home/projects/accessor-class-field-not-supported-by-vite/src/model.ts:2:11

1: export class Model {
2:   accessor counter: number = 0;
              ^
3: }

    at Module.getRollupError (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/parseAst.js:558:41)
    at ParseError.initialise (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:13306:41)
    at convertNode (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:15017:10)
    at convertProgram (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:14260:12)
    at Module.setSource (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:16003:24)
    at async ModuleLoader.addModuleSource (file:///home/projects/accessor-class-field-not-supported-by-vite/node_modules/.pnpm/rollup@4.24.4/node_modules/rollup/dist/es/shared/node-entry.js:19881:13)

Validations

@sapphi-red
Copy link
Member

The workaround is to set esbuild.supported.decorators: false in the config.

export default defineConfig({
  esbuild: {
    supported: {
      decorators: false
    }
  }
})

It seems rollup does not support parsing auto-accessors and esbuild cannot enable auto-accessors transform separately from decorators.
Maybe we can set esbuild.supported.decorators: false by default, but I'm not sure about the impact of doing that.

@sapphi-red sapphi-red added has workaround p3-minor-bug An edge case that only affects very specific usage (priority) labels Nov 8, 2024
@dardino
Copy link
Author

dardino commented Nov 11, 2024

Thanks for your solution, now it works perfectly.

@dardino dardino closed this as completed Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

2 participants