vite-plugin-dynamic-publicpath icon indicating copy to clipboard operation
vite-plugin-dynamic-publicpath copied to clipboard

ReferenceError: __VITE_PRELOAD__ is not defined

Open eigan opened this issue 3 years ago • 11 comments

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.

eigan avatar Feb 16 '22 14:02 eigan

can you support a reproduce demo ?

jy0529 avatar Feb 26 '22 01:02 jy0529

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 avatar Mar 10 '22 19:03 eigan

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 avatar Mar 10 '22 19:03 eigan

@jy0529 need reproduce label can be removed I think?

eigan avatar Mar 31 '22 11:03 eigan

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?

benjaminprojas avatar May 05 '22 19:05 benjaminprojas

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

arnaudbroes avatar May 05 '22 19:05 arnaudbroes

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.

benjaminprojas avatar May 05 '22 19:05 benjaminprojas

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

francislavoie avatar Jun 22 '22 22:06 francislavoie

@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: https://github.com/vitejs/vite/issues/2009

This PR https://github.com/vitejs/vite/pull/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.

eigan avatar Jun 23 '22 04:06 eigan

@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}`;
};

francislavoie avatar Jul 13 '22 17:07 francislavoie

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

eigan avatar Jul 25 '22 08:07 eigan