question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@vitejs/plugin-legacy does not build

See original GitHub issue

Environment

  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-rc.8
  • Package Manager: npm@7.17.0
  • Builder: vite
  • User Config: vite
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-qzrpnn?file=nuxt.config.ts

Describe the bug

When adding the Vite plugin @vitejs/plugin-legacy and trying to build using nuxt build I get an error.

The error does not happen when using a clean Vite project, so I guess it is related to Nuxt.

Additional context

No response

Logs

ERROR  [vite:terser] _require2 is not defined                                                            11:12:21


 ERROR  [vite:legacy-generate-polyfill-chunk] _require2 is not defined                                    11:12:21


 ERROR  _require2 is not defined                                                                          11:12:21

  at doWork (eval at __global_eval__ (https://github-qzrpnn.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:8:32190), <anonymous>:5:20)
  at _0x51acfb.eval (eval at __global_eval__ (https://github-qzrpnn.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:8:32190), <anonymous>:12:21)
  at [nodejs.internal.kHybridDispatch] (https://github-qzrpnn.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:353083)
  at t.emitMessage (https://github-qzrpnn.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:1031392)
  at _started.<computed>._source.<computed> (https://github-qzrpnn.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:15:103757)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Aug 20, 2022

This is caused by the following code, inlined in @vite/legacy-vue:

    const makeWorker = () => new Worker_1(async (terserPath, code, options) => {
        // test fails when using `import`. maybe related: https://github.com/nodejs/node/issues/43205
        // eslint-disable-next-line no-restricted-globals -- this function runs inside cjs
        const terser = require(terserPath);
        return terser.minify(code, options);
    });

The problem is that jiti, which transpiles nuxt.config and anything used within it, is mangling require(terserPath) => require2(terserPath) because this is an ES module and Babel doesn’t understand that this is meant to run within a CJS context.

0reactions
JiatLncommented, Nov 30, 2022

I have same problem, need to help 😢

Read more comments on GitHub >

github_iconTop Results From Across the Web

About @vitejs/plugin-legacy does not support library mode
I have successfully built these components with Vite (with dynamic import and everything) and they work juts fine on modern browser. Describe ...
Read more >
@vitejs/plugin-legacy - npm
This plugin provides support for legacy browsers that do not support those features when building for production. By default, this plugin ...
Read more >
React app created with VITE does not work in IE 11
I have tried the @vitejs/plugin-react and added it in the ... which says "@vite/plugin-legacy only affects build mode, not dev mode".
Read more >
Migration from v3 | Vite (main branch)
If you need to support older browsers, you can add @vitejs/plugin-legacy as usual. ... In part of this change, vite dev and vite...
Read more >
@vitejs/plugin-legacy examples - CodeSandbox
Learn how to use @vitejs/plugin-legacy by viewing and forking ... by viewing and forking example apps that make use of @vitejs/plugin-legacy on CodeSandbox....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found