vite-plugin-dynamic-publicpath
vite-plugin-dynamic-publicpath copied to clipboard
ReferenceError: __VITE_PRELOAD__ is not defined
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.
can you support a reproduce demo ?
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
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')
@jy0529 need reproduce label can be removed I think?
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?
I'm having this issue as well now in my personal projects.
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.
I hate to bump, but @jy0529 any chance we get a fix for this?
@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 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 Haven't tested yet. But thanks for testing and giving us a solution! 🙂