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.

Export `fileToUrl` function for plugins

See original GitHub issue

Clear and concise description of the problem

Vite’s some internal plugins uses the internal helper function fileToUrl, which emits asset file to dev-server/bundle and return a URL for using in generated module code. It’s also very useful for external plugins to emit asset files.

Suggested solution

Export fileToUrl function from Vite and document it in the docs.

Alternative

Plugin authors could copy the code from fileToUrl from their plugins. This is very inelegant.

Additional context

No response

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Mencicommented, Mar 4, 2022

I’m working on a plugin bringing WebAssembly ESM integration to Vite, which is equivalent to Webpack’s asyncWebAssembly.

I need to emit the WASM file just like the internal wasm plugin but I have no way to do it. My workaround is calling the original plugin’s load function, which is inelegant and won’t work for other types of files (for those without a internal plugin in Vite that emits the file directly).

https://github.com/Menci/vite-plugin-wasm/blob/e501eb234ad22d81d9bc94cb20086b759b4b07df/src/index.ts#L50-L52

1reaction
sapphi-redcommented, Jul 23, 2022

If it’s in JS, I think import url from './something.js?url' could be used for now.

@Menci For example for your plugin, I guess this part https://github.com/Menci/vite-plugin-wasm/blob/d98e0384950b6aea3156031a17ae65d28bbc7abd/src/index.ts#L37-L57 could be like

`
import __wasmUrl from "${id}?url"
import __vite__initWasm from "${wasmHelper.id}"
${imports
  .map(
    ({ from, names }, i) =>
      `import { ${names.map((name, j) => `${name} as __vite__wasmImport_${i}_${j}`).join(", ")} } from ${JSON.stringify(
        from
      )};`
  )
  .join("\n")}
const __vite__wasmModule = await __vite__initWasm({ ${imports
        .map(
          ({ from, names }, i) =>
            `${JSON.stringify(from)}: { ${names.map((name, j) => `${name}: __vite__wasmImport_${i}_${j}`).join(", ")} }`
        )
        .join(", ")} }, __wasmUrl});
${exports
  .map(name => `export ${name === "default" ? "default" : `const ${name} =`} __vite__wasmModule.${name};`)
  .join("\n")}
`;

or

`
import __vite__initWasm from "${id}?init"
${imports
  .map(
    ({ from, names }, i) =>
      `import { ${names.map((name, j) => `${name} as __vite__wasmImport_${i}_${j}`).join(", ")} } from ${JSON.stringify(
        from
      )};`
  )
  .join("\n")}
const __vite__wasmModule = await __vite__initWasm({ ${imports
        .map(
          ({ from, names }, i) =>
            `${JSON.stringify(from)}: { ${names.map((name, j) => `${name}: __vite__wasmImport_${i}_${j}`).join(", ")} }`
        )
        .join(", ")} });
${exports
  .map(name => `export ${name === "default" ? "default" : `const ${name} =`} __vite__wasmModule.${name};`)
  .join("\n")}
`;
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correctly export and import functions for cypress plugin ...
In this video I see that there is a way to store functions in separate files and then export them using module.exports =...
Read more >
File.js-1 Source Code | ExtReact - Sencha Documentation
url: function() {. //<debug>. Ext.log.warn('Exporter Plugin\'s file transformation server https://exporter.sencha.com ' +. 'will be deprecated (turned down) ...
Read more >
File.toURL() returns wrong result for UNC pathname (win)
Bug - A problem which impairs or prevents the functions of the product. JDK-6585937 URL.getFile() returns different results for UNC pathnames.
Read more >
Export url products - WordPress.org
By default, WooCommerce core export functionality does not include a URL ... Upon checking, it sounds like the WP All Import plugin will...
Read more >
Writing Plugins - Intuit
import { Auto, IPlugin } from "auto"; export default class TestPlugin ... of the options passed to plugins, auto uses io-ts and exports...
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