Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

ReferenceError: __VITE_PRELOAD__ is not defined #13

Open
eigan opened this issue Feb 16, 2022 · 11 comments
Open

ReferenceError: __VITE_PRELOAD__ is not defined #13

eigan opened this issue Feb 16, 2022 · 11 comments
Labels
need reproduce need reproduce demo

Comments

@eigan
Copy link

eigan commented Feb 16, 2022

There is some places where the plugin will leave __VITE_PRELOAD__ present in the build.

See example:

Without plugin

{path:"edit",name:"project-edit",component:()=>oe(()=>Promise.resolve().then(function(){return ss}),void 0)

With plugin

{path:"edit",name:"project-edit",component:()=>oe(()=>Promise.resolve().then(function(){return ss}),(window.__dynamicImportPreload__ || function(importer) { return importer; })((__VITE_PRELOAD__)))

Temporary solution

Main file:

window.__VITE_PRELOAD__ = undefined;

__dynamicImportHandler__ and __dynamicImportPreload__ must now handle when first argument is undefined.

@jy0529
Copy link
Owner

jy0529 commented Feb 26, 2022

can you support a reproduce demo ?

@eigan
Copy link
Author

eigan commented Mar 10, 2022

Easiest reproduction is probably:

import foo from './foo.js'
import('./foo.js');
console.log(foo);

Here is a repro: https://github.com/eigan/repro-vite-dynamic-publicpath

Commited the build file with the constant still present https://github.com/eigan/repro-vite-dynamic-publicpath/blob/master/dist/assets/app.09997e2f.js

@eigan
Copy link
Author

eigan commented Mar 10, 2022

Probably relevant code in Vite:
https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/importAnalysisBuild.ts#L340

// there may still be markers due to inlined dynamic imports, remove
// all the markers regardless
chunk.code = chunk.code.replace(preloadMarkerRE, 'void 0')

@eigan
Copy link
Author

eigan commented Mar 31, 2022

@jy0529 need reproduce label can be removed I think?

@benjaminprojas
Copy link

I am also having this issue. The temporary fix also works for me (thanks @eigan).

@jy0529 when can we expect a permanent fix for this?

@arnaudbroes
Copy link

I'm having this issue as well now in my personal projects.

@benjaminprojas
Copy link

I'd like to mention that it still breaks in the latest version of vite (as of this time: 2.9.8). Reverting back to 2.9.5 with the temporary fix helps this work again.

@francislavoie
Copy link

I hate to bump, but @jy0529 any chance we get a fix for this?

@eigan
Copy link
Author

eigan commented Jun 23, 2022

@benjaminprojas Not sure what you expected to be fixed in vite 2.9, but vite 3 might fix an issue that sent me here to jy0529/vite-plugin-dynamic-publicpath. Take a look at the merged PRs from this issue: vitejs/vite#2009

This PR vitejs/vite#8450 might replace this plugin completely.
So for us - spending time fixing the issue here in jy0529/vite-plugin-dynamic-publicpath is a waste of time as the temporary fix works fine enough until Vite 3 is ready.

@francislavoie
Copy link

francislavoie commented Jul 13, 2022

@eigan Vite v3 is out, have you gotten a chance to try that new feature out (docs: https://vitejs.dev/guide/build.html#advanced-base-options)? I'm trying it out, but I can't get it to work.

Edit: Nevermind!! I got it:

  experimental: {
    renderBuiltUrl: (filename, { hostType }) => {
      if (hostType === 'js') {
        return { runtime: `window.__dynamicImportPreload__(${JSON.stringify(filename)})` }
      } else {
        return { relative: true }
      }
    },
  }

And then in the index:

window.__dynamicImportPreload__ = function(filename: string) {
  return `whateverPrefix/${filename}`;
};

@eigan
Copy link
Author

eigan commented Jul 25, 2022

@francislavoie Haven't tested yet. But thanks for testing and giving us a solution! 🙂

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need reproduce need reproduce demo
Projects
None yet
Development

No branches or pull requests

5 participants