web icon indicating copy to clipboard operation
web copied to clipboard

[rollup-plugin-import-meta-assets]: The output result was wrapped twice with `new URL()`

Open emosheeep opened this issue 1 year ago • 8 comments

I was using the plugin in vite and here's the source code:

image

and it result as follows that confused me:

image

Maybe there's something wrong, please help, thanks!

Here's my vite configuration 截屏2023-11-20 11 15 26

emosheeep avatar Nov 20 '23 03:11 emosheeep

Is this just confusing code or it's broken too?

It's indeed confusing, but IIRC we process all import.meta.url and add extra URL wrapper to resolve it relatively to the original file location after bundling, where the JS file location often changes, so the extra wrapper is necessary in a general case.

bashmish avatar Nov 20 '23 10:11 bashmish

It's broken in rspack, but I think of it as a flaw of rspack after your explanation, I have filed an issue to their team.

emosheeep avatar Nov 20 '23 13:11 emosheeep

Oh, actually, I just looked once more at the screenshot. After bundling the wrapper should contain the original file name, and not just import.meta.url twice, e.g. smth like this - a rewritten example from my app bundle, but should be clear I think:

new URL("./../../../assets/images/",new URL("./node_modules/path/to/my/original/file.js",import.meta.url).href).href;

Looks like smth is not working correctly in Vite. Maybe our plugin is not Vite compatible, not sure if anyone ever tested it with it. IIRC, Vite plugins are an extension of rollup API and should be working fine with most of rollup plugins, but I can imagine it's not always guaranteed.

bashmish avatar Nov 20 '23 16:11 bashmish

emm, actually the output result is working fine in webpack where the double URL would be replaced with the real path. 🤔🤔

emosheeep avatar Nov 21 '23 01:11 emosheeep

emm, actually the output result is working fine in webpack where the double URL would be replaced with the real path. 🤔🤔

if it's working, then probably your JS files remain on the same location in the folder structure, otherwise won't work, but if so, you might not need this plugin, I think the main goal of it is to be able to bundle JS and extract assets

on a different note: why do you need this plugin in Vite? I think Vite has it's own assets handling guidelines?

bashmish avatar Nov 21 '23 13:11 bashmish

in vite's library mode,all assets will be inlined,which causes a long time cost.unfortunately It doesn't support to extract assets now...

emosheeep avatar Nov 21 '23 14:11 emosheeep

Pure Rollup gives me:

const program_path = new URL(new URL('asset/program.wasm', import.meta.url).href, import.meta.url);

The second URL constructor looks pointless to me.

bergwerf avatar Dec 07 '23 00:12 bergwerf

Pure Rollup gives me:

const program_path = new URL(new URL('asset/program.wasm', import.meta.url).href, import.meta.url);

The second URL constructor looks pointless to me. Does it mean you can remove the double wrap?🤔

emosheeep avatar Dec 07 '23 02:12 emosheeep