Skip to content

Build fails if doing destructuring assignment outside of the define function of entrypoints #1485

Closed
@kairi003

Description

@kairi003

Describe the bug

In the entrypoints, if a destructuring assignment is performed outside of the define function and references that variable, the build fails.

Reproduction

For example, the code that fails in entrypoints/background.ts is as follows:

const [ a ] = [ 123, 456 ];
// or object as well,
// const { a } = { a: 123 };
console.log(a);

export default defineBackground(() => {
  console.log('Hello background!', { id: browser.runtime.id });
});

The build failure message is as follows:

$ npm run build

> wxt-starter@0.0.0 build
> wxt build


WXT 0.19.27                                                                                                                                                                                                                                                                                                  7:35:16 PM
ℹ Building chrome-mv3 for production with Vite 6.0.8                                                                                                                                                                                                                                                        7:35:16 PM
✖ Command failed after 566 ms                                                                                                                                                                                                                                                                               7:35:17 PM

 ERROR  a is not defined                                                                                                                                                                                                                                                                                     7:35:17 PM

    at entrypoints/background.ts:2:1
    at ViteNodeRunner.runModule (node_modules/vite-node/dist/client.mjs:401:5)
    at ViteNodeRunner.directRequest (node_modules/vite-node/dist/client.mjs:381:5)
    at ViteNodeRunner.cachedRequest (node_modules/vite-node/dist/client.mjs:206:14)
    at ViteNodeRunner.executeFile (node_modules/vite-node/dist/client.mjs:169:12)
    at node_modules/wxt/dist/core/builders/vite/index.mjs:245:29
    at async Promise.all (index 0)
    at Object.run (node_modules/wxt/dist/core/utils/environments/environment.mjs:13:14)
    at Object.importEntrypoints (node_modules/wxt/dist/core/builders/vite/index.mjs:242:23)
    at node_modules/wxt/dist/core/utils/building/find-entrypoints.mjs:139:19

However, if the variable reference is within a define function, it succeeds.

const [ a ] = [ 123, 456 ];

export default defineBackground(() => {
  console.log(a);
  console.log('Hello background!', { id: browser.runtime.id });
});

Or, if no destructuring assignment is done, it will succeed.
In the success example, it can be seen that the value is output to the console.

const a = [ 123, 456 ][0];
console.log(a);

export default defineBackground(() => {
  console.log('Hello background!', { id: browser.runtime.id });
});

Steps to reproduce

This can be seen immediately on the demo project created by npx wxt@latest init.

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 3800X 8-Core Processor
    Memory: 11.79 GB / 15.58 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.14.0 - ~/.local/share/pnpm/node
    npm: 10.9.2 - ~/.local/share/pnpm/npm
    pnpm: 9.12.3 - ~/.local/share/pnpm/pnpm
  Browsers:
    Chromium: 133.0.6943.141
  npmPackages:
    wxt: ^0.19.13 => 0.19.27

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions