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

Error: Using declaration is not enabled. Set jsc.parser.usingDecl to true #15464

Closed
7 tasks done
justjake opened this issue Dec 29, 2023 · 5 comments
Closed
7 tasks done
Labels
bug: upstream Bug in a dependency of Vite has workaround p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@justjake
Copy link

justjake commented Dec 29, 2023

Describe the bug

I am using a stock, default vite build, and trying to adopt the using statement supported by Typescript 5.2: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management

This results in an error:

Using declaration is not enabled. Set jsc.parser.usingDecl to true
file: /home/projects/vitejs-vite-94zdjw/main.js:6:0
4: import { setupCounter } from './counter.js'
5: 
6: using _x = 1
   ^
7: 
8: document.querySelector('#app').innerHTML = `
error during build:
RollupError: Using declaration is not enabled. Set jsc.parser.usingDecl to true
    at error (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:468:30)
    at nodeConverters (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:2215:9)
    at convertNode (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:1100:12)
    at convertProgram (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:1091:48)
    at Module.parseAstAsync (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:2281:20)
    at async Module.tryParseAsync (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/node-entry.js:13538:21)
    at async Module.setSource (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/node-entry.js:13119:35)
    at async ModuleLoader.addModuleSource (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/node-entry.js:17782:13)

Note that the error advises:

Set jsc.parser.usingDecl to true

I tried putting this in vite.config.js, but it didn't typecheck and had no effect on the error. I think it's an SWC option, and I don't see a way to pass options to SWC.

Reproduction

https://stackblitz.com/edit/vitejs-vite-94zdjw?file=main.js

Steps to reproduce

Add a using statement to a new vite project, like using _x = 1.

System Info

$ npx envinfo --system --npmPackages '{vite,@vitejs/*,rollup}' --binaries --browsers

Need to install the following packages:
envinfo@7.11.0
Ok to proceed? (y)

  System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M2
    Memory: 95.41 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/Library/pnpm/node
    Yarn: 4.0.2 - ~/Library/pnpm/yarn
    npm: 10.2.3 - ~/Library/pnpm/npm
    pnpm: 8.12.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 120.0.6099.129
    Safari: 17.0
  npmPackages:
    @vitejs/plugin-vue: ^4.5.2 => 4.5.2
    vite: ^5.0.8 => 5.0.10

Used Package Manager

npm

Logs

Click to expand!
~/projects/vitejs-vite-94zdjw
❯ npx vite build --debug
  vite:config no config file found. +0ms
  vite:config using resolved config: {
  vite:config   root: '/home/projects/vitejs-vite-94zdjw',
  vite:config   base: '/',
  vite:config   mode: 'production',
  vite:config   configFile: undefined,
  vite:config   logLevel: undefined,
  vite:config   clearScreen: undefined,
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite: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: {},
  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   configFileDependencies: [],
  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   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/vitejs-vite-94zdjw/public',
  vite:config   cacheDir: '/home/projects/vitejs-vite-94zdjw/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   ssr: {
  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: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: false
  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/vitejs-vite-94zdjw',
  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/vitejs-vite-94zdjw' => {
  vite:config       dir: '/home/projects/vitejs-vite-94zdjw',
  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   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 } +3ms
vite v5.0.10 building for production...
✓ 3 modules transformed.
Using declaration is not enabled. Set jsc.parser.usingDecl to true
file: /home/projects/vitejs-vite-94zdjw/main.js:6:0
4: import { setupCounter } from './counter.js'
5: 
6: using _x = 1
   ^
7: 
8: document.querySelector('#app').innerHTML = `
error during build:
RollupError: Using declaration is not enabled. Set jsc.parser.usingDecl to true
    at error (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:468:30)
    at nodeConverters (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:2215:9)
    at convertNode (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:1100:12)
    at convertProgram (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:1091:48)
    at Module.parseAstAsync (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/parseAst.js:2281:20)
    at async Module.tryParseAsync (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/node-entry.js:13538:21)
    at async Module.setSource (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/node-entry.js:13119:35)
    at async ModuleLoader.addModuleSource (/home/projects/vitejs-vite-94zdjw/node_modules/rollup/dist/es/shared/node-entry.js:17782:13)

Validations

Copy link

stackblitz bot commented Dec 29, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

Rollup doesn't support using declaration yet (rollup/rollup#5113) (also the browser doesn't support it yet) and requires to be transpiled. You need to configure it like:

export default defineConfig({
  esbuild: {
    target: 'es2020',
    include: /\.(m?[jt]s|[jt]sx)$/,
    exclude: []
  }
})

Note that this configuration will make all files to be transpiled by esbuild, to make it more performant, only run the transpile on the files using using by creating a plugin.

If you just need to enable it in .ts files, setting esbuild.target: 'es2020' will suffice.

related: #13756

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite has workaround p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels Dec 30, 2023
@jrnail23
Copy link

jrnail23 commented Feb 1, 2024

@sapphi-red, I'm having the same issue with vitest, but I'm polyfilling the dispose implementation if needed -- this works fine with plain tsc, but is there any way around the parsing error? Perhaps some sort of pragma that can tell vite/rollup/esbuild to ignore it?

@neko-para
Copy link

neko-para commented Mar 30, 2024

Rollup doesn't support using declaration yet (rollup/rollup#5113) (also the browser doesn't support it yet) and requires to be transpiled. You need to configure it like:

export default defineConfig({
  esbuild: {
    target: 'es2020',
    include: /\.(m?[jt]s|[jt]sx)$/,
    exclude: []
  }
})

Note that this configuration will make all files to be transpiled by esbuild, to make it more performant, only run the transpile on the files using using by creating a plugin.

If you just need to enable it in .ts files, setting esbuild.target: 'es2020' will suffice.

related: #13756

This seems only works in built mode. As in dev mode, no transpilers are involved. So is it possible to make a plugin to convert them? Or maybe I have to use watch mode instead of dev mode.


Found out that vite has emulated rollup build hooks in development mode, so it's possible to call esbuild to transform code.
Here's an example.

import { transform } from 'esbuild'
// ...
  plugins: [
    vue(),
    vueJsx(),
    {
      name: 'esbuild-transform',
      async transform(code, id, options) {
        if (id.endsWith('.ts') || id.endsWith('.vue')) {
          code = (
            await transform(code, {
              target: 'es2022'
            })
          ).code
        }
        return code
      }
    }
  ],

@sapphi-red
Copy link
Member

Closing as this is now supported by rollup since v4.14.0.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite has workaround p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

No branches or pull requests

4 participants